9b99aa39f9e139a5de47b82bb7af9ba016d1da3e
[WebKit-https.git] / WebCore / platform / network / cf / ResourceHandleCFNet.cpp
1 /*
2  * Copyright (C) 2004, 2005, 2006, 2007 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
28 #include "ResourceHandle.h"
29 #include "ResourceHandleClient.h"
30 #include "ResourceHandleInternal.h"
31
32 #include "AuthenticationCF.h"
33 #include "AuthenticationChallenge.h"
34 #include "DocLoader.h"
35 #include "Frame.h"
36 #include "FrameLoader.h"
37 #include "Logging.h"
38 #include "NotImplemented.h"
39 #include "ResourceError.h"
40 #include "ResourceResponse.h"
41
42 #include <WTF/HashMap.h>
43
44 #include <sys/types.h>
45 #include <sys/stat.h>
46 #include <process.h> // for _beginthread()
47
48 #include <CFNetwork/CFNetwork.h>
49 #include <WebKitSystemInterface/WebKitSystemInterface.h>
50
51 namespace WebCore {
52
53 static HashSet<String>& allowsAnyHTTPSCertificateHosts()
54 {
55     static HashSet<String> hosts;
56
57     return hosts;
58 }
59
60 CFURLRequestRef willSendRequest(CFURLConnectionRef conn, CFURLRequestRef cfRequest, CFURLResponseRef cfRedirectResponse, const void* clientInfo)
61 {
62     ResourceHandle* handle = (ResourceHandle*)clientInfo;
63
64     LOG(Network, "CFNet - willSendRequest(conn=%p, handle=%p) (%s)", conn, handle, handle->request().url().url().ascii());
65
66     ResourceRequest request(cfRequest);
67     if (handle->client())
68         handle->client()->willSendRequest(handle, request, cfRedirectResponse);
69
70     cfRequest = request.cfURLRequest();
71
72     CFRetain(cfRequest);
73     return cfRequest;
74 }
75
76 void didReceiveResponse(CFURLConnectionRef conn, CFURLResponseRef cfResponse, const void* clientInfo) 
77 {
78     ResourceHandle* handle = (ResourceHandle*)clientInfo;
79
80     LOG(Network, "CFNet - didReceiveResponse(conn=%p, handle=%p) (%s)", conn, handle, handle->request().url().url().ascii());
81
82     if (handle->client())
83         handle->client()->didReceiveResponse(handle, cfResponse);
84 }
85
86 void didReceiveData(CFURLConnectionRef conn, CFDataRef data, CFIndex originalLength, const void* clientInfo) 
87 {
88     ResourceHandle* handle = (ResourceHandle*)clientInfo;
89     const UInt8* bytes = CFDataGetBytePtr(data);
90     CFIndex length = CFDataGetLength(data);
91
92     LOG(Network, "CFNet - didReceiveData(conn=%p, handle=%p, bytes=%d) (%s)", conn, handle, length, handle->request().url().url().ascii());
93
94     if (handle->client())
95         handle->client()->didReceiveData(handle, (const char*)bytes, length, originalLength);
96 }
97
98 void didFinishLoading(CFURLConnectionRef conn, const void* clientInfo) 
99 {
100     ResourceHandle* handle = (ResourceHandle*)clientInfo;
101
102     LOG(Network, "CFNet - didFinishLoading(conn=%p, handle=%p) (%s)", conn, handle, handle->request().url().url().ascii());
103
104     if (handle->client())
105         handle->client()->didFinishLoading(handle);
106 }
107
108 void didFail(CFURLConnectionRef conn, CFErrorRef error, const void* clientInfo) 
109 {
110     ResourceHandle* handle = (ResourceHandle*)clientInfo;
111
112     LOG(Network, "CFNet - didFail(conn=%p, handle=%p, error = %p) (%s)", conn, handle, error, handle->request().url().url().ascii());
113
114     if (handle->client())
115         handle->client()->didFail(handle, ResourceError(error));
116 }
117
118 CFCachedURLResponseRef willCacheResponse(CFURLConnectionRef conn, CFCachedURLResponseRef cachedResponse, const void* clientInfo) 
119 {
120     ResourceHandle* handle = (ResourceHandle*)clientInfo;
121
122     CacheStoragePolicy policy = static_cast<CacheStoragePolicy>(CFCachedURLResponseGetStoragePolicy(cachedResponse));
123
124     if (handle->client())
125         handle->client()->willCacheResponse(handle, policy);
126
127     if (static_cast<CFURLCacheStoragePolicy>(policy) != CFCachedURLResponseGetStoragePolicy(cachedResponse))
128         cachedResponse = CFCachedURLResponseCreateWithUserInfo(kCFAllocatorDefault, 
129                                                                CFCachedURLResponseGetWrappedResponse(cachedResponse),
130                                                                CFCachedURLResponseGetReceiverData(cachedResponse),
131                                                                CFCachedURLResponseGetUserInfo(cachedResponse), 
132                                                                static_cast<CFURLCacheStoragePolicy>(policy));
133     CFRetain(cachedResponse);
134
135     return cachedResponse;
136 }
137
138 void didReceiveChallenge(CFURLConnectionRef conn, CFURLAuthChallengeRef challenge, const void* clientInfo)
139 {
140     ResourceHandle* handle = (ResourceHandle*)clientInfo;
141     ASSERT(handle);
142     LOG(Network, "CFNet - didReceiveChallenge(conn=%p, handle=%p (%s)", conn, handle, handle->request().url().url().ascii());
143
144     handle->didReceiveAuthenticationChallenge(AuthenticationChallenge(challenge, handle));
145 }
146
147 void addHeadersFromHashMap(CFMutableURLRequestRef request, const HTTPHeaderMap& requestHeaders) 
148 {
149     if (!requestHeaders.size())
150         return;
151
152     HTTPHeaderMap::const_iterator end = requestHeaders.end();
153     for (HTTPHeaderMap::const_iterator it = requestHeaders.begin(); it != end; ++it) {
154         CFStringRef key = it->first.createCFString();
155         CFStringRef value = it->second.createCFString();
156         CFURLRequestSetHTTPHeaderFieldValue(request, key, value);
157         CFRelease(key);
158         CFRelease(value);
159     }
160 }
161
162 ResourceHandleInternal::~ResourceHandleInternal()
163 {
164     if (m_connection) {
165         LOG(Network, "CFNet - Cancelling connection %p (%s)", m_connection, m_request.url().url().ascii());
166         CFURLConnectionCancel(m_connection.get());
167     }
168 }
169
170 ResourceHandle::~ResourceHandle()
171 {
172     LOG(Network, "CFNet - Destroying job %p (%s)", this, d->m_request.url().url().ascii());
173 }
174
175 CFArrayRef arrayFromFormData(const FormData& d)
176 {
177     size_t size = d.elements().size();
178     CFMutableArrayRef a = CFArrayCreateMutable(0, d.elements().size(), &kCFTypeArrayCallBacks);
179     for (size_t i = 0; i < size; ++i) {
180         const FormDataElement& e = d.elements()[i];
181         if (e.m_type == FormDataElement::data) {
182             CFDataRef data = CFDataCreate(0, (const UInt8*)e.m_data.data(), e.m_data.size());
183             CFArrayAppendValue(a, data);
184             CFRelease(data);
185         } else {
186             ASSERT(e.m_type == FormDataElement::encodedFile);
187             CFStringRef filename = e.m_filename.createCFString();
188             CFArrayAppendValue(a, filename);
189             CFRelease(filename);
190         }
191     }
192     return a;
193 }
194
195 void emptyPerform(void* unused) 
196 {
197 }
198
199 static CFRunLoopRef loaderRL = 0;
200 void runLoaderThread(void *unused)
201 {
202     loaderRL = CFRunLoopGetCurrent();
203
204     // Must add a source to the run loop to prevent CFRunLoopRun() from exiting
205     CFRunLoopSourceContext ctxt = {0, (void *)1 /*must be non-NULL*/, 0, 0, 0, 0, 0, 0, 0, emptyPerform};
206     CFRunLoopSourceRef bogusSource = CFRunLoopSourceCreate(0, 0, &ctxt);
207     CFRunLoopAddSource(loaderRL, bogusSource,kCFRunLoopDefaultMode);
208
209     CFRunLoopRun();
210 }
211
212 CFRunLoopRef ResourceHandle::loaderRunLoop()
213 {
214     if (!loaderRL) {
215         _beginthread(runLoaderThread, 0, 0);
216         while (loaderRL == 0) {
217             // FIXME: sleep 10? that can't be right...
218             Sleep(10);
219         }
220     }
221     return loaderRL;
222 }
223
224 static CFURLRequestRef makeFinalRequest(const ResourceRequest& request)
225 {
226     CFMutableURLRequestRef newRequest = CFURLRequestCreateMutableCopy(kCFAllocatorDefault, request.cfURLRequest());
227     
228     if (allowsAnyHTTPSCertificateHosts().contains(request.url().host().lower())) {
229         CFTypeRef keys[] = { kCFStreamSSLAllowsAnyRoot, kCFStreamSSLAllowsExpiredRoots };  
230         CFTypeRef values[] = { kCFBooleanTrue, kCFBooleanTrue };
231         static CFDictionaryRef sslProps = CFDictionaryCreate(kCFAllocatorDefault, keys, values, sizeof(keys) / sizeof(keys[0]), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
232         CFURLRequestSetSSLProperties(newRequest, sslProps);
233     }
234
235     if (CFHTTPCookieStorageRef defaultCookieStorage = wkGetDefaultHTTPCookieStorage())
236         CFURLRequestSetHTTPCookieStorageAcceptPolicy(newRequest, CFHTTPCookieStorageGetCookieAcceptPolicy(defaultCookieStorage));
237
238     return newRequest;
239 }
240
241 bool ResourceHandle::start(Frame* frame)
242 {
243     // If we are no longer attached to a Page, this must be an attempted load from an
244     // onUnload handler, so let's just block it.
245     if (!frame->page())
246         return false;
247
248     RetainPtr<CFURLRequestRef> request(AdoptCF, makeFinalRequest(d->m_request));
249
250     // CFURLConnection Callback API currently at version 1
251     const int CFURLConnectionClientVersion = 1;
252     CFURLConnectionClient client = {CFURLConnectionClientVersion, this, 0, 0, 0, willSendRequest, didReceiveResponse, didReceiveData, NULL, didFinishLoading, didFail, willCacheResponse, didReceiveChallenge};
253
254     d->m_connection.adoptCF(CFURLConnectionCreate(0, request.get(), &client));
255
256     CFURLConnectionScheduleWithCurrentMessageQueue(d->m_connection.get());
257     CFURLConnectionScheduleDownloadWithRunLoop(d->m_connection.get(), loaderRunLoop(), kCFRunLoopDefaultMode);
258     CFURLConnectionStart(d->m_connection.get());
259
260     LOG(Network, "CFNet - Starting URL %s (handle=%p, conn=%p)", d->m_request.url().url().ascii(), this, d->m_connection);
261
262     return true;
263 }
264
265 void ResourceHandle::cancel()
266 {
267     if (d->m_connection) {
268         CFURLConnectionCancel(d->m_connection.get());
269         d->m_connection = 0;
270     }
271 }
272
273 PassRefPtr<SharedBuffer> ResourceHandle::bufferedData()
274 {
275     ASSERT_NOT_REACHED();
276     return 0;
277 }
278
279 bool ResourceHandle::supportsBufferedData()
280 {
281     return false;
282 }
283
284 void ResourceHandle::didReceiveAuthenticationChallenge(const AuthenticationChallenge& challenge)
285 {
286     LOG(Network, "CFNet - didReceiveAuthenticationChallenge()");
287     ASSERT(!d->m_currentCFChallenge);
288     ASSERT(d->m_currentWebChallenge.isNull());
289     // Since CFURLConnection networking relies on keeping a reference to the original CFURLAuthChallengeRef,
290     // we make sure that is actually present
291     ASSERT(challenge.cfURLAuthChallengeRef());
292         
293     d->m_currentCFChallenge = challenge.cfURLAuthChallengeRef();
294     d->m_currentWebChallenge = AuthenticationChallenge(d->m_currentCFChallenge, this);
295     
296     if (client())
297         client()->didReceiveAuthenticationChallenge(this, d->m_currentWebChallenge);
298 }
299
300 void ResourceHandle::receivedCredential(const AuthenticationChallenge& challenge, const Credential& credential)
301 {
302     LOG(Network, "CFNet - receivedCredential()");
303     ASSERT(!challenge.isNull());
304     ASSERT(challenge.cfURLAuthChallengeRef());
305     if (challenge != d->m_currentWebChallenge)
306         return;
307
308     CFURLCredentialRef cfCredential = createCF(credential);
309     CFURLConnectionUseCredential(d->m_connection.get(), cfCredential, challenge.cfURLAuthChallengeRef());
310     CFRelease(cfCredential);
311
312     clearAuthentication();
313 }
314
315 void ResourceHandle::receivedRequestToContinueWithoutCredential(const AuthenticationChallenge& challenge)
316 {
317     LOG(Network, "CFNet - receivedRequestToContinueWithoutCredential()");
318     ASSERT(!challenge.isNull());
319     ASSERT(challenge.cfURLAuthChallengeRef());
320     if (challenge != d->m_currentWebChallenge)
321         return;
322
323     CFURLConnectionUseCredential(d->m_connection.get(), 0, challenge.cfURLAuthChallengeRef());
324
325     clearAuthentication();
326 }
327
328 void ResourceHandle::receivedCancellation(const AuthenticationChallenge& challenge)
329 {
330     LOG(Network, "CFNet - receivedCancellation()");
331     if (challenge != d->m_currentWebChallenge)
332         return;
333
334     if (client())
335         client()->receivedCancellation(this, challenge);
336 }
337
338 CFURLConnectionRef ResourceHandle::connection() const
339 {
340     return d->m_connection.get();
341 }
342
343 CFURLConnectionRef ResourceHandle::releaseConnectionForDownload()
344 {
345     LOG(Network, "CFNet - Job %p releasing connection %p for download", this, d->m_connection.get());
346     return d->m_connection.releaseRef();
347 }
348
349 void ResourceHandle::loadResourceSynchronously(const ResourceRequest& request, ResourceError& error, ResourceResponse& response, Vector<char>& vector)
350 {
351     ASSERT(!request.isEmpty());
352     CFURLResponseRef cfResponse = 0;
353     CFErrorRef cfError = 0;
354     RetainPtr<CFURLRequestRef> cfRequest(AdoptCF, makeFinalRequest(request));
355
356     CFDataRef data = CFURLConnectionSendSynchronousRequest(cfRequest.get(), &cfResponse, &cfError, request.timeoutInterval());
357
358     response = cfResponse;
359     if (cfResponse)
360         CFRelease(cfResponse);
361
362     error = cfError;
363     if (cfError)
364         CFRelease(cfError);
365
366     if (data) {
367         ASSERT(vector.isEmpty());
368         vector.append(CFDataGetBytePtr(data), CFDataGetLength(data));
369         CFRelease(data);
370     }
371 }
372
373 void ResourceHandle::setHostAllowsAnyHTTPSCertificate(const String& host)
374 {
375     allowsAnyHTTPSCertificateHosts().add(host.lower());
376 }
377
378 void ResourceHandle::setDefersLoading(bool defers)
379 {
380     if (defers)
381         CFURLConnectionHalt(d->m_connection.get());
382     else
383         CFURLConnectionResume(d->m_connection.get());
384 }
385
386 bool ResourceHandle::loadsBlocked()
387 {
388     return false;
389 }
390
391 bool ResourceHandle::willLoadFromCache(ResourceRequest&)
392 {
393     // Not having this function means that we'll ask the user about re-posting a form
394     // even when we go back to a page that's still in the cache.
395     notImplemented();
396     return false;
397 }
398
399 } // namespace WebCore