2 * Copyright (C) 2004, 2005, 2006, 2007, 2009, 2010, 2011 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
28 #include "ResourceHandleInternal.h"
30 #include "AuthenticationCF.h"
31 #include "AuthenticationChallenge.h"
32 #include "CredentialStorage.h"
33 #include "CachedResourceLoader.h"
34 #include "FormDataStreamCFNet.h"
36 #include "FrameLoader.h"
37 #include "LoaderRunLoopCF.h"
39 #include "MIMETypeRegistry.h"
40 #include "NetworkingContext.h"
41 #include "ResourceError.h"
42 #include "ResourceHandleClient.h"
43 #include "ResourceResponse.h"
44 #include "SharedBuffer.h"
45 #include "SynchronousLoaderClient.h"
46 #include <CFNetwork/CFNetwork.h>
48 #include <sys/types.h>
49 #include <wtf/HashMap.h>
50 #include <wtf/Threading.h>
51 #include <wtf/text/Base64.h>
52 #include <wtf/text/CString.h>
55 #include "WebCoreSystemInterface.h"
57 #include "WebCoreURLResponse.h"
58 #include <CFNetwork/CFURLConnectionPriv.h>
59 #include <CFNetwork/CFURLRequestPriv.h>
64 #include <WebKitSystemInterface/WebKitSystemInterface.h>
67 // FIXME: Remove this declaration once it's in WebKitSupportLibrary.
69 __declspec(dllimport) CFURLConnectionRef CFURLConnectionCreateWithProperties(
71 CFURLRequestRef request,
72 CFURLConnectionClient * client,
73 CFDictionaryRef properties);
81 static HashSet<String>& allowsAnyHTTPSCertificateHosts()
83 DEFINE_STATIC_LOCAL(HashSet<String>, hosts, ());
87 static HashMap<String, RetainPtr<CFDataRef> >& clientCerts()
89 typedef HashMap<String, RetainPtr<CFDataRef> > CertsMap;
90 DEFINE_STATIC_LOCAL(CertsMap, certs, ());
95 static void setDefaultMIMEType(CFURLResponseRef response)
97 static CFStringRef defaultMIMETypeString = defaultMIMEType().createCFString().leakRef();
99 CFURLResponseSetMIMEType(response, defaultMIMETypeString);
103 static void applyBasicAuthorizationHeader(ResourceRequest& request, const Credential& credential)
105 String authenticationHeader = "Basic " + base64Encode(String(credential.user() + ":" + credential.password()).utf8());
106 request.clearHTTPAuthorization(); // FIXME: Should addHTTPHeaderField be smart enough to not build comma-separated lists in headers like Authorization?
107 request.addHTTPHeaderField("Authorization", authenticationHeader);
110 static CFURLRequestRef willSendRequest(CFURLConnectionRef conn, CFURLRequestRef cfRequest, CFURLResponseRef cfRedirectResponse, const void* clientInfo)
115 ResourceHandle* handle = static_cast<ResourceHandle*>(const_cast<void*>(clientInfo));
117 if (!cfRedirectResponse) {
122 LOG(Network, "CFNet - willSendRequest(conn=%p, handle=%p) (%s)", conn, handle, handle->firstRequest().url().string().utf8().data());
124 ResourceRequest request;
125 if (cfRedirectResponse) {
126 CFHTTPMessageRef httpMessage = CFURLResponseGetHTTPResponse(cfRedirectResponse);
127 if (httpMessage && CFHTTPMessageGetResponseStatusCode(httpMessage) == 307) {
128 RetainPtr<CFStringRef> lastHTTPMethod = handle->lastHTTPMethod().createCFString();
129 RetainPtr<CFStringRef> newMethod = adoptCF(CFURLRequestCopyHTTPRequestMethod(cfRequest));
130 if (CFStringCompareWithOptions(lastHTTPMethod.get(), newMethod.get(), CFRangeMake(0, CFStringGetLength(lastHTTPMethod.get())), kCFCompareCaseInsensitive)) {
131 RetainPtr<CFMutableURLRequestRef> mutableRequest(AdoptCF, CFURLRequestCreateMutableCopy(0, cfRequest));
132 wkSetRequestStorageSession(handle->storageSession(), mutableRequest.get());
133 CFURLRequestSetHTTPRequestMethod(mutableRequest.get(), lastHTTPMethod.get());
135 FormData* body = handle->firstRequest().httpBody();
136 if (!equalIgnoringCase(handle->firstRequest().httpMethod(), "GET") && body && !body->isEmpty())
137 WebCore::setHTTPBody(mutableRequest.get(), body);
139 String originalContentType = handle->firstRequest().httpContentType();
140 if (!originalContentType.isEmpty())
141 CFURLRequestSetHTTPHeaderFieldValue(mutableRequest.get(), CFSTR("Content-Type"), originalContentType.createCFString().get());
143 request = mutableRequest.get();
147 if (request.isNull())
150 // Should not set Referer after a redirect from a secure resource to non-secure one.
151 if (!request.url().protocolIs("https") && protocolIs(request.httpReferrer(), "https") && handle->context()->shouldClearReferrerOnHTTPSToHTTPRedirect())
152 request.clearHTTPReferrer();
154 handle->willSendRequest(request, cfRedirectResponse);
156 if (request.isNull())
159 cfRequest = request.cfURLRequest(UpdateHTTPBody);
165 static void didReceiveResponse(CFURLConnectionRef conn, CFURLResponseRef cfResponse, const void* clientInfo)
170 ResourceHandle* handle = static_cast<ResourceHandle*>(const_cast<void*>(clientInfo));
172 LOG(Network, "CFNet - didReceiveResponse(conn=%p, handle=%p) (%s)", conn, handle, handle->firstRequest().url().string().utf8().data());
174 if (!handle->client())
178 // Avoid MIME type sniffing if the response comes back as 304 Not Modified.
179 CFHTTPMessageRef msg = wkGetCFURLResponseHTTPResponse(cfResponse);
180 int statusCode = msg ? CFHTTPMessageGetResponseStatusCode(msg) : 0;
182 if (statusCode != 304)
183 adjustMIMETypeIfNecessary(cfResponse);
185 if (_CFURLRequestCopyProtocolPropertyForKey(handle->firstRequest().cfURLRequest(DoNotUpdateHTTPBody), CFSTR("ForceHTMLMIMEType")))
186 wkSetCFURLResponseMIMEType(cfResponse, CFSTR("text/html"));
188 if (!CFURLResponseGetMIMEType(cfResponse)) {
189 // We should never be applying the default MIMEType if we told the networking layer to do content sniffing for handle.
190 ASSERT(!handle->shouldContentSniff());
191 setDefaultMIMEType(cfResponse);
195 handle->client()->didReceiveResponse(handle, cfResponse);
198 #if USE(NETWORK_CFDATA_ARRAY_CALLBACK)
199 static void didReceiveDataArray(CFURLConnectionRef conn, CFArrayRef dataArray, const void* clientInfo)
204 ResourceHandle* handle = static_cast<ResourceHandle*>(const_cast<void*>(clientInfo));
205 if (!handle->client())
208 LOG(Network, "CFNet - didReceiveDataArray(conn=%p, handle=%p, arrayLength=%ld) (%s)", conn, handle, CFArrayGetCount(dataArray), handle->firstRequest().url().string().utf8().data());
210 handle->handleDataArray(dataArray);
214 static void didReceiveData(CFURLConnectionRef conn, CFDataRef data, CFIndex originalLength, const void* clientInfo)
219 ResourceHandle* handle = static_cast<ResourceHandle*>(const_cast<void*>(clientInfo));
220 const UInt8* bytes = CFDataGetBytePtr(data);
221 CFIndex length = CFDataGetLength(data);
223 LOG(Network, "CFNet - didReceiveData(conn=%p, handle=%p, bytes=%ld) (%s)", conn, handle, length, handle->firstRequest().url().string().utf8().data());
225 if (handle->client())
226 handle->client()->didReceiveData(handle, (const char*)bytes, length, originalLength);
229 static void didSendBodyData(CFURLConnectionRef, CFIndex, CFIndex totalBytesWritten, CFIndex totalBytesExpectedToWrite, const void *clientInfo)
231 ResourceHandle* handle = static_cast<ResourceHandle*>(const_cast<void*>(clientInfo));
232 if (!handle || !handle->client())
234 handle->client()->didSendData(handle, totalBytesWritten, totalBytesExpectedToWrite);
237 static Boolean shouldUseCredentialStorageCallback(CFURLConnectionRef conn, const void* clientInfo)
242 ResourceHandle* handle = const_cast<ResourceHandle*>(static_cast<const ResourceHandle*>(clientInfo));
244 LOG(Network, "CFNet - shouldUseCredentialStorage(conn=%p, handle=%p) (%s)", conn, handle, handle->firstRequest().url().string().utf8().data());
249 return handle->shouldUseCredentialStorage();
252 static void didFinishLoading(CFURLConnectionRef conn, const void* clientInfo)
257 ResourceHandle* handle = static_cast<ResourceHandle*>(const_cast<void*>(clientInfo));
259 LOG(Network, "CFNet - didFinishLoading(conn=%p, handle=%p) (%s)", conn, handle, handle->firstRequest().url().string().utf8().data());
261 if (handle->client())
262 handle->client()->didFinishLoading(handle, 0);
265 static void didFail(CFURLConnectionRef conn, CFErrorRef error, const void* clientInfo)
270 ResourceHandle* handle = static_cast<ResourceHandle*>(const_cast<void*>(clientInfo));
272 LOG(Network, "CFNet - didFail(conn=%p, handle=%p, error = %p) (%s)", conn, handle, error, handle->firstRequest().url().string().utf8().data());
274 if (handle->client())
275 handle->client()->didFail(handle, ResourceError(error));
278 static CFCachedURLResponseRef willCacheResponse(CFURLConnectionRef, CFCachedURLResponseRef cachedResponse, const void* clientInfo)
280 ResourceHandle* handle = static_cast<ResourceHandle*>(const_cast<void*>(clientInfo));
281 CFURLResponseRef wrappedResponse = CFCachedURLResponseGetWrappedResponse(cachedResponse);
283 // Workaround for <rdar://problem/6300990> Caching does not respect Vary HTTP header.
284 // FIXME: WebCore cache has issues with Vary, too (bug 58797, bug 71509).
285 if (CFHTTPMessageRef httpResponse = CFURLResponseGetHTTPResponse(wrappedResponse)) {
286 ASSERT(CFHTTPMessageIsHeaderComplete(httpResponse));
287 RetainPtr<CFStringRef> varyValue = adoptCF(CFHTTPMessageCopyHeaderFieldValue(httpResponse, CFSTR("Vary")));
293 if (handle->client() && !handle->client()->shouldCacheResponse(handle, cachedResponse))
296 CFCachedURLResponseRef newResponse = handle->client()->willCacheResponse(handle, cachedResponse);
297 if (newResponse != cachedResponse)
301 CFRetain(cachedResponse);
303 return cachedResponse;
306 static void didReceiveChallenge(CFURLConnectionRef conn, CFURLAuthChallengeRef challenge, const void* clientInfo)
311 ResourceHandle* handle = static_cast<ResourceHandle*>(const_cast<void*>(clientInfo));
313 LOG(Network, "CFNet - didReceiveChallenge(conn=%p, handle=%p (%s)", conn, handle, handle->firstRequest().url().string().utf8().data());
315 handle->didReceiveAuthenticationChallenge(AuthenticationChallenge(challenge, handle));
318 #if USE(PROTECTION_SPACE_AUTH_CALLBACK)
319 static Boolean canRespondToProtectionSpace(CFURLConnectionRef conn, CFURLProtectionSpaceRef protectionSpace, const void* clientInfo)
324 ResourceHandle* handle = static_cast<ResourceHandle*>(const_cast<void*>(clientInfo));
327 LOG(Network, "CFNet - canRespondToProtectionSpace(conn=%p, handle=%p (%s)", conn, handle, handle->firstRequest().url().string().utf8().data());
329 return handle->canAuthenticateAgainstProtectionSpace(core(protectionSpace));
333 ResourceHandleInternal::~ResourceHandleInternal()
336 LOG(Network, "CFNet - Cancelling connection %p (%s)", m_connection.get(), m_firstRequest.url().string().utf8().data());
337 CFURLConnectionCancel(m_connection.get());
341 ResourceHandle::~ResourceHandle()
343 LOG(Network, "CFNet - Destroying job %p (%s)", this, d->m_firstRequest.url().string().utf8().data());
346 void ResourceHandle::createCFURLConnection(bool shouldUseCredentialStorage, bool shouldContentSniff)
348 if ((!d->m_user.isEmpty() || !d->m_pass.isEmpty()) && !firstRequest().url().protocolIsInHTTPFamily()) {
349 // Credentials for ftp can only be passed in URL, the didReceiveAuthenticationChallenge delegate call won't be made.
350 KURL urlWithCredentials(firstRequest().url());
351 urlWithCredentials.setUser(d->m_user);
352 urlWithCredentials.setPass(d->m_pass);
353 firstRequest().setURL(urlWithCredentials);
356 // <rdar://problem/7174050> - For URLs that match the paths of those previously challenged for HTTP Basic authentication,
357 // try and reuse the credential preemptively, as allowed by RFC 2617.
358 if (shouldUseCredentialStorage && firstRequest().url().protocolIsInHTTPFamily()) {
359 if (d->m_user.isEmpty() && d->m_pass.isEmpty()) {
360 // <rdar://problem/7174050> - For URLs that match the paths of those previously challenged for HTTP Basic authentication,
361 // try and reuse the credential preemptively, as allowed by RFC 2617.
362 d->m_initialCredential = CredentialStorage::get(firstRequest().url());
364 // If there is already a protection space known for the URL, update stored credentials before sending a request.
365 // This makes it possible to implement logout by sending an XMLHttpRequest with known incorrect credentials, and aborting it immediately
366 // (so that an authentication dialog doesn't pop up).
367 CredentialStorage::set(Credential(d->m_user, d->m_pass, CredentialPersistenceNone), firstRequest().url());
371 if (!d->m_initialCredential.isEmpty()) {
372 // FIXME: Support Digest authentication, and Proxy-Authorization.
373 applyBasicAuthorizationHeader(firstRequest(), d->m_initialCredential);
376 RetainPtr<CFMutableURLRequestRef> request = adoptCF(CFURLRequestCreateMutableCopy(kCFAllocatorDefault, firstRequest().cfURLRequest(UpdateHTTPBody)));
377 wkSetRequestStorageSession(d->m_storageSession.get(), request.get());
379 if (!shouldContentSniff)
380 wkSetCFURLRequestShouldContentSniff(request.get(), false);
382 RetainPtr<CFMutableDictionaryRef> sslProps;
384 if (allowsAnyHTTPSCertificateHosts().contains(firstRequest().url().host().lower())) {
385 sslProps.adoptCF(CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
386 CFDictionaryAddValue(sslProps.get(), kCFStreamSSLAllowsAnyRoot, kCFBooleanTrue);
387 CFDictionaryAddValue(sslProps.get(), kCFStreamSSLAllowsExpiredRoots, kCFBooleanTrue);
388 CFDictionaryAddValue(sslProps.get(), kCFStreamSSLAllowsExpiredCertificates, kCFBooleanTrue);
389 CFDictionaryAddValue(sslProps.get(), kCFStreamSSLValidatesCertificateChain, kCFBooleanFalse);
392 HashMap<String, RetainPtr<CFDataRef> >::iterator clientCert = clientCerts().find(firstRequest().url().host().lower());
393 if (clientCert != clientCerts().end()) {
395 sslProps.adoptCF(CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
397 wkSetClientCertificateInSSLProperties(sslProps.get(), (clientCert->value).get());
402 CFURLRequestSetSSLProperties(request.get(), sslProps.get());
405 if (CFHTTPCookieStorageRef cookieStorage = overridenCookieStorage()) {
406 // Overridden cookie storage doesn't come from a session, so the request does not have it yet.
407 CFURLRequestSetHTTPCookieStorage(request.get(), cookieStorage);
411 CFURLConnectionClient_V6 client = { 6, this, 0, 0, 0, WebCore::willSendRequest, didReceiveResponse, didReceiveData, 0, didFinishLoading, didFail, willCacheResponse, didReceiveChallenge, didSendBodyData, shouldUseCredentialStorageCallback, 0,
412 #if USE(PROTECTION_SPACE_AUTH_CALLBACK)
413 canRespondToProtectionSpace,
418 #if USE(NETWORK_CFDATA_ARRAY_CALLBACK)
425 CFMutableDictionaryRef streamProperties = CFDictionaryCreateMutable(0, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
426 if (!shouldUseCredentialStorage)
427 CFDictionarySetValue(streamProperties, CFSTR("_kCFURLConnectionSessionID"), CFSTR("WebKitPrivateSession"));
429 #if PLATFORM(IOS) || (PLATFORM(MAC) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1090)
430 RetainPtr<CFDataRef> sourceApplicationAuditData = d->m_context->sourceApplicationAuditData();
431 if (sourceApplicationAuditData)
432 CFDictionarySetValue(streamProperties, CFSTR("kCFStreamPropertySourceApplication"), sourceApplicationAuditData.get());
435 static const CFStringRef kCFURLConnectionSocketStreamProperties = CFSTR("kCFURLConnectionSocketStreamProperties");
436 RetainPtr<CFDictionaryRef> propertiesDictionary = adoptCF(CFDictionaryCreate(0, (const void**)&kCFURLConnectionSocketStreamProperties, (const void**)&streamProperties, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
437 CFRelease(streamProperties);
439 d->m_connection.adoptCF(CFURLConnectionCreateWithProperties(0, request.get(), reinterpret_cast<CFURLConnectionClient*>(&client), propertiesDictionary.get()));
442 bool ResourceHandle::start()
447 // If NetworkingContext is invalid then we are no longer attached to a Page,
448 // this must be an attempted load from an unload handler, so let's just block it.
449 if (!d->m_context->isValid())
452 d->m_storageSession = d->m_context->storageSession().platformSession();
454 bool shouldUseCredentialStorage = !client() || client()->shouldUseCredentialStorage(this);
456 createCFURLConnection(shouldUseCredentialStorage, d->m_shouldContentSniff);
459 CFURLConnectionScheduleWithCurrentMessageQueue(d->m_connection.get());
461 CFURLConnectionScheduleWithRunLoop(d->m_connection.get(), CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
463 CFURLConnectionScheduleDownloadWithRunLoop(d->m_connection.get(), loaderRunLoop(), kCFRunLoopDefaultMode);
464 CFURLConnectionStart(d->m_connection.get());
466 LOG(Network, "CFNet - Starting URL %s (handle=%p, conn=%p)", firstRequest().url().string().utf8().data(), this, d->m_connection.get());
471 void ResourceHandle::cancel()
473 if (d->m_connection) {
474 CFURLConnectionCancel(d->m_connection.get());
479 void ResourceHandle::willSendRequest(ResourceRequest& request, const ResourceResponse& redirectResponse)
481 const KURL& url = request.url();
482 d->m_user = url.user();
483 d->m_pass = url.pass();
484 d->m_lastHTTPMethod = request.httpMethod();
485 request.removeCredentials();
487 if (!protocolHostAndPortAreEqual(request.url(), redirectResponse.url())) {
488 // If the network layer carries over authentication headers from the original request
489 // in a cross-origin redirect, we want to clear those headers here.
490 request.clearHTTPAuthorization();
492 // Only consider applying authentication credentials if this is actually a redirect and the redirect
493 // URL didn't include credentials of its own.
494 if (d->m_user.isEmpty() && d->m_pass.isEmpty() && !redirectResponse.isNull()) {
495 Credential credential = CredentialStorage::get(request.url());
496 if (!credential.isEmpty()) {
497 d->m_initialCredential = credential;
499 // FIXME: Support Digest authentication, and Proxy-Authorization.
500 applyBasicAuthorizationHeader(request, d->m_initialCredential);
505 RefPtr<ResourceHandle> protect(this);
506 client()->willSendRequest(this, request, redirectResponse);
508 // Client call may not preserve the session, especially if the request is sent over IPC.
509 if (!request.isNull())
510 request.setStorageSession(d->m_storageSession.get());
513 bool ResourceHandle::shouldUseCredentialStorage()
515 LOG(Network, "CFNet - shouldUseCredentialStorage()");
517 return client()->shouldUseCredentialStorage(this);
522 void ResourceHandle::didReceiveAuthenticationChallenge(const AuthenticationChallenge& challenge)
524 LOG(Network, "CFNet - didReceiveAuthenticationChallenge()");
525 ASSERT(d->m_currentWebChallenge.isNull());
526 // Since CFURLConnection networking relies on keeping a reference to the original CFURLAuthChallengeRef,
527 // we make sure that is actually present
528 ASSERT(challenge.cfURLAuthChallengeRef());
529 ASSERT(challenge.authenticationClient() == this); // Should be already set.
532 // Proxy authentication is handled by CFNetwork internally. We can get here if the user cancels
533 // CFNetwork authentication dialog, and we shouldn't ask the client to display another one in that case.
534 if (challenge.protectionSpace().isProxy()) {
535 // Cannot use receivedRequestToContinueWithoutCredential(), because current challenge is not yet set.
536 CFURLConnectionUseCredential(d->m_connection.get(), 0, challenge.cfURLAuthChallengeRef());
541 if (!d->m_user.isNull() && !d->m_pass.isNull()) {
542 RetainPtr<CFURLCredentialRef> credential = adoptCF(CFURLCredentialCreate(kCFAllocatorDefault, d->m_user.createCFString().get(), d->m_pass.createCFString().get(), 0, kCFURLCredentialPersistenceNone));
545 if (challenge.failureResponse().httpStatusCode() == 401)
546 urlToStore = challenge.failureResponse().url();
547 CredentialStorage::set(core(credential.get()), challenge.protectionSpace(), urlToStore);
549 CFURLConnectionUseCredential(d->m_connection.get(), credential.get(), challenge.cfURLAuthChallengeRef());
550 d->m_user = String();
551 d->m_pass = String();
552 // FIXME: Per the specification, the user shouldn't be asked for credentials if there were incorrect ones provided explicitly.
556 if (!client() || client()->shouldUseCredentialStorage(this)) {
557 if (!d->m_initialCredential.isEmpty() || challenge.previousFailureCount()) {
558 // The stored credential wasn't accepted, stop using it.
559 // There is a race condition here, since a different credential might have already been stored by another ResourceHandle,
560 // but the observable effect should be very minor, if any.
561 CredentialStorage::remove(challenge.protectionSpace());
564 if (!challenge.previousFailureCount()) {
565 Credential credential = CredentialStorage::get(challenge.protectionSpace());
566 if (!credential.isEmpty() && credential != d->m_initialCredential) {
567 ASSERT(credential.persistence() == CredentialPersistenceNone);
568 if (challenge.failureResponse().httpStatusCode() == 401) {
569 // Store the credential back, possibly adding it as a default for this directory.
570 CredentialStorage::set(credential, challenge.protectionSpace(), challenge.failureResponse().url());
572 RetainPtr<CFURLCredentialRef> cfCredential(AdoptCF, createCF(credential));
573 CFURLConnectionUseCredential(d->m_connection.get(), cfCredential.get(), challenge.cfURLAuthChallengeRef());
579 d->m_currentWebChallenge = challenge;
582 client()->didReceiveAuthenticationChallenge(this, d->m_currentWebChallenge);
585 #if USE(PROTECTION_SPACE_AUTH_CALLBACK)
586 bool ResourceHandle::canAuthenticateAgainstProtectionSpace(const ProtectionSpace& protectionSpace)
589 return client()->canAuthenticateAgainstProtectionSpace(this, protectionSpace);
595 void ResourceHandle::receivedCredential(const AuthenticationChallenge& challenge, const Credential& credential)
597 LOG(Network, "CFNet - receivedCredential()");
598 ASSERT(!challenge.isNull());
599 ASSERT(challenge.cfURLAuthChallengeRef());
600 if (challenge != d->m_currentWebChallenge)
603 // FIXME: Support empty credentials. Currently, an empty credential cannot be stored in WebCore credential storage, as that's empty value for its map.
604 if (credential.isEmpty()) {
605 receivedRequestToContinueWithoutCredential(challenge);
609 if (credential.persistence() == CredentialPersistenceForSession) {
610 // Manage per-session credentials internally, because once NSURLCredentialPersistencePerSession is used, there is no way
611 // to ignore it for a particular request (short of removing it altogether).
612 Credential webCredential(credential.user(), credential.password(), CredentialPersistenceNone);
613 RetainPtr<CFURLCredentialRef> cfCredential(AdoptCF, createCF(webCredential));
616 if (challenge.failureResponse().httpStatusCode() == 401)
617 urlToStore = challenge.failureResponse().url();
618 CredentialStorage::set(webCredential, challenge.protectionSpace(), urlToStore);
620 CFURLConnectionUseCredential(d->m_connection.get(), cfCredential.get(), challenge.cfURLAuthChallengeRef());
622 RetainPtr<CFURLCredentialRef> cfCredential(AdoptCF, createCF(credential));
623 CFURLConnectionUseCredential(d->m_connection.get(), cfCredential.get(), challenge.cfURLAuthChallengeRef());
626 clearAuthentication();
629 void ResourceHandle::receivedRequestToContinueWithoutCredential(const AuthenticationChallenge& challenge)
631 LOG(Network, "CFNet - receivedRequestToContinueWithoutCredential()");
632 ASSERT(!challenge.isNull());
633 ASSERT(challenge.cfURLAuthChallengeRef());
634 if (challenge != d->m_currentWebChallenge)
637 CFURLConnectionUseCredential(d->m_connection.get(), 0, challenge.cfURLAuthChallengeRef());
639 clearAuthentication();
642 void ResourceHandle::receivedCancellation(const AuthenticationChallenge& challenge)
644 LOG(Network, "CFNet - receivedCancellation()");
645 if (challenge != d->m_currentWebChallenge)
649 client()->receivedCancellation(this, challenge);
652 CFURLStorageSessionRef ResourceHandle::storageSession() const
654 return d->m_storageSession.get();
657 CFURLConnectionRef ResourceHandle::connection() const
659 return d->m_connection.get();
662 CFURLConnectionRef ResourceHandle::releaseConnectionForDownload()
664 LOG(Network, "CFNet - Job %p releasing connection %p for download", this, d->m_connection.get());
665 return d->m_connection.leakRef();
668 CFStringRef ResourceHandle::synchronousLoadRunLoopMode()
670 return CFSTR("WebCoreSynchronousLoaderRunLoopMode");
673 void ResourceHandle::platformLoadResourceSynchronously(NetworkingContext* context, const ResourceRequest& request, StoredCredentials storedCredentials, ResourceError& error, ResourceResponse& response, Vector<char>& data)
675 LOG(Network, "ResourceHandle::platformLoadResourceSynchronously:%s allowStoredCredentials:%u", request.url().string().utf8().data(), storedCredentials);
677 ASSERT(!request.isEmpty());
679 ASSERT(response.isNull());
680 ASSERT(error.isNull());
682 OwnPtr<SynchronousLoaderClient> client = SynchronousLoaderClient::create();
683 client->setAllowStoredCredentials(storedCredentials == AllowStoredCredentials);
685 RefPtr<ResourceHandle> handle = adoptRef(new ResourceHandle(context, request, client.get(), false /*defersLoading*/, true /*shouldContentSniff*/));
687 handle->d->m_storageSession = context->storageSession().platformSession();
689 if (handle->d->m_scheduledFailureType != NoFailure) {
690 error = context->blockedError(request);
694 handle->createCFURLConnection(storedCredentials == AllowStoredCredentials, ResourceHandle::shouldContentSniffURL(request.url()));
696 CFURLConnectionScheduleWithRunLoop(handle->connection(), CFRunLoopGetCurrent(), synchronousLoadRunLoopMode());
697 CFURLConnectionScheduleDownloadWithRunLoop(handle->connection(), CFRunLoopGetCurrent(), synchronousLoadRunLoopMode());
698 CFURLConnectionStart(handle->connection());
700 while (!client->isDone())
701 CFRunLoopRunInMode(synchronousLoadRunLoopMode(), UINT_MAX, true);
703 error = client->error();
705 CFURLConnectionCancel(handle->connection());
708 response = client->response();
710 response = ResourceResponse(request.url(), String(), 0, String(), String());
711 // FIXME: ResourceHandleMac also handles authentication errors by setting code to 401. CFNet version should probably do the same.
712 if (error.domain() == String(kCFErrorDomainCFNetwork))
713 response.setHTTPStatusCode(error.errorCode());
715 response.setHTTPStatusCode(404);
718 data.swap(client->mutableData());
721 void ResourceHandle::setHostAllowsAnyHTTPSCertificate(const String& host)
723 allowsAnyHTTPSCertificateHosts().add(host.lower());
726 void ResourceHandle::setClientCertificate(const String& host, CFDataRef cert)
728 clientCerts().set(host.lower(), cert);
731 void ResourceHandle::platformSetDefersLoading(bool defers)
733 if (!d->m_connection)
737 CFURLConnectionHalt(d->m_connection.get());
739 CFURLConnectionResume(d->m_connection.get());
742 bool ResourceHandle::loadsBlocked()
748 void ResourceHandle::schedule(SchedulePair* pair)
750 CFRunLoopRef runLoop = pair->runLoop();
754 CFURLConnectionScheduleWithRunLoop(d->m_connection.get(), runLoop, pair->mode());
755 if (d->m_startWhenScheduled) {
756 CFURLConnectionStart(d->m_connection.get());
757 d->m_startWhenScheduled = false;
761 void ResourceHandle::unschedule(SchedulePair* pair)
763 CFRunLoopRef runLoop = pair->runLoop();
767 CFURLConnectionUnscheduleFromRunLoop(d->m_connection.get(), runLoop, pair->mode());
771 #endif // USE(CFNETWORK)
773 #if USE(NETWORK_CFDATA_ARRAY_CALLBACK)
774 void ResourceHandle::handleDataArray(CFArrayRef dataArray)
777 if (client()->supportsDataArray()) {
778 client()->didReceiveDataArray(this, dataArray);
782 CFIndex count = CFArrayGetCount(dataArray);
785 CFDataRef data = static_cast<CFDataRef>(CFArrayGetValueAtIndex(dataArray, 0));
786 CFIndex length = CFDataGetLength(data);
787 client()->didReceiveData(this, reinterpret_cast<const char*>(CFDataGetBytePtr(data)), length, static_cast<int>(length));
791 CFIndex totalSize = 0;
793 for (index = 0; index < count; index++)
794 totalSize += CFDataGetLength(static_cast<CFDataRef>(CFArrayGetValueAtIndex(dataArray, index)));
796 RetainPtr<CFMutableDataRef> mergedData(AdoptCF, CFDataCreateMutable(kCFAllocatorDefault, totalSize));
797 for (index = 0; index < count; index++) {
798 CFDataRef data = static_cast<CFDataRef>(CFArrayGetValueAtIndex(dataArray, index));
799 CFDataAppendBytes(mergedData.get(), CFDataGetBytePtr(data), CFDataGetLength(data));
802 client()->didReceiveData(this, reinterpret_cast<const char*>(CFDataGetBytePtr(mergedData.get())), totalSize, static_cast<int>(totalSize));
806 } // namespace WebCore