2 * Copyright (C) 2003, 2006, 2008 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #import "BlockExceptions.h"
33 #import "CookieStorage.h"
34 #import "CookieStorageCFNet.h"
37 #import "WebCoreSystemInterface.h"
38 #import <wtf/RetainPtr.h>
41 @interface NSHTTPCookie (WebCoreHTTPOnlyCookies)
47 static bool isHTTPOnly(NSHTTPCookie *cookie)
49 // Once we require a newer version of Foundation with the isHTTPOnly method,
50 // we can eliminate the instancesRespondToSelector: check.
51 static bool supportsHTTPOnlyCookies = [NSHTTPCookie instancesRespondToSelector:@selector(isHTTPOnly)];
52 return supportsHTTPOnlyCookies && [cookie isHTTPOnly];
55 static RetainPtr<NSArray> filterCookies(NSArray *unfilteredCookies)
57 NSUInteger count = [unfilteredCookies count];
58 RetainPtr<NSMutableArray> filteredCookies(AdoptNS, [[NSMutableArray alloc] initWithCapacity:count]);
60 for (NSUInteger i = 0; i < count; ++i) {
61 NSHTTPCookie *cookie = (NSHTTPCookie *)[unfilteredCookies objectAtIndex:i];
63 // <rdar://problem/5632883> On 10.5, NSHTTPCookieStorage would store an empty cookie,
64 // which would be sent as "Cookie: =". We have a workaround in setCookies() to prevent
65 // that, but we also need to avoid sending cookies that were previously stored, and
66 // there's no harm to doing this check because such a cookie is never valid.
67 if (![[cookie name] length])
70 if (isHTTPOnly(cookie))
73 [filteredCookies.get() addObject:cookie];
76 return filteredCookies;
79 String cookies(const Document*, const KURL& url)
81 BEGIN_BLOCK_OBJC_EXCEPTIONS;
83 NSURL *cookieURL = url;
85 #if USE(CFURLSTORAGESESSIONS)
86 if (RetainPtr<CFHTTPCookieStorageRef> cfCookieStorage = currentCFHTTPCookieStorage())
87 cookies = wkHTTPCookiesForURL(cfCookieStorage.get(), cookieURL);
90 cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:cookieURL];
92 return [[NSHTTPCookie requestHeaderFieldsWithCookies:filterCookies(cookies).get()] objectForKey:@"Cookie"];
94 END_BLOCK_OBJC_EXCEPTIONS;
98 String cookieRequestHeaderFieldValue(const Document*, const KURL& url)
100 BEGIN_BLOCK_OBJC_EXCEPTIONS;
102 NSURL *cookieURL = url;
104 #if USE(CFURLSTORAGESESSIONS)
105 if (RetainPtr<CFHTTPCookieStorageRef> cfCookieStorage = currentCFHTTPCookieStorage())
106 cookies = wkHTTPCookiesForURL(cfCookieStorage.get(), cookieURL);
109 cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:cookieURL];
111 return [[NSHTTPCookie requestHeaderFieldsWithCookies:cookies] objectForKey:@"Cookie"];
113 END_BLOCK_OBJC_EXCEPTIONS;
117 void setCookies(Document* document, const KURL& url, const String& cookieStr)
119 BEGIN_BLOCK_OBJC_EXCEPTIONS;
121 // <rdar://problem/5632883> On 10.5, NSHTTPCookieStorage would store an empty cookie,
122 // which would be sent as "Cookie: =".
123 if (cookieStr.isEmpty())
126 // <http://bugs.webkit.org/show_bug.cgi?id=6531>, <rdar://4409034>
127 // cookiesWithResponseHeaderFields doesn't parse cookies without a value
128 String cookieString = cookieStr.contains('=') ? cookieStr : cookieStr + "=";
130 NSURL *cookieURL = url;
131 RetainPtr<NSArray> filteredCookies = filterCookies([NSHTTPCookie cookiesWithResponseHeaderFields:[NSDictionary dictionaryWithObject:cookieString forKey:@"Set-Cookie"] forURL:cookieURL]);
133 #if USE(CFURLSTORAGESESSIONS)
134 if (RetainPtr<CFHTTPCookieStorageRef> cfCookieStorage = currentCFHTTPCookieStorage())
135 wkSetHTTPCookiesForURL(cfCookieStorage.get(), filteredCookies.get(), cookieURL, document->firstPartyForCookies());
138 [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookies:filteredCookies.get() forURL:cookieURL mainDocumentURL:document->firstPartyForCookies()];
140 END_BLOCK_OBJC_EXCEPTIONS;
143 bool cookiesEnabled(const Document*)
145 BEGIN_BLOCK_OBJC_EXCEPTIONS;
147 NSHTTPCookieAcceptPolicy cookieAcceptPolicy;
148 #if USE(CFURLSTORAGESESSIONS)
149 if (RetainPtr<CFHTTPCookieStorageRef> cfCookieStorage = currentCFHTTPCookieStorage())
150 cookieAcceptPolicy = wkGetHTTPCookieAcceptPolicy(cfCookieStorage.get());
153 cookieAcceptPolicy = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookieAcceptPolicy];
155 return cookieAcceptPolicy == NSHTTPCookieAcceptPolicyAlways || cookieAcceptPolicy == NSHTTPCookieAcceptPolicyOnlyFromMainDocumentDomain;
157 END_BLOCK_OBJC_EXCEPTIONS;
161 bool getRawCookies(const Document*, const KURL& url, Vector<Cookie>& rawCookies)
164 BEGIN_BLOCK_OBJC_EXCEPTIONS;
166 NSURL *cookieURL = url;
168 #if USE(CFURLSTORAGESESSIONS)
169 if (RetainPtr<CFHTTPCookieStorageRef> cfCookieStorage = currentCFHTTPCookieStorage())
170 cookies = wkHTTPCookiesForURL(cfCookieStorage.get(), cookieURL);
173 cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:cookieURL];
175 NSUInteger count = [cookies count];
176 rawCookies.reserveCapacity(count);
177 for (NSUInteger i = 0; i < count; ++i) {
178 NSHTTPCookie *cookie = (NSHTTPCookie *)[cookies objectAtIndex:i];
179 NSString *name = [cookie name];
180 NSString *value = [cookie value];
181 NSString *domain = [cookie domain];
182 NSString *path = [cookie path];
183 NSTimeInterval expires = [[cookie expiresDate] timeIntervalSince1970] * 1000;
184 bool httpOnly = [cookie isHTTPOnly];
185 bool secure = [cookie isSecure];
186 bool session = [cookie isSessionOnly];
187 rawCookies.uncheckedAppend(Cookie(name, value, domain, path, expires, httpOnly, secure, session));
190 END_BLOCK_OBJC_EXCEPTIONS;
194 void deleteCookie(const Document*, const KURL& url, const String& cookieName)
196 BEGIN_BLOCK_OBJC_EXCEPTIONS;
198 NSURL *cookieURL = url;
199 NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
201 #if USE(CFURLSTORAGESESSIONS)
202 RetainPtr<CFHTTPCookieStorageRef> cfCookieStorage = currentCFHTTPCookieStorage();
204 cookies = wkHTTPCookiesForURL(cfCookieStorage.get(), cookieURL);
207 cookies = [cookieStorage cookiesForURL:cookieURL];
209 NSString *cookieNameString = (NSString *) cookieName;
211 NSUInteger count = [cookies count];
212 for (NSUInteger i = 0; i < count; ++i) {
213 NSHTTPCookie *cookie = (NSHTTPCookie *)[cookies objectAtIndex:i];
214 if ([[cookie name] isEqualToString:cookieNameString]) {
215 #if USE(CFURLSTORAGESESSIONS)
217 wkDeleteHTTPCookie(cfCookieStorage.get(), cookie);
220 [cookieStorage deleteCookie:cookie];
225 END_BLOCK_OBJC_EXCEPTIONS;
228 void getHostnamesWithCookies(HashSet<String>& hostnames)
230 BEGIN_BLOCK_OBJC_EXCEPTIONS;
232 NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
233 NSArray *cookies = [cookieStorage cookies];
235 for (NSHTTPCookie* cookie in cookies)
236 hostnames.add([cookie domain]);
238 END_BLOCK_OBJC_EXCEPTIONS;
241 void deleteCookiesForHostname(const String& hostname)
243 BEGIN_BLOCK_OBJC_EXCEPTIONS;
245 NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
246 NSArray *cookies = [cookieStorage cookies];
250 for (NSHTTPCookie* cookie in cookies) {
251 if (hostname == String([cookie domain]))
252 [cookieStorage deleteCookie:cookie];
255 END_BLOCK_OBJC_EXCEPTIONS;
258 void deleteAllCookies()
260 BEGIN_BLOCK_OBJC_EXCEPTIONS;
262 NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
263 NSArray *cookies = [cookieStorage cookies];
267 for (NSHTTPCookie* cookie in cookies)
268 [cookieStorage deleteCookie:cookie];
270 END_BLOCK_OBJC_EXCEPTIONS;
275 #endif // !USE(CFNETWORK)