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 "KURL.h"
     34 #include "PlatformString.h"
     35 
     36 #include "qwebpage.h"
     37 #include "qwebframe.h"
     38 #include "FrameLoaderClientQt.h"
     39 #include <QNetworkAccessManager>
     40 #include <QNetworkCookie>
     41 
     42 namespace WebCore {
     43 
     44 static QNetworkCookieJar *cookieJar(const Document *document)
     45 {
     46     if (!document)
     47         return 0;
     48     Frame *frame = document->frame();
     49     if (!frame)
     50         return 0;
     51     FrameLoader *loader = frame->loader();
     52     if (!loader)
     53         return 0;
     54     QWebFrame* webFrame = static_cast<FrameLoaderClientQt*>(loader->client())->webFrame();
     55     QWebPage* page = webFrame->page();
     56     QNetworkAccessManager* manager = page->networkAccessManager();
     57     QNetworkCookieJar* jar = manager->cookieJar();
     58     return jar;
     59 }
     60 
     61 void setCookies(Document* document, const KURL& url, const String& value)
     62 {
     63     QUrl u(url);
     64     QUrl p(document->firstPartyForCookies());
     65     QNetworkCookieJar* jar = cookieJar(document);
     66     if (!jar)
     67         return;
     68 
     69     QList<QNetworkCookie> cookies = QNetworkCookie::parseCookies(QString(value).toAscii());
     70 #if QT_VERSION >= 0x040500
     71     QList<QNetworkCookie>::Iterator it = cookies.begin();
     72     while (it != cookies.end()) {
     73         if (it->isHttpOnly())
     74             it = cookies.erase(it);
     75         else
     76             ++it;
     77     }
     78 #endif
     79     jar->setCookiesFromUrl(cookies, u);
     80 }
     81 
     82 String cookies(const Document* document, const KURL& url)
     83 {
     84     QUrl u(url);
     85     QNetworkCookieJar* jar = cookieJar(document);
     86     if (!jar)
     87         return String();
     88 
     89     QList<QNetworkCookie> cookies = jar->cookiesForUrl(u);
     90     if (cookies.isEmpty())
     91         return String();
     92 
     93     QStringList resultCookies;
     94     foreach (QNetworkCookie networkCookie, cookies) {
     95 #if QT_VERSION >= 0x040500
     96         if (networkCookie.isHttpOnly())
     97             continue;
     98 #endif
     99         resultCookies.append(QString::fromAscii(
    100                              networkCookie.toRawForm(QNetworkCookie::NameAndValueOnly).constData()));
    101     }
    102 
    103     return resultCookies.join(QLatin1String("; "));
    104 }
    105 
    106 String cookieRequestHeaderFieldValue(const Document* document, const KURL &url)
    107 {
    108     QUrl u(url);
    109     QNetworkCookieJar* jar = cookieJar(document);
    110     if (!jar)
    111         return String();
    112 
    113     QList<QNetworkCookie> cookies = jar->cookiesForUrl(u);
    114     if (cookies.isEmpty())
    115         return String();
    116 
    117     QStringList resultCookies;
    118     foreach (QNetworkCookie networkCookie, cookies) {
    119         resultCookies.append(QString::fromAscii(
    120                              networkCookie.toRawForm(QNetworkCookie::NameAndValueOnly).constData()));
    121     }
    122 
    123     return resultCookies.join(QLatin1String("; "));
    124 }
    125 
    126 bool cookiesEnabled(const Document* document)
    127 {
    128     QNetworkCookieJar* jar = cookieJar(document);
    129     return (jar != 0);
    130 }
    131 
    132 bool getRawCookies(const Document*, const KURL&, Vector<Cookie>& rawCookies)
    133 {
    134     // FIXME: Not yet implemented
    135     rawCookies.clear();
    136     return false; // return true when implemented
    137 }
    138 
    139 void deleteCookie(const Document*, const KURL&, const String&)
    140 {
    141     // FIXME: Not yet implemented
    142 }
    143 
    144 }
    145 
    146 // vim: ts=4 sw=4 et
    147