Update std::expected to match libc++ coding style
[WebKit-https.git] / Source / WebCore / loader / DocumentThreadableLoader.cpp
1 /*
2  * Copyright (C) 2011, 2012 Google 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 are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include "config.h"
32 #include "DocumentThreadableLoader.h"
33
34 #include "CachedRawResource.h"
35 #include "CachedResourceLoader.h"
36 #include "CachedResourceRequest.h"
37 #include "CachedResourceRequestInitiators.h"
38 #include "CrossOriginAccessControl.h"
39 #include "CrossOriginPreflightChecker.h"
40 #include "CrossOriginPreflightResultCache.h"
41 #include "DOMWindow.h"
42 #include "Document.h"
43 #include "Frame.h"
44 #include "FrameLoader.h"
45 #include "InspectorInstrumentation.h"
46 #include "LoadTiming.h"
47 #include "Performance.h"
48 #include "ProgressTracker.h"
49 #include "ResourceError.h"
50 #include "ResourceRequest.h"
51 #include "ResourceTiming.h"
52 #include "RuntimeEnabledFeatures.h"
53 #include "SchemeRegistry.h"
54 #include "SecurityOrigin.h"
55 #include "SharedBuffer.h"
56 #include "SubresourceIntegrity.h"
57 #include "SubresourceLoader.h"
58 #include "ThreadableLoaderClient.h"
59 #include <wtf/Assertions.h>
60 #include <wtf/Ref.h>
61
62 namespace WebCore {
63
64 void DocumentThreadableLoader::loadResourceSynchronously(Document& document, ResourceRequest&& request, ThreadableLoaderClient& client, const ThreadableLoaderOptions& options, RefPtr<SecurityOrigin>&& origin, std::unique_ptr<ContentSecurityPolicy>&& contentSecurityPolicy)
65 {
66     // The loader will be deleted as soon as this function exits.
67     Ref<DocumentThreadableLoader> loader = adoptRef(*new DocumentThreadableLoader(document, client, LoadSynchronously, WTFMove(request), options, WTFMove(origin), WTFMove(contentSecurityPolicy), String(), ShouldLogError::Yes));
68     ASSERT(loader->hasOneRef());
69 }
70
71 void DocumentThreadableLoader::loadResourceSynchronously(Document& document, ResourceRequest&& request, ThreadableLoaderClient& client, const ThreadableLoaderOptions& options)
72 {
73     loadResourceSynchronously(document, WTFMove(request), client, options, nullptr, nullptr);
74 }
75
76 RefPtr<DocumentThreadableLoader> DocumentThreadableLoader::create(Document& document, ThreadableLoaderClient& client,
77 ResourceRequest&& request, const ThreadableLoaderOptions& options, RefPtr<SecurityOrigin>&& origin,
78 std::unique_ptr<ContentSecurityPolicy>&& contentSecurityPolicy, String&& referrer, ShouldLogError shouldLogError)
79 {
80     RefPtr<DocumentThreadableLoader> loader = adoptRef(new DocumentThreadableLoader(document, client, LoadAsynchronously, WTFMove(request), options, WTFMove(origin), WTFMove(contentSecurityPolicy), WTFMove(referrer), shouldLogError));
81     if (!loader->isLoading())
82         loader = nullptr;
83     return loader;
84 }
85
86 RefPtr<DocumentThreadableLoader> DocumentThreadableLoader::create(Document& document, ThreadableLoaderClient& client, ResourceRequest&& request, const ThreadableLoaderOptions& options, String&& referrer)
87 {
88     return create(document, client, WTFMove(request), options, nullptr, nullptr, WTFMove(referrer), ShouldLogError::Yes);
89 }
90
91 DocumentThreadableLoader::DocumentThreadableLoader(Document& document, ThreadableLoaderClient& client, BlockingBehavior blockingBehavior, ResourceRequest&& request, const ThreadableLoaderOptions& options, RefPtr<SecurityOrigin>&& origin, std::unique_ptr<ContentSecurityPolicy>&& contentSecurityPolicy, String&& referrer, ShouldLogError shouldLogError)
92     : m_client(&client)
93     , m_document(document)
94     , m_options(options)
95     , m_origin(WTFMove(origin))
96     , m_referrer(WTFMove(referrer))
97     , m_sameOriginRequest(securityOrigin().canRequest(request.url()))
98     , m_simpleRequest(true)
99     , m_async(blockingBehavior == LoadAsynchronously)
100     , m_delayCallbacksForIntegrityCheck(!m_options.integrity.isEmpty())
101     , m_contentSecurityPolicy(WTFMove(contentSecurityPolicy))
102     , m_shouldLogError(shouldLogError)
103 {
104     relaxAdoptionRequirement();
105
106     // Setting a referrer header is only supported in the async code path.
107     ASSERT(m_async || m_referrer.isEmpty());
108
109     // Referrer and Origin headers should be set after the preflight if any.
110     ASSERT(!request.hasHTTPReferrer() && !request.hasHTTPOrigin());
111
112     ASSERT_WITH_SECURITY_IMPLICATION(isAllowedByContentSecurityPolicy(request.url(), ContentSecurityPolicy::RedirectResponseReceived::No));
113
114     m_options.storedCredentialsPolicy = (m_options.credentials == FetchOptions::Credentials::Include || (m_options.credentials == FetchOptions::Credentials::SameOrigin && m_sameOriginRequest)) ? StoredCredentialsPolicy::Use : StoredCredentialsPolicy::DoNotUse;
115
116     ASSERT(!request.httpHeaderFields().contains(HTTPHeaderName::Origin));
117
118     // Copy headers if we need to replay the request after a redirection.
119     if (m_async && m_options.mode == FetchOptions::Mode::Cors)
120         m_originalHeaders = request.httpHeaderFields();
121
122     if (document.page() && document.page()->isRunningUserScripts() && SchemeRegistry::isUserExtensionScheme(request.url().protocol().toStringWithoutCopying())) {
123         m_options.mode = FetchOptions::Mode::NoCors;
124         m_options.filteringPolicy = ResponseFilteringPolicy::Disable;
125     }
126
127     // As per step 11 of https://fetch.spec.whatwg.org/#main-fetch, data scheme (if same-origin data-URL flag is set) and about scheme are considered same-origin.
128     if (request.url().protocolIsData())
129         m_sameOriginRequest = options.sameOriginDataURLFlag == SameOriginDataURLFlag::Set;
130
131     if (m_sameOriginRequest || m_options.mode == FetchOptions::Mode::NoCors) {
132         loadRequest(WTFMove(request), DoSecurityCheck);
133         return;
134     }
135
136     if (m_options.mode == FetchOptions::Mode::SameOrigin) {
137         logErrorAndFail(ResourceError(errorDomainWebKitInternal, 0, request.url(), "Cross origin requests are not allowed when using same-origin fetch mode."));
138         return;
139     }
140
141     makeCrossOriginAccessRequest(WTFMove(request));
142 }
143
144 void DocumentThreadableLoader::makeCrossOriginAccessRequest(ResourceRequest&& request)
145 {
146     ASSERT(m_options.mode == FetchOptions::Mode::Cors);
147
148     if ((m_options.preflightPolicy == ConsiderPreflight && isSimpleCrossOriginAccessRequest(request.httpMethod(), request.httpHeaderFields())) || m_options.preflightPolicy == PreventPreflight)
149         makeSimpleCrossOriginAccessRequest(WTFMove(request));
150     else {
151 #if ENABLE(SERVICE_WORKER)
152         if (m_options.serviceWorkersMode == ServiceWorkersMode::All && m_async) {
153             if (m_options.serviceWorkerIdentifier || document().activeServiceWorker()) {
154                 ASSERT(!m_bypassingPreflightForServiceWorkerRequest);
155                 m_bypassingPreflightForServiceWorkerRequest = WTFMove(request);
156                 m_options.serviceWorkersMode = ServiceWorkersMode::Only;
157                 loadRequest(ResourceRequest { m_bypassingPreflightForServiceWorkerRequest.value() }, SkipSecurityCheck);
158                 return;
159             }
160         }
161 #endif
162         m_simpleRequest = false;
163         if (CrossOriginPreflightResultCache::singleton().canSkipPreflight(securityOrigin().toString(), request.url(), m_options.storedCredentialsPolicy, request.httpMethod(), request.httpHeaderFields()))
164             preflightSuccess(WTFMove(request));
165         else
166             makeCrossOriginAccessRequestWithPreflight(WTFMove(request));
167     }
168 }
169
170 void DocumentThreadableLoader::makeSimpleCrossOriginAccessRequest(ResourceRequest&& request)
171 {
172     ASSERT(m_options.preflightPolicy != ForcePreflight);
173     ASSERT(m_options.preflightPolicy == PreventPreflight || isSimpleCrossOriginAccessRequest(request.httpMethod(), request.httpHeaderFields()));
174
175     // Cross-origin requests are only allowed for HTTP and registered schemes. We would catch this when checking response headers later, but there is no reason to send a request that's guaranteed to be denied.
176     if (!SchemeRegistry::shouldTreatURLSchemeAsCORSEnabled(request.url().protocol().toStringWithoutCopying())) {
177         logErrorAndFail(ResourceError(errorDomainWebKitInternal, 0, request.url(), "Cross origin requests are only supported for HTTP.", ResourceError::Type::AccessControl));
178         return;
179     }
180
181     updateRequestForAccessControl(request, securityOrigin(), m_options.storedCredentialsPolicy);
182     loadRequest(WTFMove(request), DoSecurityCheck);
183 }
184
185 void DocumentThreadableLoader::makeCrossOriginAccessRequestWithPreflight(ResourceRequest&& request)
186 {
187     if (m_async) {
188         m_preflightChecker.emplace(*this, WTFMove(request));
189         m_preflightChecker->startPreflight();
190         return;
191     }
192     CrossOriginPreflightChecker::doPreflight(*this, WTFMove(request));
193 }
194
195 DocumentThreadableLoader::~DocumentThreadableLoader()
196 {
197     if (m_resource)
198         m_resource->removeClient(*this);
199 }
200
201 void DocumentThreadableLoader::cancel()
202 {
203     Ref<DocumentThreadableLoader> protectedThis(*this);
204
205     // Cancel can re-enter and m_resource might be null here as a result.
206     if (m_client && m_resource) {
207         // FIXME: This error is sent to the client in didFail(), so it should not be an internal one. Use FrameLoaderClient::cancelledError() instead.
208         ResourceError error(errorDomainWebKitInternal, 0, m_resource->url(), "Load cancelled", ResourceError::Type::Cancellation);
209         m_client->didFail(error);
210     }
211     clearResource();
212     m_client = nullptr;
213 }
214
215 void DocumentThreadableLoader::setDefersLoading(bool value)
216 {
217     if (m_resource)
218         m_resource->setDefersLoading(value);
219     if (m_preflightChecker)
220         m_preflightChecker->setDefersLoading(value);
221 }
222
223 void DocumentThreadableLoader::clearResource()
224 {
225     // Script can cancel and restart a request reentrantly within removeClient(),
226     // which could lead to calling CachedResource::removeClient() multiple times for
227     // this DocumentThreadableLoader. Save off a copy of m_resource and clear it to
228     // prevent the reentrancy.
229     if (CachedResourceHandle<CachedRawResource> resource = m_resource) {
230         m_resource = nullptr;
231         resource->removeClient(*this);
232     }
233     if (m_preflightChecker)
234         m_preflightChecker = std::nullopt;
235 }
236
237 void DocumentThreadableLoader::redirectReceived(CachedResource& resource, ResourceRequest&& request, const ResourceResponse& redirectResponse, CompletionHandler<void(ResourceRequest&&)>&& completionHandler)
238 {
239     ASSERT(m_client);
240     ASSERT_UNUSED(resource, &resource == m_resource);
241
242     Ref<DocumentThreadableLoader> protectedThis(*this);
243     --m_options.maxRedirectCount;
244
245     // FIXME: We restrict this check to Fetch API for the moment, as this might disrupt WorkerScriptLoader.
246     // Reassess this check based on https://github.com/whatwg/fetch/issues/393 discussions.
247     // We should also disable that check in navigation mode.
248     if (!request.url().protocolIsInHTTPFamily() && m_options.initiator == cachedResourceRequestInitiators().fetch) {
249         reportRedirectionWithBadScheme(request.url());
250         clearResource();
251         return completionHandler(WTFMove(request));
252     }
253
254     if (!isAllowedByContentSecurityPolicy(request.url(), redirectResponse.isNull() ? ContentSecurityPolicy::RedirectResponseReceived::No : ContentSecurityPolicy::RedirectResponseReceived::Yes)) {
255         reportContentSecurityPolicyError(redirectResponse.url());
256         clearResource();
257         return completionHandler(WTFMove(request));
258     }
259
260     // Allow same origin requests to continue after allowing clients to audit the redirect.
261     if (isAllowedRedirect(request.url()))
262         return completionHandler(WTFMove(request));
263
264     // Force any subsequent request to use these checks.
265     m_sameOriginRequest = false;
266
267     ASSERT(m_resource);
268     ASSERT(m_originalHeaders);
269
270     // Use a unique for subsequent loads if needed.
271     // https://fetch.spec.whatwg.org/#concept-http-redirect-fetch (Step 10).
272     ASSERT(m_options.mode == FetchOptions::Mode::Cors);
273     if (!securityOrigin().canRequest(redirectResponse.url()) && !protocolHostAndPortAreEqual(redirectResponse.url(), request.url()))
274         m_origin = SecurityOrigin::createUnique();
275
276     // Except in case where preflight is needed, loading should be able to continue on its own.
277     // But we also handle credentials here if it is restricted to SameOrigin.
278     if (m_options.credentials != FetchOptions::Credentials::SameOrigin && m_simpleRequest && isSimpleCrossOriginAccessRequest(request.httpMethod(), *m_originalHeaders))
279         return completionHandler(WTFMove(request));
280
281     m_options.storedCredentialsPolicy = StoredCredentialsPolicy::DoNotUse;
282
283     clearResource();
284
285     // Let's fetch the request with the original headers (equivalent to request cloning specified by fetch algorithm).
286     // Do not copy the Authorization header if removed by the network layer.
287     if (!request.httpHeaderFields().contains(HTTPHeaderName::Authorization))
288         m_originalHeaders->remove(HTTPHeaderName::Authorization);
289     request.setHTTPHeaderFields(*m_originalHeaders);
290
291     makeCrossOriginAccessRequest(ResourceRequest(request));
292     completionHandler(WTFMove(request));
293 }
294
295 void DocumentThreadableLoader::dataSent(CachedResource& resource, unsigned long long bytesSent, unsigned long long totalBytesToBeSent)
296 {
297     ASSERT(m_client);
298     ASSERT_UNUSED(resource, &resource == m_resource);
299     m_client->didSendData(bytesSent, totalBytesToBeSent);
300 }
301
302 void DocumentThreadableLoader::responseReceived(CachedResource& resource, const ResourceResponse& response)
303 {
304     ASSERT_UNUSED(resource, &resource == m_resource);
305     didReceiveResponse(m_resource->identifier(), response);
306 }
307
308 void DocumentThreadableLoader::didReceiveResponse(unsigned long identifier, const ResourceResponse& response)
309 {
310     ASSERT(m_client);
311     ASSERT(response.type() != ResourceResponse::Type::Error);
312
313     InspectorInstrumentation::didReceiveThreadableLoaderResponse(*this, identifier);
314
315     if (m_delayCallbacksForIntegrityCheck)
316         return;
317
318     if (options().filteringPolicy == ResponseFilteringPolicy::Disable) {
319         m_client->didReceiveResponse(identifier, response);
320         return;
321     }
322
323     if (response.type() == ResourceResponse::Type::Default) {
324         m_client->didReceiveResponse(identifier, ResourceResponseBase::filter(response));
325         if (response.tainting() == ResourceResponse::Tainting::Opaque) {
326             clearResource();
327             if (m_client)
328                 m_client->didFinishLoading(identifier);
329         }
330         return;
331     }
332     ASSERT(response.type() == ResourceResponse::Type::Opaqueredirect || response.source() == ResourceResponse::Source::ServiceWorker);
333     m_client->didReceiveResponse(identifier, response);
334 }
335
336 void DocumentThreadableLoader::dataReceived(CachedResource& resource, const char* data, int dataLength)
337 {
338     ASSERT_UNUSED(resource, &resource == m_resource);
339     didReceiveData(m_resource->identifier(), data, dataLength);
340 }
341
342 void DocumentThreadableLoader::didReceiveData(unsigned long, const char* data, int dataLength)
343 {
344     ASSERT(m_client);
345
346     if (m_delayCallbacksForIntegrityCheck)
347         return;
348
349     m_client->didReceiveData(data, dataLength);
350 }
351
352 void DocumentThreadableLoader::finishedTimingForWorkerLoad(CachedResource& resource, const ResourceTiming& resourceTiming)
353 {
354     ASSERT(m_client);
355     ASSERT_UNUSED(resource, &resource == m_resource);
356
357     finishedTimingForWorkerLoad(resourceTiming);
358 }
359
360 void DocumentThreadableLoader::finishedTimingForWorkerLoad(const ResourceTiming& resourceTiming)
361 {
362     ASSERT(m_options.initiatorContext == InitiatorContext::Worker);
363
364     m_client->didFinishTiming(resourceTiming);
365 }
366
367 void DocumentThreadableLoader::notifyFinished(CachedResource& resource)
368 {
369     ASSERT(m_client);
370     ASSERT_UNUSED(resource, &resource == m_resource);
371
372     if (m_resource->errorOccurred())
373         didFail(m_resource->identifier(), m_resource->resourceError());
374     else
375         didFinishLoading(m_resource->identifier());
376 }
377
378 void DocumentThreadableLoader::didFinishLoading(unsigned long identifier)
379 {
380     ASSERT(m_client);
381
382     if (m_delayCallbacksForIntegrityCheck) {
383         if (!matchIntegrityMetadata(*m_resource, m_options.integrity)) {
384             reportIntegrityMetadataError(m_resource->url());
385             return;
386         }
387
388         auto response = m_resource->response();
389
390         if (options().filteringPolicy == ResponseFilteringPolicy::Disable) {
391             m_client->didReceiveResponse(identifier, response);
392             m_client->didReceiveData(m_resource->resourceBuffer()->data(), m_resource->resourceBuffer()->size());
393         } else {
394             ASSERT(response.type() == ResourceResponse::Type::Default);
395
396             m_client->didReceiveResponse(identifier, ResourceResponseBase::filter(response));
397             m_client->didReceiveData(m_resource->resourceBuffer()->data(), m_resource->resourceBuffer()->size());
398         }
399     }
400
401     m_client->didFinishLoading(identifier);
402 }
403
404 void DocumentThreadableLoader::didFail(unsigned long, const ResourceError& error)
405 {
406     ASSERT(m_client);
407 #if ENABLE(SERVICE_WORKER)
408     if (m_bypassingPreflightForServiceWorkerRequest) {
409         m_options.serviceWorkersMode = ServiceWorkersMode::None;
410         makeCrossOriginAccessRequest(WTFMove(m_bypassingPreflightForServiceWorkerRequest.value()));
411         return;
412     }
413 #endif
414     logErrorAndFail(error);
415 }
416
417 void DocumentThreadableLoader::preflightSuccess(ResourceRequest&& request)
418 {
419     ResourceRequest actualRequest(WTFMove(request));
420     updateRequestForAccessControl(actualRequest, securityOrigin(), m_options.storedCredentialsPolicy);
421
422     m_preflightChecker = std::nullopt;
423
424     // It should be ok to skip the security check since we already asked about the preflight request.
425     loadRequest(WTFMove(actualRequest), SkipSecurityCheck);
426 }
427
428 void DocumentThreadableLoader::preflightFailure(unsigned long identifier, const ResourceError& error)
429 {
430     m_preflightChecker = std::nullopt;
431
432     InspectorInstrumentation::didFailLoading(m_document.frame(), m_document.frame()->loader().documentLoader(), identifier, error);
433     ASSERT(m_client);
434     logErrorAndFail(error);
435 }
436
437 void DocumentThreadableLoader::loadRequest(ResourceRequest&& request, SecurityCheckPolicy securityCheck)
438 {
439     Ref<DocumentThreadableLoader> protectedThis(*this);
440
441     // Any credential should have been removed from the cross-site requests.
442     const URL& requestURL = request.url();
443     m_options.securityCheck = securityCheck;
444     ASSERT(m_sameOriginRequest || requestURL.user().isEmpty());
445     ASSERT(m_sameOriginRequest || requestURL.pass().isEmpty());
446
447     if (!m_referrer.isNull())
448         request.setHTTPReferrer(m_referrer);
449
450     if (m_async) {
451         ResourceLoaderOptions options = m_options;
452         options.clientCredentialPolicy = m_sameOriginRequest ? ClientCredentialPolicy::MayAskClientForCredentials : ClientCredentialPolicy::CannotAskClientForCredentials;
453         options.contentSecurityPolicyImposition = ContentSecurityPolicyImposition::SkipPolicyCheck;
454         
455         // If there is integrity metadata to validate, we must buffer.
456         if (!m_options.integrity.isEmpty())
457             options.dataBufferingPolicy = BufferData;
458
459         request.setAllowCookies(m_options.storedCredentialsPolicy == StoredCredentialsPolicy::Use);
460         CachedResourceRequest newRequest(WTFMove(request), options);
461         if (RuntimeEnabledFeatures::sharedFeatures().resourceTimingEnabled())
462             newRequest.setInitiator(m_options.initiator);
463         newRequest.setOrigin(securityOrigin());
464
465         ASSERT(!m_resource);
466         if (m_resource) {
467             CachedResourceHandle<CachedRawResource> resource = std::exchange(m_resource, nullptr);
468             resource->removeClient(*this);
469         }
470
471         auto cachedResource = m_document.cachedResourceLoader().requestRawResource(WTFMove(newRequest));
472         m_resource = cachedResource.value_or(nullptr);
473         if (m_resource)
474             m_resource->addClient(*this);
475         else
476             logErrorAndFail(cachedResource.error());
477         return;
478     }
479
480     // If credentials mode is 'Omit', we should disable cookie sending.
481     ASSERT(m_options.credentials != FetchOptions::Credentials::Omit);
482
483     LoadTiming loadTiming;
484     loadTiming.markStartTimeAndFetchStart();
485
486     // FIXME: ThreadableLoaderOptions.sniffContent is not supported for synchronous requests.
487     RefPtr<SharedBuffer> data;
488     ResourceError error;
489     ResourceResponse response;
490     unsigned long identifier = std::numeric_limits<unsigned long>::max();
491     if (m_document.frame()) {
492         auto& frameLoader = m_document.frame()->loader();
493         if (!frameLoader.mixedContentChecker().canRunInsecureContent(m_document.securityOrigin(), requestURL))
494             return;
495         identifier = frameLoader.loadResourceSynchronously(request, m_options.storedCredentialsPolicy, m_options.clientCredentialPolicy, error, response, data);
496     }
497
498     loadTiming.setResponseEnd(MonotonicTime::now());
499
500     if (!error.isNull() && response.httpStatusCode() <= 0) {
501         if (requestURL.isLocalFile()) {
502             // We don't want XMLHttpRequest to raise an exception for file:// resources, see <rdar://problem/4962298>.
503             // FIXME: XMLHttpRequest quirks should be in XMLHttpRequest code, not in DocumentThreadableLoader.cpp.
504             didReceiveResponse(identifier, response);
505             didFinishLoading(identifier);
506             return;
507         }
508         logErrorAndFail(error);
509         return;
510     }
511
512     // FIXME: FrameLoader::loadSynchronously() does not tell us whether a redirect happened or not, so we guess by comparing the
513     // request and response URLs. This isn't a perfect test though, since a server can serve a redirect to the same URL that was
514     // requested. Also comparing the request and response URLs as strings will fail if the requestURL still has its credentials.
515     bool didRedirect = requestURL != response.url();
516     if (didRedirect) {
517         if (!isAllowedByContentSecurityPolicy(response.url(), ContentSecurityPolicy::RedirectResponseReceived::Yes)) {
518             reportContentSecurityPolicyError(requestURL);
519             return;
520         }
521         if (!isAllowedRedirect(response.url())) {
522             reportCrossOriginResourceSharingError(requestURL);
523             return;
524         }
525     }
526
527     if (!m_sameOriginRequest) {
528         if (m_options.mode == FetchOptions::Mode::NoCors)
529             response.setTainting(ResourceResponse::Tainting::Opaque);
530         else {
531             ASSERT(m_options.mode == FetchOptions::Mode::Cors);
532             response.setTainting(ResourceResponse::Tainting::Cors);
533             String accessControlErrorDescription;
534             if (!passesAccessControlCheck(response, m_options.storedCredentialsPolicy, securityOrigin(), accessControlErrorDescription)) {
535                 logErrorAndFail(ResourceError(errorDomainWebKitInternal, 0, response.url(), accessControlErrorDescription, ResourceError::Type::AccessControl));
536                 return;
537             }
538         }
539     }
540     didReceiveResponse(identifier, response);
541
542     if (data)
543         didReceiveData(identifier, data->data(), data->size());
544
545     if (RuntimeEnabledFeatures::sharedFeatures().resourceTimingEnabled()) {
546         auto resourceTiming = ResourceTiming::fromSynchronousLoad(requestURL, m_options.initiator, loadTiming, response.deprecatedNetworkLoadMetrics(), response, securityOrigin());
547         if (options().initiatorContext == InitiatorContext::Worker)
548             finishedTimingForWorkerLoad(resourceTiming);
549         else {
550             if (document().domWindow() && document().domWindow()->performance())
551                 document().domWindow()->performance()->addResourceTiming(WTFMove(resourceTiming));
552         }
553     }
554
555     didFinishLoading(identifier);
556 }
557
558 bool DocumentThreadableLoader::isAllowedByContentSecurityPolicy(const URL& url, ContentSecurityPolicy::RedirectResponseReceived redirectResponseReceived)
559 {
560     switch (m_options.contentSecurityPolicyEnforcement) {
561     case ContentSecurityPolicyEnforcement::DoNotEnforce:
562         return true;
563     case ContentSecurityPolicyEnforcement::EnforceChildSrcDirective:
564         return contentSecurityPolicy().allowChildContextFromSource(url, redirectResponseReceived);
565     case ContentSecurityPolicyEnforcement::EnforceConnectSrcDirective:
566         return contentSecurityPolicy().allowConnectToSource(url, redirectResponseReceived);
567     case ContentSecurityPolicyEnforcement::EnforceScriptSrcDirective:
568         return contentSecurityPolicy().allowScriptFromSource(url, redirectResponseReceived);
569     }
570     ASSERT_NOT_REACHED();
571     return false;
572 }
573
574 bool DocumentThreadableLoader::isAllowedRedirect(const URL& url)
575 {
576     if (m_options.mode == FetchOptions::Mode::NoCors)
577         return true;
578
579     return m_sameOriginRequest && securityOrigin().canRequest(url);
580 }
581
582 bool DocumentThreadableLoader::isXMLHttpRequest() const
583 {
584     return m_options.initiator == cachedResourceRequestInitiators().xmlhttprequest;
585 }
586
587 SecurityOrigin& DocumentThreadableLoader::securityOrigin() const
588 {
589     return m_origin ? *m_origin : m_document.securityOrigin();
590 }
591
592 const ContentSecurityPolicy& DocumentThreadableLoader::contentSecurityPolicy() const
593 {
594     if (m_contentSecurityPolicy)
595         return *m_contentSecurityPolicy.get();
596     ASSERT(m_document.contentSecurityPolicy());
597     return *m_document.contentSecurityPolicy();
598 }
599
600 void DocumentThreadableLoader::reportRedirectionWithBadScheme(const URL& url)
601 {
602     logErrorAndFail(ResourceError(errorDomainWebKitInternal, 0, url, "Redirection to URL with a scheme that is not HTTP(S).", ResourceError::Type::AccessControl));
603 }
604
605 void DocumentThreadableLoader::reportContentSecurityPolicyError(const URL& url)
606 {
607     logErrorAndFail(ResourceError(errorDomainWebKitInternal, 0, url, "Cross-origin redirection denied by Content Security Policy.", ResourceError::Type::AccessControl));
608 }
609
610 void DocumentThreadableLoader::reportCrossOriginResourceSharingError(const URL& url)
611 {
612     logErrorAndFail(ResourceError(errorDomainWebKitInternal, 0, url, "Cross-origin redirection denied by Cross-Origin Resource Sharing policy.", ResourceError::Type::AccessControl));
613 }
614
615 void DocumentThreadableLoader::reportIntegrityMetadataError(const URL& url)
616 {
617     logErrorAndFail(ResourceError(errorDomainWebKitInternal, 0, url, "Failed integrity metadata check.", ResourceError::Type::General));
618 }
619
620 void DocumentThreadableLoader::logErrorAndFail(const ResourceError& error)
621 {
622     if (m_shouldLogError == ShouldLogError::Yes)
623         logError(m_document, error, m_options.initiator);
624     ASSERT(m_client);
625     m_client->didFail(error);
626 }
627
628 } // namespace WebCore