2 * Copyright (C) 2006, 2007 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.
28 #include "PlatformString.h"
29 #include "DeprecatedString.h"
30 #include "ResourceHandle.h"
33 #include <CoreFoundation/CoreFoundation.h>
34 #include <CFNetwork/CFHTTPCookiesPriv.h>
35 #include <WebKitSystemInterface/WebKitSystemInterface.h>
44 static const CFStringRef s_setCookieKeyCF = CFSTR("Set-Cookie");
45 static const CFStringRef s_cookieCF = CFSTR("Cookie");
49 void setCookies(const Document* /*document*/, const KURL& url, const KURL& policyURL, const String& value)
52 // <rdar://problem/5632883> CFHTTPCookieStorage happily stores an empty cookie, which would be sent as "Cookie: =".
56 CFHTTPCookieStorageRef defaultCookieStorage = wkGetDefaultHTTPCookieStorage();
57 if (!defaultCookieStorage)
60 RetainPtr<CFURLRef> urlCF(AdoptCF, url.createCFURL());
61 RetainPtr<CFURLRef> policyURLCF(AdoptCF, policyURL.createCFURL());
63 // <http://bugzilla.opendarwin.org/show_bug.cgi?id=6531>, <rdar://4409034>
64 // cookiesWithResponseHeaderFields doesn't parse cookies without a value
65 String cookieString = value.contains('=') ? value : value + "=";
67 RetainPtr<CFStringRef> cookieStringCF(AdoptCF, cookieString.createCFString());
68 RetainPtr<CFDictionaryRef> headerFieldsCF(AdoptCF, CFDictionaryCreate(kCFAllocatorDefault, (const void**)&s_setCookieKeyCF,
69 (const void**)&cookieStringCF, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
71 RetainPtr<CFArrayRef> cookiesCF(AdoptCF, CFHTTPCookieCreateWithResponseHeaderFields(kCFAllocatorDefault,
72 headerFieldsCF.get(), urlCF.get()));
74 CFHTTPCookieStorageSetCookies(defaultCookieStorage, cookiesCF.get(), urlCF.get(), policyURLCF.get());
76 // FIXME: Deal with the policy URL.
77 DeprecatedString str = url.deprecatedString();
78 str.append((UChar)'\0');
79 DeprecatedString val = value.deprecatedString();
80 val.append((UChar)'\0');
81 InternetSetCookie((UChar*)str.unicode(), 0, (UChar*)val.unicode());
85 String cookies(const Document* /*document*/, const KURL& url)
88 CFHTTPCookieStorageRef defaultCookieStorage = wkGetDefaultHTTPCookieStorage();
89 if (!defaultCookieStorage)
93 RetainPtr<CFURLRef> urlCF(AdoptCF, url.createCFURL());
95 bool secure = equalIgnoringCase(url.protocol(), "https");
97 RetainPtr<CFArrayRef> cookiesCF(AdoptCF, CFHTTPCookieStorageCopyCookiesForURL(defaultCookieStorage, urlCF.get(), secure));
99 // <rdar://problem/5632883> CFHTTPCookieStorage happily stores an empty cookie, which would be sent as "Cookie: =".
100 // We have a workaround in setCookies() to prevent that, but we also need to avoid sending cookies that were previously stored.
101 CFIndex count = CFArrayGetCount(cookiesCF.get());
102 RetainPtr<CFMutableArrayRef> cookiesForURLFilteredCopy(AdoptCF, CFArrayCreateMutable(0, count, &kCFTypeArrayCallBacks));
103 for (CFIndex i = 0; i < count; ++i) {
104 CFHTTPCookieRef cookie = (CFHTTPCookieRef)CFArrayGetValueAtIndex(cookiesCF.get(), i);
105 if (CFStringGetLength(CFHTTPCookieGetName(cookie)) != 0)
106 CFArrayAppendValue(cookiesForURLFilteredCopy.get(), cookie);
108 RetainPtr<CFDictionaryRef> headerCF(AdoptCF, CFHTTPCookieCopyRequestHeaderFields(kCFAllocatorDefault, cookiesForURLFilteredCopy.get()));
110 return (CFStringRef)CFDictionaryGetValue(headerCF.get(), s_cookieCF);
112 DeprecatedString str = url.deprecatedString();
113 str.append((UChar)'\0');
115 DWORD count = str.length();
116 InternetGetCookie((UChar*)str.unicode(), 0, 0, &count);
117 if (count <= 1) // Null terminator counts as 1.
120 UChar* buffer = new UChar[count];
121 InternetGetCookie((UChar*)str.unicode(), 0, buffer, &count);
122 String& result = String(buffer, count-1); // Ignore the null terminator.
128 bool cookiesEnabled(const Document* /*document*/)
130 CFHTTPCookieStorageAcceptPolicy policy = CFHTTPCookieStorageAcceptPolicyOnlyFromMainDocumentDomain;
131 if (CFHTTPCookieStorageRef defaultCookieStorage = wkGetDefaultHTTPCookieStorage())
132 policy = CFHTTPCookieStorageGetCookieAcceptPolicy(defaultCookieStorage);
133 return policy == CFHTTPCookieStorageAcceptPolicyOnlyFromMainDocumentDomain || policy == CFHTTPCookieStorageAcceptPolicyAlways;