Using cookies

Cascades stores cookies to maintain persistent information across all webpage requests. A cookie is set by a remote server when it replies to a request on a webpage, such as a navigation request. The server expects the same cookie to be sent back when any other requests are sent. In Cascades, you can store cookies by using the WebCookieJar, which is an SQL database that's stored in the file system. The cookie jar keeps track of all the cookies set in previous requests.

You can use cookies to connect to servers in web apps. Sometimes, a cookie must be set in order for the server to accept the request to navigate to a webpage. You can also use cookies to reuse login information from another app so that the user doesn't need to authenticate twice. 

The WebCookieJar class is similar to the QNetworkCookieJar class, except that Cascades does not have a QNetworkCookie class. Instead, cookies are represented using strings that are formatted according to the RFC 6265 specification, found at http://tools.ietf.org/html/rfc6265.

Specifying cookies in Cascades

Cookies in Cascades follow the RFC 6265 specification. It's important to understand how URL and path matching behaves in your app so that you can get and set cookies correctly.

URL matching follows the domain matching rules of section 5.1.3 of the RFC 6265 specification. For example, if you are setting a cookie to a URL such as http://a.b.c.com, the cookie is visible to URL requests made from the following domains:

  • a.b.c.com (the domain string and the URL string are identical)
  • b.c.com (the domain string is a suffix of the URL string)
  • c.com (the domain string is a suffix of the URL string and the string is a host name, not an IP address)

Path matching follows the path matching rules of section 5.1.4 of the RFC 6265 specification. For example, if you are setting a cookie to a URL such as http://a.b.c.com/path1/, the cookie is visible to URL requests made with the following origin:

  • a.b.c.com/path1/path2/path3 (the cookie path is a prefix of the request path)
  • a.b.c.com/path1/path2/ (the cookie path is a prefix of the request path and the last character of the cookie path is "/")
  • a.b.com/path1 (the cookie path and the request path are identical)

Cookies that are set with this method must follow the RFC 6265 specification or they will be ignored. You cannot set a cookie to a country-level top-level domain (such as .co.uk, .com.hk, and so on) or a top-level domain (such as .com or .org).

The cookiesForUrl() function returns the cookie list whose domain and path match the provided URL. The ordering of the cookie list returned by the cookiesForUrl() function is specified in section 5.4 of the RFC 6265 specification:

  • Cookies with longer paths are listed before cookies with shorter paths.

  • For cookies that have equal-length path fields, cookies with earlier creation times are listed before cookies with later creation times.

Using the WebCookieJar

The WebCookieJar is accessed through the WebStorage class, since the cookie jar represents an SQL database that's stored in the file system.

Here's an example of how to access the WebCookieJar in QML:

webView.storage.cookieJar

And here's an example of how to access the WebCookieJar in C++:

webView->storage()->cookieJar();

The following C++ code sample demonstrates how to create a QUrl and set it to the Google search engine. Then it retrieves the WebCookieJar for this WebView. The setCookiesFromUrl() function is used to add the cookies to the WebCookieJar. The cookiesForUrl() function is called to retrieve the cookies whose domain and path match the URL that was provided. Because the cookies are returned as a list of strings, they are parsed and stored locally.

QUrl url = QUrl("http://www.google.com"); 

// Initialize a cookie jar.  
WebCookieJar* m_pWebCookieJar =(m_pWebView->storage())->cookieJar(); 

// Create some "cookies".
QStringList cookies; 
cookies << "Name1=Value1;"   << "Name2=Value2";
  
// Add the cookies to the cookieJar. 
m_pWebCookieJar->setCookiesFromUrl(url,cookies); 


// Retrieve cookies for this URL.
QStringList cookiesFromJar = m_pWebCookieJar->cookiesForUrl(url); 

// Parse the list of cookies.
for (int i = 0; i < cookiesFromJar.count(); i++) 
{     
    m_pTextInfoArea->setText(m_pTextInfoArea->text()+ "\n
        cookies [" + cookies[i] + "] \n     
    cookiesFromJar [" + cookiesFromJar[i] + "] ");
}

Last modified: 2013-12-21

comments powered by Disqus