CookieJarCFNetWin.cpp [plain text]
#include "config.h"
#include "CookieJar.h"
#include "Cookie.h"
#include "CookieStorageWin.h"
#include "Document.h"
#include "KURL.h"
#include "PlatformString.h"
#include "ResourceHandle.h"
#include <CFNetwork/CFHTTPCookiesPriv.h>
#include <CoreFoundation/CoreFoundation.h>
#include <WebKitSystemInterface/WebKitSystemInterface.h>
#include <windows.h>
namespace WebCore {
static const CFStringRef s_setCookieKeyCF = CFSTR("Set-Cookie");
static const CFStringRef s_cookieCF = CFSTR("Cookie");
static RetainPtr<CFArrayRef> filterCookies(CFArrayRef unfilteredCookies)
{
CFIndex count = CFArrayGetCount(unfilteredCookies);
RetainPtr<CFMutableArrayRef> filteredCookies(AdoptCF, CFArrayCreateMutable(0, count, &kCFTypeArrayCallBacks));
for (CFIndex i = 0; i < count; ++i) {
CFHTTPCookieRef cookie = (CFHTTPCookieRef)CFArrayGetValueAtIndex(unfilteredCookies, i);
if (!CFStringGetLength(CFHTTPCookieGetName(cookie)))
continue;
if (CFHTTPCookieIsHTTPOnly(cookie))
continue;
CFArrayAppendValue(filteredCookies.get(), cookie);
}
return filteredCookies;
}
void setCookies(Document* document, const KURL& url, const String& value)
{
if (value.isEmpty())
return;
CFHTTPCookieStorageRef cookieStorage = currentCookieStorage();
if (!cookieStorage)
return;
RetainPtr<CFURLRef> urlCF(AdoptCF, url.createCFURL());
RetainPtr<CFURLRef> firstPartyForCookiesCF(AdoptCF, document->firstPartyForCookies().createCFURL());
String cookieString = value.contains('=') ? value : value + "=";
RetainPtr<CFStringRef> cookieStringCF(AdoptCF, cookieString.createCFString());
RetainPtr<CFDictionaryRef> headerFieldsCF(AdoptCF, CFDictionaryCreate(kCFAllocatorDefault,
(const void**)&s_setCookieKeyCF, (const void**)&cookieStringCF, 1,
&kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
RetainPtr<CFArrayRef> cookiesCF(AdoptCF, CFHTTPCookieCreateWithResponseHeaderFields(kCFAllocatorDefault,
headerFieldsCF.get(), urlCF.get()));
CFHTTPCookieStorageSetCookies(cookieStorage, filterCookies(cookiesCF.get()).get(), urlCF.get(), firstPartyForCookiesCF.get());
}
String cookies(const Document* , const KURL& url)
{
CFHTTPCookieStorageRef cookieStorage = currentCookieStorage();
if (!cookieStorage)
return String();
RetainPtr<CFURLRef> urlCF(AdoptCF, url.createCFURL());
bool secure = url.protocolIs("https");
RetainPtr<CFArrayRef> cookiesCF(AdoptCF, CFHTTPCookieStorageCopyCookiesForURL(cookieStorage, urlCF.get(), secure));
RetainPtr<CFDictionaryRef> headerCF(AdoptCF, CFHTTPCookieCopyRequestHeaderFields(kCFAllocatorDefault, filterCookies(cookiesCF.get()).get()));
return (CFStringRef)CFDictionaryGetValue(headerCF.get(), s_cookieCF);
}
String cookieRequestHeaderFieldValue(const Document* , const KURL& url)
{
CFHTTPCookieStorageRef cookieStorage = currentCookieStorage();
if (!cookieStorage)
return String();
RetainPtr<CFURLRef> urlCF(AdoptCF, url.createCFURL());
bool secure = url.protocolIs("https");
RetainPtr<CFArrayRef> cookiesCF(AdoptCF, CFHTTPCookieStorageCopyCookiesForURL(cookieStorage, urlCF.get(), secure));
RetainPtr<CFDictionaryRef> headerCF(AdoptCF, CFHTTPCookieCopyRequestHeaderFields(kCFAllocatorDefault, cookiesCF.get()));
return (CFStringRef)CFDictionaryGetValue(headerCF.get(), s_cookieCF);
}
bool cookiesEnabled(const Document* )
{
CFHTTPCookieStorageAcceptPolicy policy = CFHTTPCookieStorageAcceptPolicyOnlyFromMainDocumentDomain;
if (CFHTTPCookieStorageRef cookieStorage = currentCookieStorage())
policy = CFHTTPCookieStorageGetCookieAcceptPolicy(cookieStorage);
return policy == CFHTTPCookieStorageAcceptPolicyOnlyFromMainDocumentDomain || policy == CFHTTPCookieStorageAcceptPolicyAlways;
}
bool getRawCookies(const Document*, const KURL& url, Vector<Cookie>& rawCookies)
{
rawCookies.clear();
CFHTTPCookieStorageRef cookieStorage = currentCookieStorage();
if (!cookieStorage)
return false;
RetainPtr<CFURLRef> urlCF(AdoptCF, url.createCFURL());
bool sendSecureCookies = url.protocolIs("https");
RetainPtr<CFArrayRef> cookiesCF(AdoptCF, CFHTTPCookieStorageCopyCookiesForURL(cookieStorage, urlCF.get(), sendSecureCookies));
CFIndex count = CFArrayGetCount(cookiesCF.get());
rawCookies.reserveCapacity(count);
for (CFIndex i = 0; i < count; i++) {
CFHTTPCookieRef cookie = (CFHTTPCookieRef)CFArrayGetValueAtIndex(cookiesCF.get(), i);
String name = CFHTTPCookieGetName(cookie);
String value = CFHTTPCookieGetValue(cookie);
String domain = CFHTTPCookieGetDomain(cookie);
String path = CFHTTPCookieGetPath(cookie);
double expires = (CFDateGetAbsoluteTime(CFHTTPCookieGetExpiratonDate(cookie)) + kCFAbsoluteTimeIntervalSince1970) * 1000;
bool httpOnly = CFHTTPCookieIsHTTPOnly(cookie);
bool secure = CFHTTPCookieIsSecure(cookie);
bool session = false;
rawCookies.uncheckedAppend(Cookie(name, value, domain, path, expires, httpOnly, secure, session));
}
return true;
}
void deleteCookie(const Document*, const KURL& url, const String& name)
{
CFHTTPCookieStorageRef cookieStorage = currentCookieStorage();
if (!cookieStorage)
return;
RetainPtr<CFURLRef> urlCF(AdoptCF, url.createCFURL());
bool sendSecureCookies = url.protocolIs("https");
RetainPtr<CFArrayRef> cookiesCF(AdoptCF, CFHTTPCookieStorageCopyCookiesForURL(cookieStorage, urlCF.get(), sendSecureCookies));
CFIndex count = CFArrayGetCount(cookiesCF.get());
for (CFIndex i = 0; i < count; i++) {
CFHTTPCookieRef cookie = (CFHTTPCookieRef)CFArrayGetValueAtIndex(cookiesCF.get(), i);
String cookieName = CFHTTPCookieGetName(cookie);
if (cookieName == name) {
CFHTTPCookieStorageDeleteCookie(cookieStorage, cookie);
break;
}
}
}
}