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