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"
31 #include "ResourceHandle.h"
34 #include <CoreFoundation/CoreFoundation.h>
35 #include <CFNetwork/CFHTTPCookiesPriv.h>
36 #include <WebKitSystemInterface/WebKitSystemInterface.h>
45 static const CFStringRef s_setCookieKeyCF = CFSTR("Set-Cookie");
46 static const CFStringRef s_cookieCF = CFSTR("Cookie");
50 void setCookies(const Document* /*document*/, const KURL& url, const KURL& policyURL, const String& value)
53 // <rdar://problem/5632883> CFHTTPCookieStorage happily stores an empty cookie, which would be sent as "Cookie: =".
57 CFHTTPCookieStorageRef defaultCookieStorage = wkGetDefaultHTTPCookieStorage();
58 if (!defaultCookieStorage)
61 RetainPtr<CFURLRef> urlCF(AdoptCF, url.createCFURL());
62 RetainPtr<CFURLRef> policyURLCF(AdoptCF, policyURL.createCFURL());
64 // <http://bugzilla.opendarwin.org/show_bug.cgi?id=6531>, <rdar://4409034>
65 // cookiesWithResponseHeaderFields doesn't parse cookies without a value
66 String cookieString = value.contains('=') ? value : value + "=";
68 RetainPtr<CFStringRef> cookieStringCF(AdoptCF, cookieString.createCFString());
69 RetainPtr<CFDictionaryRef> headerFieldsCF(AdoptCF, CFDictionaryCreate(kCFAllocatorDefault, (const void**)&s_setCookieKeyCF,
70 (const void**)&cookieStringCF, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
72 RetainPtr<CFArrayRef> cookiesCF(AdoptCF, CFHTTPCookieCreateWithResponseHeaderFields(kCFAllocatorDefault,
73 headerFieldsCF.get(), urlCF.get()));
75 CFHTTPCookieStorageSetCookies(defaultCookieStorage, cookiesCF.get(), urlCF.get(), policyURLCF.get());
77 // FIXME: Deal with the policy URL.
78 DeprecatedString str = url.deprecatedString();
79 str.append((UChar)'\0');
80 DeprecatedString val = value.deprecatedString();
81 val.append((UChar)'\0');
82 InternetSetCookie((UChar*)str.unicode(), 0, (UChar*)val.unicode());
86 String cookies(const Document* /*document*/, const KURL& url)
89 CFHTTPCookieStorageRef defaultCookieStorage = wkGetDefaultHTTPCookieStorage();
90 if (!defaultCookieStorage)
94 RetainPtr<CFURLRef> urlCF(AdoptCF, url.createCFURL());
96 bool secure = equalIgnoringCase(url.protocol(), "https");
98 RetainPtr<CFArrayRef> cookiesCF(AdoptCF, CFHTTPCookieStorageCopyCookiesForURL(defaultCookieStorage, urlCF.get(), secure));
100 // <rdar://problem/5632883> CFHTTPCookieStorage happily stores an empty cookie, which would be sent as "Cookie: =".
101 // We have a workaround in setCookies() to prevent that, but we also need to avoid sending cookies that were previously stored.
102 CFIndex count = CFArrayGetCount(cookiesCF.get());
103 RetainPtr<CFMutableArrayRef> cookiesForURLFilteredCopy(AdoptCF, CFArrayCreateMutable(0, count, &kCFTypeArrayCallBacks));
104 for (CFIndex i = 0; i < count; ++i) {
105 CFHTTPCookieRef cookie = (CFHTTPCookieRef)CFArrayGetValueAtIndex(cookiesCF.get(), i);
106 if (CFStringGetLength(CFHTTPCookieGetName(cookie)) != 0)
107 CFArrayAppendValue(cookiesForURLFilteredCopy.get(), cookie);
109 RetainPtr<CFDictionaryRef> headerCF(AdoptCF, CFHTTPCookieCopyRequestHeaderFields(kCFAllocatorDefault, cookiesForURLFilteredCopy.get()));
111 return (CFStringRef)CFDictionaryGetValue(headerCF.get(), s_cookieCF);
113 DeprecatedString str = url.deprecatedString();
114 str.append((UChar)'\0');
116 DWORD count = str.length();
117 InternetGetCookie((UChar*)str.unicode(), 0, 0, &count);
118 if (count <= 1) // Null terminator counts as 1.
121 UChar* buffer = new UChar[count];
122 InternetGetCookie((UChar*)str.unicode(), 0, buffer, &count);
123 String& result = String(buffer, count-1); // Ignore the null terminator.
129 bool cookiesEnabled(const Document* /*document*/)
131 CFHTTPCookieStorageAcceptPolicy policy = CFHTTPCookieStorageAcceptPolicyOnlyFromMainDocumentDomain;
132 if (CFHTTPCookieStorageRef defaultCookieStorage = wkGetDefaultHTTPCookieStorage())
133 policy = CFHTTPCookieStorageGetCookieAcceptPolicy(defaultCookieStorage);
134 return policy == CFHTTPCookieStorageAcceptPolicyOnlyFromMainDocumentDomain || policy == CFHTTPCookieStorageAcceptPolicyAlways;