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 "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
cookieJar(const Document * document)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
setCookies(Document * document,const KURL & url,const String & value)60 void setCookies(Document* document, const KURL& url, const String& value)
61 {
62 QNetworkCookieJar* jar = cookieJar(document);
63 if (!jar)
64 return;
65
66 QList<QNetworkCookie> cookies = QNetworkCookie::parseCookies(QString(value).toLatin1());
67 QList<QNetworkCookie>::Iterator it = cookies.begin();
68 while (it != cookies.end()) {
69 if (it->isHttpOnly())
70 it = cookies.erase(it);
71 else
72 ++it;
73 }
74 jar->setCookiesFromUrl(cookies, QUrl(url));
75 }
76
cookies(const Document * document,const KURL & url)77 String cookies(const Document* document, const KURL& url)
78 {
79 QNetworkCookieJar* jar = cookieJar(document);
80 if (!jar)
81 return String();
82
83 QList<QNetworkCookie> cookies = jar->cookiesForUrl(QUrl(url));
84 if (cookies.isEmpty())
85 return String();
86
87 QStringList resultCookies;
88 foreach (const QNetworkCookie& networkCookie, cookies) {
89 if (networkCookie.isHttpOnly())
90 continue;
91 resultCookies.append(QString::fromLatin1(networkCookie.toRawForm(QNetworkCookie::NameAndValueOnly).constData()));
92 }
93
94 return resultCookies.join(QLatin1String("; "));
95 }
96
cookieRequestHeaderFieldValue(const Document * document,const KURL & url)97 String cookieRequestHeaderFieldValue(const Document* document, const KURL &url)
98 {
99 QNetworkCookieJar* jar = cookieJar(document);
100 if (!jar)
101 return String();
102
103 QList<QNetworkCookie> cookies = jar->cookiesForUrl(QUrl(url));
104 if (cookies.isEmpty())
105 return String();
106
107 QStringList resultCookies;
108 foreach (QNetworkCookie networkCookie, cookies)
109 resultCookies.append(QString::fromLatin1(networkCookie.toRawForm(QNetworkCookie::NameAndValueOnly).constData()));
110
111 return resultCookies.join(QLatin1String("; "));
112 }
113
cookiesEnabled(const Document * document)114 bool cookiesEnabled(const Document* document)
115 {
116 QNetworkCookieJar* jar = cookieJar(document);
117 return jar;
118 }
119
getRawCookies(const Document *,const KURL &,Vector<Cookie> & rawCookies)120 bool getRawCookies(const Document*, const KURL&, Vector<Cookie>& rawCookies)
121 {
122 // FIXME: Not yet implemented
123 rawCookies.clear();
124 return false; // return true when implemented
125 }
126
deleteCookie(const Document *,const KURL &,const String &)127 void deleteCookie(const Document*, const KURL&, const String&)
128 {
129 // FIXME: Not yet implemented
130 }
131
getHostnamesWithCookies(HashSet<String> & hostnames)132 void getHostnamesWithCookies(HashSet<String>& hostnames)
133 {
134 // FIXME: Not yet implemented
135 }
136
deleteCookiesForHostname(const String & hostname)137 void deleteCookiesForHostname(const String& hostname)
138 {
139 // FIXME: Not yet implemented
140 }
141
deleteAllCookies()142 void deleteAllCookies()
143 {
144 // FIXME: Not yet implemented
145 }
146
147 }
148
149 // vim: ts=4 sw=4 et
150