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 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683
|
/*
* Copyright (C) 2016 Canon Inc.
* Copyright (C) 2020 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted, provided that the following conditions
* are required to be met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. 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.
* 3. Neither the name of Canon 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 CANON INC. AND ITS 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 CANON INC. AND ITS 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 "FetchResponse.h"
#include "FetchRequest.h"
#include "FetchResponseBodyLoader.h"
#include "HTTPParsers.h"
#include "InspectorInstrumentation.h"
#include "JSBlob.h"
#include "MIMETypeRegistry.h"
#include "ReadableStreamSink.h"
#include "ResourceError.h"
#include "ScriptExecutionContext.h"
#include <JavaScriptCore/JSONObject.h>
#include <wtf/text/StringConcatenateNumbers.h>
namespace WebCore {
// https://fetch.spec.whatwg.org/#null-body-status
static inline bool isNullBodyStatus(int status)
{
return status == 101 || status == 204 || status == 205 || status == 304;
}
Ref<FetchResponse> FetchResponse::create(ScriptExecutionContext* context, std::optional<FetchBody>&& body, FetchHeaders::Guard guard, ResourceResponse&& response)
{
bool isSynthetic = response.type() == ResourceResponse::Type::Default || response.type() == ResourceResponse::Type::Error;
bool isOpaque = response.tainting() == ResourceResponse::Tainting::Opaque;
auto headers = isOpaque ? FetchHeaders::create(guard) : FetchHeaders::create(guard, HTTPHeaderMap { response.httpHeaderFields() });
auto fetchResponse = adoptRef(*new FetchResponse(context, WTFMove(body), WTFMove(headers), WTFMove(response)));
fetchResponse->suspendIfNeeded();
if (!isSynthetic)
fetchResponse->m_filteredResponse = ResourceResponseBase::filter(fetchResponse->m_internalResponse, ResourceResponse::PerformExposeAllHeadersCheck::Yes);
if (isOpaque)
fetchResponse->setBodyAsOpaque();
return fetchResponse;
}
ExceptionOr<Ref<FetchResponse>> FetchResponse::create(ScriptExecutionContext& context, std::optional<FetchBodyWithType>&& bodyWithType, Init&& init)
{
// https://fetch.spec.whatwg.org/#initialize-a-response
// 1. If init["status"] is not in the range 200 to 599, inclusive, then throw a RangeError.
if (init.status < 200 || init.status > 599)
return Exception { RangeError, "Status must be between 200 and 599"_s };
// 2. If init["statusText"] does not match the reason-phrase token production, then throw a TypeError.
if (!isValidReasonPhrase(init.statusText))
return Exception { TypeError, "Status text must be a valid reason-phrase."_s };
// Both uses of "initialize a response" (the Response constructor and Response.json) create the
// Response object with the "response" header guard.
auto headers = FetchHeaders::create(FetchHeaders::Guard::Response);
// 5. If init["headers"] exists, then fill response’s headers with init["headers"].
if (init.headers) {
auto result = headers->fill(*init.headers);
if (result.hasException())
return result.releaseException();
}
std::optional<FetchBody> body;
// 6. If body was given, then:
if (bodyWithType) {
// 6.1 If response’s status is a null body status, then throw a TypeError.
// (NOTE: 101 and 103 are included in null body status due to their use elsewhere. It does not affect this step.)
if (isNullBodyStatus(init.status))
return Exception { TypeError, "Response cannot have a body with the given status."_s };
// 6.2 Set response’s body to body’s body.
body = WTFMove(bodyWithType->body);
// 6.3 If body’s type is non-null and response’s header list does not contain `Content-Type`, then append
// (`Content-Type`, body’s type) to response’s header list.
if (!bodyWithType->type.isNull() && !headers->fastHas(HTTPHeaderName::ContentType))
headers->fastSet(HTTPHeaderName::ContentType, bodyWithType->type);
}
auto contentType = headers->fastGet(HTTPHeaderName::ContentType);
auto r = adoptRef(*new FetchResponse(&context, WTFMove(body), WTFMove(headers), { }));
r->suspendIfNeeded();
AtomString mimeType { extractMIMETypeFromMediaType(contentType) };
r->m_internalResponse.setMimeType(mimeType.isEmpty() ? AtomString { defaultMIMEType() } : mimeType);
r->m_internalResponse.setTextEncodingName(extractCharsetFromMediaType(contentType).toAtomString());
// 3. Set response’s response’s status to init["status"].
r->m_internalResponse.setHTTPStatusCode(init.status);
// 4. Set response’s response’s status message to init["statusText"].
r->m_internalResponse.setHTTPStatusText(init.statusText);
return r;
}
ExceptionOr<Ref<FetchResponse>> FetchResponse::create(ScriptExecutionContext& context, std::optional<FetchBody::Init>&& body, Init&& init)
{
std::optional<FetchBodyWithType> bodyWithType;
if (body) {
String type;
auto result = FetchBody::extract(WTFMove(*body), type);
if (result.hasException())
return result.releaseException();
bodyWithType = { result.releaseReturnValue(), WTFMove(type) };
}
return FetchResponse::create(context, WTFMove(bodyWithType), WTFMove(init));
}
Ref<FetchResponse> FetchResponse::error(ScriptExecutionContext& context)
{
auto response = adoptRef(*new FetchResponse(&context, { }, FetchHeaders::create(FetchHeaders::Guard::Immutable), { }));
response->suspendIfNeeded();
response->m_internalResponse.setType(Type::Error);
return response;
}
ExceptionOr<Ref<FetchResponse>> FetchResponse::redirect(ScriptExecutionContext& context, const String& url, int status)
{
// FIXME: Tighten the URL parsing algorithm according https://url.spec.whatwg.org/#concept-url-parser.
URL requestURL = context.completeURL(url);
if (!requestURL.isValid())
return Exception { TypeError, makeString("Redirection URL '", requestURL.string(), "' is invalid") };
if (requestURL.hasCredentials())
return Exception { TypeError, "Redirection URL contains credentials"_s };
if (!ResourceResponse::isRedirectionStatusCode(status))
return Exception { RangeError, makeString("Status code ", status, "is not a redirection status code") };
auto redirectResponse = adoptRef(*new FetchResponse(&context, { }, FetchHeaders::create(FetchHeaders::Guard::Immutable), { }));
redirectResponse->suspendIfNeeded();
redirectResponse->m_internalResponse.setHTTPStatusCode(status);
redirectResponse->m_internalResponse.setHTTPHeaderField(HTTPHeaderName::Location, requestURL.string());
redirectResponse->m_headers->fastSet(HTTPHeaderName::Location, requestURL.string());
return redirectResponse;
}
ExceptionOr<Ref<FetchResponse>> FetchResponse::jsonForBindings(ScriptExecutionContext& context, JSC::JSValue data, Init&& init)
{
auto* globalObject = context.globalObject();
if (!globalObject)
return Exception { InvalidStateError, "Context is stopped"_s };
String jsonString = JSC::JSONStringify(globalObject, data, 0);
if (jsonString.isNull())
return Exception { TypeError, "Value doesn't have a JSON representation"_s };
FetchBodyWithType body { FetchBody(WTFMove(jsonString)), "application/json"_s };
return FetchResponse::create(context, WTFMove(body), WTFMove(init));
}
FetchResponse::FetchResponse(ScriptExecutionContext* context, std::optional<FetchBody>&& body, Ref<FetchHeaders>&& headers, ResourceResponse&& response)
: FetchBodyOwner(context, WTFMove(body), WTFMove(headers))
, m_internalResponse(WTFMove(response))
{
}
ExceptionOr<Ref<FetchResponse>> FetchResponse::clone()
{
if (isDisturbedOrLocked())
return Exception { TypeError, "Body is disturbed or locked"_s };
// If loading, let's create a stream so that data is teed on both clones.
if (isLoading() && !m_readableStreamSource) {
auto* context = scriptExecutionContext();
auto* globalObject = context ? context->globalObject() : nullptr;
if (!globalObject)
return Exception { InvalidStateError, "Context is stopped"_s };
auto voidOrException = createReadableStream(*globalObject);
if (UNLIKELY(voidOrException.hasException()))
return voidOrException.releaseException();
}
// Synthetic responses do not store headers in m_internalResponse.
if (m_internalResponse.type() == ResourceResponse::Type::Default)
m_internalResponse.setHTTPHeaderFields(HTTPHeaderMap { headers().internalHeaders() });
auto clone = FetchResponse::create(scriptExecutionContext(), std::nullopt, headers().guard(), ResourceResponse { m_internalResponse });
clone->cloneBody(*this);
clone->m_headers = FetchHeaders::create(headers());
clone->m_opaqueLoadIdentifier = m_opaqueLoadIdentifier;
clone->m_bodySizeWithPadding = m_bodySizeWithPadding;
return clone;
}
void FetchResponse::addAbortSteps(Ref<AbortSignal>&& signal)
{
m_abortSignal = WTFMove(signal);
m_abortSignal->addAlgorithm([this, weakThis = WeakPtr { *this }](JSC::JSValue) {
RefPtr protectedThis = weakThis.get();
if (!protectedThis)
return;
m_abortSignal = nullptr;
setLoadingError(Exception { AbortError, "Fetch is aborted"_s });
if (m_loader) {
if (auto callback = m_loader->takeNotificationCallback())
callback(Exception { AbortError, "Fetch is aborted"_s });
if (auto callback = m_loader->takeConsumeDataCallback())
callback(Exception { AbortError, "Fetch is aborted"_s });
}
if (m_readableStreamSource) {
if (!m_readableStreamSource->isCancelling())
m_readableStreamSource->error(*loadingException());
m_readableStreamSource = nullptr;
}
if (m_body)
m_body->loadingFailed(*loadingException());
if (auto loader = WTFMove(m_loader))
loader->stop();
if (auto bodyLoader = WTFMove(m_bodyLoader))
bodyLoader->stop();
});
}
Ref<FetchResponse> FetchResponse::createFetchResponse(ScriptExecutionContext& context, FetchRequest& request, NotificationCallback&& responseCallback)
{
auto response = adoptRef(*new FetchResponse(&context, FetchBody { }, FetchHeaders::create(FetchHeaders::Guard::Immutable), { }));
response->suspendIfNeeded();
response->body().consumer().setAsLoading();
response->addAbortSteps(request.signal());
response->m_loader = makeUnique<Loader>(response.get(), WTFMove(responseCallback));
return response;
}
void FetchResponse::fetch(ScriptExecutionContext& context, FetchRequest& request, NotificationCallback&& responseCallback, const String& initiator)
{
if (request.isReadableStreamBody()) {
responseCallback(Exception { NotSupportedError, "ReadableStream uploading is not supported"_s });
return;
}
auto response = createFetchResponse(context, request, WTFMove(responseCallback));
response->startLoader(context, request, initiator);
}
void FetchResponse::startLoader(ScriptExecutionContext& context, FetchRequest& request, const String& initiator)
{
InspectorInstrumentation::willFetch(context, request.url().string());
if (m_loader && !m_loader->start(context, request, initiator))
m_loader = nullptr;
}
const String& FetchResponse::url() const
{
if (m_responseURL.isNull()) {
URL url = filteredResponse().url();
url.removeFragmentIdentifier();
m_responseURL = url.string();
}
return m_responseURL;
}
const ResourceResponse& FetchResponse::filteredResponse() const
{
if (m_filteredResponse)
return m_filteredResponse.value();
return m_internalResponse;
}
void FetchResponse::Loader::didSucceed(const NetworkLoadMetrics& metrics)
{
ASSERT(m_response.hasPendingActivity());
m_response.didSucceed(metrics);
if (m_loader->isStarted()) {
Ref<FetchResponse> protector(m_response);
m_response.m_loader = nullptr;
}
}
void FetchResponse::Loader::didFail(const ResourceError& error)
{
ASSERT(m_response.hasPendingActivity());
m_response.setLoadingError(ResourceError { error });
m_response.processReceivedError();
// Check whether didFail is called as part of FetchLoader::start.
if (m_loader && m_loader->isStarted()) {
Ref<FetchResponse> protector(m_response);
m_response.m_loader = nullptr;
}
}
static std::atomic<uint64_t> nextOpaqueLoadIdentifier;
void FetchResponse::setReceivedInternalResponse(const ResourceResponse& resourceResponse, FetchOptions::Credentials credentials)
{
if (m_hasInitializedInternalResponse)
return;
m_hasInitializedInternalResponse = true;
auto performCheck = credentials == FetchOptions::Credentials::Include ? ResourceResponse::PerformExposeAllHeadersCheck::No : ResourceResponse::PerformExposeAllHeadersCheck::Yes;
m_filteredResponse = ResourceResponseBase::filter(resourceResponse, performCheck);
m_internalResponse = resourceResponse;
m_internalResponse.setType(m_filteredResponse->type());
if (resourceResponse.tainting() == ResourceResponse::Tainting::Opaque) {
m_opaqueLoadIdentifier = ++nextOpaqueLoadIdentifier;
setBodyAsOpaque();
}
m_headers->filterAndFill(m_filteredResponse->httpHeaderFields(), FetchHeaders::Guard::Response);
}
FetchResponse::Loader::Loader(FetchResponse& response, NotificationCallback&& responseCallback)
: m_response(response)
, m_responseCallback(WTFMove(responseCallback))
, m_pendingActivity(m_response.makePendingActivity(m_response))
{
}
FetchResponse::Loader::~Loader()
{
}
void FetchResponse::Loader::didReceiveResponse(const ResourceResponse& resourceResponse)
{
m_response.setReceivedInternalResponse(resourceResponse, m_credentials);
if (auto responseCallback = WTFMove(m_responseCallback))
responseCallback(Ref { m_response });
}
void FetchResponse::Loader::didReceiveData(const SharedBuffer& buffer)
{
ASSERT(m_response.m_readableStreamSource || m_consumeDataCallback);
if (m_consumeDataCallback) {
auto chunk = buffer.dataAsSpanForContiguousData();
m_consumeDataCallback(&chunk);
return;
}
auto& source = *m_response.m_readableStreamSource;
if (!source.isPulling()) {
m_response.body().consumer().append(buffer);
return;
}
if (m_response.body().consumer().hasData() && !source.enqueue(m_response.body().consumer().takeAsArrayBuffer())) {
stop();
return;
}
if (!source.enqueue(buffer.tryCreateArrayBuffer())) {
stop();
return;
}
source.resolvePullPromise();
}
bool FetchResponse::Loader::start(ScriptExecutionContext& context, const FetchRequest& request, const String& initiator)
{
m_credentials = request.fetchOptions().credentials;
m_loader = makeUnique<FetchLoader>(*this, &m_response.m_body->consumer());
m_loader->start(context, request, initiator);
if (!m_loader->isStarted())
return false;
if (m_shouldStartStreaming) {
auto data = m_loader->startStreaming();
ASSERT_UNUSED(data, !data);
}
return true;
}
void FetchResponse::Loader::stop()
{
m_responseCallback = { };
if (m_loader)
m_loader->stop();
}
void FetchResponse::Loader::consumeDataByChunk(ConsumeDataByChunkCallback&& consumeDataCallback)
{
ASSERT(!m_consumeDataCallback);
m_consumeDataCallback = WTFMove(consumeDataCallback);
auto data = startStreaming();
if (!data)
return;
auto contiguousBuffer = data->makeContiguous();
auto chunk = contiguousBuffer->dataAsSpanForContiguousData();
m_consumeDataCallback(&chunk);
}
FetchResponse::ResponseData FetchResponse::consumeBody()
{
ASSERT(!isBodyReceivedByChunk());
if (isBodyNull())
return nullptr;
ASSERT(!m_isDisturbed);
m_isDisturbed = true;
return body().take();
}
void FetchResponse::markAsUsedForPreload()
{
ASSERT(!m_isDisturbed);
m_isDisturbed = true;
m_isUsedForPreload = true;
}
void FetchResponse::consumeBodyReceivedByChunk(ConsumeDataByChunkCallback&& callback)
{
ASSERT(isBodyReceivedByChunk());
ASSERT(!isDisturbed());
m_isDisturbed = true;
if (hasReadableStreamBody()) {
m_body->consumer().extract(*m_body->readableStream(), WTFMove(callback));
return;
}
ASSERT(isLoading());
m_loader->consumeDataByChunk(WTFMove(callback));
}
void FetchResponse::setBodyData(ResponseData&& data, uint64_t bodySizeWithPadding)
{
m_bodySizeWithPadding = bodySizeWithPadding;
WTF::switchOn(data,
[this](Ref<FormData>& formData) {
if (isBodyNull())
setBody({ });
body().setAsFormData(WTFMove(formData));
},
[this](Ref<SharedBuffer>& buffer) {
if (isBodyNull())
setBody({ });
body().consumer().setData(WTFMove(buffer));
},
[](std::nullptr_t&) {
}
);
}
void FetchResponse::consumeChunk(Ref<JSC::Uint8Array>&& chunk)
{
body().consumer().append(SharedBuffer::create(chunk->data(), chunk->byteLength()));
}
void FetchResponse::consumeBodyAsStream()
{
ASSERT(m_readableStreamSource);
if (!isLoading()) {
FetchBodyOwner::consumeBodyAsStream();
return;
}
ASSERT(m_loader);
auto data = m_loader->startStreaming();
if (data) {
if (!m_readableStreamSource->enqueue(data->tryCreateArrayBuffer())) {
stop();
return;
}
m_readableStreamSource->resolvePullPromise();
}
}
void FetchResponse::closeStream()
{
ASSERT(m_readableStreamSource);
m_readableStreamSource->close();
m_readableStreamSource = nullptr;
}
void FetchResponse::cancelStream()
{
if (isAllowedToRunScript() && hasReadableStreamBody()) {
body().readableStream()->cancel(Exception { AbortError, "load is cancelled"_s });
return;
}
cancel();
}
void FetchResponse::feedStream()
{
ASSERT(m_readableStreamSource);
bool shouldCloseStream = !m_loader;
if (body().consumer().hasData()) {
if (!m_readableStreamSource->enqueue(body().consumer().takeAsArrayBuffer())) {
stop();
return;
}
if (!shouldCloseStream) {
m_readableStreamSource->resolvePullPromise();
return;
}
} else if (!shouldCloseStream)
return;
closeStream();
}
RefPtr<FragmentedSharedBuffer> FetchResponse::Loader::startStreaming()
{
if (!m_loader) {
m_shouldStartStreaming = true;
return nullptr;
}
return m_loader->startStreaming();
}
void FetchResponse::cancel()
{
m_isDisturbed = true;
stop();
}
void FetchResponse::stop()
{
RefPtr<FetchResponse> protectedThis(this);
FetchBodyOwner::stop();
if (auto loader = WTFMove(m_loader))
loader->stop();
if (auto bodyLoader = WTFMove(m_bodyLoader))
bodyLoader->stop();
}
const char* FetchResponse::activeDOMObjectName() const
{
return "Response";
}
void FetchResponse::loadBody()
{
if (m_bodyLoader)
m_bodyLoader->start();
}
void FetchResponse::setBodyLoader(UniqueRef<FetchResponseBodyLoader>&& bodyLoader)
{
ASSERT(!m_loader);
ASSERT(isBodyNull());
setBody({ });
body().consumer().setAsLoading();
m_bodyLoader = bodyLoader.moveToUniquePtr();
}
void FetchResponse::receivedError(Exception&& exception)
{
setLoadingError(WTFMove(exception));
processReceivedError();
}
void FetchResponse::receivedError(ResourceError&& error)
{
setLoadingError(WTFMove(error));
processReceivedError();
}
void FetchResponse::processReceivedError()
{
if (m_loader) {
if (auto callback = m_loader->takeNotificationCallback())
callback(*loadingException());
else if (auto callback = m_loader->takeConsumeDataCallback())
callback(*loadingException());
}
if (m_readableStreamSource) {
if (!m_readableStreamSource->isCancelling())
m_readableStreamSource->error(*loadingException());
m_readableStreamSource = nullptr;
}
if (m_body)
m_body->loadingFailed(*loadingException());
}
void FetchResponse::didSucceed(const NetworkLoadMetrics& metrics)
{
setNetworkLoadMetrics(metrics);
if (m_loader) {
if (auto consumeDataCallback = m_loader->takeConsumeDataCallback())
consumeDataCallback(nullptr);
}
if (m_readableStreamSource) {
if (body().consumer().hasData())
m_readableStreamSource->enqueue(body().consumer().takeAsArrayBuffer());
closeStream();
}
if (m_body)
m_body->loadingSucceeded(contentType());
}
void FetchResponse::receivedData(Ref<SharedBuffer>&& buffer)
{
body().consumer().append(buffer.get());
}
ResourceResponse FetchResponse::resourceResponse() const
{
auto response = m_internalResponse;
if (headers().guard() != FetchHeaders::Guard::Immutable) {
// FIXME: Add a setHTTPHeaderFields on ResourceResponseBase.
for (auto& header : headers().internalHeaders()) {
if (header.keyAsHTTPHeaderName)
response.setHTTPHeaderField(*header.keyAsHTTPHeaderName, header.value);
else
response.setUncommonHTTPHeaderField(header.key, header.value);
}
}
return response;
}
bool FetchResponse::isCORSSameOrigin() const
{
// https://html.spec.whatwg.org/#cors-same-origin
// A response whose type is "basic", "cors", or "default" is CORS-same-origin.
switch (type()) {
case ResourceResponse::Type::Basic:
case ResourceResponse::Type::Cors:
case ResourceResponse::Type::Default:
return true;
default:
return false;
}
}
bool FetchResponse::hasWasmMIMEType() const
{
return MIMETypeRegistry::isSupportedWebAssemblyMIMEType(m_headers->fastGet(HTTPHeaderName::ContentType));
}
} // namespace WebCore
|