bba3d3e700a0d98cb639d917ab17dea170d028d7
[WebKit-https.git] / 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 "FormDataStreamCFNet.h"
30 #include "ResourceRequest.h"
31
32 #include <CFNetwork/CFURLRequestPriv.h>
33 #include <WebKitSystemInterface/WebKitSystemInterface.h>
34
35 namespace WebCore {
36
37 typedef void (*CFURLRequestSetContentDispositionEncodingFallbackArrayFunction)(CFMutableURLRequestRef, CFArrayRef);
38 typedef CFArrayRef (*CFURLRequestCopyContentDispositionEncodingFallbackArrayFunction)(CFURLRequestRef);
39
40 static HMODULE findCFNetworkModule()
41 {
42     if (HMODULE module = GetModuleHandleA("CFNetwork"))
43         return module;
44     return GetModuleHandleA("CFNetwork_debug");
45 }
46
47 static CFURLRequestSetContentDispositionEncodingFallbackArrayFunction findCFURLRequestSetContentDispositionEncodingFallbackArrayFunction()
48 {
49     return reinterpret_cast<CFURLRequestSetContentDispositionEncodingFallbackArrayFunction>(GetProcAddress(findCFNetworkModule(), "_CFURLRequestSetContentDispositionEncodingFallbackArray"));
50 }
51
52 static CFURLRequestCopyContentDispositionEncodingFallbackArrayFunction findCFURLRequestCopyContentDispositionEncodingFallbackArrayFunction()
53 {
54     return reinterpret_cast<CFURLRequestCopyContentDispositionEncodingFallbackArrayFunction>(GetProcAddress(findCFNetworkModule(), "_CFURLRequestCopyContentDispositionEncodingFallbackArray"));
55 }
56
57 static void setContentDispositionEncodingFallbackArray(CFMutableURLRequestRef request, CFArrayRef fallbackArray)
58 {
59     static CFURLRequestSetContentDispositionEncodingFallbackArrayFunction function = findCFURLRequestSetContentDispositionEncodingFallbackArrayFunction();
60     if (function)
61         function(request, fallbackArray);
62 }
63
64 static CFArrayRef copyContentDispositionEncodingFallbackArray(CFURLRequestRef request)
65 {
66     static CFURLRequestCopyContentDispositionEncodingFallbackArrayFunction function = findCFURLRequestCopyContentDispositionEncodingFallbackArrayFunction();
67     if (!function)
68         return 0;
69     return function(request);
70 }
71
72 CFURLRequestRef ResourceRequest::cfURLRequest() const
73 {
74     updatePlatformRequest();
75
76     return m_cfRequest.get();
77 }
78
79 static inline void addHeadersFromHashMap(CFMutableURLRequestRef request, const HTTPHeaderMap& requestHeaders) 
80 {
81     if (!requestHeaders.size())
82         return;
83         
84     HTTPHeaderMap::const_iterator end = requestHeaders.end();
85     for (HTTPHeaderMap::const_iterator it = requestHeaders.begin(); it != end; ++it) {
86         CFStringRef key = it->first.createCFString();
87         CFStringRef value = it->second.createCFString();
88         CFURLRequestSetHTTPHeaderFieldValue(request, key, value);
89         CFRelease(key);
90         CFRelease(value);
91     }
92 }
93
94 void ResourceRequest::doUpdatePlatformRequest()
95 {
96     CFMutableURLRequestRef cfRequest;
97
98     RetainPtr<CFURLRef> url(AdoptCF, ResourceRequest::url().createCFURL());
99     RetainPtr<CFURLRef> firstPartyForCookies(AdoptCF, ResourceRequest::firstPartyForCookies().createCFURL());
100     if (m_cfRequest) {
101         cfRequest = CFURLRequestCreateMutableCopy(0, m_cfRequest.get());
102         CFURLRequestSetURL(cfRequest, url.get());
103         CFURLRequestSetMainDocumentURL(cfRequest, firstPartyForCookies.get());
104         CFURLRequestSetCachePolicy(cfRequest, (CFURLRequestCachePolicy)cachePolicy());
105         CFURLRequestSetTimeoutInterval(cfRequest, timeoutInterval());
106     } else {
107         cfRequest = CFURLRequestCreateMutable(0, url.get(), (CFURLRequestCachePolicy)cachePolicy(), timeoutInterval(), firstPartyForCookies.get());
108     }
109
110     RetainPtr<CFStringRef> requestMethod(AdoptCF, httpMethod().createCFString());
111     CFURLRequestSetHTTPRequestMethod(cfRequest, requestMethod.get());
112
113     addHeadersFromHashMap(cfRequest, httpHeaderFields());
114     WebCore::setHTTPBody(cfRequest, httpBody());
115     CFURLRequestSetShouldHandleHTTPCookies(cfRequest, allowCookies());
116
117     unsigned fallbackCount = m_responseContentDispositionEncodingFallbackArray.size();
118     RetainPtr<CFMutableArrayRef> encodingFallbacks(AdoptCF, CFArrayCreateMutable(kCFAllocatorDefault, fallbackCount, 0));
119     for (unsigned i = 0; i != fallbackCount; ++i) {
120         RetainPtr<CFStringRef> encodingName(AdoptCF, m_responseContentDispositionEncodingFallbackArray[i].createCFString());
121         CFStringEncoding encoding = CFStringConvertIANACharSetNameToEncoding(encodingName.get());
122         if (encoding != kCFStringEncodingInvalidId)
123             CFArrayAppendValue(encodingFallbacks.get(), reinterpret_cast<const void*>(encoding));
124     }
125     setContentDispositionEncodingFallbackArray(cfRequest, encodingFallbacks.get());
126
127     if (m_cfRequest) {
128         RetainPtr<CFHTTPCookieStorageRef> cookieStorage(AdoptCF, CFURLRequestCopyHTTPCookieStorage(m_cfRequest.get()));
129         if (cookieStorage)
130             CFURLRequestSetHTTPCookieStorage(cfRequest, cookieStorage.get());
131         CFURLRequestSetHTTPCookieStorageAcceptPolicy(cfRequest, CFURLRequestGetHTTPCookieStorageAcceptPolicy(m_cfRequest.get()));
132         CFURLRequestSetSSLProperties(cfRequest, CFURLRequestGetSSLProperties(m_cfRequest.get()));
133     }
134
135     m_cfRequest.adoptCF(cfRequest);
136 }
137
138 void ResourceRequest::doUpdateResourceRequest()
139 {
140     m_url = CFURLRequestGetURL(m_cfRequest.get());
141
142     m_cachePolicy = (ResourceRequestCachePolicy)CFURLRequestGetCachePolicy(m_cfRequest.get());
143     m_timeoutInterval = CFURLRequestGetTimeoutInterval(m_cfRequest.get());
144     m_firstPartyForCookies = CFURLRequestGetMainDocumentURL(m_cfRequest.get());
145     if (CFStringRef method = CFURLRequestCopyHTTPRequestMethod(m_cfRequest.get())) {
146         m_httpMethod = method;
147         CFRelease(method);
148     }
149     m_allowCookies = CFURLRequestShouldHandleHTTPCookies(m_cfRequest.get());
150
151     if (CFDictionaryRef headers = CFURLRequestCopyAllHTTPHeaderFields(m_cfRequest.get())) {
152         CFIndex headerCount = CFDictionaryGetCount(headers);
153         Vector<const void*, 128> keys(headerCount);
154         Vector<const void*, 128> values(headerCount);
155         CFDictionaryGetKeysAndValues(headers, keys.data(), values.data());
156         for (int i = 0; i < headerCount; ++i)
157             m_httpHeaderFields.set((CFStringRef)keys[i], (CFStringRef)values[i]);
158         CFRelease(headers);
159     }
160
161     m_responseContentDispositionEncodingFallbackArray.clear();
162     RetainPtr<CFArrayRef> encodingFallbacks(AdoptCF, copyContentDispositionEncodingFallbackArray(m_cfRequest.get()));
163     if (encodingFallbacks) {
164         CFIndex count = CFArrayGetCount(encodingFallbacks.get());
165         for (CFIndex i = 0; i < count; ++i) {
166             CFStringEncoding encoding = reinterpret_cast<CFIndex>(CFArrayGetValueAtIndex(encodingFallbacks.get(), i));
167             if (encoding != kCFStringEncodingInvalidId)
168                 m_responseContentDispositionEncodingFallbackArray.append(CFStringConvertEncodingToIANACharSetName(encoding));
169         }
170     }
171
172     m_httpBody = httpBodyFromRequest(m_cfRequest.get());
173 }
174
175 unsigned initializeMaximumHTTPConnectionCountPerHost()
176 {
177     static const unsigned preferredConnectionCount = 6;
178     return wkInitializeMaximumHTTPConnectionCountPerHost(preferredConnectionCount);
179 }
180
181 }