b193733413fe584084f3a44026c497118b7924fb
[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 "Document.h"
42 #include "Frame.h"
43 #include "FrameLoader.h"
44 #include "InspectorInstrumentation.h"
45 #include "ProgressTracker.h"
46 #include "ResourceError.h"
47 #include "ResourceRequest.h"
48 #include "RuntimeEnabledFeatures.h"
49 #include "SchemeRegistry.h"
50 #include "SecurityOrigin.h"
51 #include "SubresourceLoader.h"
52 #include "ThreadableLoaderClient.h"
53 #include <wtf/Assertions.h>
54 #include <wtf/Ref.h>
55
56 namespace WebCore {
57
58 void DocumentThreadableLoader::loadResourceSynchronously(Document& document, ResourceRequest&& request, ThreadableLoaderClient& client, const ThreadableLoaderOptions& options, RefPtr<SecurityOrigin>&& origin, std::unique_ptr<ContentSecurityPolicy>&& contentSecurityPolicy)
59 {
60     // The loader will be deleted as soon as this function exits.
61     Ref<DocumentThreadableLoader> loader = adoptRef(*new DocumentThreadableLoader(document, client, LoadSynchronously, WTFMove(request), options, WTFMove(origin), WTFMove(contentSecurityPolicy), String(), ShouldLogError::Yes));
62     ASSERT(loader->hasOneRef());
63 }
64
65 void DocumentThreadableLoader::loadResourceSynchronously(Document& document, ResourceRequest&& request, ThreadableLoaderClient& client, const ThreadableLoaderOptions& options)
66 {
67     loadResourceSynchronously(document, WTFMove(request), client, options, nullptr, nullptr);
68 }
69
70 RefPtr<DocumentThreadableLoader> DocumentThreadableLoader::create(Document& document, ThreadableLoaderClient& client,
71 ResourceRequest&& request, const ThreadableLoaderOptions& options, RefPtr<SecurityOrigin>&& origin,
72 std::unique_ptr<ContentSecurityPolicy>&& contentSecurityPolicy, String&& referrer, ShouldLogError shouldLogError)
73 {
74     RefPtr<DocumentThreadableLoader> loader = adoptRef(new DocumentThreadableLoader(document, client, LoadAsynchronously, WTFMove(request), options, WTFMove(origin), WTFMove(contentSecurityPolicy), WTFMove(referrer), shouldLogError));
75     if (!loader->isLoading())
76         loader = nullptr;
77     return loader;
78 }
79
80 RefPtr<DocumentThreadableLoader> DocumentThreadableLoader::create(Document& document, ThreadableLoaderClient& client, ResourceRequest&& request, const ThreadableLoaderOptions& options, String&& referrer)
81 {
82     return create(document, client, WTFMove(request), options, nullptr, nullptr, WTFMove(referrer), ShouldLogError::Yes);
83 }
84
85 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)
86     : m_client(&client)
87     , m_document(document)
88     , m_options(options)
89     , m_origin(WTFMove(origin))
90     , m_referrer(WTFMove(referrer))
91     , m_sameOriginRequest(securityOrigin().canRequest(request.url()))
92     , m_simpleRequest(true)
93     , m_async(blockingBehavior == LoadAsynchronously)
94     , m_contentSecurityPolicy(WTFMove(contentSecurityPolicy))
95     , m_shouldLogError(shouldLogError)
96 {
97     // Setting a referrer header is only supported in the async code path.
98     ASSERT(m_async || m_referrer.isEmpty());
99
100     // Referrer and Origin headers should be set after the preflight if any.
101     ASSERT(!request.hasHTTPReferrer() && !request.hasHTTPOrigin());
102
103     ASSERT_WITH_SECURITY_IMPLICATION(isAllowedByContentSecurityPolicy(request.url(), ContentSecurityPolicy::RedirectResponseReceived::No));
104
105     m_options.allowCredentials = (m_options.credentials == FetchOptions::Credentials::Include || (m_options.credentials == FetchOptions::Credentials::SameOrigin && m_sameOriginRequest)) ? AllowStoredCredentials : DoNotAllowStoredCredentials;
106
107     ASSERT(!request.httpHeaderFields().contains(HTTPHeaderName::Origin));
108
109     // Copy headers if we need to replay the request after a redirection.
110     if (m_async && m_options.mode == FetchOptions::Mode::Cors)
111         m_originalHeaders = request.httpHeaderFields();
112
113     // 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.
114     if (request.url().protocolIsData())
115         m_sameOriginRequest = options.sameOriginDataURLFlag == SameOriginDataURLFlag::Set;
116
117     if (m_sameOriginRequest || m_options.mode == FetchOptions::Mode::NoCors) {
118         loadRequest(WTFMove(request), DoSecurityCheck);
119         return;
120     }
121
122     if (m_options.mode == FetchOptions::Mode::SameOrigin) {
123         logErrorAndFail(ResourceError(errorDomainWebKitInternal, 0, request.url(), "Cross origin requests are not allowed when using same-origin fetch mode."));
124         return;
125     }
126
127     makeCrossOriginAccessRequest(WTFMove(request));
128 }
129
130 void DocumentThreadableLoader::makeCrossOriginAccessRequest(ResourceRequest&& request)
131 {
132     ASSERT(m_options.mode == FetchOptions::Mode::Cors);
133
134     if ((m_options.preflightPolicy == ConsiderPreflight && isSimpleCrossOriginAccessRequest(request.httpMethod(), request.httpHeaderFields())) || m_options.preflightPolicy == PreventPreflight)
135         makeSimpleCrossOriginAccessRequest(WTFMove(request));
136     else {
137         m_simpleRequest = false;
138         if (CrossOriginPreflightResultCache::singleton().canSkipPreflight(securityOrigin().toString(), request.url(), m_options.allowCredentials, request.httpMethod(), request.httpHeaderFields()))
139             preflightSuccess(WTFMove(request));
140         else
141             makeCrossOriginAccessRequestWithPreflight(WTFMove(request));
142     }
143 }
144
145 void DocumentThreadableLoader::makeSimpleCrossOriginAccessRequest(ResourceRequest&& request)
146 {
147     ASSERT(m_options.preflightPolicy != ForcePreflight);
148     ASSERT(m_options.preflightPolicy == PreventPreflight || isSimpleCrossOriginAccessRequest(request.httpMethod(), request.httpHeaderFields()));
149
150     // 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.
151     if (!SchemeRegistry::shouldTreatURLSchemeAsCORSEnabled(request.url().protocol().toStringWithoutCopying())) {
152         logErrorAndFail(ResourceError(errorDomainWebKitInternal, 0, request.url(), "Cross origin requests are only supported for HTTP.", ResourceError::Type::AccessControl));
153         return;
154     }
155
156     updateRequestForAccessControl(request, securityOrigin(), m_options.allowCredentials);
157     loadRequest(WTFMove(request), DoSecurityCheck);
158 }
159
160 void DocumentThreadableLoader::makeCrossOriginAccessRequestWithPreflight(ResourceRequest&& request)
161 {
162     if (m_async) {
163         m_preflightChecker.emplace(*this, WTFMove(request));
164         m_preflightChecker->startPreflight();
165         return;
166     }
167     CrossOriginPreflightChecker::doPreflight(*this, WTFMove(request));
168 }
169
170 DocumentThreadableLoader::~DocumentThreadableLoader()
171 {
172     if (m_resource)
173         m_resource->removeClient(*this);
174 }
175
176 void DocumentThreadableLoader::cancel()
177 {
178     Ref<DocumentThreadableLoader> protectedThis(*this);
179
180     // Cancel can re-enter and m_resource might be null here as a result.
181     if (m_client && m_resource) {
182         // FIXME: This error is sent to the client in didFail(), so it should not be an internal one. Use FrameLoaderClient::cancelledError() instead.
183         ResourceError error(errorDomainWebKitInternal, 0, m_resource->url(), "Load cancelled", ResourceError::Type::Cancellation);
184         m_client->didFail(error);
185     }
186     clearResource();
187     m_client = nullptr;
188 }
189
190 void DocumentThreadableLoader::setDefersLoading(bool value)
191 {
192     if (m_resource)
193         m_resource->setDefersLoading(value);
194     if (m_preflightChecker)
195         m_preflightChecker->setDefersLoading(value);
196 }
197
198 void DocumentThreadableLoader::clearResource()
199 {
200     // Script can cancel and restart a request reentrantly within removeClient(),
201     // which could lead to calling CachedResource::removeClient() multiple times for
202     // this DocumentThreadableLoader. Save off a copy of m_resource and clear it to
203     // prevent the reentrancy.
204     if (CachedResourceHandle<CachedRawResource> resource = m_resource) {
205         m_resource = nullptr;
206         resource->removeClient(*this);
207     }
208     if (m_preflightChecker)
209         m_preflightChecker = std::nullopt;
210 }
211
212 void DocumentThreadableLoader::redirectReceived(CachedResource& resource, ResourceRequest& request, const ResourceResponse& redirectResponse)
213 {
214     ASSERT(m_client);
215     ASSERT_UNUSED(resource, &resource == m_resource);
216
217     Ref<DocumentThreadableLoader> protectedThis(*this);
218
219     // FIXME: We restrict this check to Fetch API for the moment, as this might disrupt WorkerScriptLoader.
220     // Reassess this check based on https://github.com/whatwg/fetch/issues/393 discussions.
221     // We should also disable that check in navigation mode.
222     if (!request.url().protocolIsInHTTPFamily() && m_options.initiator == cachedResourceRequestInitiators().fetch) {
223         reportRedirectionWithBadScheme(request.url());
224         clearResource();
225         return;
226     }
227
228     if (!isAllowedByContentSecurityPolicy(request.url(), redirectResponse.isNull() ? ContentSecurityPolicy::RedirectResponseReceived::No : ContentSecurityPolicy::RedirectResponseReceived::Yes)) {
229         reportContentSecurityPolicyError(redirectResponse.url());
230         clearResource();
231         return;
232     }
233
234     // Allow same origin requests to continue after allowing clients to audit the redirect.
235     if (isAllowedRedirect(request.url()))
236         return;
237
238     // Force any subsequent request to use these checks.
239     m_sameOriginRequest = false;
240
241     ASSERT(m_resource);
242     ASSERT(m_resource->loader());
243     ASSERT(m_options.mode == FetchOptions::Mode::Cors);
244     ASSERT(m_originalHeaders);
245
246     // Loader might have modified the origin to a unique one, let's reuse it for subsequent loads.
247     m_origin = m_resource->loader()->origin();
248
249     // Except in case where preflight is needed, loading should be able to continue on its own.
250     // But we also handle credentials here if it is restricted to SameOrigin.
251     if (m_options.credentials != FetchOptions::Credentials::SameOrigin && m_simpleRequest && isSimpleCrossOriginAccessRequest(request.httpMethod(), *m_originalHeaders))
252         return;
253
254     m_options.allowCredentials = DoNotAllowStoredCredentials;
255     m_options.maxRedirectCount -= m_resource->loader()->redirectCount();
256
257     clearResource();
258
259     // Let's fetch the request with the original headers (equivalent to request cloning specified by fetch algorithm).
260     // Do not copy the Authorization header if removed by the network layer.
261     if (!request.httpHeaderFields().contains(HTTPHeaderName::Authorization))
262         m_originalHeaders->remove(HTTPHeaderName::Authorization);
263     request.setHTTPHeaderFields(*m_originalHeaders);
264
265     makeCrossOriginAccessRequest(ResourceRequest(request));
266 }
267
268 void DocumentThreadableLoader::dataSent(CachedResource& resource, unsigned long long bytesSent, unsigned long long totalBytesToBeSent)
269 {
270     ASSERT(m_client);
271     ASSERT_UNUSED(resource, &resource == m_resource);
272     m_client->didSendData(bytesSent, totalBytesToBeSent);
273 }
274
275 void DocumentThreadableLoader::responseReceived(CachedResource& resource, const ResourceResponse& response)
276 {
277     ASSERT_UNUSED(resource, &resource == m_resource);
278     didReceiveResponse(m_resource->identifier(), response, m_resource->responseTainting());
279 }
280
281 void DocumentThreadableLoader::didReceiveResponse(unsigned long identifier, const ResourceResponse& response, ResourceResponse::Tainting tainting)
282 {
283     ASSERT(m_client);
284     ASSERT(response.type() != ResourceResponse::Type::Error);
285
286     InspectorInstrumentation::didReceiveThreadableLoaderResponse(*this, identifier);
287
288     if (options().filteringPolicy == ResponseFilteringPolicy::Disable) {
289         m_client->didReceiveResponse(identifier, response);
290         return;
291     }
292
293     if (response.type() == ResourceResponse::Type::Default) {
294         m_client->didReceiveResponse(identifier, ResourceResponse::filterResponse(response, tainting));
295         if (tainting == ResourceResponse::Tainting::Opaque) {
296             clearResource();
297             if (m_client)
298                 m_client->didFinishLoading(identifier, 0.0);
299         }
300     } else {
301         ASSERT(response.type() == ResourceResponse::Type::Opaqueredirect);
302         m_client->didReceiveResponse(identifier, response);
303     }
304 }
305
306 void DocumentThreadableLoader::dataReceived(CachedResource& resource, const char* data, int dataLength)
307 {
308     ASSERT_UNUSED(resource, &resource == m_resource);
309     didReceiveData(m_resource->identifier(), data, dataLength);
310 }
311
312 void DocumentThreadableLoader::didReceiveData(unsigned long, const char* data, int dataLength)
313 {
314     ASSERT(m_client);
315
316     m_client->didReceiveData(data, dataLength);
317 }
318
319 void DocumentThreadableLoader::notifyFinished(CachedResource& resource)
320 {
321     ASSERT(m_client);
322     ASSERT_UNUSED(resource, &resource == m_resource);
323
324     if (m_resource->errorOccurred())
325         didFail(m_resource->identifier(), m_resource->resourceError());
326     else
327         didFinishLoading(m_resource->identifier(), m_resource->loadFinishTime());
328 }
329
330 void DocumentThreadableLoader::didFinishLoading(unsigned long identifier, double finishTime)
331 {
332     ASSERT(m_client);
333     m_client->didFinishLoading(identifier, finishTime);
334 }
335
336 void DocumentThreadableLoader::didFail(unsigned long, const ResourceError& error)
337 {
338     ASSERT(m_client);
339     logErrorAndFail(error);
340 }
341
342 void DocumentThreadableLoader::preflightSuccess(ResourceRequest&& request)
343 {
344     ResourceRequest actualRequest(WTFMove(request));
345     updateRequestForAccessControl(actualRequest, securityOrigin(), m_options.allowCredentials);
346
347     m_preflightChecker = std::nullopt;
348
349     // It should be ok to skip the security check since we already asked about the preflight request.
350     loadRequest(WTFMove(actualRequest), SkipSecurityCheck);
351 }
352
353 void DocumentThreadableLoader::preflightFailure(unsigned long identifier, const ResourceError& error)
354 {
355     m_preflightChecker = std::nullopt;
356
357     InspectorInstrumentation::didFailLoading(m_document.frame(), m_document.frame()->loader().documentLoader(), identifier, error);
358     ASSERT(m_client);
359     logErrorAndFail(error);
360 }
361
362 void DocumentThreadableLoader::loadRequest(ResourceRequest&& request, SecurityCheckPolicy securityCheck)
363 {
364     // Any credential should have been removed from the cross-site requests.
365     const URL& requestURL = request.url();
366     m_options.securityCheck = securityCheck;
367     ASSERT(m_sameOriginRequest || requestURL.user().isEmpty());
368     ASSERT(m_sameOriginRequest || requestURL.pass().isEmpty());
369
370     if (!m_referrer.isNull())
371         request.setHTTPReferrer(m_referrer);
372
373     if (m_async) {
374         ResourceLoaderOptions options = m_options;
375         options.clientCredentialPolicy = m_sameOriginRequest ? ClientCredentialPolicy::MayAskClientForCredentials : ClientCredentialPolicy::CannotAskClientForCredentials;
376         options.contentSecurityPolicyImposition = ContentSecurityPolicyImposition::SkipPolicyCheck;
377
378         request.setAllowCookies(m_options.allowCredentials == AllowStoredCredentials);
379         CachedResourceRequest newRequest(WTFMove(request), options);
380         if (RuntimeEnabledFeatures::sharedFeatures().resourceTimingEnabled())
381             newRequest.setInitiator(m_options.initiator);
382         newRequest.setOrigin(&securityOrigin());
383
384         ASSERT(!m_resource);
385         // We create an URL here as the request will be moved in requestRawResource
386         URL requestUrl = newRequest.resourceRequest().url();
387         m_resource = m_document.cachedResourceLoader().requestRawResource(WTFMove(newRequest));
388         if (m_resource)
389             m_resource->addClient(*this);
390         else {
391             // FIXME: Since we receive a synchronous error, this is probably due to some AccessControl checks. We should try to retrieve the actual error.
392             logErrorAndFail(ResourceError(String(), 0, requestUrl, String(), ResourceError::Type::AccessControl));
393         }
394         return;
395     }
396
397     // If credentials mode is 'Omit', we should disable cookie sending.
398     ASSERT(m_options.credentials != FetchOptions::Credentials::Omit);
399
400     // FIXME: ThreadableLoaderOptions.sniffContent is not supported for synchronous requests.
401     RefPtr<SharedBuffer> data;
402     ResourceError error;
403     ResourceResponse response;
404     unsigned long identifier = std::numeric_limits<unsigned long>::max();
405     if (m_document.frame()) {
406         auto& frameLoader = m_document.frame()->loader();
407         if (!frameLoader.mixedContentChecker().canRunInsecureContent(m_document.securityOrigin(), requestURL))
408             return;
409         identifier = frameLoader.loadResourceSynchronously(request, m_options.allowCredentials, m_options.clientCredentialPolicy, error, response, data);
410     }
411
412     if (!error.isNull() && response.httpStatusCode() <= 0) {
413         if (requestURL.isLocalFile()) {
414             // We don't want XMLHttpRequest to raise an exception for file:// resources, see <rdar://problem/4962298>.
415             // FIXME: XMLHttpRequest quirks should be in XMLHttpRequest code, not in DocumentThreadableLoader.cpp.
416             didReceiveResponse(identifier, response, ResourceResponse::Tainting::Basic);
417             didFinishLoading(identifier, 0.0);
418             return;
419         }
420         logErrorAndFail(error);
421         return;
422     }
423
424     // FIXME: FrameLoader::loadSynchronously() does not tell us whether a redirect happened or not, so we guess by comparing the
425     // request and response URLs. This isn't a perfect test though, since a server can serve a redirect to the same URL that was
426     // requested. Also comparing the request and response URLs as strings will fail if the requestURL still has its credentials.
427     bool didRedirect = requestURL != response.url();
428     if (didRedirect) {
429         if (!isAllowedByContentSecurityPolicy(response.url(), ContentSecurityPolicy::RedirectResponseReceived::Yes)) {
430             reportContentSecurityPolicyError(requestURL);
431             return;
432         }
433         if (!isAllowedRedirect(response.url())) {
434             reportCrossOriginResourceSharingError(requestURL);
435             return;
436         }
437     }
438
439     ResourceResponse::Tainting tainting = ResourceResponse::Tainting::Basic;
440     if (!m_sameOriginRequest) {
441         if (m_options.mode == FetchOptions::Mode::NoCors)
442             tainting = ResourceResponse::Tainting::Opaque;
443         else {
444             ASSERT(m_options.mode == FetchOptions::Mode::Cors);
445             tainting = ResourceResponse::Tainting::Cors;
446             String accessControlErrorDescription;
447             if (!passesAccessControlCheck(response, m_options.allowCredentials, securityOrigin(), accessControlErrorDescription)) {
448                 logErrorAndFail(ResourceError(errorDomainWebKitInternal, 0, response.url(), accessControlErrorDescription, ResourceError::Type::AccessControl));
449                 return;
450             }
451         }
452     }
453     didReceiveResponse(identifier, response, tainting);
454
455     if (data)
456         didReceiveData(identifier, data->data(), data->size());
457     didFinishLoading(identifier, 0.0);
458 }
459
460 bool DocumentThreadableLoader::isAllowedByContentSecurityPolicy(const URL& url, ContentSecurityPolicy::RedirectResponseReceived redirectResponseReceived)
461 {
462     switch (m_options.contentSecurityPolicyEnforcement) {
463     case ContentSecurityPolicyEnforcement::DoNotEnforce:
464         return true;
465     case ContentSecurityPolicyEnforcement::EnforceChildSrcDirective:
466         return contentSecurityPolicy().allowChildContextFromSource(url, redirectResponseReceived);
467     case ContentSecurityPolicyEnforcement::EnforceConnectSrcDirective:
468         return contentSecurityPolicy().allowConnectToSource(url, redirectResponseReceived);
469     case ContentSecurityPolicyEnforcement::EnforceScriptSrcDirective:
470         return contentSecurityPolicy().allowScriptFromSource(url, redirectResponseReceived);
471     }
472     ASSERT_NOT_REACHED();
473     return false;
474 }
475
476 bool DocumentThreadableLoader::isAllowedRedirect(const URL& url)
477 {
478     if (m_options.mode == FetchOptions::Mode::NoCors)
479         return true;
480
481     return m_sameOriginRequest && securityOrigin().canRequest(url);
482 }
483
484 bool DocumentThreadableLoader::isXMLHttpRequest() const
485 {
486     return m_options.initiator == cachedResourceRequestInitiators().xmlhttprequest;
487 }
488
489 SecurityOrigin& DocumentThreadableLoader::securityOrigin() const
490 {
491     ASSERT(m_document.securityOrigin());
492     return m_origin ? *m_origin : *m_document.securityOrigin();
493 }
494
495 const ContentSecurityPolicy& DocumentThreadableLoader::contentSecurityPolicy() const
496 {
497     if (m_contentSecurityPolicy)
498         return *m_contentSecurityPolicy.get();
499     ASSERT(m_document.contentSecurityPolicy());
500     return *m_document.contentSecurityPolicy();
501 }
502
503 void DocumentThreadableLoader::reportRedirectionWithBadScheme(const URL& url)
504 {
505     logErrorAndFail(ResourceError(errorDomainWebKitInternal, 0, url, "Redirection to URL with a scheme that is not HTTP(S).", ResourceError::Type::AccessControl));
506 }
507
508 void DocumentThreadableLoader::reportContentSecurityPolicyError(const URL& url)
509 {
510     logErrorAndFail(ResourceError(errorDomainWebKitInternal, 0, url, "Cross-origin redirection denied by Content Security Policy.", ResourceError::Type::AccessControl));
511 }
512
513 void DocumentThreadableLoader::reportCrossOriginResourceSharingError(const URL& url)
514 {
515     logErrorAndFail(ResourceError(errorDomainWebKitInternal, 0, url, "Cross-origin redirection denied by Cross-Origin Resource Sharing policy.", ResourceError::Type::AccessControl));
516 }
517
518 void DocumentThreadableLoader::logErrorAndFail(const ResourceError& error)
519 {
520     if (m_shouldLogError == ShouldLogError::Yes)
521         logError(m_document, error, m_options.initiator);
522     ASSERT(m_client);
523     m_client->didFail(error);
524 }
525
526 } // namespace WebCore