Set minimum priority for fast lane connections
[WebKit-https.git] / Source / WebCore / platform / network / cf / ResourceRequestCFNet.cpp
1 /*
2  * Copyright (C) 2006, 2007, 2008 Apple Inc.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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. 
24  */
25
26 #include "config.h"
27 #include "ResourceRequestCFNet.h"
28
29 #include "ResourceRequest.h"
30
31 #if PLATFORM(MAC)
32 #include "ResourceLoadPriority.h"
33 #include "WebCoreSystemInterface.h"
34 #endif
35
36 #if USE(CFNETWORK)
37 #include "FormDataStreamCFNet.h"
38 #include <CFNetwork/CFURLRequestPriv.h>
39 #include <WebKitSystemInterface/WebKitSystemInterface.h>
40 #endif
41
42 namespace WebCore {
43
44 #if USE(CFNETWORK)
45
46 typedef void (*CFURLRequestSetContentDispositionEncodingFallbackArrayFunction)(CFMutableURLRequestRef, CFArrayRef);
47 typedef CFArrayRef (*CFURLRequestCopyContentDispositionEncodingFallbackArrayFunction)(CFURLRequestRef);
48
49 static HMODULE findCFNetworkModule()
50 {
51 #ifndef DEBUG_ALL
52     return GetModuleHandleA("CFNetwork");
53 #else
54     return GetModuleHandleA("CFNetwork_debug");
55 #endif
56 }
57
58 static CFURLRequestSetContentDispositionEncodingFallbackArrayFunction findCFURLRequestSetContentDispositionEncodingFallbackArrayFunction()
59 {
60     return reinterpret_cast<CFURLRequestSetContentDispositionEncodingFallbackArrayFunction>(GetProcAddress(findCFNetworkModule(), "_CFURLRequestSetContentDispositionEncodingFallbackArray"));
61 }
62
63 static CFURLRequestCopyContentDispositionEncodingFallbackArrayFunction findCFURLRequestCopyContentDispositionEncodingFallbackArrayFunction()
64 {
65     return reinterpret_cast<CFURLRequestCopyContentDispositionEncodingFallbackArrayFunction>(GetProcAddress(findCFNetworkModule(), "_CFURLRequestCopyContentDispositionEncodingFallbackArray"));
66 }
67
68 static void setContentDispositionEncodingFallbackArray(CFMutableURLRequestRef request, CFArrayRef fallbackArray)
69 {
70     static CFURLRequestSetContentDispositionEncodingFallbackArrayFunction function = findCFURLRequestSetContentDispositionEncodingFallbackArrayFunction();
71     if (function)
72         function(request, fallbackArray);
73 }
74
75 static CFArrayRef copyContentDispositionEncodingFallbackArray(CFURLRequestRef request)
76 {
77     static CFURLRequestCopyContentDispositionEncodingFallbackArrayFunction function = findCFURLRequestCopyContentDispositionEncodingFallbackArrayFunction();
78     if (!function)
79         return 0;
80     return function(request);
81 }
82
83 CFURLRequestRef ResourceRequest::cfURLRequest() const
84 {
85     updatePlatformRequest();
86
87     return m_cfRequest.get();
88 }
89
90 static inline void setHeaderFields(CFMutableURLRequestRef request, const HTTPHeaderMap& requestHeaders) 
91 {
92     // Remove existing headers first, as some of them may no longer be present in the map.
93     RetainPtr<CFDictionaryRef> oldHeaderFields(AdoptCF, CFURLRequestCopyAllHTTPHeaderFields(request));
94     CFIndex oldHeaderFieldCount = CFDictionaryGetCount(oldHeaderFields.get());
95     if (oldHeaderFieldCount) {
96         Vector<CFStringRef> oldHeaderFieldNames(oldHeaderFieldCount);
97         CFDictionaryGetKeysAndValues(oldHeaderFields.get(), reinterpret_cast<const void**>(&oldHeaderFieldNames[0]), 0);
98         for (CFIndex i = 0; i < oldHeaderFieldCount; ++i)
99             CFURLRequestSetHTTPHeaderFieldValue(request, oldHeaderFieldNames[i], 0);
100     }
101
102     HTTPHeaderMap::const_iterator end = requestHeaders.end();
103     for (HTTPHeaderMap::const_iterator it = requestHeaders.begin(); it != end; ++it) {
104         CFStringRef key = it->first.createCFString();
105         CFStringRef value = it->second.createCFString();
106         CFURLRequestSetHTTPHeaderFieldValue(request, key, value);
107         CFRelease(key);
108         CFRelease(value);
109     }
110 }
111
112 void ResourceRequest::doUpdatePlatformRequest()
113 {
114     CFMutableURLRequestRef cfRequest;
115
116     RetainPtr<CFURLRef> url(AdoptCF, ResourceRequest::url().createCFURL());
117     RetainPtr<CFURLRef> firstPartyForCookies(AdoptCF, ResourceRequest::firstPartyForCookies().createCFURL());
118     if (m_cfRequest) {
119         cfRequest = CFURLRequestCreateMutableCopy(0, m_cfRequest.get());
120         CFURLRequestSetURL(cfRequest, url.get());
121         CFURLRequestSetMainDocumentURL(cfRequest, firstPartyForCookies.get());
122         CFURLRequestSetCachePolicy(cfRequest, (CFURLRequestCachePolicy)cachePolicy());
123         CFURLRequestSetTimeoutInterval(cfRequest, timeoutInterval());
124     } else {
125         cfRequest = CFURLRequestCreateMutable(0, url.get(), (CFURLRequestCachePolicy)cachePolicy(), timeoutInterval(), firstPartyForCookies.get());
126     }
127
128     RetainPtr<CFStringRef> requestMethod(AdoptCF, httpMethod().createCFString());
129     CFURLRequestSetHTTPRequestMethod(cfRequest, requestMethod.get());
130
131     setHeaderFields(cfRequest, httpHeaderFields());
132     WebCore::setHTTPBody(cfRequest, httpBody());
133     CFURLRequestSetShouldHandleHTTPCookies(cfRequest, allowCookies());
134
135     unsigned fallbackCount = m_responseContentDispositionEncodingFallbackArray.size();
136     RetainPtr<CFMutableArrayRef> encodingFallbacks(AdoptCF, CFArrayCreateMutable(kCFAllocatorDefault, fallbackCount, 0));
137     for (unsigned i = 0; i != fallbackCount; ++i) {
138         RetainPtr<CFStringRef> encodingName(AdoptCF, m_responseContentDispositionEncodingFallbackArray[i].createCFString());
139         CFStringEncoding encoding = CFStringConvertIANACharSetNameToEncoding(encodingName.get());
140         if (encoding != kCFStringEncodingInvalidId)
141             CFArrayAppendValue(encodingFallbacks.get(), reinterpret_cast<const void*>(encoding));
142     }
143     setContentDispositionEncodingFallbackArray(cfRequest, encodingFallbacks.get());
144
145     if (m_cfRequest) {
146         RetainPtr<CFHTTPCookieStorageRef> cookieStorage(AdoptCF, CFURLRequestCopyHTTPCookieStorage(m_cfRequest.get()));
147         if (cookieStorage)
148             CFURLRequestSetHTTPCookieStorage(cfRequest, cookieStorage.get());
149         CFURLRequestSetHTTPCookieStorageAcceptPolicy(cfRequest, CFURLRequestGetHTTPCookieStorageAcceptPolicy(m_cfRequest.get()));
150         CFURLRequestSetSSLProperties(cfRequest, CFURLRequestGetSSLProperties(m_cfRequest.get()));
151     }
152
153     m_cfRequest.adoptCF(cfRequest);
154 }
155
156 void ResourceRequest::doUpdateResourceRequest()
157 {
158     if (!m_cfRequest) {
159         *this = ResourceRequest();
160         return;
161     }
162
163     m_url = CFURLRequestGetURL(m_cfRequest.get());
164
165     m_cachePolicy = (ResourceRequestCachePolicy)CFURLRequestGetCachePolicy(m_cfRequest.get());
166     m_timeoutInterval = CFURLRequestGetTimeoutInterval(m_cfRequest.get());
167     m_firstPartyForCookies = CFURLRequestGetMainDocumentURL(m_cfRequest.get());
168     if (CFStringRef method = CFURLRequestCopyHTTPRequestMethod(m_cfRequest.get())) {
169         m_httpMethod = method;
170         CFRelease(method);
171     }
172     m_allowCookies = CFURLRequestShouldHandleHTTPCookies(m_cfRequest.get());
173
174     m_httpHeaderFields.clear();
175     if (CFDictionaryRef headers = CFURLRequestCopyAllHTTPHeaderFields(m_cfRequest.get())) {
176         CFIndex headerCount = CFDictionaryGetCount(headers);
177         Vector<const void*, 128> keys(headerCount);
178         Vector<const void*, 128> values(headerCount);
179         CFDictionaryGetKeysAndValues(headers, keys.data(), values.data());
180         for (int i = 0; i < headerCount; ++i)
181             m_httpHeaderFields.set((CFStringRef)keys[i], (CFStringRef)values[i]);
182         CFRelease(headers);
183     }
184
185     m_responseContentDispositionEncodingFallbackArray.clear();
186     RetainPtr<CFArrayRef> encodingFallbacks(AdoptCF, copyContentDispositionEncodingFallbackArray(m_cfRequest.get()));
187     if (encodingFallbacks) {
188         CFIndex count = CFArrayGetCount(encodingFallbacks.get());
189         for (CFIndex i = 0; i < count; ++i) {
190             CFStringEncoding encoding = reinterpret_cast<CFIndex>(CFArrayGetValueAtIndex(encodingFallbacks.get(), i));
191             if (encoding != kCFStringEncodingInvalidId)
192                 m_responseContentDispositionEncodingFallbackArray.append(CFStringConvertEncodingToIANACharSetName(encoding));
193         }
194     }
195
196     m_httpBody = httpBodyFromRequest(m_cfRequest.get());
197 }
198
199 #if USE(CFURLSTORAGESESSIONS)
200
201 void ResourceRequest::setStorageSession(CFURLStorageSessionRef storageSession)
202 {
203     CFMutableURLRequestRef cfRequest = CFURLRequestCreateMutableCopy(0, m_cfRequest.get());
204     wkSetRequestStorageSession(storageSession, cfRequest);
205     m_cfRequest.adoptCF(cfRequest);
206 }
207
208 #endif
209
210 #endif // USE(CFNETWORK)
211
212 unsigned initializeMaximumHTTPConnectionCountPerHost()
213 {
214     static const unsigned preferredConnectionCount = 6;
215     static const unsigned unlimitedConnectionCount = 10000;
216
217     // Always set the connection count per host, even when pipelining.
218     unsigned maximumHTTPConnectionCountPerHost = wkInitializeMaximumHTTPConnectionCountPerHost(preferredConnectionCount);
219
220 #if PLATFORM(MAC)
221     if (isHTTPPipeliningEnabled()) {
222         wkSetHTTPPipeliningMaximumPriority(ResourceLoadPriorityHighest);
223         wkSetHTTPPipeliningMinimumFastLanePriority(ResourceLoadPriorityMedium);
224         // When pipelining do not rate-limit requests sent from WebCore since CFNetwork handles that.
225         return unlimitedConnectionCount;
226     }
227 #endif
228
229     return maximumHTTPConnectionCountPerHost;
230 }
231
232 static inline bool readBooleanPreference(CFStringRef key)
233 {
234     Boolean keyExistsAndHasValidFormat;
235     Boolean result = CFPreferencesGetAppBooleanValue(key, kCFPreferencesCurrentApplication, &keyExistsAndHasValidFormat);
236     return keyExistsAndHasValidFormat ? result : false;
237 }
238
239 bool isHTTPPipeliningEnabled()
240 {
241     static bool isEnabled = readBooleanPreference(CFSTR("WebKitEnableHTTPPipelining"));
242     return isEnabled;
243 }
244
245 bool shouldForceHTTPPipeliningPriorityHigh()
246 {
247     static bool shouldForcePriorityHigh = readBooleanPreference(CFSTR("WebKitForceHTTPPipeliningPriorityHigh"));
248     return shouldForcePriorityHigh;
249 }
250
251 } // namespace WebCore