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