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>
43
static const CFStringRef s_setCookieKeyCF = CFSTR("Set-Cookie");
44
static const CFStringRef s_cookieCF = CFSTR("Cookie");
48
void setCookies(const KURL& url, const KURL& policyURL, const String& value)
51
if (!ResourceHandle::cookieStorage())
54
RetainPtr<CFURLRef> urlCF(AdoptCF, url.createCFURL());
55
RetainPtr<CFURLRef> policyURLCF(AdoptCF, policyURL.createCFURL());
57
// <http://bugzilla.opendarwin.org/show_bug.cgi?id=6531>, <rdar://4409034>
58
// cookiesWithResponseHeaderFields doesn't parse cookies without a value
59
String cookieString = value.contains('=') ? value : value + "=";
61
RetainPtr<CFStringRef> cookieStringCF(AdoptCF, cookieString.createCFString());
62
RetainPtr<CFDictionaryRef> headerFieldsCF(AdoptCF, CFDictionaryCreate(kCFAllocatorDefault, (const void**)&s_setCookieKeyCF,
63
(const void**)&cookieStringCF, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
65
RetainPtr<CFArrayRef> cookiesCF(AdoptCF, CFHTTPCookieCreateWithResponseHeaderFields(kCFAllocatorDefault,
66
headerFieldsCF.get(), urlCF.get()));
68
CFHTTPCookieStorageSetCookies(ResourceHandle::cookieStorage(), cookiesCF.get(), urlCF.get(), policyURLCF.get());
70
// FIXME: Deal with the policy URL.
71
DeprecatedString str = url.url();
72
str.append((UChar)'\0');
73
DeprecatedString val = value.deprecatedString();
74
val.append((UChar)'\0');
75
InternetSetCookie((UChar*)str.unicode(), 0, (UChar*)val.unicode());
79
String cookies(const KURL& url)
82
if (!ResourceHandle::cookieStorage())
86
RetainPtr<CFURLRef> urlCF(AdoptCF, url.createCFURL());
88
bool secure = equalIgnoringCase(url.protocol(), "https");
90
RetainPtr<CFArrayRef> cookiesCF(AdoptCF, CFHTTPCookieStorageCopyCookiesForURL(ResourceHandle::cookieStorage(), urlCF.get(), secure));
91
RetainPtr<CFDictionaryRef> headerCF(AdoptCF, CFHTTPCookieCopyRequestHeaderFields(kCFAllocatorDefault, cookiesCF.get()));
93
return (CFStringRef)CFDictionaryGetValue(headerCF.get(), s_cookieCF);
95
DeprecatedString str = url.url();
96
str.append((UChar)'\0');
98
DWORD count = str.length();
99
InternetGetCookie((UChar*)str.unicode(), 0, 0, &count);
100
if (count <= 1) // Null terminator counts as 1.
103
UChar* buffer = new UChar[count];
104
InternetGetCookie((UChar*)str.unicode(), 0, buffer, &count);
105
String& result = String(buffer, count-1); // Ignore the null terminator.
111
bool cookiesEnabled()
113
return ResourceHandle::cookieStorageAcceptPolicy() == CFHTTPCookieStorageAcceptPolicyOnlyFromMainDocumentDomain ||
114
ResourceHandle::cookieStorageAcceptPolicy() == CFHTTPCookieStorageAcceptPolicyAlways;