From 14f0bc5e7c6f8f662e1c40ad6b9e00ee35b1d2f4 Mon Sep 17 00:00:00 2001 From: Brian Smith Date: Fri, 5 Jan 2024 00:57:40 -0600 Subject: [PATCH] Issue #2402 - Move code to fill InternalHeaders from an nsIChannel response into utility method. https://bugzilla.mozilla.org/show_bug.cgi?id=1337543 --- dom/fetch/FetchDriver.cpp | 39 +------------------------------ dom/fetch/InternalHeaders.cpp | 43 +++++++++++++++++++++++++++++++++++ dom/fetch/InternalHeaders.h | 1 + 3 files changed, 45 insertions(+), 38 deletions(-) diff --git a/dom/fetch/FetchDriver.cpp b/dom/fetch/FetchDriver.cpp index 2a0bb1ea2c..0198cc44aa 100644 --- a/dom/fetch/FetchDriver.cpp +++ b/dom/fetch/FetchDriver.cpp @@ -12,7 +12,6 @@ #include "nsIOutputStream.h" #include "nsIHttpChannel.h" #include "nsIHttpChannelInternal.h" -#include "nsIHttpHeaderVisitor.h" #include "nsIScriptSecurityManager.h" #include "nsIThreadRetargetableRequest.h" #include "nsIUploadChannel2.h" @@ -449,38 +448,6 @@ FetchDriver::FailWithNetworkError() mChannel = nullptr; } -namespace { -class FillResponseHeaders final : public nsIHttpHeaderVisitor { - InternalResponse* mResponse; - - ~FillResponseHeaders() - { } -public: - NS_DECL_ISUPPORTS - - explicit FillResponseHeaders(InternalResponse* aResponse) - : mResponse(aResponse) - { - } - - NS_IMETHOD - VisitHeader(const nsACString & aHeader, const nsACString & aValue) override - { - ErrorResult result; - mResponse->Headers()->Append(aHeader, aValue, result); - if (result.Failed()) { - NS_WARNING(nsPrintfCString("Fetch ignoring illegal header - '%s': '%s'", - PromiseFlatCString(aHeader).get(), - PromiseFlatCString(aValue).get()).get()); - result.SuppressException(); - } - return NS_OK; - } -}; - -NS_IMPL_ISUPPORTS(FillResponseHeaders, nsIHttpHeaderVisitor) -} // namespace - NS_IMETHODIMP FetchDriver::OnStartRequest(nsIRequest* aRequest, nsISupports* aContext) @@ -540,11 +507,7 @@ FetchDriver::OnStartRequest(nsIRequest* aRequest, response = new InternalResponse(responseStatus, statusText, mRequest->GetCredentialsMode()); - RefPtr visitor = new FillResponseHeaders(response); - rv = httpChannel->VisitResponseHeaders(visitor); - if (NS_WARN_IF(NS_FAILED(rv))) { - NS_WARNING("Failed to visit all headers."); - } + response->Headers()->FillResponseHeaders(httpChannel); // If Content-Encoding or Transfer-Encoding headers are set, then the actual // Content-Length (which refer to the decoded data) is obscured behind the encodings. diff --git a/dom/fetch/InternalHeaders.cpp b/dom/fetch/InternalHeaders.cpp index f4851b7064..54d4da093d 100644 --- a/dom/fetch/InternalHeaders.cpp +++ b/dom/fetch/InternalHeaders.cpp @@ -11,6 +11,7 @@ #include "nsCharSeparatedTokenizer.h" #include "nsContentUtils.h" +#include "nsIHttpHeaderVisitor.h" #include "nsNetUtil.h" #include "nsReadableUtils.h" @@ -478,6 +479,48 @@ InternalHeaders::Fill(const Record& aInit, ErrorResult& aR } } +namespace { + +class FillHeaders final : public nsIHttpHeaderVisitor +{ + RefPtr mInternalHeaders; + + ~FillHeaders() = default; + +public: + NS_DECL_ISUPPORTS + + explicit FillHeaders(InternalHeaders* aInternalHeaders) + : mInternalHeaders(aInternalHeaders) + { + MOZ_DIAGNOSTIC_ASSERT(mInternalHeaders); + } + + NS_IMETHOD + VisitHeader(const nsACString& aHeader, const nsACString& aValue) override + { + IgnoredErrorResult result; + mInternalHeaders->Append(aHeader, aValue, result); + return NS_OK; + } +}; + +NS_IMPL_ISUPPORTS(FillHeaders, nsIHttpHeaderVisitor) + +} // namespace + +void +InternalHeaders::FillResponseHeaders(nsIRequest* aRequest) +{ + nsCOMPtr httpChannel = do_QueryInterface(aRequest); + if (!httpChannel) { + return; + } + + RefPtr visitor = new FillHeaders(this); + httpChannel->VisitResponseHeaders(visitor); +} + bool InternalHeaders::HasOnlySimpleHeaders() const { diff --git a/dom/fetch/InternalHeaders.h b/dom/fetch/InternalHeaders.h index 0a6996ab38..c9aff2d4d9 100644 --- a/dom/fetch/InternalHeaders.h +++ b/dom/fetch/InternalHeaders.h @@ -114,6 +114,7 @@ public: void Fill(const InternalHeaders& aInit, ErrorResult& aRv); void Fill(const Sequence>& aInit, ErrorResult& aRv); void Fill(const Record& aInit, ErrorResult& aRv); + void FillResponseHeaders(nsIRequest* aRequest); bool HasOnlySimpleHeaders() const;