String cookies(const Document* /*document*/, const KURL& url) { CFHTTPCookieStorageRef cookieStorage = currentCookieStorage(); if (!cookieStorage) return String(); String cookieString; RetainPtr<CFURLRef> urlCF(AdoptCF, url.createCFURL()); bool secure = equalIgnoringCase(url.protocol(), "https"); RetainPtr<CFArrayRef> cookiesCF(AdoptCF, CFHTTPCookieStorageCopyCookiesForURL(cookieStorage, urlCF.get(), secure)); // <rdar://problem/5632883> CFHTTPCookieStorage happily stores an empty cookie, which would be sent as "Cookie: =". // We have a workaround in setCookies() to prevent that, but we also need to avoid sending cookies that were previously stored. CFIndex count = CFArrayGetCount(cookiesCF.get()); RetainPtr<CFMutableArrayRef> cookiesForURLFilteredCopy(AdoptCF, CFArrayCreateMutable(0, count, &kCFTypeArrayCallBacks)); for (CFIndex i = 0; i < count; ++i) { CFHTTPCookieRef cookie = (CFHTTPCookieRef)CFArrayGetValueAtIndex(cookiesCF.get(), i); if (CFStringGetLength(CFHTTPCookieGetName(cookie)) != 0) CFArrayAppendValue(cookiesForURLFilteredCopy.get(), cookie); } RetainPtr<CFDictionaryRef> headerCF(AdoptCF, CFHTTPCookieCopyRequestHeaderFields(kCFAllocatorDefault, cookiesForURLFilteredCopy.get())); return (CFStringRef)CFDictionaryGetValue(headerCF.get(), s_cookieCF); }
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 = 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; // FIXME: Need API for if a cookie is a session cookie. rawCookies.uncheckedAppend(Cookie(name, value, domain, path, expires, httpOnly, secure, session)); } return true; }
void setCookies(Document* document, const KURL& url, const String& value) { // <rdar://problem/5632883> CFHTTPCookieStorage stores an empty cookie, which would be sent as "Cookie: =". if (value.isEmpty()) return; CFHTTPCookieStorageRef cookieStorage = currentCookieStorage(); if (!cookieStorage) return; RetainPtr<CFURLRef> urlCF(AdoptCF, url.createCFURL()); RetainPtr<CFURLRef> firstPartyForCookiesCF(AdoptCF, document->firstPartyForCookies().createCFURL()); // <http://bugs.webkit.org/show_bug.cgi?id=6531>, <rdar://4409034> // cookiesWithResponseHeaderFields doesn't parse cookies without a value 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 KURL& url) { #if USE(CFNETWORK) CFHTTPCookieStorageRef defaultCookieStorage = wkGetDefaultHTTPCookieStorage(); if (!defaultCookieStorage) return String(); String cookieString; RetainPtr<CFURLRef> urlCF(AdoptCF, url.createCFURL()); bool secure = equalIgnoringCase(url.protocol(), "https"); RetainPtr<CFArrayRef> cookiesCF(AdoptCF, CFHTTPCookieStorageCopyCookiesForURL(defaultCookieStorage, urlCF.get(), secure)); RetainPtr<CFDictionaryRef> headerCF(AdoptCF, CFHTTPCookieCopyRequestHeaderFields(kCFAllocatorDefault, cookiesCF.get())); return (CFStringRef)CFDictionaryGetValue(headerCF.get(), s_cookieCF); #else DeprecatedString str = url.url(); str.append((UChar)'\0'); DWORD count = str.length(); InternetGetCookie((UChar*)str.unicode(), 0, 0, &count); if (count <= 1) // Null terminator counts as 1. return String(); UChar* buffer = new UChar[count]; InternetGetCookie((UChar*)str.unicode(), 0, buffer, &count); String& result = String(buffer, count-1); // Ignore the null terminator. delete[] buffer; return result; #endif }
void setCookies(const KURL& url, const KURL& policyURL, const String& value) { #if USE(CFNETWORK) CFHTTPCookieStorageRef defaultCookieStorage = wkGetDefaultHTTPCookieStorage(); if (!defaultCookieStorage) return; RetainPtr<CFURLRef> urlCF(AdoptCF, url.createCFURL()); RetainPtr<CFURLRef> policyURLCF(AdoptCF, policyURL.createCFURL()); // <http://bugzilla.opendarwin.org/show_bug.cgi?id=6531>, <rdar://4409034> // cookiesWithResponseHeaderFields doesn't parse cookies without a value 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(defaultCookieStorage, cookiesCF.get(), urlCF.get(), policyURLCF.get()); #else // FIXME: Deal with the policy URL. DeprecatedString str = url.url(); str.append((UChar)'\0'); DeprecatedString val = value.deprecatedString(); val.append((UChar)'\0'); InternetSetCookie((UChar*)str.unicode(), 0, (UChar*)val.unicode()); #endif }
String cookieRequestHeaderFieldValue(const NetworkStorageSession& session, const KURL& /*firstParty*/, const KURL& url) { RetainPtr<CFURLRef> urlCF(AdoptCF, url.createCFURL()); bool secure = url.protocolIs("https"); RetainPtr<CFArrayRef> cookiesCF(AdoptCF, CFHTTPCookieStorageCopyCookiesForURL(session.cookieStorage().get(), urlCF.get(), secure)); RetainPtr<CFDictionaryRef> headerCF(AdoptCF, CFHTTPCookieCopyRequestHeaderFields(kCFAllocatorDefault, cookiesCF.get())); return (CFStringRef)CFDictionaryGetValue(headerCF.get(), s_cookieCF); }
String cookieRequestHeaderFieldValue(const Document* /*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); }
void deleteCookie(const NetworkStorageSession& session, const KURL& url, const String& name) { RetainPtr<CFHTTPCookieStorageRef> cookieStorage = session.cookieStorage(); RetainPtr<CFURLRef> urlCF(AdoptCF, 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 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; } } }