Bug 1684634 - Use InternalHeaders for MimeType instead of caching in the Response/Request constructor. r=baku

Differential Revision: https://phabricator.services.mozilla.com/D101159
This commit is contained in:
Tom Schuster 2021-01-12 10:38:25 +00:00
Родитель a560a97624
Коммит a865c88907
7 изменённых файлов: 25 добавлений и 30 удалений

Просмотреть файл

@ -40,9 +40,11 @@ NS_INTERFACE_MAP_END_INHERITING(FetchBody<EmptyBody>)
EmptyBody::EmptyBody(nsIGlobalObject* aGlobal,
mozilla::ipc::PrincipalInfo* aPrincipalInfo,
AbortSignalImpl* aAbortSignalImpl,
const nsACString& aMimeType,
already_AddRefed<nsIInputStream> aBodyStream)
: FetchBody<EmptyBody>(aGlobal),
mAbortSignalImpl(aAbortSignalImpl),
mMimeType(aMimeType),
mBodyStream(std::move(aBodyStream)) {
if (aPrincipalInfo) {
mPrincipalInfo = MakeUnique<mozilla::ipc::PrincipalInfo>(*aPrincipalInfo);
@ -62,9 +64,9 @@ already_AddRefed<EmptyBody> EmptyBody::Create(
return nullptr;
}
RefPtr<EmptyBody> emptyBody = new EmptyBody(
aGlobal, aPrincipalInfo, aAbortSignalImpl, bodyStream.forget());
emptyBody->OverrideMimeType(aMimeType);
RefPtr<EmptyBody> emptyBody =
new EmptyBody(aGlobal, aPrincipalInfo, aAbortSignalImpl, aMimeType,
bodyStream.forget());
return emptyBody.forget();
}

Просмотреть файл

@ -38,6 +38,8 @@ class EmptyBody final : public FetchBody<EmptyBody> {
return mPrincipalInfo;
}
void GetMimeType(nsACString& aMimeType) { aMimeType = mMimeType; }
void GetBody(nsIInputStream** aStream, int64_t* aBodyLength = nullptr);
using FetchBody::BodyBlobURISpec;
@ -51,13 +53,14 @@ class EmptyBody final : public FetchBody<EmptyBody> {
private:
EmptyBody(nsIGlobalObject* aGlobal,
mozilla::ipc::PrincipalInfo* aPrincipalInfo,
AbortSignalImpl* aAbortSignalImpl,
AbortSignalImpl* aAbortSignalImpl, const nsACString& aMimeType,
already_AddRefed<nsIInputStream> mBodyStream);
~EmptyBody();
UniquePtr<mozilla::ipc::PrincipalInfo> mPrincipalInfo;
RefPtr<AbortSignalImpl> mAbortSignalImpl;
nsCString mMimeType;
nsCOMPtr<nsIInputStream> mBodyStream;
};

Просмотреть файл

@ -1212,6 +1212,9 @@ already_AddRefed<Promise> FetchBody<Derived>::ConsumeBody(
return nullptr;
}
nsAutoCString mimeType;
DerivedClass()->GetMimeType(mimeType);
// Null bodies are a special-case in the fetch spec. The Body mix-in can only
// be "disturbed" or "locked" if its associated "body" is non-null.
// Additionally, the Body min-in's "consume body" algorithm explicitly creates
@ -1226,7 +1229,7 @@ already_AddRefed<Promise> FetchBody<Derived>::ConsumeBody(
if (!bodyStream) {
RefPtr<EmptyBody> emptyBody = EmptyBody::Create(
DerivedClass()->GetParentObject(),
DerivedClass()->GetPrincipalInfo().get(), signalImpl, mMimeType, aRv);
DerivedClass()->GetPrincipalInfo().get(), signalImpl, mimeType, aRv);
if (NS_WARN_IF(aRv.Failed())) {
return nullptr;
}
@ -1259,7 +1262,7 @@ already_AddRefed<Promise> FetchBody<Derived>::ConsumeBody(
RefPtr<Promise> promise = BodyConsumer::Create(
global, mMainThreadEventTarget, bodyStream, signalImpl, aType,
BodyBlobURISpec(), BodyLocalPath(), MimeType(), blobStorageType, aRv);
BodyBlobURISpec(), BodyLocalPath(), mimeType, blobStorageType, aRv);
if (NS_WARN_IF(aRv.Failed())) {
return nullptr;
}
@ -1277,7 +1280,7 @@ template already_AddRefed<Promise> FetchBody<EmptyBody>::ConsumeBody(
JSContext* aCx, BodyConsumer::ConsumeType aType, ErrorResult& aRv);
template <class Derived>
void FetchBody<Derived>::SetMimeType() {
void FetchBody<Derived>::GetMimeType(nsACString& aMimeType) {
// Extract mime type.
ErrorResult result;
nsCString contentTypeValues;
@ -1290,19 +1293,13 @@ void FetchBody<Derived>::SetMimeType() {
// This is from the "parse a header value" of the fetch spec.
if (!contentTypeValues.IsVoid() && contentTypeValues.Find(",") == -1) {
// Convert from a bytestring to a UTF8 CString.
CopyLatin1toUTF8(contentTypeValues, mMimeType);
ToLowerCase(mMimeType);
CopyLatin1toUTF8(contentTypeValues, aMimeType);
ToLowerCase(aMimeType);
}
}
template void FetchBody<Request>::SetMimeType();
template void FetchBody<Response>::SetMimeType();
template <class Derived>
void FetchBody<Derived>::OverrideMimeType(const nsACString& aMimeType) {
mMimeType = aMimeType;
}
template void FetchBody<Request>::GetMimeType(nsACString& aMimeType);
template void FetchBody<Response>::GetMimeType(nsACString& aMimeType);
template <class Derived>
const nsACString& FetchBody<Derived>::BodyBlobURISpec() const {

Просмотреть файл

@ -156,6 +156,8 @@ class FetchBody : public BodyStreamHolder, public AbortFollower {
void GetBody(JSContext* aCx, JS::MutableHandle<JSObject*> aBodyOut,
ErrorResult& aRv);
void GetMimeType(nsACString& aMimeType);
const nsACString& BodyBlobURISpec() const;
const nsAString& BodyLocalPath() const;
@ -192,8 +194,6 @@ class FetchBody : public BodyStreamHolder, public AbortFollower {
// to the Console.
void SetBodyUsed(JSContext* aCx, ErrorResult& aRv);
const nsCString& MimeType() const { return mMimeType; }
// BodyStreamHolder
void NullifyStream() override {
mReadableStreamBody = nullptr;
@ -236,10 +236,6 @@ class FetchBody : public BodyStreamHolder, public AbortFollower {
virtual ~FetchBody();
void SetMimeType();
void OverrideMimeType(const nsACString& aMimeType);
void SetReadableStreamBody(JSContext* aCx, JSObject* aBody);
private:
@ -255,7 +251,6 @@ class FetchBody : public BodyStreamHolder, public AbortFollower {
// Only ever set once, always on target thread.
bool mBodyUsed;
nsCString mMimeType;
// The main-thread event target for runnable dispatching.
nsCOMPtr<nsIEventTarget> mMainThreadEventTarget;

Просмотреть файл

@ -479,7 +479,10 @@ bool FetchUtil::StreamResponseToJS(JSContext* aCx, JS::HandleObject aObj,
break;
}
if (strcmp(requiredMimeType, response->MimeType().Data())) {
nsAutoCString mimeType;
response->GetMimeType(mimeType);
if (!mimeType.EqualsASCII(requiredMimeType)) {
return ThrowException(aCx, JSMSG_BAD_RESPONSE_MIME_TYPE);
}

Просмотреть файл

@ -60,8 +60,6 @@ Request::Request(nsIGlobalObject* aOwner, SafeRefPtr<InternalRequest> aRequest,
MOZ_ASSERT(mRequest->Headers()->Guard() == HeadersGuardEnum::Immutable ||
mRequest->Headers()->Guard() == HeadersGuardEnum::Request ||
mRequest->Headers()->Guard() == HeadersGuardEnum::Request_no_cors);
SetMimeType();
if (aSignal) {
// If we don't have a signal as argument, we will create it when required by
// content, otherwise the Request's signal must follow what has been passed.
@ -608,7 +606,6 @@ SafeRefPtr<Request> Request::Constructor(nsIGlobalObject* aGlobal,
auto domRequest =
MakeSafeRefPtr<Request>(aGlobal, std::move(request), signal);
domRequest->SetMimeType();
if (aInput.IsRequest()) {
RefPtr<Request> inputReq = &aInput.GetAsRequest();

Просмотреть файл

@ -75,7 +75,6 @@ Response::Response(nsIGlobalObject* aGlobal,
MOZ_ASSERT(
aInternalResponse->Headers()->Guard() == HeadersGuardEnum::Immutable ||
aInternalResponse->Headers()->Guard() == HeadersGuardEnum::Response);
SetMimeType();
mozilla::HoldJSObjects(this);
}
@ -370,7 +369,6 @@ already_AddRefed<Response> Response::Constructor(
}
}
r->SetMimeType();
return r.forget();
}