1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
|
/*
* Copyright (C) 2011, 2012 Google Inc. All rights reserved.
* Copyright (C) 2013, Intel Corporation
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "core/loader/DocumentThreadableLoader.h"
#include "core/dom/Document.h"
#include "core/fetch/CrossOriginAccessControl.h"
#include "core/fetch/FetchRequest.h"
#include "core/fetch/FetchUtils.h"
#include "core/fetch/Resource.h"
#include "core/fetch/ResourceFetcher.h"
#include "core/frame/FrameConsole.h"
#include "core/frame/LocalFrame.h"
#include "core/frame/csp/ContentSecurityPolicy.h"
#include "core/inspector/InspectorInstrumentation.h"
#include "core/inspector/InspectorTraceEvents.h"
#include "core/loader/CrossOriginPreflightResultCache.h"
#include "core/loader/DocumentThreadableLoaderClient.h"
#include "core/loader/FrameLoader.h"
#include "core/loader/FrameLoaderClient.h"
#include "core/loader/ThreadableLoaderClient.h"
#include "platform/SharedBuffer.h"
#include "platform/network/ResourceRequest.h"
#include "platform/weborigin/SchemeRegistry.h"
#include "platform/weborigin/SecurityOrigin.h"
#include "public/platform/WebURLRequest.h"
#include "wtf/Assertions.h"
namespace blink {
void DocumentThreadableLoader::loadResourceSynchronously(Document& document, const ResourceRequest& request, ThreadableLoaderClient& client, const ThreadableLoaderOptions& options, const ResourceLoaderOptions& resourceLoaderOptions)
{
// The loader will be deleted as soon as this function exits.
RefPtr<DocumentThreadableLoader> loader = adoptRef(new DocumentThreadableLoader(document, &client, LoadSynchronously, request, options, resourceLoaderOptions));
ASSERT(loader->hasOneRef());
}
PassRefPtr<DocumentThreadableLoader> DocumentThreadableLoader::create(Document& document, ThreadableLoaderClient* client, const ResourceRequest& request, const ThreadableLoaderOptions& options, const ResourceLoaderOptions& resourceLoaderOptions)
{
RefPtr<DocumentThreadableLoader> loader = adoptRef(new DocumentThreadableLoader(document, client, LoadAsynchronously, request, options, resourceLoaderOptions));
if (!loader->resource())
loader = nullptr;
return loader.release();
}
DocumentThreadableLoader::DocumentThreadableLoader(Document& document, ThreadableLoaderClient* client, BlockingBehavior blockingBehavior, const ResourceRequest& request, const ThreadableLoaderOptions& options, const ResourceLoaderOptions& resourceLoaderOptions)
: m_client(client)
, m_document(document)
, m_options(options)
, m_resourceLoaderOptions(resourceLoaderOptions)
, m_forceDoNotAllowStoredCredentials(false)
, m_securityOrigin(m_resourceLoaderOptions.securityOrigin)
, m_sameOriginRequest(securityOrigin()->canRequest(request.url()))
, m_simpleRequest(true)
, m_async(blockingBehavior == LoadAsynchronously)
, m_timeoutTimer(this, &DocumentThreadableLoader::didTimeout)
, m_requestStartedSeconds(0.0)
{
ASSERT(client);
// Setting an outgoing referer is only supported in the async code path.
ASSERT(m_async || request.httpReferrer().isEmpty());
if (!m_sameOriginRequest && m_options.crossOriginRequestPolicy == DenyCrossOriginRequests) {
m_client->didFail(ResourceError(errorDomainBlinkInternal, 0, request.url().string(), "Cross origin requests are not supported."));
return;
}
m_requestStartedSeconds = monotonicallyIncreasingTime();
// Save any CORS simple headers on the request here. If this request redirects cross-origin, we cancel the old request
// create a new one, and copy these headers.
const HTTPHeaderMap& headerMap = request.httpHeaderFields();
for (const auto& header : headerMap) {
if (FetchUtils::isSimpleHeader(header.key, header.value))
m_simpleRequestHeaders.add(header.key, header.value);
}
// If the fetch request will be handled by the ServiceWorker, the
// FetchRequestMode of the request must be FetchRequestModeCORS or
// FetchRequestModeCORSWithForcedPreflight. Otherwise the ServiceWorker can
// return a opaque response which is from the other origin site and the
// script in the page can read the content.
//
// We assume that ServiceWorker is skipped for sync requests and non-HTTP
// familiy requests by content/ code.
if (m_async && !request.skipServiceWorker() && request.url().protocolIsInHTTPFamily() && m_document.fetcher()->isControlledByServiceWorker()) {
ResourceRequest newRequest(request);
// FetchRequestMode should be set by the caller. But the expected value
// of FetchRequestMode is not speced yet except for XHR. So we set here.
// FIXME: When we support fetch API in document, this value should not
// be overridden here.
if (options.preflightPolicy == ForcePreflight)
newRequest.setFetchRequestMode(WebURLRequest::FetchRequestModeCORSWithForcedPreflight);
else
newRequest.setFetchRequestMode(WebURLRequest::FetchRequestModeCORS);
m_fallbackRequestForServiceWorker = adoptPtr(new ResourceRequest(request));
m_fallbackRequestForServiceWorker->setSkipServiceWorker(true);
loadRequest(newRequest, m_resourceLoaderOptions);
return;
}
dispatchInitialRequest(request);
}
void DocumentThreadableLoader::dispatchInitialRequest(const ResourceRequest& request)
{
if (m_sameOriginRequest || m_options.crossOriginRequestPolicy == AllowCrossOriginRequests) {
loadRequest(request, m_resourceLoaderOptions);
return;
}
ASSERT(m_options.crossOriginRequestPolicy == UseAccessControl);
makeCrossOriginAccessRequest(request);
}
void DocumentThreadableLoader::makeCrossOriginAccessRequest(const ResourceRequest& request)
{
ASSERT(m_options.crossOriginRequestPolicy == UseAccessControl);
// Cross-origin requests are only allowed certain registered schemes.
// We would catch this when checking response headers later, but there
// is no reason to send a request, preflighted or not, that's guaranteed
// to be denied.
if (!SchemeRegistry::shouldTreatURLSchemeAsCORSEnabled(request.url().protocol())) {
m_client->didFailAccessControlCheck(ResourceError(errorDomainBlinkInternal, 0, request.url().string(), "Cross origin requests are only supported for protocol schemes: " + SchemeRegistry::listOfCORSEnabledURLSchemes() + "."));
return;
}
// We use isSimpleOrForbiddenRequest() here since |request| may have been
// modified in the process of loading (not from the user's input). For
// example, referrer. We need to accept them. For security, we must reject
// forbidden headers/methods at the point we accept user's input. Not here.
if ((m_options.preflightPolicy == ConsiderPreflight && FetchUtils::isSimpleOrForbiddenRequest(request.httpMethod(), request.httpHeaderFields())) || m_options.preflightPolicy == PreventPreflight) {
ResourceRequest crossOriginRequest(request);
ResourceLoaderOptions crossOriginOptions(m_resourceLoaderOptions);
updateRequestForAccessControl(crossOriginRequest, securityOrigin(), effectiveAllowCredentials());
loadRequest(crossOriginRequest, crossOriginOptions);
} else {
m_simpleRequest = false;
OwnPtr<ResourceRequest> crossOriginRequest = adoptPtr(new ResourceRequest(request));
OwnPtr<ResourceLoaderOptions> crossOriginOptions = adoptPtr(new ResourceLoaderOptions(m_resourceLoaderOptions));
// Do not set the Origin header for preflight requests.
updateRequestForAccessControl(*crossOriginRequest, 0, effectiveAllowCredentials());
m_actualRequest = crossOriginRequest.release();
m_actualOptions = crossOriginOptions.release();
bool shouldForcePreflight = InspectorInstrumentation::shouldForceCORSPreflight(&m_document);
bool canSkipPreflight = CrossOriginPreflightResultCache::shared().canSkipPreflight(securityOrigin()->toString(), m_actualRequest->url(), effectiveAllowCredentials(), m_actualRequest->httpMethod(), m_actualRequest->httpHeaderFields());
if (canSkipPreflight && !shouldForcePreflight) {
loadActualRequest();
} else {
ResourceRequest preflightRequest = createAccessControlPreflightRequest(*m_actualRequest, securityOrigin());
// Create a ResourceLoaderOptions for preflight.
ResourceLoaderOptions preflightOptions = *m_actualOptions;
preflightOptions.allowCredentials = DoNotAllowStoredCredentials;
loadRequest(preflightRequest, preflightOptions);
}
}
}
DocumentThreadableLoader::~DocumentThreadableLoader()
{
}
void DocumentThreadableLoader::overrideTimeout(unsigned long timeoutMilliseconds)
{
ASSERT(m_async);
ASSERT(m_requestStartedSeconds > 0.0);
m_timeoutTimer.stop();
// At the time of this method's implementation, it is only ever called by
// XMLHttpRequest, when the timeout attribute is set after sending the
// request.
//
// The XHR request says to resolve the time relative to when the request
// was initially sent, however other uses of this method may need to
// behave differently, in which case this should be re-arranged somehow.
if (timeoutMilliseconds) {
double elapsedTime = monotonicallyIncreasingTime() - m_requestStartedSeconds;
double nextFire = timeoutMilliseconds / 1000.0;
double resolvedTime = std::max(nextFire - elapsedTime, 0.0);
m_timeoutTimer.startOneShot(resolvedTime, FROM_HERE);
}
}
void DocumentThreadableLoader::cancel()
{
cancelWithError(ResourceError());
}
void DocumentThreadableLoader::cancelWithError(const ResourceError& error)
{
RefPtr<DocumentThreadableLoader> protect(this);
// Cancel can re-enter and m_resource might be null here as a result.
if (m_client && resource()) {
ResourceError errorForCallback = error;
if (errorForCallback.isNull()) {
// FIXME: This error is sent to the client in didFail(), so it should not be an internal one. Use FrameLoaderClient::cancelledError() instead.
errorForCallback = ResourceError(errorDomainBlinkInternal, 0, resource()->url().string(), "Load cancelled");
errorForCallback.setIsCancellation(true);
}
m_client->didFail(errorForCallback);
}
clearResource();
m_client = 0;
m_requestStartedSeconds = 0.0;
}
void DocumentThreadableLoader::setDefersLoading(bool value)
{
if (resource())
resource()->setDefersLoading(value);
}
// In this method, we can clear |request| to tell content::WebURLLoaderImpl of
// Chromium not to follow the redirect. This works only when this method is
// called by RawResource::willSendRequest(). If called by
// RawResource::didAddClient(), clearing |request| won't be propagated
// to content::WebURLLoaderImpl. So, this loader must also get detached from
// the resource by calling clearResource().
void DocumentThreadableLoader::redirectReceived(Resource* resource, ResourceRequest& request, const ResourceResponse& redirectResponse)
{
ASSERT(m_client);
ASSERT_UNUSED(resource, resource == this->resource());
ASSERT(m_async);
RefPtr<DocumentThreadableLoader> protect(this);
if (!isAllowedByContentSecurityPolicy(request.url())) {
m_client->didFailRedirectCheck();
clearResource();
request = ResourceRequest();
m_requestStartedSeconds = 0.0;
return;
}
// Allow same origin requests to continue after allowing clients to audit the redirect.
if (isAllowedRedirect(request.url())) {
if (m_client->isDocumentThreadableLoaderClient())
static_cast<DocumentThreadableLoaderClient*>(m_client)->willFollowRedirect(request, redirectResponse);
return;
}
// When using access control, only simple cross origin requests are allowed to redirect. The new request URL must have a supported
// scheme and not contain the userinfo production. In addition, the redirect response must pass the access control check if the
// original request was not same-origin.
if (m_options.crossOriginRequestPolicy == UseAccessControl) {
InspectorInstrumentation::didReceiveCORSRedirectResponse(m_document.frame(), resource->identifier(), m_document.frame()->loader().documentLoader(), redirectResponse, 0);
bool allowRedirect = false;
String accessControlErrorDescription;
if (m_simpleRequest) {
allowRedirect = CrossOriginAccessControl::isLegalRedirectLocation(request.url(), accessControlErrorDescription)
&& (m_sameOriginRequest || passesAccessControlCheck(&m_document, redirectResponse, effectiveAllowCredentials(), securityOrigin(), accessControlErrorDescription));
} else {
accessControlErrorDescription = "The request was redirected to '"+ request.url().string() + "', which is disallowed for cross-origin requests that require preflight.";
}
if (allowRedirect) {
// FIXME: consider combining this with CORS redirect handling performed by
// CrossOriginAccessControl::handleRedirect().
clearResource();
RefPtr<SecurityOrigin> originalOrigin = SecurityOrigin::create(redirectResponse.url());
RefPtr<SecurityOrigin> requestOrigin = SecurityOrigin::create(request.url());
// If the original request wasn't same-origin, then if the request URL origin is not same origin with the original URL origin,
// set the source origin to a globally unique identifier. (If the original request was same-origin, the origin of the new request
// should be the original URL origin.)
if (!m_sameOriginRequest && !originalOrigin->isSameSchemeHostPort(requestOrigin.get()))
m_securityOrigin = SecurityOrigin::createUnique();
// Force any subsequent requests to use these checks.
m_sameOriginRequest = false;
// Since the request is no longer same-origin, if the user didn't request credentials in
// the first place, update our state so we neither request them nor expect they must be allowed.
if (m_resourceLoaderOptions.credentialsRequested == ClientDidNotRequestCredentials)
m_forceDoNotAllowStoredCredentials = true;
// Remove any headers that may have been added by the network layer that cause access control to fail.
request.clearHTTPReferrer();
request.clearHTTPOrigin();
request.clearHTTPUserAgent();
// Add any CORS simple request headers which we previously saved from the original request.
for (const auto& header : m_simpleRequestHeaders)
request.setHTTPHeaderField(header.key, header.value);
makeCrossOriginAccessRequest(request);
return;
}
ResourceError error(errorDomainBlinkInternal, 0, redirectResponse.url().string(), accessControlErrorDescription);
m_client->didFailAccessControlCheck(error);
} else {
m_client->didFailRedirectCheck();
}
clearResource();
request = ResourceRequest();
m_requestStartedSeconds = 0.0;
}
void DocumentThreadableLoader::dataSent(Resource* resource, unsigned long long bytesSent, unsigned long long totalBytesToBeSent)
{
ASSERT(m_client);
ASSERT_UNUSED(resource, resource == this->resource());
ASSERT(m_async);
m_client->didSendData(bytesSent, totalBytesToBeSent);
}
void DocumentThreadableLoader::dataDownloaded(Resource* resource, int dataLength)
{
ASSERT(m_client);
ASSERT_UNUSED(resource, resource == this->resource());
ASSERT(!m_actualRequest);
ASSERT(m_async);
m_client->didDownloadData(dataLength);
}
void DocumentThreadableLoader::responseReceived(Resource* resource, const ResourceResponse& response, PassOwnPtr<WebDataConsumerHandle> handle)
{
ASSERT_UNUSED(resource, resource == this->resource());
ASSERT(m_async);
handleResponse(resource->identifier(), response, handle);
}
void DocumentThreadableLoader::handlePreflightResponse(const ResourceResponse& response)
{
String accessControlErrorDescription;
if (!passesAccessControlCheck(&m_document, response, effectiveAllowCredentials(), securityOrigin(), accessControlErrorDescription)) {
handlePreflightFailure(response.url().string(), accessControlErrorDescription);
return;
}
if (!passesPreflightStatusCheck(response, accessControlErrorDescription)) {
handlePreflightFailure(response.url().string(), accessControlErrorDescription);
return;
}
OwnPtr<CrossOriginPreflightResultCacheItem> preflightResult = adoptPtr(new CrossOriginPreflightResultCacheItem(effectiveAllowCredentials()));
if (!preflightResult->parse(response, accessControlErrorDescription)
|| !preflightResult->allowsCrossOriginMethod(m_actualRequest->httpMethod(), accessControlErrorDescription)
|| !preflightResult->allowsCrossOriginHeaders(m_actualRequest->httpHeaderFields(), accessControlErrorDescription)) {
handlePreflightFailure(response.url().string(), accessControlErrorDescription);
return;
}
CrossOriginPreflightResultCache::shared().appendEntry(securityOrigin()->toString(), m_actualRequest->url(), preflightResult.release());
}
void DocumentThreadableLoader::reportResponseReceived(unsigned long identifier, const ResourceResponse& response)
{
DocumentLoader* loader = m_document.frame()->loader().documentLoader();
TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "ResourceReceiveResponse", "data", InspectorReceiveResponseEvent::data(identifier, m_document.frame(), response));
LocalFrame* frame = m_document.frame();
InspectorInstrumentation::didReceiveResourceResponse(frame, identifier, loader, response, resource() ? resource()->loader() : 0);
// It is essential that inspector gets resource response BEFORE console.
frame->console().reportResourceResponseReceived(loader, identifier, response);
}
void DocumentThreadableLoader::handleResponse(unsigned long identifier, const ResourceResponse& response, PassOwnPtr<WebDataConsumerHandle> handle)
{
ASSERT(m_client);
if (m_actualRequest) {
reportResponseReceived(identifier, response);
handlePreflightResponse(response);
return;
}
if (response.wasFetchedViaServiceWorker()) {
ASSERT(m_fallbackRequestForServiceWorker);
if (response.wasFallbackRequiredByServiceWorker()) {
loadFallbackRequestForServiceWorker();
return;
}
m_fallbackRequestForServiceWorker = nullptr;
m_client->didReceiveResponse(identifier, response, handle);
return;
}
ASSERT(!m_fallbackRequestForServiceWorker);
if (!m_sameOriginRequest && m_options.crossOriginRequestPolicy == UseAccessControl) {
String accessControlErrorDescription;
if (!passesAccessControlCheck(&m_document, response, effectiveAllowCredentials(), securityOrigin(), accessControlErrorDescription)) {
reportResponseReceived(identifier, response);
m_client->didFailAccessControlCheck(ResourceError(errorDomainBlinkInternal, 0, response.url().string(), accessControlErrorDescription));
return;
}
}
m_client->didReceiveResponse(identifier, response, handle);
}
void DocumentThreadableLoader::dataReceived(Resource* resource, const char* data, unsigned dataLength)
{
ASSERT_UNUSED(resource, resource == this->resource());
ASSERT(m_async);
handleReceivedData(data, dataLength);
}
void DocumentThreadableLoader::handleReceivedData(const char* data, unsigned dataLength)
{
ASSERT(m_client);
// Preflight data should be invisible to clients.
if (m_actualRequest)
return;
ASSERT(!m_fallbackRequestForServiceWorker);
m_client->didReceiveData(data, dataLength);
}
void DocumentThreadableLoader::notifyFinished(Resource* resource)
{
ASSERT(m_client);
ASSERT(resource == this->resource());
ASSERT(m_async);
m_timeoutTimer.stop();
if (resource->errorOccurred())
m_client->didFail(resource->resourceError());
else
handleSuccessfulFinish(resource->identifier(), resource->loadFinishTime());
}
void DocumentThreadableLoader::handleSuccessfulFinish(unsigned long identifier, double finishTime)
{
ASSERT(!m_fallbackRequestForServiceWorker);
if (m_actualRequest) {
ASSERT(!m_sameOriginRequest);
ASSERT(m_options.crossOriginRequestPolicy == UseAccessControl);
loadActualRequest();
} else {
// FIXME: Should prevent timeout from being overridden after finished loading, without
// resetting m_requestStartedSeconds to 0.0
m_client->didFinishLoading(identifier, finishTime);
}
}
void DocumentThreadableLoader::didTimeout(Timer<DocumentThreadableLoader>* timer)
{
ASSERT_UNUSED(timer, timer == &m_timeoutTimer);
// Using values from net/base/net_error_list.h ERR_TIMED_OUT,
// Same as existing FIXME above - this error should be coming from FrameLoaderClient to be identifiable.
static const int timeoutError = -7;
ResourceError error("net", timeoutError, resource()->url(), String());
error.setIsTimeout(true);
cancelWithError(error);
}
void DocumentThreadableLoader::loadFallbackRequestForServiceWorker()
{
clearResource();
OwnPtr<ResourceRequest> fallbackRequest(m_fallbackRequestForServiceWorker.release());
dispatchInitialRequest(*fallbackRequest);
}
void DocumentThreadableLoader::loadActualRequest()
{
OwnPtr<ResourceRequest> actualRequest;
actualRequest.swap(m_actualRequest);
OwnPtr<ResourceLoaderOptions> actualOptions;
actualOptions.swap(m_actualOptions);
actualRequest->setHTTPOrigin(securityOrigin()->toAtomicString());
clearResource();
loadRequest(*actualRequest, *actualOptions);
}
void DocumentThreadableLoader::handlePreflightFailure(const String& url, const String& errorDescription)
{
ResourceError error(errorDomainBlinkInternal, 0, url, errorDescription);
// Prevent handleSuccessfulFinish() from bypassing access check.
m_actualRequest = nullptr;
// FIXME: Should prevent timeout from being overridden after preflight failure, without
// resetting m_requestStartedSeconds to 0.0
m_client->didFailAccessControlCheck(error);
}
void DocumentThreadableLoader::loadRequest(const ResourceRequest& request, ResourceLoaderOptions resourceLoaderOptions)
{
// Any credential should have been removed from the cross-site requests.
const KURL& requestURL = request.url();
ASSERT(m_sameOriginRequest || requestURL.user().isEmpty());
ASSERT(m_sameOriginRequest || requestURL.pass().isEmpty());
// Update resourceLoaderOptions with enforced values.
if (m_forceDoNotAllowStoredCredentials)
resourceLoaderOptions.allowCredentials = DoNotAllowStoredCredentials;
resourceLoaderOptions.securityOrigin = m_securityOrigin;
if (m_async) {
if (m_actualRequest)
resourceLoaderOptions.dataBufferingPolicy = BufferData;
if (m_options.timeoutMilliseconds > 0)
m_timeoutTimer.startOneShot(m_options.timeoutMilliseconds / 1000.0, FROM_HERE);
FetchRequest newRequest(request, m_options.initiator, resourceLoaderOptions);
if (m_options.crossOriginRequestPolicy == AllowCrossOriginRequests)
newRequest.setOriginRestriction(FetchRequest::NoOriginRestriction);
ASSERT(!resource());
if (request.requestContext() == blink::WebURLRequest::RequestContextVideo || request.requestContext() == blink::WebURLRequest::RequestContextAudio)
setResource(m_document.fetcher()->fetchMedia(newRequest));
else
setResource(m_document.fetcher()->fetchRawResource(newRequest));
if (resource() && resource()->loader()) {
unsigned long identifier = resource()->identifier();
InspectorInstrumentation::documentThreadableLoaderStartedLoadingForClient(&m_document, identifier, m_client);
}
return;
}
FetchRequest fetchRequest(request, m_options.initiator, resourceLoaderOptions);
if (m_options.crossOriginRequestPolicy == AllowCrossOriginRequests)
fetchRequest.setOriginRestriction(FetchRequest::NoOriginRestriction);
ResourcePtr<Resource> resource = m_document.fetcher()->fetchSynchronously(fetchRequest);
ResourceResponse response = resource ? resource->response() : ResourceResponse();
unsigned long identifier = resource ? resource->identifier() : std::numeric_limits<unsigned long>::max();
ResourceError error = resource ? resource->resourceError() : ResourceError();
InspectorInstrumentation::documentThreadableLoaderStartedLoadingForClient(&m_document, identifier, m_client);
if (!resource) {
m_client->didFail(error);
return;
}
// No exception for file:/// resources, see <rdar://problem/4962298>.
// Also, if we have an HTTP response, then it wasn't a network error in fact.
if (!error.isNull() && !requestURL.isLocalFile() && response.httpStatusCode() <= 0) {
m_client->didFail(error);
return;
}
// FIXME: A synchronous request does not tell us whether a redirect happened or not, so we guess by comparing the
// request and response URLs. This isn't a perfect test though, since a server can serve a redirect to the same URL that was
// requested. Also comparing the request and response URLs as strings will fail if the requestURL still has its credentials.
if (requestURL != response.url() && (!isAllowedByContentSecurityPolicy(response.url()) || !isAllowedRedirect(response.url()))) {
m_client->didFailRedirectCheck();
return;
}
handleResponse(identifier, response, nullptr);
SharedBuffer* data = resource->resourceBuffer();
if (data)
handleReceivedData(data->data(), data->size());
handleSuccessfulFinish(identifier, 0.0);
}
bool DocumentThreadableLoader::isAllowedRedirect(const KURL& url) const
{
if (m_options.crossOriginRequestPolicy == AllowCrossOriginRequests)
return true;
return m_sameOriginRequest && securityOrigin()->canRequest(url);
}
bool DocumentThreadableLoader::isAllowedByContentSecurityPolicy(const KURL& url) const
{
if (m_options.contentSecurityPolicyEnforcement != EnforceConnectSrcDirective)
return true;
return m_document.contentSecurityPolicy()->allowConnectToSource(url);
}
StoredCredentials DocumentThreadableLoader::effectiveAllowCredentials() const
{
if (m_forceDoNotAllowStoredCredentials)
return DoNotAllowStoredCredentials;
return m_resourceLoaderOptions.allowCredentials;
}
SecurityOrigin* DocumentThreadableLoader::securityOrigin() const
{
return m_securityOrigin ? m_securityOrigin.get() : m_document.securityOrigin();
}
} // namespace blink
|