tizen beta release
[framework/web/webkit-efl.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 "ResourceHandle.h"
30 #include "ResourceRequest.h"
31
32 #if USE(CFNETWORK)
33 #include "FormDataStreamCFNet.h"
34 #include <CFNetwork/CFURLRequestPriv.h>
35 #endif
36
37 #if PLATFORM(MAC)
38 #include "ResourceLoadPriority.h"
39 #include "WebCoreSystemInterface.h"
40 #include <dlfcn.h>
41 #endif
42
43 #if PLATFORM(WIN)
44 #include <WebKitSystemInterface/WebKitSystemInterface.h>
45 #endif
46
47 namespace WebCore {
48
49 bool ResourceRequest::s_httpPipeliningEnabled = false;
50
51 #if USE(CFNETWORK)
52
53 typedef void (*CFURLRequestSetContentDispositionEncodingFallbackArrayFunction)(CFMutableURLRequestRef, CFArrayRef);
54 typedef CFArrayRef (*CFURLRequestCopyContentDispositionEncodingFallbackArrayFunction)(CFURLRequestRef);
55
56 #if PLATFORM(WIN)
57 static HMODULE findCFNetworkModule()
58 {
59 #ifndef DEBUG_ALL
60     return GetModuleHandleA("CFNetwork");
61 #else
62     return GetModuleHandleA("CFNetwork_debug");
63 #endif
64 }
65
66 static CFURLRequestSetContentDispositionEncodingFallbackArrayFunction findCFURLRequestSetContentDispositionEncodingFallbackArrayFunction()
67 {
68     return reinterpret_cast<CFURLRequestSetContentDispositionEncodingFallbackArrayFunction>(GetProcAddress(findCFNetworkModule(), "_CFURLRequestSetContentDispositionEncodingFallbackArray"));
69 }
70
71 static CFURLRequestCopyContentDispositionEncodingFallbackArrayFunction findCFURLRequestCopyContentDispositionEncodingFallbackArrayFunction()
72 {
73     return reinterpret_cast<CFURLRequestCopyContentDispositionEncodingFallbackArrayFunction>(GetProcAddress(findCFNetworkModule(), "_CFURLRequestCopyContentDispositionEncodingFallbackArray"));
74 }
75 #elif PLATFORM(MAC)
76 static CFURLRequestSetContentDispositionEncodingFallbackArrayFunction findCFURLRequestSetContentDispositionEncodingFallbackArrayFunction()
77 {
78     return reinterpret_cast<CFURLRequestSetContentDispositionEncodingFallbackArrayFunction>(dlsym(RTLD_DEFAULT, "_CFURLRequestSetContentDispositionEncodingFallbackArray"));
79 }
80
81 static CFURLRequestCopyContentDispositionEncodingFallbackArrayFunction findCFURLRequestCopyContentDispositionEncodingFallbackArrayFunction()
82 {
83     return reinterpret_cast<CFURLRequestCopyContentDispositionEncodingFallbackArrayFunction>(dlsym(RTLD_DEFAULT, "_CFURLRequestCopyContentDispositionEncodingFallbackArray"));
84 }
85 #endif
86
87 static void setContentDispositionEncodingFallbackArray(CFMutableURLRequestRef request, CFArrayRef fallbackArray)
88 {
89     static CFURLRequestSetContentDispositionEncodingFallbackArrayFunction function = findCFURLRequestSetContentDispositionEncodingFallbackArrayFunction();
90     if (function)
91         function(request, fallbackArray);
92 }
93
94 static CFArrayRef copyContentDispositionEncodingFallbackArray(CFURLRequestRef request)
95 {
96     static CFURLRequestCopyContentDispositionEncodingFallbackArrayFunction function = findCFURLRequestCopyContentDispositionEncodingFallbackArrayFunction();
97     if (!function)
98         return 0;
99     return function(request);
100 }
101
102 CFURLRequestRef ResourceRequest::cfURLRequest() const
103 {
104     updatePlatformRequest();
105
106     return m_cfRequest.get();
107 }
108
109 static inline void setHeaderFields(CFMutableURLRequestRef request, const HTTPHeaderMap& requestHeaders) 
110 {
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);
119     }
120
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);
126         CFRelease(key);
127         CFRelease(value);
128     }
129 }
130
131 void ResourceRequest::doUpdatePlatformRequest()
132 {
133     CFMutableURLRequestRef cfRequest;
134
135     RetainPtr<CFURLRef> url(AdoptCF, ResourceRequest::url().createCFURL());
136     RetainPtr<CFURLRef> firstPartyForCookies(AdoptCF, ResourceRequest::firstPartyForCookies().createCFURL());
137     if (m_cfRequest) {
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());
143     } else
144         cfRequest = CFURLRequestCreateMutable(0, url.get(), (CFURLRequestCachePolicy)cachePolicy(), timeoutInterval(), firstPartyForCookies.get());
145 #if USE(CFURLSTORAGESESSIONS)
146     wkSetRequestStorageSession(ResourceHandle::currentStorageSession(), cfRequest);
147 #endif
148
149     RetainPtr<CFStringRef> requestMethod(AdoptCF, httpMethod().createCFString());
150     CFURLRequestSetHTTPRequestMethod(cfRequest, requestMethod.get());
151
152     if (httpPipeliningEnabled())
153         wkSetHTTPPipeliningPriority(cfRequest, toHTTPPipeliningPriority(m_priority));
154
155     setHeaderFields(cfRequest, httpHeaderFields());
156     RefPtr<FormData> formData = httpBody();
157     if (formData && !formData->isEmpty())
158         WebCore::setHTTPBody(cfRequest, formData);
159     CFURLRequestSetShouldHandleHTTPCookies(cfRequest, allowCookies());
160
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));
168     }
169     setContentDispositionEncodingFallbackArray(cfRequest, encodingFallbacks.get());
170
171     if (m_cfRequest) {
172         RetainPtr<CFHTTPCookieStorageRef> cookieStorage(AdoptCF, CFURLRequestCopyHTTPCookieStorage(m_cfRequest.get()));
173         if (cookieStorage)
174             CFURLRequestSetHTTPCookieStorage(cfRequest, cookieStorage.get());
175         CFURLRequestSetHTTPCookieStorageAcceptPolicy(cfRequest, CFURLRequestGetHTTPCookieStorageAcceptPolicy(m_cfRequest.get()));
176         CFURLRequestSetSSLProperties(cfRequest, CFURLRequestGetSSLProperties(m_cfRequest.get()));
177     }
178
179     m_cfRequest.adoptCF(cfRequest);
180 #if PLATFORM(MAC)
181     updateNSURLRequest();
182 #endif
183 }
184
185 void ResourceRequest::doUpdateResourceRequest()
186 {
187     if (!m_cfRequest) {
188         *this = ResourceRequest();
189         return;
190     }
191
192     m_url = CFURLRequestGetURL(m_cfRequest.get());
193
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;
199         CFRelease(method);
200     }
201     m_allowCookies = CFURLRequestShouldHandleHTTPCookies(m_cfRequest.get());
202
203     if (httpPipeliningEnabled())
204         m_priority = toResourceLoadPriority(wkGetHTTPPipeliningPriority(m_cfRequest.get()));
205
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]);
214         CFRelease(headers);
215     }
216
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));
225         }
226     }
227
228     m_httpBody = httpBodyFromRequest(m_cfRequest.get());
229 }
230
231 #if USE(CFURLSTORAGESESSIONS)
232
233 void ResourceRequest::setStorageSession(CFURLStorageSessionRef storageSession)
234 {
235     CFMutableURLRequestRef cfRequest = CFURLRequestCreateMutableCopy(0, m_cfRequest.get());
236     wkSetRequestStorageSession(storageSession, cfRequest);
237     m_cfRequest.adoptCF(cfRequest);
238 #if PLATFORM(MAC)
239     updateNSURLRequest();
240 #endif
241 }
242
243 #endif
244
245 #if PLATFORM(MAC)
246 void ResourceRequest::applyWebArchiveHackForMail()
247 {
248     // Hack because Mail checks for this property to detect data / archive loads
249     _CFURLRequestSetProtocolProperty(cfURLRequest(), CFSTR("WebDataRequest"), CFSTR(""));
250 }
251 #endif
252
253 #endif // USE(CFNETWORK)
254
255 bool ResourceRequest::httpPipeliningEnabled()
256 {
257     return s_httpPipeliningEnabled;
258 }
259
260 void ResourceRequest::setHTTPPipeliningEnabled(bool flag)
261 {
262     s_httpPipeliningEnabled = flag;
263 }
264
265 #if USE(CFNETWORK) || PLATFORM(MAC) && !defined(BUILDING_ON_LEOPARD) && !defined(BUILDING_ON_SNOW_LEOPARD)
266 static inline bool readBooleanPreference(CFStringRef key)
267 {
268     Boolean keyExistsAndHasValidFormat;
269     Boolean result = CFPreferencesGetAppBooleanValue(key, kCFPreferencesCurrentApplication, &keyExistsAndHasValidFormat);
270     return keyExistsAndHasValidFormat ? result : false;
271 }
272 #endif
273
274 unsigned initializeMaximumHTTPConnectionCountPerHost()
275 {
276     static const unsigned preferredConnectionCount = 6;
277
278     // Always set the connection count per host, even when pipelining.
279     unsigned maximumHTTPConnectionCountPerHost = wkInitializeMaximumHTTPConnectionCountPerHost(preferredConnectionCount);
280
281 #if USE(CFNETWORK) || PLATFORM(MAC) && !defined(BUILDING_ON_LEOPARD) && !defined(BUILDING_ON_SNOW_LEOPARD)
282     static const unsigned unlimitedConnectionCount = 10000;
283
284     if (!ResourceRequest::httpPipeliningEnabled() && readBooleanPreference(CFSTR("WebKitEnableHTTPPipelining")))
285         ResourceRequest::setHTTPPipeliningEnabled(true);
286
287     if (ResourceRequest::httpPipeliningEnabled()) {
288         wkSetHTTPPipeliningMaximumPriority(toHTTPPipeliningPriority(ResourceLoadPriorityHighest));
289 #if !PLATFORM(WIN)
290         // FIXME: <rdar://problem/9375609> Implement minimum fast lane priority setting on Windows
291         wkSetHTTPPipeliningMinimumFastLanePriority(toHTTPPipeliningPriority(ResourceLoadPriorityMedium));
292 #endif
293         // When pipelining do not rate-limit requests sent from WebCore since CFNetwork handles that.
294         return unlimitedConnectionCount;
295     }
296 #endif
297
298     return maximumHTTPConnectionCountPerHost;
299 }
300
301 } // namespace WebCore