Build fix, not reviewed.
[WebKit.git] / WebCore / platform / network / cf / ResourceHandleCFNet.cpp
1 /*
2  * Copyright (C) 2004, 2005, 2006, 2007, 2009 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 "Base64.h"
35 #include "CString.h"
36 #include "CookieStorageWin.h"
37 #include "CredentialStorage.h"
38 #include "DocLoader.h"
39 #include "FormDataStreamCFNet.h"
40 #include "Frame.h"
41 #include "FrameLoader.h"
42 #include "Logging.h"
43 #include "MIMETypeRegistry.h"
44 #include "ResourceError.h"
45 #include "ResourceResponse.h"
46
47 #include <wtf/HashMap.h>
48 #include <wtf/Threading.h>
49
50 #include <sys/types.h>
51 #include <sys/stat.h>
52 #include <process.h> // for _beginthread()
53
54 #include <CFNetwork/CFNetwork.h>
55 #include <WebKitSystemInterface/WebKitSystemInterface.h>
56
57 namespace WebCore {
58
59 static CFStringRef WebCoreSynchronousLoaderRunLoopMode = CFSTR("WebCoreSynchronousLoaderRunLoopMode");
60
61 class WebCoreSynchronousLoader {
62 public:
63     static RetainPtr<CFDataRef> load(const ResourceRequest&, StoredCredentials, ResourceResponse&, ResourceError&);
64
65 private:
66     WebCoreSynchronousLoader(ResourceResponse& response, ResourceError& error)
67         : m_isDone(false)
68         , m_response(response)
69         , m_error(error)
70     {
71     }
72
73     static CFURLRequestRef willSendRequest(CFURLConnectionRef, CFURLRequestRef, CFURLResponseRef, const void* clientInfo);
74     static void didReceiveResponse(CFURLConnectionRef, CFURLResponseRef, const void* clientInfo);
75     static void didReceiveData(CFURLConnectionRef, CFDataRef, CFIndex, const void* clientInfo);
76     static void didFinishLoading(CFURLConnectionRef, const void* clientInfo);
77     static void didFail(CFURLConnectionRef, CFErrorRef, const void* clientInfo);
78     static void didReceiveChallenge(CFURLConnectionRef, CFURLAuthChallengeRef, const void* clientInfo);
79     static Boolean shouldUseCredentialStorage(CFURLConnectionRef, const void* clientInfo);
80
81     bool m_isDone;
82     RetainPtr<CFURLRef> m_url;
83     RetainPtr<CFStringRef> m_user;
84     RetainPtr<CFStringRef> m_pass;
85     // Store the preemptively used initial credential so that if we get an authentication challenge, we won't use the same one again.
86     Credential m_initialCredential;
87     bool m_allowStoredCredentials;
88     ResourceResponse& m_response;
89     RetainPtr<CFMutableDataRef> m_data;
90     ResourceError& m_error;
91 };
92
93 static HashSet<String>& allowsAnyHTTPSCertificateHosts()
94 {
95     static HashSet<String> hosts;
96
97     return hosts;
98 }
99
100 static HashMap<String, RetainPtr<CFDataRef> >& clientCerts()
101 {
102     static HashMap<String, RetainPtr<CFDataRef> > certs;
103     return certs;
104 }
105
106 static void setDefaultMIMEType(CFURLResponseRef response)
107 {
108     static CFStringRef defaultMIMETypeString = defaultMIMEType().createCFString();
109     
110     CFURLResponseSetMIMEType(response, defaultMIMETypeString);
111 }
112
113 static String encodeBasicAuthorization(const String& user, const String& password)
114 {
115     CString unencodedString = (user + ":" + password).utf8();
116     Vector<char> unencoded(unencodedString.length());
117     std::copy(unencodedString.data(), unencodedString.data() + unencodedString.length(), unencoded.begin());
118     Vector<char> encoded;
119     base64Encode(unencoded, encoded);
120     return String(encoded.data(), encoded.size());
121 }
122
123 CFURLRequestRef willSendRequest(CFURLConnectionRef conn, CFURLRequestRef cfRequest, CFURLResponseRef cfRedirectResponse, const void* clientInfo)
124 {
125     ResourceHandle* handle = static_cast<ResourceHandle*>(const_cast<void*>(clientInfo));
126
127     if (!cfRedirectResponse) {
128         CFRetain(cfRequest);
129         return cfRequest;
130     }
131
132     LOG(Network, "CFNet - willSendRequest(conn=%p, handle=%p) (%s)", conn, handle, handle->request().url().string().utf8().data());
133
134     ResourceRequest request;
135     if (cfRedirectResponse) {
136         CFHTTPMessageRef httpMessage = CFURLResponseGetHTTPResponse(cfRedirectResponse);
137         if (httpMessage && CFHTTPMessageGetResponseStatusCode(httpMessage) == 307) {
138             RetainPtr<CFStringRef> originalMethod(AdoptCF, handle->request().httpMethod().createCFString());
139             RetainPtr<CFStringRef> newMethod(AdoptCF, CFURLRequestCopyHTTPRequestMethod(cfRequest));
140             if (CFStringCompareWithOptions(originalMethod.get(), newMethod.get(), CFRangeMake(0, CFStringGetLength(originalMethod.get())), kCFCompareCaseInsensitive)) {
141                 RetainPtr<CFMutableURLRequestRef> mutableRequest(AdoptCF, CFURLRequestCreateMutableCopy(0, cfRequest));
142                 CFURLRequestSetHTTPRequestMethod(mutableRequest.get(), originalMethod.get());
143
144                 FormData* body = handle->request().httpBody();
145                 if (!equalIgnoringCase(handle->request().httpMethod(), "GET") && body && !body->isEmpty())
146                     WebCore::setHTTPBody(mutableRequest, body);
147
148                 RetainPtr<CFStringRef> originalContentType(AdoptCF, handle->request.httpContentType().createCFString());
149                 if (!originalContentType.isEmpty())
150                     CFURLRequestSetHTTPHeaderFieldValue(mutableRequest.get(), CFSTR("Content-Type"), originalContentType.get());
151
152                 request = mutableRequest.get();
153             }
154         }
155     }
156     if (request.isNull())
157         request = cfRequest;
158     
159     handle->willSendRequest(request, cfRedirectResponse);
160
161     if (request.isNull())
162         return 0;
163
164     cfRequest = request.cfURLRequest();
165
166     CFRetain(cfRequest);
167     return cfRequest;
168 }
169
170 void didReceiveResponse(CFURLConnectionRef conn, CFURLResponseRef cfResponse, const void* clientInfo) 
171 {
172     ResourceHandle* handle = static_cast<ResourceHandle*>(const_cast<void*>(clientInfo));
173
174     LOG(Network, "CFNet - didReceiveResponse(conn=%p, handle=%p) (%s)", conn, handle, handle->request().url().string().utf8().data());
175
176     if (!handle->client())
177         return;
178
179     if (!CFURLResponseGetMIMEType(cfResponse)) {
180         // We should never be applying the default MIMEType if we told the networking layer to do content sniffing for handle.
181         ASSERT(!handle->shouldContentSniff());
182         setDefaultMIMEType(cfResponse);
183     }
184     
185     handle->client()->didReceiveResponse(handle, cfResponse);
186 }
187
188 void didReceiveData(CFURLConnectionRef conn, CFDataRef data, CFIndex originalLength, const void* clientInfo) 
189 {
190     ResourceHandle* handle = static_cast<ResourceHandle*>(const_cast<void*>(clientInfo));
191     const UInt8* bytes = CFDataGetBytePtr(data);
192     CFIndex length = CFDataGetLength(data);
193
194     LOG(Network, "CFNet - didReceiveData(conn=%p, handle=%p, bytes=%d) (%s)", conn, handle, length, handle->request().url().string().utf8().data());
195
196     if (handle->client())
197         handle->client()->didReceiveData(handle, (const char*)bytes, length, originalLength);
198 }
199
200 static void didSendBodyData(CFURLConnectionRef conn, CFIndex bytesWritten, CFIndex totalBytesWritten, CFIndex totalBytesExpectedToWrite, const void *clientInfo)
201 {
202     ResourceHandle* handle = static_cast<ResourceHandle*>(const_cast<void*>(clientInfo));
203     if (!handle || !handle->client())
204         return;
205     handle->client()->didSendData(handle, totalBytesWritten, totalBytesExpectedToWrite);
206 }
207
208 static Boolean shouldUseCredentialStorageCallback(CFURLConnectionRef conn, const void* clientInfo)
209 {
210     ResourceHandle* handle = const_cast<ResourceHandle*>(static_cast<const ResourceHandle*>(clientInfo));
211
212     LOG(Network, "CFNet - shouldUseCredentialStorage(conn=%p, handle=%p) (%s)", conn, handle, handle->request().url().string().utf8().data());
213
214     if (!handle)
215         return false;
216
217     return handle->shouldUseCredentialStorage();
218 }
219
220 void didFinishLoading(CFURLConnectionRef conn, const void* clientInfo) 
221 {
222     ResourceHandle* handle = static_cast<ResourceHandle*>(const_cast<void*>(clientInfo));
223
224     LOG(Network, "CFNet - didFinishLoading(conn=%p, handle=%p) (%s)", conn, handle, handle->request().url().string().utf8().data());
225
226     if (handle->client())
227         handle->client()->didFinishLoading(handle);
228 }
229
230 void didFail(CFURLConnectionRef conn, CFErrorRef error, const void* clientInfo) 
231 {
232     ResourceHandle* handle = static_cast<ResourceHandle*>(const_cast<void*>(clientInfo));
233
234     LOG(Network, "CFNet - didFail(conn=%p, handle=%p, error = %p) (%s)", conn, handle, error, handle->request().url().string().utf8().data());
235
236     if (handle->client())
237         handle->client()->didFail(handle, ResourceError(error));
238 }
239
240 CFCachedURLResponseRef willCacheResponse(CFURLConnectionRef conn, CFCachedURLResponseRef cachedResponse, const void* clientInfo) 
241 {
242     ResourceHandle* handle = static_cast<ResourceHandle*>(const_cast<void*>(clientInfo));
243
244     if (handle->client() && !handle->client()->shouldCacheResponse(handle, cachedResponse))
245         return 0;
246
247     CacheStoragePolicy policy = static_cast<CacheStoragePolicy>(CFCachedURLResponseGetStoragePolicy(cachedResponse));
248
249     if (handle->client())
250         handle->client()->willCacheResponse(handle, policy);
251
252     if (static_cast<CFURLCacheStoragePolicy>(policy) != CFCachedURLResponseGetStoragePolicy(cachedResponse))
253         cachedResponse = CFCachedURLResponseCreateWithUserInfo(kCFAllocatorDefault, 
254                                                                CFCachedURLResponseGetWrappedResponse(cachedResponse),
255                                                                CFCachedURLResponseGetReceiverData(cachedResponse),
256                                                                CFCachedURLResponseGetUserInfo(cachedResponse), 
257                                                                static_cast<CFURLCacheStoragePolicy>(policy));
258     CFRetain(cachedResponse);
259
260     return cachedResponse;
261 }
262
263 void didReceiveChallenge(CFURLConnectionRef conn, CFURLAuthChallengeRef challenge, const void* clientInfo)
264 {
265     ResourceHandle* handle = static_cast<ResourceHandle*>(const_cast<void*>(clientInfo));
266     ASSERT(handle);
267     LOG(Network, "CFNet - didReceiveChallenge(conn=%p, handle=%p (%s)", conn, handle, handle->request().url().string().utf8().data());
268
269     handle->didReceiveAuthenticationChallenge(AuthenticationChallenge(challenge, handle));
270 }
271
272 void addHeadersFromHashMap(CFMutableURLRequestRef request, const HTTPHeaderMap& requestHeaders) 
273 {
274     if (!requestHeaders.size())
275         return;
276
277     HTTPHeaderMap::const_iterator end = requestHeaders.end();
278     for (HTTPHeaderMap::const_iterator it = requestHeaders.begin(); it != end; ++it) {
279         CFStringRef key = it->first.createCFString();
280         CFStringRef value = it->second.createCFString();
281         CFURLRequestSetHTTPHeaderFieldValue(request, key, value);
282         CFRelease(key);
283         CFRelease(value);
284     }
285 }
286
287 ResourceHandleInternal::~ResourceHandleInternal()
288 {
289     if (m_connection) {
290         LOG(Network, "CFNet - Cancelling connection %p (%s)", m_connection, m_request.url().string().utf8().data());
291         CFURLConnectionCancel(m_connection.get());
292     }
293 }
294
295 ResourceHandle::~ResourceHandle()
296 {
297     LOG(Network, "CFNet - Destroying job %p (%s)", this, d->m_request.url().string().utf8().data());
298 }
299
300 CFArrayRef arrayFromFormData(const FormData& d)
301 {
302     size_t size = d.elements().size();
303     CFMutableArrayRef a = CFArrayCreateMutable(0, d.elements().size(), &kCFTypeArrayCallBacks);
304     for (size_t i = 0; i < size; ++i) {
305         const FormDataElement& e = d.elements()[i];
306         if (e.m_type == FormDataElement::data) {
307             CFDataRef data = CFDataCreate(0, (const UInt8*)e.m_data.data(), e.m_data.size());
308             CFArrayAppendValue(a, data);
309             CFRelease(data);
310         } else {
311             ASSERT(e.m_type == FormDataElement::encodedFile);
312             CFStringRef filename = e.m_filename.createCFString();
313             CFArrayAppendValue(a, filename);
314             CFRelease(filename);
315         }
316     }
317     return a;
318 }
319
320 void emptyPerform(void* unused) 
321 {
322 }
323
324 static CFRunLoopRef loaderRL = 0;
325 void* runLoaderThread(void *unused)
326 {
327     loaderRL = CFRunLoopGetCurrent();
328
329     // Must add a source to the run loop to prevent CFRunLoopRun() from exiting
330     CFRunLoopSourceContext ctxt = {0, (void *)1 /*must be non-NULL*/, 0, 0, 0, 0, 0, 0, 0, emptyPerform};
331     CFRunLoopSourceRef bogusSource = CFRunLoopSourceCreate(0, 0, &ctxt);
332     CFRunLoopAddSource(loaderRL, bogusSource,kCFRunLoopDefaultMode);
333
334     CFRunLoopRun();
335
336     return 0;
337 }
338
339 CFRunLoopRef ResourceHandle::loaderRunLoop()
340 {
341     if (!loaderRL) {
342         createThread(runLoaderThread, 0, "WebCore: CFNetwork Loader");
343         while (loaderRL == 0) {
344             // FIXME: sleep 10? that can't be right...
345             Sleep(10);
346         }
347     }
348     return loaderRL;
349 }
350
351 static CFURLRequestRef makeFinalRequest(const ResourceRequest& request, bool shouldContentSniff)
352 {
353     CFMutableURLRequestRef newRequest = CFURLRequestCreateMutableCopy(kCFAllocatorDefault, request.cfURLRequest());
354     
355     if (!shouldContentSniff)
356         wkSetCFURLRequestShouldContentSniff(newRequest, false);
357
358     RetainPtr<CFMutableDictionaryRef> sslProps;
359
360     if (allowsAnyHTTPSCertificateHosts().contains(request.url().host().lower())) {
361         sslProps.adoptCF(CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
362         CFDictionaryAddValue(sslProps.get(), kCFStreamSSLAllowsAnyRoot, kCFBooleanTrue);
363         CFDictionaryAddValue(sslProps.get(), kCFStreamSSLAllowsExpiredRoots, kCFBooleanTrue);
364         CFDictionaryAddValue(sslProps.get(), kCFStreamSSLAllowsExpiredCertificates, kCFBooleanTrue);
365         CFDictionaryAddValue(sslProps.get(), kCFStreamSSLValidatesCertificateChain, kCFBooleanFalse);
366     }
367
368     HashMap<String, RetainPtr<CFDataRef> >::iterator clientCert = clientCerts().find(request.url().host().lower());
369     if (clientCert != clientCerts().end()) {
370         if (!sslProps)
371             sslProps.adoptCF(CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
372         wkSetClientCertificateInSSLProperties(sslProps.get(), (clientCert->second).get());
373     }
374
375     if (sslProps)
376         CFURLRequestSetSSLProperties(newRequest, sslProps.get());
377
378     if (CFHTTPCookieStorageRef cookieStorage = currentCookieStorage()) {
379         CFURLRequestSetHTTPCookieStorage(newRequest, cookieStorage);
380         CFURLRequestSetHTTPCookieStorageAcceptPolicy(newRequest, CFHTTPCookieStorageGetCookieAcceptPolicy(cookieStorage));
381     }
382
383     return newRequest;
384 }
385
386 bool ResourceHandle::start(Frame* frame)
387 {
388     // If we are no longer attached to a Page, this must be an attempted load from an
389     // onUnload handler, so let's just block it.
390     if (!frame->page())
391         return false;
392
393     if ((!d->m_user.isEmpty() || !d->m_pass.isEmpty()) && !d->m_request.url().protocolInHTTPFamily()) {
394         // Credentials for ftp can only be passed in URL, the didReceiveAuthenticationChallenge delegate call won't be made.
395         KURL urlWithCredentials(d->m_request.url());
396         urlWithCredentials.setUser(d->m_user);
397         urlWithCredentials.setPass(d->m_pass);
398         d->m_request.setURL(urlWithCredentials);
399     }
400
401     // <rdar://problem/7174050> - For URLs that match the paths of those previously challenged for HTTP Basic authentication, 
402     // try and reuse the credential preemptively, as allowed by RFC 2617.
403     if (!client() || client()->shouldUseCredentialStorage(this) && d->m_request.url().protocolInHTTPFamily())
404         d->m_initialCredential = CredentialStorage::getDefaultAuthenticationCredential(d->m_request.url());
405         
406     if (!d->m_initialCredential.isEmpty()) {
407         String authHeader = "Basic " + encodeBasicAuthorization(d->m_initialCredential.user(), d->m_initialCredential.password());
408         d->m_request.addHTTPHeaderField("Authorization", authHeader);
409     }
410
411     RetainPtr<CFURLRequestRef> request(AdoptCF, makeFinalRequest(d->m_request, d->m_shouldContentSniff));
412
413     CFURLConnectionClient_V3 client = { 3, this, 0, 0, 0, WebCore::willSendRequest, didReceiveResponse, didReceiveData, NULL, didFinishLoading, didFail, willCacheResponse, didReceiveChallenge, didSendBodyData, shouldUseCredentialStorageCallback, 0};
414
415     d->m_connection.adoptCF(CFURLConnectionCreate(0, request.get(), reinterpret_cast<CFURLConnectionClient*>(&client)));
416
417     CFURLConnectionScheduleWithCurrentMessageQueue(d->m_connection.get());
418     CFURLConnectionScheduleDownloadWithRunLoop(d->m_connection.get(), loaderRunLoop(), kCFRunLoopDefaultMode);
419     CFURLConnectionStart(d->m_connection.get());
420
421     LOG(Network, "CFNet - Starting URL %s (handle=%p, conn=%p)", d->m_request.url().string().utf8().data(), this, d->m_connection);
422
423     return true;
424 }
425
426 void ResourceHandle::cancel()
427 {
428     if (d->m_connection) {
429         CFURLConnectionCancel(d->m_connection.get());
430         d->m_connection = 0;
431     }
432 }
433
434 PassRefPtr<SharedBuffer> ResourceHandle::bufferedData()
435 {
436     ASSERT_NOT_REACHED();
437     return 0;
438 }
439
440 bool ResourceHandle::supportsBufferedData()
441 {
442     return false;
443 }
444
445 void ResourceHandle::willSendRequest(ResourceRequest& request, const ResourceResponse& redirectResponse)
446 {
447     const KURL& url = request.url();
448     d->m_user = url.user();
449     d->m_pass = url.pass();
450     request.removeCredentials();
451
452     client()->willSendRequest(this, request, redirectResponse);
453 }
454
455 bool ResourceHandle::shouldUseCredentialStorage()
456 {
457     LOG(Network, "CFNet - shouldUseCredentialStorage()");
458     if (client())
459         return client()->shouldUseCredentialStorage(this);
460
461     return false;
462 }
463
464 void ResourceHandle::didReceiveAuthenticationChallenge(const AuthenticationChallenge& challenge)
465 {
466     LOG(Network, "CFNet - didReceiveAuthenticationChallenge()");
467     ASSERT(!d->m_currentCFChallenge);
468     ASSERT(d->m_currentWebChallenge.isNull());
469     // Since CFURLConnection networking relies on keeping a reference to the original CFURLAuthChallengeRef,
470     // we make sure that is actually present
471     ASSERT(challenge.cfURLAuthChallengeRef());
472
473     if (!d->m_user.isNull() && !d->m_pass.isNull()) {
474         RetainPtr<CFStringRef> user(AdoptCF, d->m_user.createCFString());
475         RetainPtr<CFStringRef> pass(AdoptCF, d->m_pass.createCFString());
476         RetainPtr<CFURLCredentialRef> credential(AdoptCF,
477             CFURLCredentialCreate(kCFAllocatorDefault, user.get(), pass.get(), 0, kCFURLCredentialPersistenceNone));
478         
479         KURL urlToStore;
480         if (challenge.failureResponse().httpStatusCode() == 401)
481             urlToStore = d->m_request.url();
482         CredentialStorage::set(core(credential.get()), challenge.protectionSpace(), urlToStore);
483         
484         CFURLConnectionUseCredential(d->m_connection.get(), credential.get(), challenge.cfURLAuthChallengeRef());
485         d->m_user = String();
486         d->m_pass = String();
487         // FIXME: Per the specification, the user shouldn't be asked for credentials if there were incorrect ones provided explicitly.
488         return;
489     }
490
491     if (!challenge.previousFailureCount() && (!client() || client()->shouldUseCredentialStorage(this))) {
492         Credential credential = CredentialStorage::get(challenge.protectionSpace());
493         if (!credential.isEmpty() && credential != d->m_initialCredential) {
494             ASSERT(credential.persistence() == CredentialPersistenceNone);
495             RetainPtr<CFURLCredentialRef> cfCredential(AdoptCF, createCF(credential));
496             CFURLConnectionUseCredential(d->m_connection.get(), cfCredential.get(), challenge.cfURLAuthChallengeRef());
497             return;
498         }
499     }
500
501     d->m_currentCFChallenge = challenge.cfURLAuthChallengeRef();
502     d->m_currentWebChallenge = AuthenticationChallenge(d->m_currentCFChallenge, this);
503     
504     if (client())
505         client()->didReceiveAuthenticationChallenge(this, d->m_currentWebChallenge);
506 }
507
508 void ResourceHandle::receivedCredential(const AuthenticationChallenge& challenge, const Credential& credential)
509 {
510     LOG(Network, "CFNet - receivedCredential()");
511     ASSERT(!challenge.isNull());
512     ASSERT(challenge.cfURLAuthChallengeRef());
513     if (challenge != d->m_currentWebChallenge)
514         return;
515
516     if (credential.persistence() == CredentialPersistenceForSession) {
517         // Manage per-session credentials internally, because once NSURLCredentialPersistencePerSession is used, there is no way
518         // to ignore it for a particular request (short of removing it altogether).
519         Credential webCredential(credential.user(), credential.password(), CredentialPersistenceNone);
520         RetainPtr<CFURLCredentialRef> cfCredential(AdoptCF, createCF(webCredential));
521         
522         KURL urlToStore;
523         if (challenge.failureResponse().httpStatusCode() == 401)
524             urlToStore = d->m_request.url();      
525         CredentialStorage::set(webCredential, challenge.protectionSpace(), urlToStore);
526
527         CFURLConnectionUseCredential(d->m_connection.get(), cfCredential.get(), challenge.cfURLAuthChallengeRef());
528     } else {
529         RetainPtr<CFURLCredentialRef> cfCredential(AdoptCF, createCF(credential));
530         CFURLConnectionUseCredential(d->m_connection.get(), cfCredential.get(), challenge.cfURLAuthChallengeRef());
531     }
532
533     clearAuthentication();
534 }
535
536 void ResourceHandle::receivedRequestToContinueWithoutCredential(const AuthenticationChallenge& challenge)
537 {
538     LOG(Network, "CFNet - receivedRequestToContinueWithoutCredential()");
539     ASSERT(!challenge.isNull());
540     ASSERT(challenge.cfURLAuthChallengeRef());
541     if (challenge != d->m_currentWebChallenge)
542         return;
543
544     CFURLConnectionUseCredential(d->m_connection.get(), 0, challenge.cfURLAuthChallengeRef());
545
546     clearAuthentication();
547 }
548
549 void ResourceHandle::receivedCancellation(const AuthenticationChallenge& challenge)
550 {
551     LOG(Network, "CFNet - receivedCancellation()");
552     if (challenge != d->m_currentWebChallenge)
553         return;
554
555     if (client())
556         client()->receivedCancellation(this, challenge);
557 }
558
559 CFURLConnectionRef ResourceHandle::connection() const
560 {
561     return d->m_connection.get();
562 }
563
564 CFURLConnectionRef ResourceHandle::releaseConnectionForDownload()
565 {
566     LOG(Network, "CFNet - Job %p releasing connection %p for download", this, d->m_connection.get());
567     return d->m_connection.releaseRef();
568 }
569
570 void ResourceHandle::loadResourceSynchronously(const ResourceRequest& request, StoredCredentials storedCredentials, ResourceError& error, ResourceResponse& response, Vector<char>& vector, Frame*)
571 {
572     ASSERT(!request.isEmpty());
573
574     RetainPtr<CFDataRef> data = WebCoreSynchronousLoader::load(request, storedCredentials, response, error);
575
576     if (!error.isNull()) {
577         response = ResourceResponse(request.url(), String(), 0, String(), String());
578
579         CFErrorRef cfError = error;
580         CFStringRef domain = CFErrorGetDomain(cfError);
581         // FIXME: Return the actual response for failed authentication.
582         if (domain == kCFErrorDomainCFNetwork)
583             response.setHTTPStatusCode(CFErrorGetCode(cfError));
584         else
585             response.setHTTPStatusCode(404);
586     }
587
588     if (data) {
589         ASSERT(vector.isEmpty());
590         vector.append(CFDataGetBytePtr(data.get()), CFDataGetLength(data.get()));
591     }
592 }
593
594 void ResourceHandle::setHostAllowsAnyHTTPSCertificate(const String& host)
595 {
596     allowsAnyHTTPSCertificateHosts().add(host.lower());
597 }
598
599 void ResourceHandle::setClientCertificate(const String& host, CFDataRef cert)
600 {
601     clientCerts().set(host.lower(), cert);
602 }
603
604 void ResourceHandle::setDefersLoading(bool defers)
605 {
606     if (!d->m_connection)
607         return;
608
609     if (defers)
610         CFURLConnectionHalt(d->m_connection.get());
611     else
612         CFURLConnectionResume(d->m_connection.get());
613 }
614
615 bool ResourceHandle::loadsBlocked()
616 {
617     return false;
618 }
619
620 bool ResourceHandle::willLoadFromCache(ResourceRequest& request, Frame* frame)
621 {
622     request.setCachePolicy(ReturnCacheDataDontLoad);
623     
624     CFURLResponseRef cfResponse = 0;
625     CFErrorRef cfError = 0;
626     RetainPtr<CFURLRequestRef> cfRequest(AdoptCF, makeFinalRequest(request, true));
627     RetainPtr<CFDataRef> data(AdoptCF, CFURLConnectionSendSynchronousRequest(cfRequest.get(), &cfResponse, &cfError, request.timeoutInterval()));
628     bool cached = cfResponse && !cfError;
629
630     if (cfError)
631         CFRelease(cfError);
632     if (cfResponse)
633         CFRelease(cfResponse);
634
635     return cached;
636 }
637
638 CFURLRequestRef WebCoreSynchronousLoader::willSendRequest(CFURLConnectionRef, CFURLRequestRef cfRequest, CFURLResponseRef cfRedirectResponse, const void* clientInfo)
639 {
640     WebCoreSynchronousLoader* loader = static_cast<WebCoreSynchronousLoader*>(const_cast<void*>(clientInfo));
641
642     // FIXME: This needs to be fixed to follow the redirect correctly even for cross-domain requests.
643     if (loader->m_url && !protocolHostAndPortAreEqual(loader->m_url.get(), CFURLRequestGetURL(cfRequest))) {
644         RetainPtr<CFErrorRef> cfError(AdoptCF, CFErrorCreate(kCFAllocatorDefault, kCFErrorDomainCFNetwork, kCFURLErrorBadServerResponse, 0));
645         loader->m_error = cfError.get();
646         loader->m_isDone = true;
647         return 0;
648     }
649
650     loader->m_url = CFURLRequestGetURL(cfRequest);
651
652     if (cfRedirectResponse) {
653         // Take user/pass out of the URL.
654         loader->m_user.adoptCF(CFURLCopyUserName(loader->m_url.get()));
655         loader->m_pass.adoptCF(CFURLCopyPassword(loader->m_url.get()));
656         if (loader->m_user || loader->m_pass) {
657             ResourceRequest requestWithoutCredentials = cfRequest;
658             requestWithoutCredentials.removeCredentials();
659             cfRequest = requestWithoutCredentials.cfURLRequest();
660         }
661     }
662
663     CFRetain(cfRequest);
664     return cfRequest;
665 }
666
667 void WebCoreSynchronousLoader::didReceiveResponse(CFURLConnectionRef, CFURLResponseRef cfResponse, const void* clientInfo) 
668 {
669     WebCoreSynchronousLoader* loader = static_cast<WebCoreSynchronousLoader*>(const_cast<void*>(clientInfo));
670
671     loader->m_response = cfResponse;
672 }
673
674 void WebCoreSynchronousLoader::didReceiveData(CFURLConnectionRef, CFDataRef data, CFIndex originalLength, const void* clientInfo)
675 {
676     WebCoreSynchronousLoader* loader = static_cast<WebCoreSynchronousLoader*>(const_cast<void*>(clientInfo));
677
678     if (!loader->m_data)
679         loader->m_data.adoptCF(CFDataCreateMutable(kCFAllocatorDefault, 0));
680
681     const UInt8* bytes = CFDataGetBytePtr(data);
682     CFIndex length = CFDataGetLength(data);
683
684     CFDataAppendBytes(loader->m_data.get(), bytes, length);
685 }
686
687 void WebCoreSynchronousLoader::didFinishLoading(CFURLConnectionRef, const void* clientInfo)
688 {
689     WebCoreSynchronousLoader* loader = static_cast<WebCoreSynchronousLoader*>(const_cast<void*>(clientInfo));
690
691     loader->m_isDone = true;
692 }
693
694 void WebCoreSynchronousLoader::didFail(CFURLConnectionRef, CFErrorRef error, const void* clientInfo)
695 {
696     WebCoreSynchronousLoader* loader = static_cast<WebCoreSynchronousLoader*>(const_cast<void*>(clientInfo));
697
698     loader->m_error = error;
699     loader->m_isDone = true;
700 }
701
702 void WebCoreSynchronousLoader::didReceiveChallenge(CFURLConnectionRef conn, CFURLAuthChallengeRef challenge, const void* clientInfo)
703 {
704     WebCoreSynchronousLoader* loader = static_cast<WebCoreSynchronousLoader*>(const_cast<void*>(clientInfo));
705
706     if (loader->m_user && loader->m_pass) {
707         Credential credential(loader->m_user.get(), loader->m_pass.get(), CredentialPersistenceNone);
708         RetainPtr<CFURLCredentialRef> cfCredential(AdoptCF, createCF(credential));
709         
710         CFURLResponseRef urlResponse = (CFURLResponseRef)CFURLAuthChallengeGetFailureResponse(challenge);
711         CFHTTPMessageRef httpResponse = urlResponse ? CFURLResponseGetHTTPResponse(urlResponse) : 0;
712         KURL urlToStore;
713         if (httpResponse && CFHTTPMessageGetResponseStatusCode(httpResponse) == 401)
714             urlToStore = loader->m_url.get();
715             
716         CredentialStorage::set(credential, core(CFURLAuthChallengeGetProtectionSpace(challenge)), urlToStore);
717         
718         CFURLConnectionUseCredential(conn, cfCredential.get(), challenge);
719         loader->m_user = 0;
720         loader->m_pass = 0;
721         return;
722     }
723     if (!CFURLAuthChallengeGetPreviousFailureCount(challenge) && loader->m_allowStoredCredentials) {
724         Credential credential = CredentialStorage::get(core(CFURLAuthChallengeGetProtectionSpace(challenge)));
725         if (!credential.isEmpty() && credential != loader->m_initialCredential) {
726             ASSERT(credential.persistence() == CredentialPersistenceNone);
727             RetainPtr<CFURLCredentialRef> cfCredential(AdoptCF, createCF(credential));
728             CFURLConnectionUseCredential(conn, cfCredential.get(), challenge);
729             return;
730         }
731     }
732     // FIXME: The user should be asked for credentials, as in async case.
733     CFURLConnectionUseCredential(conn, 0, challenge);
734 }
735
736 Boolean WebCoreSynchronousLoader::shouldUseCredentialStorage(CFURLConnectionRef, const void* clientInfo)
737 {
738     WebCoreSynchronousLoader* loader = static_cast<WebCoreSynchronousLoader*>(const_cast<void*>(clientInfo));
739
740     // FIXME: We should ask FrameLoaderClient whether using credential storage is globally forbidden.
741     return loader->m_allowStoredCredentials;
742 }
743
744 RetainPtr<CFDataRef> WebCoreSynchronousLoader::load(const ResourceRequest& request, StoredCredentials storedCredentials, ResourceResponse& response, ResourceError& error)
745 {
746     ASSERT(response.isNull());
747     ASSERT(error.isNull());
748
749     WebCoreSynchronousLoader loader(response, error);
750
751     KURL url = request.url();
752
753     if (url.user().length())
754         loader.m_user.adoptCF(url.user().createCFString());
755     if (url.pass().length())
756         loader.m_pass.adoptCF(url.pass().createCFString());
757     loader.m_allowStoredCredentials = (storedCredentials == AllowStoredCredentials);
758
759     // Take user/pass out of the URL.
760     // Credentials for ftp can only be passed in URL, the didReceiveAuthenticationChallenge delegate call won't be made.
761     RetainPtr<CFURLRequestRef> cfRequest;
762     if ((loader.m_user || loader.m_pass) && url.protocolInHTTPFamily()) {
763         ResourceRequest requestWithoutCredentials(request);
764         requestWithoutCredentials.removeCredentials();
765         cfRequest.adoptCF(makeFinalRequest(requestWithoutCredentials, ResourceHandle::shouldContentSniffURL(requestWithoutCredentials.url())));
766     } else {
767         // <rdar://problem/7174050> - For URLs that match the paths of those previously challenged for HTTP Basic authentication, 
768         // try and reuse the credential preemptively, as allowed by RFC 2617.
769         ResourceRequest requestWithInitialCredential(request);
770         if (loader.m_allowStoredCredentials && url.protocolInHTTPFamily())
771             loader.m_initialCredential = CredentialStorage::getDefaultAuthenticationCredential(url);
772
773         if (!loader.m_initialCredential.isEmpty()) {
774             String authHeader = "Basic " + encodeBasicAuthorization(loader.m_initialCredential.user(), loader.m_initialCredential.password());
775             requestWithInitialCredential.addHTTPHeaderField("Authorization", authHeader);
776         }
777
778         cfRequest.adoptCF(makeFinalRequest(requestWithInitialCredential, ResourceHandle::shouldContentSniffURL(requestWithInitialCredential.url())));
779     }
780
781     CFURLConnectionClient_V3 client = { 3, &loader, 0, 0, 0, willSendRequest, didReceiveResponse, didReceiveData, 0, didFinishLoading, didFail, 0, didReceiveChallenge, 0, shouldUseCredentialStorage, 0 };
782     RetainPtr<CFURLConnectionRef> connection(AdoptCF, CFURLConnectionCreate(kCFAllocatorDefault, cfRequest.get(), reinterpret_cast<CFURLConnectionClient*>(&client)));
783
784     CFURLConnectionScheduleWithRunLoop(connection.get(), CFRunLoopGetCurrent(), WebCoreSynchronousLoaderRunLoopMode);
785     CFURLConnectionScheduleDownloadWithRunLoop(connection.get(), CFRunLoopGetCurrent(), WebCoreSynchronousLoaderRunLoopMode);
786     CFURLConnectionStart(connection.get());
787
788     while (!loader.m_isDone)
789         CFRunLoopRunInMode(WebCoreSynchronousLoaderRunLoopMode, UINT_MAX, true);
790
791     CFURLConnectionCancel(connection.get());
792     
793     if (error.isNull() && loader.m_response.mimeType().isNull())
794         setDefaultMIMEType(loader.m_response.cfURLResponse());
795
796     return loader.m_data;
797 }
798
799 } // namespace WebCore