[iOS] WebCore fails to build: platform/network/mac/ResourceHandleMac.mm:729:62: error...
[WebKit-https.git] / Source / WebCore / platform / network / mac / ResourceHandleMac.mm
1 /*
2  * Copyright (C) 2004, 2006, 2007, 2008, 2009, 2010, 2011, 2012 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 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 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 #import "config.h"
27 #import "ResourceHandleInternal.h"
28
29 #import "AuthenticationChallenge.h"
30 #import "AuthenticationMac.h"
31 #import "BlockExceptions.h"
32 #import "CookieStorage.h"
33 #import "CredentialStorage.h"
34 #import "CachedResourceLoader.h"
35 #import "FormDataStreamMac.h"
36 #import "Frame.h"
37 #import "FrameLoader.h"
38 #import "Logging.h"
39 #import "MIMETypeRegistry.h"
40 #import "NetworkingContext.h"
41 #import "Page.h"
42 #import "ResourceError.h"
43 #import "ResourceResponse.h"
44 #import "Settings.h"
45 #import "SharedBuffer.h"
46 #import "SubresourceLoader.h"
47 #import "WebCoreResourceHandleAsDelegate.h"
48 #import "WebCoreResourceHandleAsOperationQueueDelegate.h"
49 #import "SynchronousLoaderClient.h"
50 #import "WebCoreSystemInterface.h"
51 #import "WebCoreURLResponse.h"
52 #import <wtf/Ref.h>
53 #import <wtf/SchedulePair.h>
54 #import <wtf/text/Base64.h>
55 #import <wtf/text/CString.h>
56
57 #if USE(CFNETWORK)
58 #if __has_include(<CFNetwork/CFURLConnectionPriv.h>)
59 #import <CFNetwork/CFURLConnectionPriv.h>
60 #endif
61 typedef struct _CFURLConnection* CFURLConnectionRef;
62 extern "C" {
63 CFDictionaryRef _CFURLConnectionCopyTimingData(CFURLConnectionRef);
64 }
65 #endif // USE(CFNETWORK)
66
67 #if __has_include(<Foundation/NSURLConnectionPrivate.h>)
68 #import <Foundation/NSURLConnectionPrivate.h>
69 #else
70 @interface NSURLConnection (TimingData)
71 + (void)_setCollectsTimingData:(BOOL)collect;
72 - (NSDictionary *)_timingData;
73 @end
74 #endif
75
76 #if PLATFORM(IOS)
77 #import <CFNetwork/CFURLRequest.h>
78
79 #import "RuntimeApplicationChecksIOS.h"
80 #import "WebCoreThreadRun.h"
81
82 @interface NSURLRequest (iOSDetails)
83 - (CFURLRequestRef) _CFURLRequest;
84 @end
85 #endif
86
87 #if USE(QUICK_LOOK)
88 #import "QuickLook.h"
89 #endif
90
91 using namespace WebCore;
92
93 @interface NSURLConnection (Details)
94 -(id)_initWithRequest:(NSURLRequest *)request delegate:(id)delegate usesCache:(BOOL)usesCacheFlag maxContentLength:(long long)maxContentLength startImmediately:(BOOL)startImmediately connectionProperties:(NSDictionary *)connectionProperties;
95 @end
96
97 namespace WebCore {
98     
99 #if !USE(CFNETWORK)
100     
101 static void applyBasicAuthorizationHeader(ResourceRequest& request, const Credential& credential)
102 {
103     String authenticationHeader = "Basic " + base64Encode(String(credential.user() + ":" + credential.password()).utf8());
104     request.clearHTTPAuthorization(); // FIXME: Should addHTTPHeaderField be smart enough to not build comma-separated lists in headers like Authorization?
105     request.addHTTPHeaderField("Authorization", authenticationHeader);
106 }
107
108 static NSOperationQueue *operationQueueForAsyncClients()
109 {
110     static NSOperationQueue *queue;
111     if (!queue) {
112         queue = [[NSOperationQueue alloc] init];
113         // Default concurrent operation count depends on current system workload, but delegate methods are mostly idling in IPC, so we can run as many as needed.
114         [queue setMaxConcurrentOperationCount:NSOperationQueueDefaultMaxConcurrentOperationCount];
115     }
116     return queue;
117 }
118
119 ResourceHandleInternal::~ResourceHandleInternal()
120 {
121 }
122
123 ResourceHandle::~ResourceHandle()
124 {
125     releaseDelegate();
126     d->m_currentWebChallenge.setAuthenticationClient(0);
127
128     LOG(Network, "Handle %p destroyed", this);
129 }
130
131 #if PLATFORM(IOS)
132 static bool synchronousWillSendRequestEnabled()
133 {
134     static bool disabled = [[NSUserDefaults standardUserDefaults] boolForKey:@"WebKitDisableSynchronousWillSendRequestPreferenceKey"];
135     return !disabled;
136 }
137 #endif
138
139 #if !PLATFORM(IOS)
140 void ResourceHandle::createNSURLConnection(id delegate, bool shouldUseCredentialStorage, bool shouldContentSniff, SchedulingBehavior schedulingBehavior)
141 #else
142 void ResourceHandle::createNSURLConnection(id delegate, bool shouldUseCredentialStorage, bool shouldContentSniff, SchedulingBehavior schedulingBehavior, NSDictionary *connectionProperties)
143 #endif
144 {
145     // Credentials for ftp can only be passed in URL, the connection:didReceiveAuthenticationChallenge: delegate call won't be made.
146     if ((!d->m_user.isEmpty() || !d->m_pass.isEmpty()) && !firstRequest().url().protocolIsInHTTPFamily()) {
147         URL urlWithCredentials(firstRequest().url());
148         urlWithCredentials.setUser(d->m_user);
149         urlWithCredentials.setPass(d->m_pass);
150         firstRequest().setURL(urlWithCredentials);
151     }
152
153     if (shouldUseCredentialStorage && firstRequest().url().protocolIsInHTTPFamily()) {
154         if (d->m_user.isEmpty() && d->m_pass.isEmpty()) {
155             // <rdar://problem/7174050> - For URLs that match the paths of those previously challenged for HTTP Basic authentication, 
156             // try and reuse the credential preemptively, as allowed by RFC 2617.
157             d->m_initialCredential = CredentialStorage::get(firstRequest().url());
158         } else {
159             // If there is already a protection space known for the URL, update stored credentials before sending a request.
160             // This makes it possible to implement logout by sending an XMLHttpRequest with known incorrect credentials, and aborting it immediately
161             // (so that an authentication dialog doesn't pop up).
162             CredentialStorage::set(Credential(d->m_user, d->m_pass, CredentialPersistenceNone), firstRequest().url());
163         }
164     }
165         
166     if (!d->m_initialCredential.isEmpty()) {
167         // FIXME: Support Digest authentication, and Proxy-Authorization.
168         applyBasicAuthorizationHeader(firstRequest(), d->m_initialCredential);
169     }
170
171     NSURLRequest *nsRequest = firstRequest().nsURLRequest(UpdateHTTPBody);
172     if (!shouldContentSniff) {
173         NSMutableURLRequest *mutableRequest = [[nsRequest mutableCopy] autorelease];
174         wkSetNSURLRequestShouldContentSniff(mutableRequest, NO);
175         nsRequest = mutableRequest;
176     }
177
178     if (d->m_storageSession)
179         nsRequest = [wkCopyRequestWithStorageSession(d->m_storageSession.get(), nsRequest) autorelease];
180
181     ASSERT([NSURLConnection instancesRespondToSelector:@selector(_initWithRequest:delegate:usesCache:maxContentLength:startImmediately:connectionProperties:)]);
182
183 #if PLATFORM(IOS)
184     // FIXME: This code is different from iOS code in ResourceHandleCFNet.cpp in that here we respect stream properties that were present in client properties.
185     NSDictionary *streamPropertiesFromClient = [connectionProperties objectForKey:@"kCFURLConnectionSocketStreamProperties"];
186     NSMutableDictionary *streamProperties = streamPropertiesFromClient ? [[streamPropertiesFromClient mutableCopy] autorelease] : [NSMutableDictionary dictionary];
187 #else
188     NSMutableDictionary *streamProperties = [NSMutableDictionary dictionary];
189 #endif
190
191     if (!shouldUseCredentialStorage) {
192         // Avoid using existing connections, because they may be already authenticated.
193         [streamProperties setObject:@"WebKitPrivateSession" forKey:@"_kCFURLConnectionSessionID"];
194     }
195
196     if (schedulingBehavior == SchedulingBehavior::Synchronous) {
197         // Synchronous requests should not be subject to regular connection count limit to avoid deadlocks.
198         // If we are using all available connections for async requests, and make a sync request, then prior
199         // requests may get stuck waiting for delegate calls while we are in nested run loop, and the sync
200         // request won't start because there are no available connections.
201         // Connections are grouped by their socket stream properties, with each group having a separate count.
202         [streamProperties setObject:@TRUE forKey:@"_WebKitSynchronousRequest"];
203     }
204
205 #if PLATFORM(IOS) || __MAC_OS_X_VERSION_MIN_REQUIRED >= 1090
206     RetainPtr<CFDataRef> sourceApplicationAuditData = d->m_context->sourceApplicationAuditData();
207     if (sourceApplicationAuditData)
208         [streamProperties setObject:(NSData *)sourceApplicationAuditData.get() forKey:@"kCFStreamPropertySourceApplication"];
209 #endif
210
211 #if PLATFORM(IOS)
212     NSMutableDictionary *propertyDictionary = [NSMutableDictionary dictionaryWithDictionary:connectionProperties];
213     [propertyDictionary setObject:streamProperties forKey:@"kCFURLConnectionSocketStreamProperties"];
214     const bool usesCache = false;
215     if (synchronousWillSendRequestEnabled())
216         CFURLRequestSetShouldStartSynchronously([nsRequest _CFURLRequest], 1);
217 #else
218     NSDictionary *propertyDictionary = [NSDictionary dictionaryWithObject:streamProperties forKey:@"kCFURLConnectionSocketStreamProperties"];
219     const bool usesCache = true;
220 #endif
221     d->m_connection = adoptNS([[NSURLConnection alloc] _initWithRequest:nsRequest delegate:delegate usesCache:usesCache maxContentLength:0 startImmediately:NO connectionProperties:propertyDictionary]);
222 #if ENABLE(WEB_TIMING)
223     [NSURLConnection _setCollectsTimingData:YES];
224 #endif
225 }
226
227 bool ResourceHandle::start()
228 {
229     if (!d->m_context)
230         return false;
231
232     BEGIN_BLOCK_OBJC_EXCEPTIONS;
233
234     // If NetworkingContext is invalid then we are no longer attached to a Page,
235     // this must be an attempted load from an unload event handler, so let's just block it.
236     if (!d->m_context->isValid())
237         return false;
238
239     d->m_storageSession = d->m_context->storageSession().platformSession();
240
241     // FIXME: Do not use the sync version of shouldUseCredentialStorage when the client returns true from usesAsyncCallbacks.
242     bool shouldUseCredentialStorage = !client() || client()->shouldUseCredentialStorage(this);
243
244     d->m_needsSiteSpecificQuirks = d->m_context->needsSiteSpecificQuirks();
245
246 #if !PLATFORM(IOS)
247     createNSURLConnection(
248         ResourceHandle::makeDelegate(shouldUseCredentialStorage),
249         shouldUseCredentialStorage,
250         d->m_shouldContentSniff || d->m_context->localFileContentSniffingEnabled(),
251         SchedulingBehavior::Asynchronous);
252 #else
253     createNSURLConnection(
254         d->m_proxy.get(),
255         shouldUseCredentialStorage,
256         d->m_shouldContentSniff || d->m_context->localFileContentSniffingEnabled(),
257         SchedulingBehavior::Asynchronous,
258         (NSDictionary *)client()->connectionProperties(this));
259 #endif
260
261 #if PLATFORM(IOS)
262     NSURLConnection *urlConnection = connection();
263     [urlConnection scheduleInRunLoop:WebThreadNSRunLoop() forMode:NSDefaultRunLoopMode];
264     [urlConnection start];
265 #else
266     bool scheduled = false;
267     if (SchedulePairHashSet* scheduledPairs = d->m_context->scheduledRunLoopPairs()) {
268         SchedulePairHashSet::iterator end = scheduledPairs->end();
269         for (SchedulePairHashSet::iterator it = scheduledPairs->begin(); it != end; ++it) {
270             if (NSRunLoop *runLoop = (*it)->nsRunLoop()) {
271                 [connection() scheduleInRunLoop:runLoop forMode:(NSString *)(*it)->mode()];
272                 scheduled = true;
273             }
274         }
275     }
276
277     if (client() && client()->usesAsyncCallbacks()) {
278         ASSERT(!scheduled);
279         [connection() setDelegateQueue:operationQueueForAsyncClients()];
280         scheduled = true;
281     }
282
283     // Start the connection if we did schedule with at least one runloop.
284     // We can't start the connection until we have one runloop scheduled.
285     if (scheduled)
286         [connection() start];
287     else
288         d->m_startWhenScheduled = true;
289 #endif
290
291     LOG(Network, "Handle %p starting connection %p for %@", this, connection(), firstRequest().nsURLRequest(DoNotUpdateHTTPBody));
292     
293     if (d->m_connection) {
294         if (d->m_defersLoading)
295             wkSetNSURLConnectionDefersCallbacks(connection(), YES);
296
297         return true;
298     }
299
300     END_BLOCK_OBJC_EXCEPTIONS;
301
302     return false;
303 }
304
305 void ResourceHandle::cancel()
306 {
307     LOG(Network, "Handle %p cancel connection %p", this, d->m_connection.get());
308
309     // Leaks were seen on HTTP tests without this; can be removed once <rdar://problem/6886937> is fixed.
310     if (d->m_currentMacChallenge)
311         [[d->m_currentMacChallenge sender] cancelAuthenticationChallenge:d->m_currentMacChallenge];
312
313     [d->m_connection.get() cancel];
314 }
315
316 void ResourceHandle::platformSetDefersLoading(bool defers)
317 {
318     if (d->m_connection)
319         wkSetNSURLConnectionDefersCallbacks(d->m_connection.get(), defers);
320 }
321
322 #if PLATFORM(MAC)
323
324 void ResourceHandle::schedule(SchedulePair& pair)
325 {
326     NSRunLoop *runLoop = pair.nsRunLoop();
327     if (!runLoop)
328         return;
329     [d->m_connection.get() scheduleInRunLoop:runLoop forMode:(NSString *)pair.mode()];
330     if (d->m_startWhenScheduled) {
331         [d->m_connection.get() start];
332         d->m_startWhenScheduled = false;
333     }
334 }
335
336 void ResourceHandle::unschedule(SchedulePair& pair)
337 {
338     if (NSRunLoop *runLoop = pair.nsRunLoop())
339         [d->m_connection.get() unscheduleFromRunLoop:runLoop forMode:(NSString *)pair.mode()];
340 }
341
342 #endif
343
344 id ResourceHandle::makeDelegate(bool shouldUseCredentialStorage)
345 {
346     ASSERT(!d->m_delegate);
347
348     id <NSURLConnectionDelegate> delegate;
349     if (client() && client()->usesAsyncCallbacks()) {
350         if (shouldUseCredentialStorage)
351             delegate = [[WebCoreResourceHandleAsOperationQueueDelegate alloc] initWithHandle:this];
352         else
353             delegate = [[WebCoreResourceHandleWithCredentialStorageAsOperationQueueDelegate alloc] initWithHandle:this];
354     } else
355         delegate = [[WebCoreResourceHandleAsDelegate alloc] initWithHandle:this];
356
357     d->m_delegate = delegate;
358     [delegate release];
359
360     return d->m_delegate.get();
361 }
362
363 id ResourceHandle::delegate()
364 {
365     if (!d->m_delegate)
366         return makeDelegate(false);
367     return d->m_delegate.get();
368 }
369
370 void ResourceHandle::releaseDelegate()
371 {
372     if (!d->m_delegate)
373         return;
374     [d->m_delegate.get() detachHandle];
375     d->m_delegate = nil;
376 }
377
378 NSURLConnection *ResourceHandle::connection() const
379 {
380     return d->m_connection.get();
381 }
382     
383 bool ResourceHandle::loadsBlocked()
384 {
385     return false;
386 }
387
388 CFStringRef ResourceHandle::synchronousLoadRunLoopMode()
389 {
390     return CFSTR("WebCoreSynchronousLoaderRunLoopMode");
391 }
392
393 void ResourceHandle::platformLoadResourceSynchronously(NetworkingContext* context, const ResourceRequest& request, StoredCredentials storedCredentials, ResourceError& error, ResourceResponse& response, Vector<char>& data)
394 {
395     LOG(Network, "ResourceHandle::platformLoadResourceSynchronously:%@ allowStoredCredentials:%u", request.nsURLRequest(DoNotUpdateHTTPBody), storedCredentials);
396
397     ASSERT(!request.isEmpty());
398     
399     OwnPtr<SynchronousLoaderClient> client = SynchronousLoaderClient::create();
400     client->setAllowStoredCredentials(storedCredentials == AllowStoredCredentials);
401
402     RefPtr<ResourceHandle> handle = adoptRef(new ResourceHandle(context, request, client.get(), false /*defersLoading*/, true /*shouldContentSniff*/));
403
404     handle->d->m_storageSession = context->storageSession().platformSession();
405
406     if (context && handle->d->m_scheduledFailureType != NoFailure) {
407         error = context->blockedError(request);
408         return;
409     }
410
411 #if !PLATFORM(IOS)
412     bool shouldUseCredentialStorage = storedCredentials == AllowStoredCredentials;
413     handle->createNSURLConnection(
414         handle->makeDelegate(shouldUseCredentialStorage),
415         shouldUseCredentialStorage,
416         handle->shouldContentSniff() || context->localFileContentSniffingEnabled(),
417         SchedulingBehavior::Synchronous);
418 #else
419     handle->createNSURLConnection(
420         handle->delegate(), // A synchronous request cannot turn into a download, so there is no need to proxy the delegate.
421         storedCredentials == AllowStoredCredentials,
422         handle->shouldContentSniff() || (context && context->localFileContentSniffingEnabled()),
423         SchedulingBehavior::Synchronous,
424         (NSDictionary *)handle->client()->connectionProperties(handle.get()));
425 #endif
426
427     [handle->connection() scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:(NSString *)synchronousLoadRunLoopMode()];
428     [handle->connection() start];
429     
430     while (!client->isDone())
431         [[NSRunLoop currentRunLoop] runMode:(NSString *)synchronousLoadRunLoopMode() beforeDate:[NSDate distantFuture]];
432
433     error = client->error();
434     
435     [handle->connection() cancel];
436
437     if (error.isNull())
438         response = client->response();
439
440     data.swap(client->mutableData());
441 }
442
443 void ResourceHandle::willSendRequest(ResourceRequest& request, const ResourceResponse& redirectResponse)
444 {
445     ASSERT(!redirectResponse.isNull());
446
447     if (redirectResponse.httpStatusCode() == 307) {
448         String lastHTTPMethod = d->m_lastHTTPMethod;
449         if (!equalIgnoringCase(lastHTTPMethod, request.httpMethod())) {
450             request.setHTTPMethod(lastHTTPMethod);
451     
452             FormData* body = d->m_firstRequest.httpBody();
453             if (!equalIgnoringCase(lastHTTPMethod, "GET") && body && !body->isEmpty())
454                 request.setHTTPBody(body);
455
456             String originalContentType = d->m_firstRequest.httpContentType();
457             if (!originalContentType.isEmpty())
458                 request.setHTTPHeaderField("Content-Type", originalContentType);
459         }
460     }
461
462     // Should not set Referer after a redirect from a secure resource to non-secure one.
463     if (!request.url().protocolIs("https") && protocolIs(request.httpReferrer(), "https") && d->m_context->shouldClearReferrerOnHTTPSToHTTPRedirect())
464         request.clearHTTPReferrer();
465
466     const URL& url = request.url();
467     d->m_user = url.user();
468     d->m_pass = url.pass();
469     d->m_lastHTTPMethod = request.httpMethod();
470     request.removeCredentials();
471
472     if (!protocolHostAndPortAreEqual(request.url(), redirectResponse.url())) {
473         // If the network layer carries over authentication headers from the original request
474         // in a cross-origin redirect, we want to clear those headers here.
475         // As of Lion, CFNetwork no longer does this.
476         request.clearHTTPAuthorization();
477     } else {
478         // Only consider applying authentication credentials if this is actually a redirect and the redirect
479         // URL didn't include credentials of its own.
480         if (d->m_user.isEmpty() && d->m_pass.isEmpty() && !redirectResponse.isNull()) {
481             Credential credential = CredentialStorage::get(request.url());
482             if (!credential.isEmpty()) {
483                 d->m_initialCredential = credential;
484                 
485                 // FIXME: Support Digest authentication, and Proxy-Authorization.
486                 applyBasicAuthorizationHeader(request, d->m_initialCredential);
487             }
488         }
489     }
490
491     if (client()->usesAsyncCallbacks()) {
492         client()->willSendRequestAsync(this, request, redirectResponse);
493     } else {
494         Ref<ResourceHandle> protect(*this);
495         client()->willSendRequest(this, request, redirectResponse);
496
497         // Client call may not preserve the session, especially if the request is sent over IPC.
498         if (!request.isNull())
499             request.setStorageSession(d->m_storageSession.get());
500     }
501 }
502
503 void ResourceHandle::continueWillSendRequest(const ResourceRequest& request)
504 {
505     ASSERT(client());
506     ASSERT(client()->usesAsyncCallbacks());
507
508     // Client call may not preserve the session, especially if the request is sent over IPC.
509     ResourceRequest newRequest = request;
510     if (!newRequest.isNull())
511         newRequest.setStorageSession(d->m_storageSession.get());
512     [(id)delegate() continueWillSendRequest:newRequest.nsURLRequest(UpdateHTTPBody)];
513 }
514
515 void ResourceHandle::continueDidReceiveResponse()
516 {
517     ASSERT(client());
518     ASSERT(client()->usesAsyncCallbacks());
519
520     [delegate() continueDidReceiveResponse];
521 }
522
523 bool ResourceHandle::shouldUseCredentialStorage()
524 {
525     ASSERT(!client()->usesAsyncCallbacks());
526     return client() && client()->shouldUseCredentialStorage(this);
527 }
528
529 void ResourceHandle::didReceiveAuthenticationChallenge(const AuthenticationChallenge& challenge)
530 {
531     ASSERT(!d->m_currentMacChallenge);
532     ASSERT(d->m_currentWebChallenge.isNull());
533     // Since NSURLConnection networking relies on keeping a reference to the original NSURLAuthenticationChallenge,
534     // we make sure that is actually present
535     ASSERT(challenge.nsURLAuthenticationChallenge());
536
537     // Proxy authentication is handled by CFNetwork internally. We can get here if the user cancels
538     // CFNetwork authentication dialog, and we shouldn't ask the client to display another one in that case.
539     if (challenge.protectionSpace().isProxy()) {
540         // Cannot use receivedRequestToContinueWithoutCredential(), because current challenge is not yet set.
541         [challenge.sender() continueWithoutCredentialForAuthenticationChallenge:challenge.nsURLAuthenticationChallenge()];
542         return;
543     }
544
545     if (!d->m_user.isNull() && !d->m_pass.isNull()) {
546         NSURLCredential *credential = [[NSURLCredential alloc] initWithUser:d->m_user
547                                                                    password:d->m_pass
548                                                                 persistence:NSURLCredentialPersistenceForSession];
549         d->m_currentMacChallenge = challenge.nsURLAuthenticationChallenge();
550         d->m_currentWebChallenge = challenge;
551         receivedCredential(challenge, core(credential));
552         [credential release];
553         // FIXME: Per the specification, the user shouldn't be asked for credentials if there were incorrect ones provided explicitly.
554         d->m_user = String();
555         d->m_pass = String();
556         return;
557     }
558
559     // FIXME: Do not use the sync version of shouldUseCredentialStorage when the client returns true from usesAsyncCallbacks.
560     if (!client() || client()->shouldUseCredentialStorage(this)) {
561         if (!d->m_initialCredential.isEmpty() || challenge.previousFailureCount()) {
562             // The stored credential wasn't accepted, stop using it.
563             // There is a race condition here, since a different credential might have already been stored by another ResourceHandle,
564             // but the observable effect should be very minor, if any.
565             CredentialStorage::remove(challenge.protectionSpace());
566         }
567
568         if (!challenge.previousFailureCount()) {
569             Credential credential = CredentialStorage::get(challenge.protectionSpace());
570             if (!credential.isEmpty() && credential != d->m_initialCredential) {
571                 ASSERT(credential.persistence() == CredentialPersistenceNone);
572                 if (challenge.failureResponse().httpStatusCode() == 401) {
573                     // Store the credential back, possibly adding it as a default for this directory.
574                     CredentialStorage::set(credential, challenge.protectionSpace(), challenge.failureResponse().url());
575                 }
576                 [challenge.sender() useCredential:mac(credential) forAuthenticationChallenge:mac(challenge)];
577                 return;
578             }
579         }
580     }
581
582 #if PLATFORM(IOS)
583     // If the challenge is for a proxy protection space, look for default credentials in
584     // the keychain.  CFNetwork used to handle this until WebCore was changed to always
585     // return NO to -connectionShouldUseCredentialStorage: for <rdar://problem/7704943>.
586     if (!challenge.previousFailureCount() && challenge.protectionSpace().isProxy()) {
587         NSURLAuthenticationChallenge *macChallenge = mac(challenge);
588         if (NSURLCredential *credential = [[NSURLCredentialStorage sharedCredentialStorage] defaultCredentialForProtectionSpace:[macChallenge protectionSpace]]) {
589             [challenge.sender() useCredential:credential forAuthenticationChallenge:macChallenge];
590             return;
591         }
592     }
593 #endif // PLATFORM(IOS)
594
595     d->m_currentMacChallenge = challenge.nsURLAuthenticationChallenge();
596     d->m_currentWebChallenge = core(d->m_currentMacChallenge);
597     d->m_currentWebChallenge.setAuthenticationClient(this);
598
599     // FIXME: Several concurrent requests can return with the an authentication challenge for the same protection space.
600     // We should avoid making additional client calls for the same protection space when already waiting for the user,
601     // because typing the same credentials several times is annoying.
602     if (client())
603         client()->didReceiveAuthenticationChallenge(this, d->m_currentWebChallenge);
604 }
605
606 void ResourceHandle::didCancelAuthenticationChallenge(const AuthenticationChallenge& challenge)
607 {
608     ASSERT(d->m_currentMacChallenge);
609     ASSERT(d->m_currentMacChallenge == challenge.nsURLAuthenticationChallenge());
610     ASSERT(!d->m_currentWebChallenge.isNull());
611
612     if (client())
613         client()->didCancelAuthenticationChallenge(this, challenge);
614 }
615
616 #if USE(PROTECTION_SPACE_AUTH_CALLBACK)
617 bool ResourceHandle::canAuthenticateAgainstProtectionSpace(const ProtectionSpace& protectionSpace)
618 {
619     if (client()->usesAsyncCallbacks()) {
620         if (client())
621             client()->canAuthenticateAgainstProtectionSpaceAsync(this, protectionSpace);
622         else
623             continueCanAuthenticateAgainstProtectionSpace(false);
624         return false; // Ignored by caller.
625     } else
626         return client() && client()->canAuthenticateAgainstProtectionSpace(this, protectionSpace);
627 }
628
629 void ResourceHandle::continueCanAuthenticateAgainstProtectionSpace(bool result)
630 {
631     ASSERT(client());
632     ASSERT(client()->usesAsyncCallbacks());
633
634     [(id)delegate() continueCanAuthenticateAgainstProtectionSpace:result];
635 }
636 #endif
637
638 void ResourceHandle::receivedCredential(const AuthenticationChallenge& challenge, const Credential& credential)
639 {
640     LOG(Network, "Handle %p receivedCredential", this);
641
642     ASSERT(!challenge.isNull());
643     if (challenge != d->m_currentWebChallenge)
644         return;
645
646     // FIXME: Support empty credentials. Currently, an empty credential cannot be stored in WebCore credential storage, as that's empty value for its map.
647     if (credential.isEmpty()) {
648         receivedRequestToContinueWithoutCredential(challenge);
649         return;
650     }
651
652     if (credential.persistence() == CredentialPersistenceForSession && (!d->m_needsSiteSpecificQuirks || ![[[mac(challenge) protectionSpace] host] isEqualToString:@"gallery.me.com"])) {
653         // Manage per-session credentials internally, because once NSURLCredentialPersistenceForSession is used, there is no way
654         // to ignore it for a particular request (short of removing it altogether).
655         // <rdar://problem/6867598> gallery.me.com is temporarily whitelisted, so that QuickTime plug-in could see the credentials.
656         Credential webCredential(credential, CredentialPersistenceNone);
657         URL urlToStore;
658         if (challenge.failureResponse().httpStatusCode() == 401)
659             urlToStore = challenge.failureResponse().url();
660         CredentialStorage::set(webCredential, core([d->m_currentMacChallenge protectionSpace]), urlToStore);
661         [[d->m_currentMacChallenge sender] useCredential:mac(webCredential) forAuthenticationChallenge:d->m_currentMacChallenge];
662     } else
663         [[d->m_currentMacChallenge sender] useCredential:mac(credential) forAuthenticationChallenge:d->m_currentMacChallenge];
664
665     clearAuthentication();
666 }
667
668 void ResourceHandle::receivedRequestToContinueWithoutCredential(const AuthenticationChallenge& challenge)
669 {
670     LOG(Network, "Handle %p receivedRequestToContinueWithoutCredential", this);
671
672     ASSERT(!challenge.isNull());
673     if (challenge != d->m_currentWebChallenge)
674         return;
675
676     [[d->m_currentMacChallenge sender] continueWithoutCredentialForAuthenticationChallenge:d->m_currentMacChallenge];
677
678     clearAuthentication();
679 }
680
681 void ResourceHandle::receivedCancellation(const AuthenticationChallenge& challenge)
682 {
683     LOG(Network, "Handle %p receivedCancellation", this);
684
685     if (challenge != d->m_currentWebChallenge)
686         return;
687
688     if (client())
689         client()->receivedCancellation(this, challenge);
690 }
691
692 void ResourceHandle::continueWillCacheResponse(NSCachedURLResponse *response)
693 {
694     ASSERT(client());
695     ASSERT(client()->usesAsyncCallbacks());
696
697     [(id)delegate() continueWillCacheResponse:response];
698 }
699     
700 #endif // !USE(CFNETWORK)
701     
702 #if ENABLE(WEB_TIMING)
703     
704 void ResourceHandle::getConnectionTimingData(NSDictionary *timingData, ResourceLoadTiming& timing)
705 {
706     if (!timingData)
707         return;
708
709     // This is not the navigationStart time in monotonic time, but the other times are relative to this time
710     // and only the differences between times are stored.
711     double referenceStart = [[timingData valueForKey:@"_kCFNTimingDataTimingDataInit"] doubleValue];
712             
713     double domainLookupStart = [[timingData valueForKey:@"_kCFNTimingDataDomainLookupStart"] doubleValue];
714     double domainLookupEnd = [[timingData valueForKey:@"_kCFNTimingDataDomainLookupEnd"] doubleValue];
715     double connectStart = [[timingData valueForKey:@"_kCFNTimingDataConnectStart"] doubleValue];
716     double secureConnectionStart = [[timingData valueForKey:@"_kCFNTimingDataSecureConnectionStart"] doubleValue];
717     double connectEnd = [[timingData valueForKey:@"_kCFNTimingDataConnectEnd"] doubleValue];
718     double requestStart = [[timingData valueForKey:@"_kCFNTimingDataRequestStart"] doubleValue];
719     double responseStart = [[timingData valueForKey:@"_kCFNTimingDataResponseStart"] doubleValue];
720         
721     timing.domainLookupStart = domainLookupStart <= 0 ? -1 : (domainLookupStart - referenceStart) * 1000;
722     timing.domainLookupEnd = domainLookupEnd <= 0 ? -1 : (domainLookupEnd - referenceStart) * 1000;
723     timing.connectStart = connectStart <= 0 ? -1 : (connectStart - referenceStart) * 1000;
724     timing.secureConnectionStart = secureConnectionStart <= 0 ? -1 : (secureConnectionStart - referenceStart) * 1000;
725     timing.connectEnd = connectEnd <= 0 ? -1 : (connectEnd - referenceStart) * 1000;
726     timing.requestStart = requestStart <= 0 ? 0 : (requestStart - referenceStart) * 1000;
727     timing.responseStart = responseStart <= 0 ? 0 : (responseStart - referenceStart) * 1000;
728 }
729     
730 #if USE(CFNETWORK)
731     
732 void ResourceHandle::setCollectsTimingData()
733 {
734     [NSURLConnection _setCollectsTimingData:YES];
735 }
736     
737 void ResourceHandle::getConnectionTimingData(CFURLConnectionRef connection, ResourceLoadTiming& timing)
738 {
739     getConnectionTimingData((__bridge NSDictionary*)(adoptCF(_CFURLConnectionCopyTimingData(connection)).get()), timing);
740 }
741     
742 #else
743     
744 void ResourceHandle::getConnectionTimingData(NSURLConnection *connection, ResourceLoadTiming& timing)
745 {
746     getConnectionTimingData([connection _timingData], timing);
747 }
748     
749 #endif
750     
751 #endif // ENABLE(WEB_TIMING)
752
753 } // namespace WebCore
754