WebCore:
[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, bool shouldContentSniff)
225 {
226     CFMutableURLRequestRef newRequest = CFURLRequestCreateMutableCopy(kCFAllocatorDefault, request.cfURLRequest());
227     
228     if (!shouldContentSniff)
229         wkSetCFURLRequestShouldContentSniff(newRequest, false);
230
231     if (allowsAnyHTTPSCertificateHosts().contains(request.url().host().lower())) {
232         CFTypeRef keys[] = { kCFStreamSSLAllowsAnyRoot, kCFStreamSSLAllowsExpiredRoots };  
233         CFTypeRef values[] = { kCFBooleanTrue, kCFBooleanTrue };
234         static CFDictionaryRef sslProps = CFDictionaryCreate(kCFAllocatorDefault, keys, values, sizeof(keys) / sizeof(keys[0]), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
235         CFURLRequestSetSSLProperties(newRequest, sslProps);
236     }
237
238     if (CFHTTPCookieStorageRef defaultCookieStorage = wkGetDefaultHTTPCookieStorage())
239         CFURLRequestSetHTTPCookieStorageAcceptPolicy(newRequest, CFHTTPCookieStorageGetCookieAcceptPolicy(defaultCookieStorage));
240
241     return newRequest;
242 }
243
244 bool ResourceHandle::start(Frame* frame)
245 {
246     // If we are no longer attached to a Page, this must be an attempted load from an
247     // onUnload handler, so let's just block it.
248     if (!frame->page())
249         return false;
250
251     RetainPtr<CFURLRequestRef> request(AdoptCF, makeFinalRequest(d->m_request, d->m_shouldContentSniff));
252
253     // CFURLConnection Callback API currently at version 1
254     const int CFURLConnectionClientVersion = 1;
255     CFURLConnectionClient client = {CFURLConnectionClientVersion, this, 0, 0, 0, willSendRequest, didReceiveResponse, didReceiveData, NULL, didFinishLoading, didFail, willCacheResponse, didReceiveChallenge};
256
257     d->m_connection.adoptCF(CFURLConnectionCreate(0, request.get(), &client));
258
259     CFURLConnectionScheduleWithCurrentMessageQueue(d->m_connection.get());
260     CFURLConnectionScheduleDownloadWithRunLoop(d->m_connection.get(), loaderRunLoop(), kCFRunLoopDefaultMode);
261     CFURLConnectionStart(d->m_connection.get());
262
263     LOG(Network, "CFNet - Starting URL %s (handle=%p, conn=%p)", d->m_request.url().url().ascii(), this, d->m_connection);
264
265     return true;
266 }
267
268 void ResourceHandle::cancel()
269 {
270     if (d->m_connection) {
271         CFURLConnectionCancel(d->m_connection.get());
272         d->m_connection = 0;
273     }
274 }
275
276 PassRefPtr<SharedBuffer> ResourceHandle::bufferedData()
277 {
278     ASSERT_NOT_REACHED();
279     return 0;
280 }
281
282 bool ResourceHandle::supportsBufferedData()
283 {
284     return false;
285 }
286
287 void ResourceHandle::didReceiveAuthenticationChallenge(const AuthenticationChallenge& challenge)
288 {
289     LOG(Network, "CFNet - didReceiveAuthenticationChallenge()");
290     ASSERT(!d->m_currentCFChallenge);
291     ASSERT(d->m_currentWebChallenge.isNull());
292     // Since CFURLConnection networking relies on keeping a reference to the original CFURLAuthChallengeRef,
293     // we make sure that is actually present
294     ASSERT(challenge.cfURLAuthChallengeRef());
295         
296     d->m_currentCFChallenge = challenge.cfURLAuthChallengeRef();
297     d->m_currentWebChallenge = AuthenticationChallenge(d->m_currentCFChallenge, this);
298     
299     if (client())
300         client()->didReceiveAuthenticationChallenge(this, d->m_currentWebChallenge);
301 }
302
303 void ResourceHandle::receivedCredential(const AuthenticationChallenge& challenge, const Credential& credential)
304 {
305     LOG(Network, "CFNet - receivedCredential()");
306     ASSERT(!challenge.isNull());
307     ASSERT(challenge.cfURLAuthChallengeRef());
308     if (challenge != d->m_currentWebChallenge)
309         return;
310
311     CFURLCredentialRef cfCredential = createCF(credential);
312     CFURLConnectionUseCredential(d->m_connection.get(), cfCredential, challenge.cfURLAuthChallengeRef());
313     CFRelease(cfCredential);
314
315     clearAuthentication();
316 }
317
318 void ResourceHandle::receivedRequestToContinueWithoutCredential(const AuthenticationChallenge& challenge)
319 {
320     LOG(Network, "CFNet - receivedRequestToContinueWithoutCredential()");
321     ASSERT(!challenge.isNull());
322     ASSERT(challenge.cfURLAuthChallengeRef());
323     if (challenge != d->m_currentWebChallenge)
324         return;
325
326     CFURLConnectionUseCredential(d->m_connection.get(), 0, challenge.cfURLAuthChallengeRef());
327
328     clearAuthentication();
329 }
330
331 void ResourceHandle::receivedCancellation(const AuthenticationChallenge& challenge)
332 {
333     LOG(Network, "CFNet - receivedCancellation()");
334     if (challenge != d->m_currentWebChallenge)
335         return;
336
337     if (client())
338         client()->receivedCancellation(this, challenge);
339 }
340
341 CFURLConnectionRef ResourceHandle::connection() const
342 {
343     return d->m_connection.get();
344 }
345
346 CFURLConnectionRef ResourceHandle::releaseConnectionForDownload()
347 {
348     LOG(Network, "CFNet - Job %p releasing connection %p for download", this, d->m_connection.get());
349     return d->m_connection.releaseRef();
350 }
351
352 void ResourceHandle::loadResourceSynchronously(const ResourceRequest& request, ResourceError& error, ResourceResponse& response, Vector<char>& vector)
353 {
354     ASSERT(!request.isEmpty());
355     CFURLResponseRef cfResponse = 0;
356     CFErrorRef cfError = 0;
357     RetainPtr<CFURLRequestRef> cfRequest(AdoptCF, makeFinalRequest(request, true));
358
359     CFDataRef data = CFURLConnectionSendSynchronousRequest(cfRequest.get(), &cfResponse, &cfError, request.timeoutInterval());
360
361     response = cfResponse;
362     if (cfResponse)
363         CFRelease(cfResponse);
364
365     error = cfError;
366     if (cfError)
367         CFRelease(cfError);
368
369     if (data) {
370         ASSERT(vector.isEmpty());
371         vector.append(CFDataGetBytePtr(data), CFDataGetLength(data));
372         CFRelease(data);
373     }
374 }
375
376 void ResourceHandle::setHostAllowsAnyHTTPSCertificate(const String& host)
377 {
378     allowsAnyHTTPSCertificateHosts().add(host.lower());
379 }
380
381 void ResourceHandle::setDefersLoading(bool defers)
382 {
383     if (defers)
384         CFURLConnectionHalt(d->m_connection.get());
385     else
386         CFURLConnectionResume(d->m_connection.get());
387 }
388
389 bool ResourceHandle::loadsBlocked()
390 {
391     return false;
392 }
393
394 bool ResourceHandle::willLoadFromCache(ResourceRequest&)
395 {
396     // Not having this function means that we'll ask the user about re-posting a form
397     // even when we go back to a page that's still in the cache.
398     notImplemented();
399     return false;
400 }
401
402 } // namespace WebCore