Home | History | Annotate | Download | only in qt
      1 /*
      2  * Copyright (C) 2006 George Staikos <staikos (at) kde.org>
      3  *
      4  * All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
     16  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     18  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
     19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     22  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     23  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     25  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 
     28 #include "config.h"
     29 #include "CookieJar.h"
     30 
     31 #include "Cookie.h"
     32 #include "Document.h"
     33 #include "FrameLoaderClientQt.h"
     34 #include "KURL.h"
     35 #include "NetworkingContext.h"
     36 #include "PlatformString.h"
     37 #include "qwebframe.h"
     38 #include "qwebpage.h"
     39 #include <QNetworkAccessManager>
     40 #include <QNetworkCookie>
     41 #include <QStringList>
     42 
     43 namespace WebCore {
     44 
     45 static QNetworkCookieJar *cookieJar(const Document *document)
     46 {
     47     if (!document)
     48         return 0;
     49     Frame* frame = document->frame();
     50     if (!frame)
     51         return 0;
     52     FrameLoader* loader = frame->loader();
     53     if (!loader)
     54         return 0;
     55     QNetworkAccessManager* manager = loader->networkingContext()->networkAccessManager();
     56     QNetworkCookieJar* jar = manager->cookieJar();
     57     return jar;
     58 }
     59 
     60 void setCookies(Document* document, const KURL& url, const String& value)
     61 {
     62     QUrl u(url);
     63     QUrl p(document->firstPartyForCookies());
     64     QNetworkCookieJar* jar = cookieJar(document);
     65     if (!jar)
     66         return;
     67 
     68     QList<QNetworkCookie> cookies = QNetworkCookie::parseCookies(QString(value).toAscii());
     69     QList<QNetworkCookie>::Iterator it = cookies.begin();
     70     while (it != cookies.end()) {
     71         if (it->isHttpOnly())
     72             it = cookies.erase(it);
     73         else
     74             ++it;
     75     }
     76     jar->setCookiesFromUrl(cookies, u);
     77 }
     78 
     79 String cookies(const Document* document, const KURL& url)
     80 {
     81     QUrl u(url);
     82     QNetworkCookieJar* jar = cookieJar(document);
     83     if (!jar)
     84         return String();
     85 
     86     QList<QNetworkCookie> cookies = jar->cookiesForUrl(u);
     87     if (cookies.isEmpty())
     88         return String();
     89 
     90     QStringList resultCookies;
     91     foreach (QNetworkCookie networkCookie, cookies) {
     92         if (networkCookie.isHttpOnly())
     93             continue;
     94         resultCookies.append(QString::fromLatin1(networkCookie.toRawForm(QNetworkCookie::NameAndValueOnly).constData()));
     95     }
     96 
     97     return resultCookies.join(QLatin1String("; "));
     98 }
     99 
    100 String cookieRequestHeaderFieldValue(const Document* document, const KURL &url)
    101 {
    102     QUrl u(url);
    103     QNetworkCookieJar* jar = cookieJar(document);
    104     if (!jar)
    105         return String();
    106 
    107     QList<QNetworkCookie> cookies = jar->cookiesForUrl(u);
    108     if (cookies.isEmpty())
    109         return String();
    110 
    111     QStringList resultCookies;
    112     foreach (QNetworkCookie networkCookie, cookies)
    113         resultCookies.append(QString::fromLatin1(networkCookie.toRawForm(QNetworkCookie::NameAndValueOnly).constData()));
    114 
    115     return resultCookies.join(QLatin1String("; "));
    116 }
    117 
    118 bool cookiesEnabled(const Document* document)
    119 {
    120     QNetworkCookieJar* jar = cookieJar(document);
    121     return jar;
    122 }
    123 
    124 bool getRawCookies(const Document*, const KURL&, Vector<Cookie>& rawCookies)
    125 {
    126     // FIXME: Not yet implemented
    127     rawCookies.clear();
    128     return false; // return true when implemented
    129 }
    130 
    131 void deleteCookie(const Document*, const KURL&, const String&)
    132 {
    133     // FIXME: Not yet implemented
    134 }
    135 
    136 void getHostnamesWithCookies(HashSet<String>& hostnames)
    137 {
    138     // FIXME: Not yet implemented
    139 }
    140 
    141 void deleteCookiesForHostname(const String& hostname)
    142 {
    143     // FIXME: Not yet implemented
    144 }
    145 
    146 void deleteAllCookies()
    147 {
    148     // FIXME: Not yet implemented
    149 }
    150 
    151 }
    152 
    153 // vim: ts=4 sw=4 et
    154