This commit is contained in:
Brian Smith 2023-09-28 14:05:55 -05:00 committed by roytam1
commit a8a75090c0
14 changed files with 413 additions and 72 deletions

View file

@ -12,12 +12,15 @@
#include "mozilla/ErrorResult.h"
#include "mozilla/dom/FetchBinding.h"
#include "mozilla/dom/ResponseBinding.h"
#include "mozilla/dom/Headers.h"
#include "mozilla/dom/Promise.h"
#include "mozilla/dom/URL.h"
#include "nsDOMString.h"
#include "BodyExtractor.h"
#include "FetchStream.h"
#include "InternalResponse.h"
#include "WorkerPrivate.h"
@ -130,7 +133,7 @@ Response::Redirect(const GlobalObject& aGlobal, const nsAString& aUrl,
return nullptr;
}
Optional<Nullable<ArrayBufferOrArrayBufferViewOrBlobOrFormDataOrUSVStringOrURLSearchParams>> body;
Optional<fetch::ResponseBodyInit> body;
ResponseInit init;
init.mStatus = aStatus;
RefPtr<Response> r = Response::Constructor(aGlobal, body, init, aRv);
@ -151,7 +154,7 @@ Response::Redirect(const GlobalObject& aGlobal, const nsAString& aUrl,
/*static*/ already_AddRefed<Response>
Response::Constructor(const GlobalObject& aGlobal,
const Optional<Nullable<ArrayBufferOrArrayBufferViewOrBlobOrFormDataOrUSVStringOrURLSearchParams>>& aBody,
const Optional<fetch::ResponseBodyInit>& aBody,
const ResponseInit& aInit, ErrorResult& aRv)
{
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
@ -217,22 +220,65 @@ Response::Constructor(const GlobalObject& aGlobal,
}
}
if (aBody.WasPassed() && !aBody.Value().IsNull()) {
if (aBody.WasPassed()) {
if (aInit.mStatus == 204 || aInit.mStatus == 205 || aInit.mStatus == 304) {
aRv.ThrowTypeError<MSG_RESPONSE_NULL_STATUS_WITH_BODY>();
return nullptr;
}
nsCOMPtr<nsIInputStream> bodyStream;
nsCString contentTypeWithCharset;
nsCOMPtr<nsIInputStream> bodyStream;
uint64_t bodySize = 0;
aRv = ExtractByteStreamFromBody(aBody.Value().Value(),
getter_AddRefs(bodyStream),
contentTypeWithCharset,
bodySize);
if (NS_WARN_IF(aRv.Failed())) {
return nullptr;
if (aBody.Value().IsReadableStream()) {
const ReadableStream& readableStream =
aBody.Value().GetAsReadableStream();
JS::Rooted<JSObject*> readableStreamObj(aGlobal.Context(),
readableStream.Obj());
if (JS::ReadableStreamIsDisturbed(readableStreamObj) ||
JS::ReadableStreamIsLocked(readableStreamObj) ||
!JS::ReadableStreamIsReadable(readableStreamObj)) {
aRv.ThrowTypeError<MSG_FETCH_BODY_CONSUMED_ERROR>();
return nullptr;
}
r->SetReadableStreamBody(readableStreamObj);
// XXXMC: TODO
MOZ_ASSERT(JS::ReadableStreamGetMode(readableStreamObj) !=
JS::ReadableStreamMode::ExternalSource);
void* underlyingSource = nullptr;
if (!JS::ReadableStreamGetExternalUnderlyingSource(aGlobal.Context(),
readableStreamObj,
&underlyingSource)) {
aRv.StealExceptionFromJSContext(aGlobal.Context());
return nullptr;
}
bodySize = InternalResponse::UNKNOWN_BODY_SIZE;
MOZ_ASSERT(underlyingSource);
aRv = FetchStream::RetrieveInputStream(underlyingSource,
getter_AddRefs(bodyStream));
// Releasing of the external source is needed in order to avoid an extra stream lock.
JS::ReadableStreamReleaseExternalUnderlyingSource(readableStreamObj);
if (NS_WARN_IF(aRv.Failed())) {
return nullptr;
}
} else {
aRv = ExtractByteStreamFromBody(aBody.Value(),
getter_AddRefs(bodyStream),
contentTypeWithCharset,
bodySize);
if (NS_WARN_IF(aRv.Failed())) {
return nullptr;
}
}
internalResponse->SetBody(bodyStream, bodySize);
if (!contentTypeWithCharset.IsVoid() &&
@ -254,7 +300,7 @@ Response::Constructor(const GlobalObject& aGlobal,
}
already_AddRefed<Response>
Response::Clone(ErrorResult& aRv) const
Response::Clone(JSContext* aCx, ErrorResult& aRv)
{
if (BodyUsed()) {
aRv.ThrowTypeError<MSG_FETCH_BODY_CONSUMED_ERROR>();
@ -263,11 +309,26 @@ Response::Clone(ErrorResult& aRv) const
RefPtr<InternalResponse> ir = mInternalResponse->Clone();
RefPtr<Response> response = new Response(mOwner, ir, mSignal);
JS::Rooted<JSObject*> body(aCx);
MaybeTeeReadableStreamBody(aCx, &body, aRv);
if (NS_WARN_IF(aRv.Failed())) {
return nullptr;
}
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(body);
}
return response.forget();
}
already_AddRefed<Response>
Response::CloneUnfiltered(ErrorResult& aRv) const
Response::CloneUnfiltered(JSContext* aCx, ErrorResult& aRv)
{
if (BodyUsed()) {
aRv.ThrowTypeError<MSG_FETCH_BODY_CONSUMED_ERROR>();
@ -277,6 +338,21 @@ Response::CloneUnfiltered(ErrorResult& aRv) const
RefPtr<InternalResponse> clone = mInternalResponse->Clone();
RefPtr<InternalResponse> ir = clone->Unfiltered();
RefPtr<Response> ref = new Response(mOwner, ir, mSignal);
JS::Rooted<JSObject*> body(aCx);
MaybeTeeReadableStreamBody(aCx, &body, aRv);
if (NS_WARN_IF(aRv.Failed())) {
return nullptr;
}
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(body);
}
return ref.forget();
}