CookieJarCFNet.cpp [plain text]
#include "config.h"
#include "PlatformCookieJar.h"
#if USE(CFNETWORK)
#include "Cookie.h"
#include "URL.h"
#include "NetworkStorageSession.h"
#include "SoftLinking.h"
#include <CFNetwork/CFHTTPCookiesPriv.h>
#include <CoreFoundation/CoreFoundation.h>
#include <wtf/text/WTFString.h>
#if PLATFORM(WIN)
#include <WebKitSystemInterface/WebKitSystemInterface.h>
#include <windows.h>
#endif
#if PLATFORM(WIN) || (PLATFORM(MAC) && __MAC_OS_X_VERSION_MIN_REQUIRED <= 1090) || (PLATFORM(IOS) && __IPHONE_OS_VERSION_MIN_REQUIRED < 80000)
enum {
CFHTTPCookieStorageAcceptPolicyExclusivelyFromMainDocumentDomain = 3
};
#endif
namespace WebCore {
static const CFStringRef s_setCookieKeyCF = CFSTR("Set-Cookie");
static const CFStringRef s_cookieCF = CFSTR("Cookie");
static inline RetainPtr<CFStringRef> cookieDomain(CFHTTPCookieRef cookie)
{
return adoptCF(CFHTTPCookieCopyDomain(cookie));
}
static inline CFAbsoluteTime cookieExpirationTime(CFHTTPCookieRef cookie)
{
return CFHTTPCookieGetExpirationTime(cookie);
}
static inline RetainPtr<CFStringRef> cookieName(CFHTTPCookieRef cookie)
{
return adoptCF(CFHTTPCookieCopyName(cookie));
}
static inline RetainPtr<CFStringRef> cookiePath(CFHTTPCookieRef cookie)
{
return adoptCF(CFHTTPCookieCopyPath(cookie));
}
static inline RetainPtr<CFStringRef> cookieValue(CFHTTPCookieRef cookie)
{
return adoptCF(CFHTTPCookieCopyValue(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(cookieName(cookie).get()))
continue;
if (CFHTTPCookieIsHTTPOnly(cookie))
continue;
CFArrayAppendValue(filteredCookies.get(), cookie);
}
return filteredCookies;
}
static RetainPtr<CFArrayRef> copyCookiesForURLWithFirstPartyURL(const NetworkStorageSession& session, const URL& firstParty, const URL& url)
{
bool secure = url.protocolIs("https");
#if (PLATFORM(IOS) && __IPHONE_OS_VERSION_MIN_REQUIRED >= 80000) || (PLATFORM(MAC) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 10100)
return adoptCF(_CFHTTPCookieStorageCopyCookiesForURLWithMainDocumentURL(session.cookieStorage().get(), url.createCFURL().get(), firstParty.createCFURL().get(), secure));
#else
UNUSED_PARAM(firstParty);
return adoptCF(CFHTTPCookieStorageCopyCookiesForURL(session.cookieStorage().get(), url.createCFURL().get(), secure));
#endif
}
void setCookiesFromDOM(const NetworkStorageSession& session, const URL& firstParty, const URL& url, const String& value)
{
if (value.isEmpty())
return;
RetainPtr<CFURLRef> urlCF = url.createCFURL();
RetainPtr<CFURLRef> firstPartyForCookiesCF = firstParty.createCFURL();
String cookieString = value.contains('=') ? value : value + "=";
RetainPtr<CFStringRef> cookieStringCF = 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(session.cookieStorage().get(), filterCookies(cookiesCF.get()).get(), urlCF.get(), firstPartyForCookiesCF.get());
}
String cookiesForDOM(const NetworkStorageSession& session, const URL& firstParty, const URL& url)
{
RetainPtr<CFArrayRef> cookiesCF = copyCookiesForURLWithFirstPartyURL(session, firstParty, url);
RetainPtr<CFDictionaryRef> headerCF = adoptCF(CFHTTPCookieCopyRequestHeaderFields(kCFAllocatorDefault, filterCookies(cookiesCF.get()).get()));
return (CFStringRef)CFDictionaryGetValue(headerCF.get(), s_cookieCF);
}
String cookieRequestHeaderFieldValue(const NetworkStorageSession& session, const URL& firstParty, const URL& url)
{
RetainPtr<CFArrayRef> cookiesCF = copyCookiesForURLWithFirstPartyURL(session, firstParty, url);
RetainPtr<CFDictionaryRef> headerCF = adoptCF(CFHTTPCookieCopyRequestHeaderFields(kCFAllocatorDefault, cookiesCF.get()));
return (CFStringRef)CFDictionaryGetValue(headerCF.get(), s_cookieCF);
}
bool cookiesEnabled(const NetworkStorageSession& session, const URL& , const URL& )
{
CFHTTPCookieStorageAcceptPolicy policy = CFHTTPCookieStorageGetCookieAcceptPolicy(session.cookieStorage().get());
return policy == CFHTTPCookieStorageAcceptPolicyOnlyFromMainDocumentDomain || policy == CFHTTPCookieStorageAcceptPolicyExclusivelyFromMainDocumentDomain || policy == CFHTTPCookieStorageAcceptPolicyAlways;
}
bool getRawCookies(const NetworkStorageSession& session, const URL& firstParty, const URL& url, Vector<Cookie>& rawCookies)
{
rawCookies.clear();
RetainPtr<CFArrayRef> cookiesCF = copyCookiesForURLWithFirstPartyURL(session, firstParty, url);
CFIndex count = CFArrayGetCount(cookiesCF.get());
rawCookies.reserveCapacity(count);
for (CFIndex i = 0; i < count; i++) {
CFHTTPCookieRef cookie = (CFHTTPCookieRef)CFArrayGetValueAtIndex(cookiesCF.get(), i);
String name = cookieName(cookie).get();
String value = cookieValue(cookie).get();
String domain = cookieDomain(cookie).get();
String path = cookiePath(cookie).get();
double expires = (cookieExpirationTime(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 NetworkStorageSession& session, const URL& url, const String& name)
{
RetainPtr<CFHTTPCookieStorageRef> cookieStorage = session.cookieStorage();
RetainPtr<CFURLRef> urlCF = url.createCFURL();
bool sendSecureCookies = url.protocolIs("https");
RetainPtr<CFArrayRef> cookiesCF = adoptCF(CFHTTPCookieStorageCopyCookiesForURL(cookieStorage.get(), urlCF.get(), sendSecureCookies));
CFIndex count = CFArrayGetCount(cookiesCF.get());
for (CFIndex i = 0; i < count; i++) {
CFHTTPCookieRef cookie = (CFHTTPCookieRef)CFArrayGetValueAtIndex(cookiesCF.get(), i);
if (String(cookieName(cookie).get()) == name) {
CFHTTPCookieStorageDeleteCookie(cookieStorage.get(), cookie);
break;
}
}
}
void getHostnamesWithCookies(const NetworkStorageSession& session, HashSet<String>& hostnames)
{
RetainPtr<CFArrayRef> cookiesCF = adoptCF(CFHTTPCookieStorageCopyCookies(session.cookieStorage().get()));
if (!cookiesCF)
return;
CFIndex count = CFArrayGetCount(cookiesCF.get());
for (CFIndex i = 0; i < count; ++i) {
CFHTTPCookieRef cookie = static_cast<CFHTTPCookieRef>(const_cast<void *>(CFArrayGetValueAtIndex(cookiesCF.get(), i)));
RetainPtr<CFStringRef> domain = cookieDomain(cookie);
hostnames.add(domain.get());
}
}
void deleteCookiesForHostname(const NetworkStorageSession& session, const String& hostname)
{
RetainPtr<CFHTTPCookieStorageRef> cookieStorage = session.cookieStorage();
RetainPtr<CFArrayRef> cookiesCF = adoptCF(CFHTTPCookieStorageCopyCookies(cookieStorage.get()));
if (!cookiesCF)
return;
CFIndex count = CFArrayGetCount(cookiesCF.get());
for (CFIndex i = count - 1; i >=0; i--) {
CFHTTPCookieRef cookie = static_cast<CFHTTPCookieRef>(const_cast<void *>(CFArrayGetValueAtIndex(cookiesCF.get(), i)));
RetainPtr<CFStringRef> domain = cookieDomain(cookie);
if (String(domain.get()) == hostname)
CFHTTPCookieStorageDeleteCookie(cookieStorage.get(), cookie);
}
}
void deleteAllCookies(const NetworkStorageSession& session)
{
CFHTTPCookieStorageDeleteAllCookies(session.cookieStorage().get());
}
void deleteAllCookiesModifiedAfterDate(const NetworkStorageSession&, double)
{
}
}
#endif // USE(CFNETWORK)