/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* vim: set ts=8 sts=2 et sw=2 tw=80: */ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// We can't just pass nullptr for our null-valued Nullable, because the // fetch::ResponseBodyInit is a non-temporary type due to the MOZ_RAII // annotations on some of its members.
Nullable<fetch::ResponseBodyInit> body;
ResponseInit init;
init.mStatus = aStatus;
init.mStatusText.AssignASCII("");
RefPtr<Response> r = Response::Constructor(aGlobal, body, init, aRv); if (NS_WARN_IF(aRv.Failed())) { return nullptr;
}
// Grab a valid channel info from the global so this response is 'valid' for // interception. if (NS_IsMainThread()) {
ChannelInfo info;
nsCOMPtr<nsPIDOMWindowInner> window = do_QueryInterface(global); if (window) {
Document* doc = window->GetExtantDoc();
MOZ_ASSERT(doc);
info.InitFromDocument(doc);
/** * The channel info is left uninitialized if neither the above `if` nor * `else if` statements are executed; this could be because we're in a * WebExtensions content script, where the global (i.e. `global`) is a * wrapper, and the principal is an expanded principal. In this case, * as far as I can tell, there's no way to get the security info, but we'd * like the `Response` to be successfully constructed.
*/
} else {
WorkerPrivate* worker = GetCurrentThreadWorkerPrivate();
MOZ_ASSERT(worker);
internalResponse->InitChannelInfo(worker->GetChannelInfo());
principalInfo =
MakeUnique<mozilla::ipc::PrincipalInfo>(worker->GetPrincipalInfo());
}
RefPtr<Response> r = new Response(global, internalResponse.clonePtr(), nullptr);
if (aInit.mHeaders.WasPassed()) {
internalResponse->Headers()->Clear();
// Instead of using Fill, create an object to allow the constructor to // unwrap the HeadersInit.
RefPtr<Headers> headers =
Headers::Create(global, aInit.mHeaders.Value(), aRv); if (aRv.Failed()) { return nullptr;
}
internalResponse->Headers()->Fill(*headers->GetInternalHeaders(), aRv); if (NS_WARN_IF(aRv.Failed())) { return nullptr;
}
}
if (!aBody.IsNull()) { if (aInit.mStatus == 204 || aInit.mStatus == 205 || aInit.mStatus == 304) {
aRv.ThrowTypeError("Response body is given with a null body status."); return nullptr;
}
if (readableStream.Locked() || readableStream.Disturbed()) {
aRv.ThrowTypeError<MSG_FETCH_BODY_CONSUMED_ERROR>(); return nullptr;
}
r->SetReadableStreamBody(cx, &readableStream);
// If this is a DOM generated ReadableStream, we can extract the // inputStream directly. if (nsIInputStream* underlyingSource =
readableStream.MaybeGetInputStreamIfUnread()) {
bodyStream = underlyingSource;
} else { // If this is a JS-created ReadableStream, let's create a // FetchStreamReader.
aRv = FetchStreamReader::Create(aGlobal.Context(), global,
getter_AddRefs(r->mFetchStreamReader),
getter_AddRefs(bodyStream)); if (NS_WARN_IF(aRv.Failed())) { return nullptr;
}
}
} else {
uint64_t size = 0;
aRv = ExtractByteStreamFromBody(body, getter_AddRefs(bodyStream),
contentTypeWithCharset, size); if (NS_WARN_IF(aRv.Failed())) { return nullptr;
}
if (!aDefaultContentType.IsVoid()) {
contentTypeWithCharset = aDefaultContentType;
}
SafeRefPtr<InternalResponse> ir =
mInternalResponse->Clone(body ? InternalResponse::eDontCloneInputStream
: InternalResponse::eCloneInputStream);
RefPtr<Response> response = new Response(mOwner, ir.clonePtr(), GetSignalImpl());
if (body) { // Maybe we have a body, but we receive null from MaybeTeeReadableStreamBody // if this body is a native stream. In this case the InternalResponse will // have a clone of the native body and the ReadableStream will be created // lazily if needed.
response->SetReadableStreamBody(aCx, body);
response->mFetchStreamReader = streamReader;
ir->SetBody(inputStream, InternalResponse::UNKNOWN_BODY_SIZE);
}
SafeRefPtr<InternalResponse> ir = clone->Unfiltered();
RefPtr<Response> ref = new Response(mOwner, ir.clonePtr(), GetSignalImpl());
if (body) { // Maybe we have a body, but we receive null from MaybeTeeReadableStreamBody // if this body is a native stream. In this case the InternalResponse will // have a clone of the native body and the ReadableStream will be created // lazily if needed.
ref->SetReadableStreamBody(aCx, body);
ref->mFetchStreamReader = streamReader;
ir->SetBody(inputStream, InternalResponse::UNKNOWN_BODY_SIZE);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.