2 * Copyright (C) 2006, 2007, 2008 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.
27 #include "ResourceRequestCFNet.h"
29 #include "ResourceHandle.h"
30 #include "ResourceRequest.h"
33 #include "FormDataStreamCFNet.h"
34 #include <CFNetwork/CFURLRequestPriv.h>
38 #include "ResourceLoadPriority.h"
39 #include "WebCoreSystemInterface.h"
44 #include <WebKitSystemInterface/WebKitSystemInterface.h>
49 bool ResourceRequest::s_httpPipeliningEnabled = false;
53 typedef void (*CFURLRequestSetContentDispositionEncodingFallbackArrayFunction)(CFMutableURLRequestRef, CFArrayRef);
54 typedef CFArrayRef (*CFURLRequestCopyContentDispositionEncodingFallbackArrayFunction)(CFURLRequestRef);
57 static HMODULE findCFNetworkModule()
60 return GetModuleHandleA("CFNetwork");
62 return GetModuleHandleA("CFNetwork_debug");
66 static CFURLRequestSetContentDispositionEncodingFallbackArrayFunction findCFURLRequestSetContentDispositionEncodingFallbackArrayFunction()
68 return reinterpret_cast<CFURLRequestSetContentDispositionEncodingFallbackArrayFunction>(GetProcAddress(findCFNetworkModule(), "_CFURLRequestSetContentDispositionEncodingFallbackArray"));
71 static CFURLRequestCopyContentDispositionEncodingFallbackArrayFunction findCFURLRequestCopyContentDispositionEncodingFallbackArrayFunction()
73 return reinterpret_cast<CFURLRequestCopyContentDispositionEncodingFallbackArrayFunction>(GetProcAddress(findCFNetworkModule(), "_CFURLRequestCopyContentDispositionEncodingFallbackArray"));
76 static CFURLRequestSetContentDispositionEncodingFallbackArrayFunction findCFURLRequestSetContentDispositionEncodingFallbackArrayFunction()
78 return reinterpret_cast<CFURLRequestSetContentDispositionEncodingFallbackArrayFunction>(dlsym(RTLD_DEFAULT, "_CFURLRequestSetContentDispositionEncodingFallbackArray"));
81 static CFURLRequestCopyContentDispositionEncodingFallbackArrayFunction findCFURLRequestCopyContentDispositionEncodingFallbackArrayFunction()
83 return reinterpret_cast<CFURLRequestCopyContentDispositionEncodingFallbackArrayFunction>(dlsym(RTLD_DEFAULT, "_CFURLRequestCopyContentDispositionEncodingFallbackArray"));
87 static void setContentDispositionEncodingFallbackArray(CFMutableURLRequestRef request, CFArrayRef fallbackArray)
89 static CFURLRequestSetContentDispositionEncodingFallbackArrayFunction function = findCFURLRequestSetContentDispositionEncodingFallbackArrayFunction();
91 function(request, fallbackArray);
94 static CFArrayRef copyContentDispositionEncodingFallbackArray(CFURLRequestRef request)
96 static CFURLRequestCopyContentDispositionEncodingFallbackArrayFunction function = findCFURLRequestCopyContentDispositionEncodingFallbackArrayFunction();
99 return function(request);
102 CFURLRequestRef ResourceRequest::cfURLRequest() const
104 updatePlatformRequest();
106 return m_cfRequest.get();
109 static inline void setHeaderFields(CFMutableURLRequestRef request, const HTTPHeaderMap& requestHeaders)
111 // Remove existing headers first, as some of them may no longer be present in the map.
112 RetainPtr<CFDictionaryRef> oldHeaderFields(AdoptCF, CFURLRequestCopyAllHTTPHeaderFields(request));
113 CFIndex oldHeaderFieldCount = CFDictionaryGetCount(oldHeaderFields.get());
114 if (oldHeaderFieldCount) {
115 Vector<CFStringRef> oldHeaderFieldNames(oldHeaderFieldCount);
116 CFDictionaryGetKeysAndValues(oldHeaderFields.get(), reinterpret_cast<const void**>(&oldHeaderFieldNames[0]), 0);
117 for (CFIndex i = 0; i < oldHeaderFieldCount; ++i)
118 CFURLRequestSetHTTPHeaderFieldValue(request, oldHeaderFieldNames[i], 0);
121 HTTPHeaderMap::const_iterator end = requestHeaders.end();
122 for (HTTPHeaderMap::const_iterator it = requestHeaders.begin(); it != end; ++it) {
123 CFStringRef key = it->first.createCFString();
124 CFStringRef value = it->second.createCFString();
125 CFURLRequestSetHTTPHeaderFieldValue(request, key, value);
131 void ResourceRequest::doUpdatePlatformRequest()
133 CFMutableURLRequestRef cfRequest;
135 RetainPtr<CFURLRef> url(AdoptCF, ResourceRequest::url().createCFURL());
136 RetainPtr<CFURLRef> firstPartyForCookies(AdoptCF, ResourceRequest::firstPartyForCookies().createCFURL());
138 cfRequest = CFURLRequestCreateMutableCopy(0, m_cfRequest.get());
139 CFURLRequestSetURL(cfRequest, url.get());
140 CFURLRequestSetMainDocumentURL(cfRequest, firstPartyForCookies.get());
141 CFURLRequestSetCachePolicy(cfRequest, (CFURLRequestCachePolicy)cachePolicy());
142 CFURLRequestSetTimeoutInterval(cfRequest, timeoutInterval());
144 cfRequest = CFURLRequestCreateMutable(0, url.get(), (CFURLRequestCachePolicy)cachePolicy(), timeoutInterval(), firstPartyForCookies.get());
145 #if USE(CFURLSTORAGESESSIONS)
146 wkSetRequestStorageSession(ResourceHandle::currentStorageSession(), cfRequest);
149 RetainPtr<CFStringRef> requestMethod(AdoptCF, httpMethod().createCFString());
150 CFURLRequestSetHTTPRequestMethod(cfRequest, requestMethod.get());
152 if (httpPipeliningEnabled())
153 wkSetHTTPPipeliningPriority(cfRequest, toHTTPPipeliningPriority(m_priority));
155 setHeaderFields(cfRequest, httpHeaderFields());
156 RefPtr<FormData> formData = httpBody();
157 if (formData && !formData->isEmpty())
158 WebCore::setHTTPBody(cfRequest, formData);
159 CFURLRequestSetShouldHandleHTTPCookies(cfRequest, allowCookies());
161 unsigned fallbackCount = m_responseContentDispositionEncodingFallbackArray.size();
162 RetainPtr<CFMutableArrayRef> encodingFallbacks(AdoptCF, CFArrayCreateMutable(kCFAllocatorDefault, fallbackCount, 0));
163 for (unsigned i = 0; i != fallbackCount; ++i) {
164 RetainPtr<CFStringRef> encodingName(AdoptCF, m_responseContentDispositionEncodingFallbackArray[i].createCFString());
165 CFStringEncoding encoding = CFStringConvertIANACharSetNameToEncoding(encodingName.get());
166 if (encoding != kCFStringEncodingInvalidId)
167 CFArrayAppendValue(encodingFallbacks.get(), reinterpret_cast<const void*>(encoding));
169 setContentDispositionEncodingFallbackArray(cfRequest, encodingFallbacks.get());
172 RetainPtr<CFHTTPCookieStorageRef> cookieStorage(AdoptCF, CFURLRequestCopyHTTPCookieStorage(m_cfRequest.get()));
174 CFURLRequestSetHTTPCookieStorage(cfRequest, cookieStorage.get());
175 CFURLRequestSetHTTPCookieStorageAcceptPolicy(cfRequest, CFURLRequestGetHTTPCookieStorageAcceptPolicy(m_cfRequest.get()));
176 CFURLRequestSetSSLProperties(cfRequest, CFURLRequestGetSSLProperties(m_cfRequest.get()));
179 m_cfRequest.adoptCF(cfRequest);
181 updateNSURLRequest();
185 void ResourceRequest::doUpdateResourceRequest()
188 *this = ResourceRequest();
192 m_url = CFURLRequestGetURL(m_cfRequest.get());
194 m_cachePolicy = (ResourceRequestCachePolicy)CFURLRequestGetCachePolicy(m_cfRequest.get());
195 m_timeoutInterval = CFURLRequestGetTimeoutInterval(m_cfRequest.get());
196 m_firstPartyForCookies = CFURLRequestGetMainDocumentURL(m_cfRequest.get());
197 if (CFStringRef method = CFURLRequestCopyHTTPRequestMethod(m_cfRequest.get())) {
198 m_httpMethod = method;
201 m_allowCookies = CFURLRequestShouldHandleHTTPCookies(m_cfRequest.get());
203 if (httpPipeliningEnabled())
204 m_priority = toResourceLoadPriority(wkGetHTTPPipeliningPriority(m_cfRequest.get()));
206 m_httpHeaderFields.clear();
207 if (CFDictionaryRef headers = CFURLRequestCopyAllHTTPHeaderFields(m_cfRequest.get())) {
208 CFIndex headerCount = CFDictionaryGetCount(headers);
209 Vector<const void*, 128> keys(headerCount);
210 Vector<const void*, 128> values(headerCount);
211 CFDictionaryGetKeysAndValues(headers, keys.data(), values.data());
212 for (int i = 0; i < headerCount; ++i)
213 m_httpHeaderFields.set((CFStringRef)keys[i], (CFStringRef)values[i]);
217 m_responseContentDispositionEncodingFallbackArray.clear();
218 RetainPtr<CFArrayRef> encodingFallbacks(AdoptCF, copyContentDispositionEncodingFallbackArray(m_cfRequest.get()));
219 if (encodingFallbacks) {
220 CFIndex count = CFArrayGetCount(encodingFallbacks.get());
221 for (CFIndex i = 0; i < count; ++i) {
222 CFStringEncoding encoding = reinterpret_cast<CFIndex>(CFArrayGetValueAtIndex(encodingFallbacks.get(), i));
223 if (encoding != kCFStringEncodingInvalidId)
224 m_responseContentDispositionEncodingFallbackArray.append(CFStringConvertEncodingToIANACharSetName(encoding));
228 m_httpBody = httpBodyFromRequest(m_cfRequest.get());
231 #if USE(CFURLSTORAGESESSIONS)
233 void ResourceRequest::setStorageSession(CFURLStorageSessionRef storageSession)
235 CFMutableURLRequestRef cfRequest = CFURLRequestCreateMutableCopy(0, m_cfRequest.get());
236 wkSetRequestStorageSession(storageSession, cfRequest);
237 m_cfRequest.adoptCF(cfRequest);
239 updateNSURLRequest();
246 void ResourceRequest::applyWebArchiveHackForMail()
248 // Hack because Mail checks for this property to detect data / archive loads
249 _CFURLRequestSetProtocolProperty(cfURLRequest(), CFSTR("WebDataRequest"), CFSTR(""));
253 #endif // USE(CFNETWORK)
255 bool ResourceRequest::httpPipeliningEnabled()
257 return s_httpPipeliningEnabled;
260 void ResourceRequest::setHTTPPipeliningEnabled(bool flag)
262 s_httpPipeliningEnabled = flag;
265 #if USE(CFNETWORK) || PLATFORM(MAC) && !defined(BUILDING_ON_LEOPARD) && !defined(BUILDING_ON_SNOW_LEOPARD)
266 static inline bool readBooleanPreference(CFStringRef key)
268 Boolean keyExistsAndHasValidFormat;
269 Boolean result = CFPreferencesGetAppBooleanValue(key, kCFPreferencesCurrentApplication, &keyExistsAndHasValidFormat);
270 return keyExistsAndHasValidFormat ? result : false;
274 unsigned initializeMaximumHTTPConnectionCountPerHost()
276 static const unsigned preferredConnectionCount = 6;
278 // Always set the connection count per host, even when pipelining.
279 unsigned maximumHTTPConnectionCountPerHost = wkInitializeMaximumHTTPConnectionCountPerHost(preferredConnectionCount);
281 #if USE(CFNETWORK) || PLATFORM(MAC) && !defined(BUILDING_ON_LEOPARD) && !defined(BUILDING_ON_SNOW_LEOPARD)
282 static const unsigned unlimitedConnectionCount = 10000;
284 if (!ResourceRequest::httpPipeliningEnabled() && readBooleanPreference(CFSTR("WebKitEnableHTTPPipelining")))
285 ResourceRequest::setHTTPPipeliningEnabled(true);
287 if (ResourceRequest::httpPipeliningEnabled()) {
288 wkSetHTTPPipeliningMaximumPriority(toHTTPPipeliningPriority(ResourceLoadPriorityHighest));
290 // FIXME: <rdar://problem/9375609> Implement minimum fast lane priority setting on Windows
291 wkSetHTTPPipeliningMinimumFastLanePriority(toHTTPPipeliningPriority(ResourceLoadPriorityMedium));
293 // When pipelining do not rate-limit requests sent from WebCore since CFNetwork handles that.
294 return unlimitedConnectionCount;
298 return maximumHTTPConnectionCountPerHost;
301 } // namespace WebCore