Changeset 143272 in webkit


Ignore:
Timestamp:
Feb 18, 2013 4:00:47 PM (11 years ago)
Author:
commit-queue@webkit.org
Message:

[Curl] The function cookiesForDOM() does not behave correctly.
https://bugs.webkit.org/show_bug.cgi?id=109923

Patch by peavo@outlook.com <peavo@outlook.com> on 2013-02-18
Reviewed by Brent Fulgham.

The cookiesForDOM() function should return a list of matching cookies, both persistent and session cookies.

  • platform/network/curl/CookieJarCurl.cpp:

(WebCore::readCurlCookieToken): Added function to read next token from Curl cookie string.
(WebCore::addMatchingCurlCookie): Added function to add matching cookies to cookie list.
(WebCore::setCookiesFromDOM): Add domain and path from url to cookie if not already set.
(WebCore::cookiesForDOM): Return a list of matching cookies, both session and persistent cookies.

Location:
trunk/Source/WebCore
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r143271 r143272  
     12013-02-18  peavo@outlook.com  <peavo@outlook.com>
     2
     3        [Curl] The function cookiesForDOM() does not behave correctly.
     4        https://bugs.webkit.org/show_bug.cgi?id=109923
     5
     6        Reviewed by Brent Fulgham.
     7
     8        The cookiesForDOM() function should return a list of matching cookies, both persistent and session cookies.
     9
     10        * platform/network/curl/CookieJarCurl.cpp:
     11        (WebCore::readCurlCookieToken): Added function to read next token from Curl cookie string.
     12        (WebCore::addMatchingCurlCookie): Added function to add matching cookies to cookie list.
     13        (WebCore::setCookiesFromDOM): Add domain and path from url to cookie if not already set.
     14        (WebCore::cookiesForDOM): Return a list of matching cookies, both session and persistent cookies.
     15
    1162013-02-17  Mark Lam  <mark.lam@apple.com>
    217
  • trunk/Source/WebCore/platform/network/curl/CookieJarCurl.cpp

    r142574 r143272  
    3030static HashMap<String, String> cookieJar;
    3131
     32static void readCurlCookieToken(const char*& cookie, String& token)
     33{
     34    // Read the next token from a cookie with the Netscape cookie format.
     35    // Curl separates each token in line with tab character.
     36    while (cookie && cookie[0] && cookie[0] != '\t') {
     37        token.append(cookie[0]);
     38        cookie++;
     39    }
     40    if (cookie[0] == '\t')
     41        cookie++;
     42}
     43
     44static void addMatchingCurlCookie(const char* cookie, const String& domain, const String& path, String& cookies)
     45{
     46    // Check if the cookie matches domain and path, and is not expired.
     47    // If so, add it to the list of cookies.
     48    //
     49    // Description of the Netscape cookie file format which Curl uses:
     50    //
     51    // .netscape.com     TRUE   /  FALSE  946684799   NETSCAPE_ID  100103
     52    //
     53    // Each line represents a single piece of stored information. A tab is inserted between each of the fields.
     54    //
     55    // From left-to-right, here is what each field represents:
     56    //
     57    // domain - The domain that created AND that can read the variable.
     58    // flag - A TRUE/FALSE value indicating if all machines within a given domain can access the variable. This value is set automatically by the browser, depending on the value you set for domain.
     59    // path - The path within the domain that the variable is valid for.
     60    // secure - A TRUE/FALSE value indicating if a secure connection with the domain is needed to access the variable.
     61    // expiration - The UNIX time that the variable will expire on. UNIX time is defined as the number of seconds since Jan 1, 1970 00:00:00 GMT.
     62    // name - The name of the variable.
     63    // value - The value of the variable.
     64
     65    if (!cookie)
     66        return;
     67
     68    String cookieDomain;
     69    readCurlCookieToken(cookie, cookieDomain);
     70
     71    bool subDomain = false;
     72
     73    if (cookieDomain[0] == '.') {
     74        // Check if domain is a subdomain of the domain in the cookie.
     75        // Curl uses a '.' in front of domains to indicate its valid on subdomains.
     76        cookieDomain.remove(0);
     77        int lenDiff = domain.length() - cookieDomain.length();
     78        int index = domain.find(cookieDomain);
     79        if (index == lenDiff)
     80            subDomain = true;
     81    }
     82
     83    if (!subDomain && cookieDomain != domain)
     84        return;
     85
     86    String strBoolean;
     87    readCurlCookieToken(cookie, strBoolean);
     88
     89    String strPath;
     90    readCurlCookieToken(cookie, strPath);
     91
     92    // Check if path matches
     93    int index = path.find(strPath);
     94    if (index)
     95        return;
     96
     97    String strSecure;
     98    readCurlCookieToken(cookie, strSecure);
     99
     100    String strExpires;
     101    readCurlCookieToken(cookie, strExpires);
     102
     103    int expires = strExpires.toInt();
     104
     105    time_t now = 0;
     106    time(&now);
     107
     108    // Check if cookie has expired
     109    if (expires && now > expires)
     110        return;
     111
     112    String strName;
     113    readCurlCookieToken(cookie, strName);
     114
     115    String strValue;
     116    readCurlCookieToken(cookie, strValue);
     117
     118    // The cookie matches, add it to the cookie list.
     119    cookies = cookies + strName + "=" + strValue + "; ";
     120}
     121
    32122void setCookiesFromDOM(const NetworkStorageSession&, const KURL&, const KURL& url, const String& value)
    33123{
     
    52142        cookie.append(String::make8BitFrom16BitSource(value.characters16(), value.length()));
    53143
     144    if (cookie.find(String("domain")) == notFound) {
     145        // If no domain is set, set domain and path from url.
     146        cookie.append(String("; domain=") + url.host());
     147        cookie.append(String("; path=") + url.path());
     148    }
     149
    54150    CString strCookie(reinterpret_cast<const char*>(cookie.characters8()), cookie.length());
    55151
     
    61157String cookiesForDOM(const NetworkStorageSession&, const KURL&, const KURL& url)
    62158{
    63     return cookieJar.get(url.string());
     159    String cookies;
     160
     161    CURL* curl = curl_easy_init();
     162
     163    if (!curl)
     164        return cookies;
     165
     166    CURLSH* curlsh = ResourceHandleManager::sharedInstance()->getCurlShareHandle();
     167
     168    curl_easy_setopt(curl, CURLOPT_SHARE, curlsh);
     169
     170    struct curl_slist* list = 0;
     171    curl_easy_getinfo(curl, CURLINFO_COOKIELIST, &list);
     172
     173    if (list) {
     174        String domain = url.host();
     175        String path = url.path();
     176
     177        struct curl_slist* item = list;
     178        while (item) {
     179            const char* cookie = item->data;
     180            addMatchingCurlCookie(cookie, domain, path, cookies);
     181            item = item->next;
     182        }
     183
     184        curl_slist_free_all(list);
     185    }
     186
     187    curl_easy_cleanup(curl);
     188
     189    return cookies;
    64190}
    65191
Note: See TracChangeset for help on using the changeset viewer.