CookieJarCFNet.cpp [plain text]
#include "config.h"
#include "PlatformCookieJar.h"
#if USE(CFURLCONNECTION)
#include "CFNetworkSPI.h"
#include "Cookie.h"
#include "URL.h"
#include "NetworkStorageSession.h"
#include "NotImplemented.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)
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)
{
ASSERT(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(COCOA)
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
}
static CFArrayRef createCookies(CFDictionaryRef headerFields, CFURLRef url)
{
#if PLATFORM(IOS)
CFArrayRef parsedCookies = _CFHTTPParsedCookiesWithResponseHeaderFields(kCFAllocatorDefault, headerFields, url);
#else
CFArrayRef parsedCookies = CFHTTPCookieCreateWithResponseHeaderFields(kCFAllocatorDefault, headerFields, url);
#endif
if (!parsedCookies)
parsedCookies = CFArrayCreate(kCFAllocatorDefault, 0, 0, &kCFTypeArrayCallBacks);
return parsedCookies;
}
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();
auto cookieStringCFPtr = cookieStringCF.get();
RetainPtr<CFDictionaryRef> headerFieldsCF = adoptCF(CFDictionaryCreate(kCFAllocatorDefault,
(const void**)&s_setCookieKeyCF, (const void**)&cookieStringCFPtr, 1,
&kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
RetainPtr<CFArrayRef> unfilteredCookies = adoptCF(createCookies(headerFieldsCF.get(), urlCF.get()));
CFHTTPCookieStorageSetCookies(session.cookieStorage().get(), filterCookies(unfilteredCookies.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 addCookie(const NetworkStorageSession&, const URL&, const Cookie&)
{
notImplemented();
}
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 deleteAllCookies(const NetworkStorageSession& session)
{
CFHTTPCookieStorageDeleteAllCookies(session.cookieStorage().get());
}
#if PLATFORM(WIN)
void deleteCookiesForHostnames(const NetworkStorageSession& session, const Vector<String>& hostnames)
{
}
void deleteAllCookiesModifiedSince(const NetworkStorageSession&, std::chrono::system_clock::time_point)
{
}
#endif
}
#endif // USE(CFURLCONNECTION)