summaryrefslogtreecommitdiffstats
path: root/dom/fetch
diff options
context:
space:
mode:
Diffstat (limited to 'dom/fetch')
-rw-r--r--dom/fetch/Fetch.cpp2
-rw-r--r--dom/fetch/Fetch.h4
-rw-r--r--dom/fetch/FetchDriver.cpp2
-rw-r--r--dom/fetch/FetchTypes.ipdlh2
-rw-r--r--dom/fetch/FetchUtil.cpp9
-rw-r--r--dom/fetch/InternalRequest.cpp4
-rw-r--r--dom/fetch/InternalRequest.h20
-rw-r--r--dom/fetch/Request.cpp130
-rw-r--r--dom/fetch/Request.h18
-rw-r--r--dom/fetch/Response.cpp32
-rw-r--r--dom/fetch/Response.h6
-rw-r--r--dom/fetch/tests/browser.toml5
-rw-r--r--dom/fetch/tests/browser_default_credentialless_fetch.js46
13 files changed, 151 insertions, 129 deletions
diff --git a/dom/fetch/Fetch.cpp b/dom/fetch/Fetch.cpp
index 965daf35eb..e2261d2e88 100644
--- a/dom/fetch/Fetch.cpp
+++ b/dom/fetch/Fetch.cpp
@@ -470,7 +470,7 @@ class MainThreadFetchRunnable : public Runnable {
};
already_AddRefed<Promise> FetchRequest(nsIGlobalObject* aGlobal,
- const RequestOrUSVString& aInput,
+ const RequestOrUTF8String& aInput,
const RequestInit& aInit,
CallerType aCallerType,
ErrorResult& aRv) {
diff --git a/dom/fetch/Fetch.h b/dom/fetch/Fetch.h
index 9b9dccd1ee..2f05c6f743 100644
--- a/dom/fetch/Fetch.h
+++ b/dom/fetch/Fetch.h
@@ -44,13 +44,13 @@ class
OwningBlobOrArrayBufferViewOrArrayBufferOrFormDataOrURLSearchParamsOrUSVString;
class ReadableStreamDefaultReader;
-class RequestOrUSVString;
+class RequestOrUTF8String;
class WorkerPrivate;
enum class CallerType : uint32_t;
already_AddRefed<Promise> FetchRequest(nsIGlobalObject* aGlobal,
- const RequestOrUSVString& aInput,
+ const RequestOrUTF8String& aInput,
const RequestInit& aInit,
CallerType aCallerType,
ErrorResult& aRv);
diff --git a/dom/fetch/FetchDriver.cpp b/dom/fetch/FetchDriver.cpp
index c4acc35cf5..79b54f4c4a 100644
--- a/dom/fetch/FetchDriver.cpp
+++ b/dom/fetch/FetchDriver.cpp
@@ -441,7 +441,7 @@ void FetchDriver::UpdateReferrerInfoFromNewChannel(nsIChannel* aChannel) {
return;
}
- nsAutoString computedReferrerSpec;
+ nsAutoCString computedReferrerSpec;
mRequest->SetReferrerPolicy(referrerInfo->ReferrerPolicy());
Unused << referrerInfo->GetComputedReferrerSpec(computedReferrerSpec);
mRequest->SetReferrer(computedReferrerSpec);
diff --git a/dom/fetch/FetchTypes.ipdlh b/dom/fetch/FetchTypes.ipdlh
index f8c594bf1a..f75de35abd 100644
--- a/dom/fetch/FetchTypes.ipdlh
+++ b/dom/fetch/FetchTypes.ipdlh
@@ -59,7 +59,7 @@ struct IPCInternalRequest {
int64_t bodySize;
nsCString preferredAlternativeDataType;
uint32_t contentPolicyType;
- nsString referrer;
+ nsCString referrer;
ReferrerPolicy referrerPolicy;
ReferrerPolicy environmentReferrerPolicy;
RequestMode requestMode;
diff --git a/dom/fetch/FetchUtil.cpp b/dom/fetch/FetchUtil.cpp
index 040e23e4dc..486b58bc37 100644
--- a/dom/fetch/FetchUtil.cpp
+++ b/dom/fetch/FetchUtil.cpp
@@ -128,7 +128,7 @@ nsresult FetchUtil::SetRequestReferrer(nsIPrincipal* aPrincipal, Document* aDoc,
MOZ_ASSERT(NS_IsMainThread());
nsresult rv = NS_OK;
- nsAutoString referrer;
+ nsAutoCString referrer;
aRequest.GetReferrer(referrer);
ReferrerPolicy policy = aRequest.ReferrerPolicy_();
@@ -154,7 +154,7 @@ nsresult FetchUtil::SetRequestReferrer(nsIPrincipal* aPrincipal, Document* aDoc,
rv = aChannel->SetReferrerInfoWithoutClone(referrerInfo);
NS_ENSURE_SUCCESS(rv, rv);
- nsAutoString computedReferrerSpec;
+ nsAutoCString computedReferrerSpec;
referrerInfo = aChannel->GetReferrerInfo();
if (referrerInfo) {
Unused << referrerInfo->GetComputedReferrerSpec(computedReferrerSpec);
@@ -719,7 +719,7 @@ bool FetchUtil::StreamResponseToJS(JSContext* aCx, JS::Handle<JSObject*> aObj,
switch (aMimeType) {
case JS::MimeType::Wasm:
- nsAutoString url;
+ nsAutoCString url;
response->GetUrl(url);
IgnoredErrorResult result;
@@ -728,9 +728,8 @@ bool FetchUtil::StreamResponseToJS(JSContext* aCx, JS::Handle<JSObject*> aObj,
if (NS_WARN_IF(result.Failed())) {
return ThrowException(aCx, JSMSG_WASM_ERROR_CONSUMING_RESPONSE);
}
- NS_ConvertUTF16toUTF8 urlUTF8(url);
aConsumer->noteResponseURLs(
- urlUTF8.get(), sourceMapUrl.IsVoid() ? nullptr : sourceMapUrl.get());
+ url.get(), sourceMapUrl.IsVoid() ? nullptr : sourceMapUrl.get());
break;
}
diff --git a/dom/fetch/InternalRequest.cpp b/dom/fetch/InternalRequest.cpp
index b5cc7cf291..d2086e58eb 100644
--- a/dom/fetch/InternalRequest.cpp
+++ b/dom/fetch/InternalRequest.cpp
@@ -86,7 +86,7 @@ InternalRequest::InternalRequest(const nsACString& aURL,
mHeaders(new InternalHeaders(HeadersGuardEnum::None)),
mBodyLength(InternalResponse::UNKNOWN_BODY_SIZE),
mContentPolicyType(nsIContentPolicy::TYPE_FETCH),
- mReferrer(NS_LITERAL_STRING_FROM_CSTRING(kFETCH_CLIENT_REFERRER_STR)),
+ mReferrer(nsLiteralCString(kFETCH_CLIENT_REFERRER_STR)),
mReferrerPolicy(ReferrerPolicy::_empty),
mEnvironmentReferrerPolicy(ReferrerPolicy::_empty),
mMode(RequestMode::No_cors),
@@ -102,7 +102,7 @@ InternalRequest::InternalRequest(
const nsACString& aMethod, already_AddRefed<InternalHeaders> aHeaders,
RequestCache aCacheMode, RequestMode aMode,
RequestRedirect aRequestRedirect, RequestCredentials aRequestCredentials,
- const nsAString& aReferrer, ReferrerPolicy aReferrerPolicy,
+ const nsACString& aReferrer, ReferrerPolicy aReferrerPolicy,
RequestPriority aPriority, nsContentPolicyType aContentPolicyType,
const nsAString& aIntegrity)
: mMethod(aMethod),
diff --git a/dom/fetch/InternalRequest.h b/dom/fetch/InternalRequest.h
index d2b7721f5e..3a14a907ad 100644
--- a/dom/fetch/InternalRequest.h
+++ b/dom/fetch/InternalRequest.h
@@ -92,7 +92,7 @@ class InternalRequest final : public AtomicSafeRefCounted<InternalRequest> {
RequestCache aCacheMode, RequestMode aMode,
RequestRedirect aRequestRedirect,
RequestCredentials aRequestCredentials,
- const nsAString& aReferrer, ReferrerPolicy aReferrerPolicy,
+ const nsACString& aReferrer, ReferrerPolicy aReferrerPolicy,
RequestPriority aPriority,
nsContentPolicyType aContentPolicyType,
const nsAString& aIntegrity);
@@ -159,9 +159,9 @@ class InternalRequest final : public AtomicSafeRefCounted<InternalRequest> {
void GetURLListWithoutFragment(nsTArray<nsCString>& aURLList) {
aURLList.Assign(mURLList);
}
- void GetReferrer(nsAString& aReferrer) const { aReferrer.Assign(mReferrer); }
+ void GetReferrer(nsACString& aReferrer) const { aReferrer.Assign(mReferrer); }
- void SetReferrer(const nsAString& aReferrer) {
+ void SetReferrer(const nsACString& aReferrer) {
#ifdef DEBUG
bool validReferrer = false;
if (aReferrer.IsEmpty() ||
@@ -179,10 +179,9 @@ class InternalRequest final : public AtomicSafeRefCounted<InternalRequest> {
uint32_t pathPos;
int32_t pathLen;
- NS_ConvertUTF16toUTF8 ref(aReferrer);
- nsresult rv =
- parser->ParseURL(ref.get(), ref.Length(), &schemePos, &schemeLen,
- &authorityPos, &authorityLen, &pathPos, &pathLen);
+ nsresult rv = parser->ParseURL(
+ aReferrer.BeginReading(), aReferrer.Length(), &schemePos,
+ &schemeLen, &authorityPos, &authorityLen, &pathPos, &pathLen);
if (NS_FAILED(rv)) {
NS_WARNING("Invalid referrer URL!");
} else if (schemeLen < 0 || authorityLen < 0) {
@@ -263,6 +262,10 @@ class InternalRequest final : public AtomicSafeRefCounted<InternalRequest> {
mIntegrity.Assign(aIntegrity);
}
+ bool GetKeepalive() const { return mKeepalive; }
+
+ void SetKeepalive(const bool aKeepalive) { mKeepalive = aKeepalive; }
+
bool MozErrors() const { return mMozErrors; }
void SetMozErrors() { mMozErrors = true; }
@@ -440,7 +443,7 @@ class InternalRequest final : public AtomicSafeRefCounted<InternalRequest> {
// Empty string: no-referrer
// "about:client": client (default)
// URL: an URL
- nsString mReferrer;
+ nsCString mReferrer;
ReferrerPolicy mReferrerPolicy;
// This will be used for request created from Window or Worker contexts
@@ -454,6 +457,7 @@ class InternalRequest final : public AtomicSafeRefCounted<InternalRequest> {
RequestRedirect mRedirectMode;
RequestPriority mPriorityMode = RequestPriority::Auto;
nsString mIntegrity;
+ bool mKeepalive = false;
bool mMozErrors = false;
nsCString mFragment;
bool mSkipServiceWorker = false;
diff --git a/dom/fetch/Request.cpp b/dom/fetch/Request.cpp
index e042dd6271..f1ae053b98 100644
--- a/dom/fetch/Request.cpp
+++ b/dom/fetch/Request.cpp
@@ -12,6 +12,7 @@
#include "nsPIDOMWindow.h"
#include "mozilla/ErrorResult.h"
+#include "mozilla/StaticPrefs_network.h"
#include "mozilla/dom/Headers.h"
#include "mozilla/dom/Fetch.h"
#include "mozilla/dom/FetchUtil.h"
@@ -77,29 +78,22 @@ SafeRefPtr<InternalRequest> Request::GetInternalRequest() {
namespace {
already_AddRefed<nsIURI> ParseURLFromDocument(Document* aDocument,
- const nsAString& aInput,
+ const nsACString& aInput,
ErrorResult& aRv) {
MOZ_ASSERT(aDocument);
MOZ_ASSERT(NS_IsMainThread());
- // Don't use NS_ConvertUTF16toUTF8 because that doesn't let us handle OOM.
- nsAutoCString input;
- if (!AppendUTF16toUTF8(aInput, input, fallible)) {
- aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
- return nullptr;
- }
-
nsCOMPtr<nsIURI> resolvedURI;
- nsresult rv = NS_NewURI(getter_AddRefs(resolvedURI), input, nullptr,
+ nsresult rv = NS_NewURI(getter_AddRefs(resolvedURI), aInput, nullptr,
aDocument->GetBaseURI());
if (NS_WARN_IF(NS_FAILED(rv))) {
- aRv.ThrowTypeError<MSG_INVALID_URL>(input);
+ aRv.ThrowTypeError<MSG_INVALID_URL>(aInput);
}
return resolvedURI.forget();
}
-void GetRequestURLFromDocument(Document* aDocument, const nsAString& aInput,
- nsAString& aRequestURL, nsACString& aURLfragment,
- ErrorResult& aRv) {
+void GetRequestURLFromDocument(Document* aDocument, const nsACString& aInput,
+ nsACString& aRequestURL,
+ nsACString& aURLfragment, ErrorResult& aRv) {
nsCOMPtr<nsIURI> resolvedURI = ParseURLFromDocument(aDocument, aInput, aRv);
if (aRv.Failed()) {
return;
@@ -109,7 +103,7 @@ void GetRequestURLFromDocument(Document* aDocument, const nsAString& aInput,
nsAutoCString credentials;
Unused << resolvedURI->GetUserPass(credentials);
if (!credentials.IsEmpty()) {
- aRv.ThrowTypeError<MSG_URL_HAS_CREDENTIALS>(NS_ConvertUTF16toUTF8(aInput));
+ aRv.ThrowTypeError<MSG_URL_HAS_CREDENTIALS>(aInput);
return;
}
@@ -118,12 +112,10 @@ void GetRequestURLFromDocument(Document* aDocument, const nsAString& aInput,
if (NS_WARN_IF(aRv.Failed())) {
return;
}
- nsAutoCString spec;
- aRv = resolvedURIClone->GetSpec(spec);
+ aRv = resolvedURIClone->GetSpec(aRequestURL);
if (NS_WARN_IF(aRv.Failed())) {
return;
}
- CopyUTF8toUTF16(spec, aRequestURL);
// Get the fragment from nsIURI.
aRv = resolvedURI->GetRef(aURLfragment);
@@ -131,24 +123,18 @@ void GetRequestURLFromDocument(Document* aDocument, const nsAString& aInput,
return;
}
}
-already_AddRefed<nsIURI> ParseURLFromChrome(const nsAString& aInput,
+already_AddRefed<nsIURI> ParseURLFromChrome(const nsACString& aInput,
ErrorResult& aRv) {
MOZ_ASSERT(NS_IsMainThread());
- // Don't use NS_ConvertUTF16toUTF8 because that doesn't let us handle OOM.
- nsAutoCString input;
- if (!AppendUTF16toUTF8(aInput, input, fallible)) {
- aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
- return nullptr;
- }
nsCOMPtr<nsIURI> uri;
- nsresult rv = NS_NewURI(getter_AddRefs(uri), input);
+ nsresult rv = NS_NewURI(getter_AddRefs(uri), aInput);
if (NS_FAILED(rv)) {
- aRv.ThrowTypeError<MSG_INVALID_URL>(input);
+ aRv.ThrowTypeError<MSG_INVALID_URL>(aInput);
}
return uri.forget();
}
-void GetRequestURLFromChrome(const nsAString& aInput, nsAString& aRequestURL,
+void GetRequestURLFromChrome(const nsACString& aInput, nsACString& aRequestURL,
nsACString& aURLfragment, ErrorResult& aRv) {
nsCOMPtr<nsIURI> uri = ParseURLFromChrome(aInput, aRv);
if (aRv.Failed()) {
@@ -159,7 +145,7 @@ void GetRequestURLFromChrome(const nsAString& aInput, nsAString& aRequestURL,
nsAutoCString credentials;
Unused << uri->GetUserPass(credentials);
if (!credentials.IsEmpty()) {
- aRv.ThrowTypeError<MSG_URL_HAS_CREDENTIALS>(NS_ConvertUTF16toUTF8(aInput));
+ aRv.ThrowTypeError<MSG_URL_HAS_CREDENTIALS>(aInput);
return;
}
@@ -168,12 +154,10 @@ void GetRequestURLFromChrome(const nsAString& aInput, nsAString& aRequestURL,
if (NS_WARN_IF(aRv.Failed())) {
return;
}
- nsAutoCString spec;
- aRv = uriClone->GetSpec(spec);
+ aRv = uriClone->GetSpec(aRequestURL);
if (NS_WARN_IF(aRv.Failed())) {
return;
}
- CopyUTF8toUTF16(spec, aRequestURL);
// Get the fragment from nsIURI.
aRv = uri->GetRef(aURLfragment);
@@ -182,55 +166,55 @@ void GetRequestURLFromChrome(const nsAString& aInput, nsAString& aRequestURL,
}
}
already_AddRefed<URL> ParseURLFromWorker(nsIGlobalObject* aGlobal,
- const nsAString& aInput,
+ const nsACString& aInput,
ErrorResult& aRv) {
WorkerPrivate* worker = GetCurrentThreadWorkerPrivate();
MOZ_ASSERT(worker);
worker->AssertIsOnWorkerThread();
- NS_ConvertUTF8toUTF16 baseURL(worker->GetLocationInfo().mHref);
+ const auto& baseURL = worker->GetLocationInfo().mHref;
RefPtr<URL> url = URL::Constructor(aGlobal, aInput, baseURL, aRv);
if (NS_WARN_IF(aRv.Failed())) {
- aRv.ThrowTypeError<MSG_INVALID_URL>(NS_ConvertUTF16toUTF8(aInput));
+ aRv.ThrowTypeError<MSG_INVALID_URL>(aInput);
}
return url.forget();
}
-void GetRequestURLFromWorker(nsIGlobalObject* aGlobal, const nsAString& aInput,
- nsAString& aRequestURL, nsACString& aURLfragment,
+void GetRequestURLFromWorker(nsIGlobalObject* aGlobal, const nsACString& aInput,
+ nsACString& aRequestURL, nsACString& aURLfragment,
ErrorResult& aRv) {
RefPtr<URL> url = ParseURLFromWorker(aGlobal, aInput, aRv);
if (aRv.Failed()) {
return;
}
- nsString username;
+ nsCString username;
url->GetUsername(username);
- nsString password;
+ nsCString password;
url->GetPassword(password);
if (!username.IsEmpty() || !password.IsEmpty()) {
- aRv.ThrowTypeError<MSG_URL_HAS_CREDENTIALS>(NS_ConvertUTF16toUTF8(aInput));
+ aRv.ThrowTypeError<MSG_URL_HAS_CREDENTIALS>(aInput);
return;
}
// Get the fragment from URL.
- nsAutoString fragment;
+ nsAutoCString fragment;
url->GetHash(fragment);
// Note: URL::GetHash() includes the "#" and we want the fragment with out
// the hash symbol.
if (!fragment.IsEmpty()) {
- CopyUTF16toUTF8(Substring(fragment, 1), aURLfragment);
+ aURLfragment = Substring(fragment, 1);
}
- url->SetHash(u""_ns);
+ url->SetHash(""_ns);
url->GetHref(aRequestURL);
}
class ReferrerSameOriginChecker final : public WorkerMainThreadRunnable {
public:
ReferrerSameOriginChecker(WorkerPrivate* aWorkerPrivate,
- const nsAString& aReferrerURL, nsresult& aResult)
+ const nsACString& aReferrerURL, nsresult& aResult)
: WorkerMainThreadRunnable(aWorkerPrivate,
"Fetch :: Referrer same origin check"_ns),
mReferrerURL(aReferrerURL),
@@ -241,8 +225,7 @@ class ReferrerSameOriginChecker final : public WorkerMainThreadRunnable {
bool MainThreadRun() override {
nsCOMPtr<nsIURI> uri;
if (NS_SUCCEEDED(NS_NewURI(getter_AddRefs(uri), mReferrerURL))) {
- nsCOMPtr<nsIPrincipal> principal = mWorkerPrivate->GetPrincipal();
- if (principal) {
+ if (nsCOMPtr<nsIPrincipal> principal = mWorkerPrivate->GetPrincipal()) {
mResult = principal->CheckMayLoad(uri,
/* allowIfInheritsPrincipal */ false);
}
@@ -251,7 +234,7 @@ class ReferrerSameOriginChecker final : public WorkerMainThreadRunnable {
}
private:
- const nsString mReferrerURL;
+ const nsCString mReferrerURL;
nsresult& mResult;
};
@@ -259,19 +242,18 @@ class ReferrerSameOriginChecker final : public WorkerMainThreadRunnable {
/*static*/
SafeRefPtr<Request> Request::Constructor(const GlobalObject& aGlobal,
- const RequestOrUSVString& aInput,
+ const RequestOrUTF8String& aInput,
const RequestInit& aInit,
ErrorResult& aRv) {
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
- return Constructor(global, aGlobal.Context(), aInput, aInit, aRv);
+ return Constructor(global, aGlobal.Context(), aInput, aInit,
+ aGlobal.CallerType(), aRv);
}
/*static*/
-SafeRefPtr<Request> Request::Constructor(nsIGlobalObject* aGlobal,
- JSContext* aCx,
- const RequestOrUSVString& aInput,
- const RequestInit& aInit,
- ErrorResult& aRv) {
+SafeRefPtr<Request> Request::Constructor(
+ nsIGlobalObject* aGlobal, JSContext* aCx, const RequestOrUTF8String& aInput,
+ const RequestInit& aInit, CallerType aCallerType, ErrorResult& aRv) {
bool hasCopiedBody = false;
SafeRefPtr<InternalRequest> request;
@@ -301,11 +283,10 @@ SafeRefPtr<Request> Request::Constructor(nsIGlobalObject* aGlobal,
request = inputReq->GetInternalRequest();
signal = inputReq->GetOrCreateSignal();
} else {
- // aInput is USVString.
+ // aInput is UTF8String.
// We need to get url before we create a InternalRequest.
- nsAutoString input;
- input.Assign(aInput.GetAsUSVString());
- nsAutoString requestURL;
+ const nsACString& input = aInput.GetAsUTF8String();
+ nsAutoCString requestURL;
nsCString fragment;
if (NS_IsMainThread()) {
nsCOMPtr<nsPIDOMWindowInner> inner(do_QueryInterface(aGlobal));
@@ -322,8 +303,7 @@ SafeRefPtr<Request> Request::Constructor(nsIGlobalObject* aGlobal,
if (aRv.Failed()) {
return nullptr;
}
- request = MakeSafeRefPtr<InternalRequest>(NS_ConvertUTF16toUTF8(requestURL),
- fragment);
+ request = MakeSafeRefPtr<InternalRequest>(requestURL, fragment);
}
request = request->GetRequestConstructorCopy(aGlobal, aRv);
if (NS_WARN_IF(aRv.Failed())) {
@@ -346,12 +326,17 @@ SafeRefPtr<Request> Request::Constructor(nsIGlobalObject* aGlobal,
if (aInit.mCache.WasPassed()) {
cache.emplace(aInit.mCache.Value());
}
- if (aInput.IsUSVString()) {
+ if (aInput.IsUTF8String()) {
if (mode.isNothing()) {
mode.emplace(RequestMode::Cors);
}
if (credentials.isNothing()) {
- credentials.emplace(RequestCredentials::Same_origin);
+ if (aCallerType == CallerType::System &&
+ StaticPrefs::network_fetch_systemDefaultsToOmittingCredentials()) {
+ credentials.emplace(RequestCredentials::Omit);
+ } else {
+ credentials.emplace(RequestCredentials::Same_origin);
+ }
}
if (cache.isNothing()) {
cache.emplace(RequestCache::Default);
@@ -363,16 +348,15 @@ SafeRefPtr<Request> Request::Constructor(nsIGlobalObject* aGlobal,
}
if (aInit.IsAnyMemberPresent()) {
- request->SetReferrer(
- NS_LITERAL_STRING_FROM_CSTRING(kFETCH_CLIENT_REFERRER_STR));
+ request->SetReferrer(nsLiteralCString(kFETCH_CLIENT_REFERRER_STR));
request->SetReferrerPolicy(ReferrerPolicy::_empty);
}
if (aInit.mReferrer.WasPassed()) {
- const nsString& referrer = aInit.mReferrer.Value();
+ const nsCString& referrer = aInit.mReferrer.Value();
if (referrer.IsEmpty()) {
- request->SetReferrer(u""_ns);
+ request->SetReferrer(""_ns);
} else {
- nsAutoString referrerURL;
+ nsAutoCString referrerURL;
if (NS_IsMainThread()) {
nsCOMPtr<nsPIDOMWindowInner> inner(do_QueryInterface(aGlobal));
Document* doc = inner ? inner->GetExtantDoc() : nullptr;
@@ -385,13 +369,10 @@ SafeRefPtr<Request> Request::Constructor(nsIGlobalObject* aGlobal,
uri = ParseURLFromChrome(referrer, aRv);
}
if (NS_WARN_IF(aRv.Failed())) {
- aRv.ThrowTypeError<MSG_INVALID_REFERRER_URL>(
- NS_ConvertUTF16toUTF8(referrer));
+ aRv.ThrowTypeError<MSG_INVALID_REFERRER_URL>(referrer);
return nullptr;
}
- nsAutoCString spec;
- uri->GetSpec(spec);
- CopyUTF8toUTF16(spec, referrerURL);
+ uri->GetSpec(referrerURL);
if (!referrerURL.EqualsLiteral(kFETCH_CLIENT_REFERRER_STR)) {
nsCOMPtr<nsIPrincipal> principal = aGlobal->PrincipalOrNull();
if (principal) {
@@ -406,8 +387,7 @@ SafeRefPtr<Request> Request::Constructor(nsIGlobalObject* aGlobal,
} else {
RefPtr<URL> url = ParseURLFromWorker(aGlobal, referrer, aRv);
if (NS_WARN_IF(aRv.Failed())) {
- aRv.ThrowTypeError<MSG_INVALID_REFERRER_URL>(
- NS_ConvertUTF16toUTF8(referrer));
+ aRv.ThrowTypeError<MSG_INVALID_REFERRER_URL>(referrer);
return nullptr;
}
url->GetHref(referrerURL);
@@ -516,6 +496,10 @@ SafeRefPtr<Request> Request::Constructor(nsIGlobalObject* aGlobal,
request->SetIntegrity(aInit.mIntegrity.Value());
}
+ if (aInit.mKeepalive.WasPassed()) {
+ request->SetKeepalive(aInit.mKeepalive.Value());
+ }
+
if (aInit.mMozErrors.WasPassed() && aInit.mMozErrors.Value()) {
request->SetMozErrors();
}
diff --git a/dom/fetch/Request.h b/dom/fetch/Request.h
index ee8deffe5a..d4d10a2deb 100644
--- a/dom/fetch/Request.h
+++ b/dom/fetch/Request.h
@@ -21,7 +21,7 @@ namespace mozilla::dom {
class Headers;
class InternalHeaders;
-class RequestOrUSVString;
+class RequestOrUTF8String;
class Request final : public FetchBody<Request>, public nsWrapperCache {
NS_DECL_ISUPPORTS_INHERITED
@@ -37,12 +37,7 @@ class Request final : public FetchBody<Request>, public nsWrapperCache {
return Request_Binding::Wrap(aCx, this, aGivenProto);
}
- void GetUrl(nsAString& aUrl) const {
- nsAutoCString url;
- mRequest->GetURL(url);
- CopyUTF8toUTF16(url, aUrl);
- }
-
+ void GetUrl(nsACString& aUrl) const { mRequest->GetURL(aUrl); }
void GetMethod(nsCString& aMethod) const { aMethod = mRequest->mMethod; }
RequestMode Mode() const { return mRequest->mMode; }
@@ -59,6 +54,8 @@ class Request final : public FetchBody<Request>, public nsWrapperCache {
aIntegrity = mRequest->GetIntegrity();
}
+ bool Keepalive() const { return mRequest->GetKeepalive(); }
+
bool MozErrors() const { return mRequest->MozErrors(); }
RequestDestination Destination() const { return mRequest->Destination(); }
@@ -72,7 +69,7 @@ class Request final : public FetchBody<Request>, public nsWrapperCache {
return mRequest->IsContentPolicyTypeOverridden();
}
- void GetReferrer(nsAString& aReferrer) const {
+ void GetReferrer(nsACString& aReferrer) const {
mRequest->GetReferrer(aReferrer);
}
@@ -103,14 +100,15 @@ class Request final : public FetchBody<Request>, public nsWrapperCache {
const nsAString& BodyLocalPath() const { return mRequest->BodyLocalPath(); }
static SafeRefPtr<Request> Constructor(const GlobalObject& aGlobal,
- const RequestOrUSVString& aInput,
+ const RequestOrUTF8String& aInput,
const RequestInit& aInit,
ErrorResult& rv);
static SafeRefPtr<Request> Constructor(nsIGlobalObject* aGlobal,
JSContext* aCx,
- const RequestOrUSVString& aInput,
+ const RequestOrUTF8String& aInput,
const RequestInit& aInit,
+ const CallerType aCallerType,
ErrorResult& rv);
nsIGlobalObject* GetParentObject() const { return mOwner; }
diff --git a/dom/fetch/Response.cpp b/dom/fetch/Response.cpp
index 9e4cdf84ce..be3decc514 100644
--- a/dom/fetch/Response.cpp
+++ b/dom/fetch/Response.cpp
@@ -84,53 +84,42 @@ already_AddRefed<Response> Response::Error(const GlobalObject& aGlobal) {
/* static */
already_AddRefed<Response> Response::Redirect(const GlobalObject& aGlobal,
- const nsAString& aUrl,
+ const nsACString& aUrl,
uint16_t aStatus,
ErrorResult& aRv) {
- nsAutoString parsedURL;
+ nsAutoCString parsedURL;
if (NS_IsMainThread()) {
nsIURI* baseURI = nullptr;
nsCOMPtr<nsPIDOMWindowInner> inner(
do_QueryInterface(aGlobal.GetAsSupports()));
- Document* doc = inner ? inner->GetExtantDoc() : nullptr;
- if (doc) {
+ if (Document* doc = inner ? inner->GetExtantDoc() : nullptr) {
baseURI = doc->GetBaseURI();
}
- // Don't use NS_ConvertUTF16toUTF8 because that doesn't let us handle OOM.
- nsAutoCString url;
- if (!AppendUTF16toUTF8(aUrl, url, fallible)) {
- aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
- return nullptr;
- }
-
nsCOMPtr<nsIURI> resolvedURI;
- nsresult rv = NS_NewURI(getter_AddRefs(resolvedURI), url, nullptr, baseURI);
+ nsresult rv =
+ NS_NewURI(getter_AddRefs(resolvedURI), aUrl, nullptr, baseURI);
if (NS_WARN_IF(NS_FAILED(rv))) {
- aRv.ThrowTypeError<MSG_INVALID_URL>(url);
+ aRv.ThrowTypeError<MSG_INVALID_URL>(aUrl);
return nullptr;
}
- nsAutoCString spec;
- rv = resolvedURI->GetSpec(spec);
+ rv = resolvedURI->GetSpec(parsedURL);
if (NS_WARN_IF(NS_FAILED(rv))) {
- aRv.ThrowTypeError<MSG_INVALID_URL>(url);
+ aRv.ThrowTypeError<MSG_INVALID_URL>(aUrl);
return nullptr;
}
-
- CopyUTF8toUTF16(spec, parsedURL);
} else {
WorkerPrivate* worker = GetCurrentThreadWorkerPrivate();
MOZ_ASSERT(worker);
worker->AssertIsOnWorkerThread();
- NS_ConvertUTF8toUTF16 baseURL(worker->GetLocationInfo().mHref);
+ const auto& baseURL = worker->GetLocationInfo().mHref;
RefPtr<URL> url =
URL::Constructor(aGlobal.GetAsSupports(), aUrl, baseURL, aRv);
if (aRv.Failed()) {
return nullptr;
}
-
url->GetHref(parsedURL);
}
@@ -152,8 +141,7 @@ already_AddRefed<Response> Response::Redirect(const GlobalObject& aGlobal,
return nullptr;
}
- r->GetInternalHeaders()->Set("Location"_ns, NS_ConvertUTF16toUTF8(parsedURL),
- aRv);
+ r->GetInternalHeaders()->Set("Location"_ns, parsedURL, aRv);
if (NS_WARN_IF(aRv.Failed())) {
return nullptr;
}
diff --git a/dom/fetch/Response.h b/dom/fetch/Response.h
index 1aa0e56c31..14c0e63bb2 100644
--- a/dom/fetch/Response.h
+++ b/dom/fetch/Response.h
@@ -43,9 +43,7 @@ class Response final : public FetchBody<Response>, public nsWrapperCache {
}
ResponseType Type() const { return mInternalResponse->Type(); }
- void GetUrl(nsAString& aUrl) const {
- CopyUTF8toUTF16(mInternalResponse->GetURL(), aUrl);
- }
+ void GetUrl(nsACString& aUrl) const { aUrl = mInternalResponse->GetURL(); }
bool Redirected() const { return mInternalResponse->IsRedirected(); }
uint16_t Status() const { return mInternalResponse->GetStatus(); }
@@ -101,7 +99,7 @@ class Response final : public FetchBody<Response>, public nsWrapperCache {
static already_AddRefed<Response> Error(const GlobalObject& aGlobal);
static already_AddRefed<Response> Redirect(const GlobalObject& aGlobal,
- const nsAString& aUrl,
+ const nsACString& aUrl,
uint16_t aStatus,
ErrorResult& aRv);
diff --git a/dom/fetch/tests/browser.toml b/dom/fetch/tests/browser.toml
index e449f9572f..f0788db856 100644
--- a/dom/fetch/tests/browser.toml
+++ b/dom/fetch/tests/browser.toml
@@ -2,6 +2,11 @@
["browser_blobFromFile.js"]
+["browser_default_credentialless_fetch.js"]
+support-files = [
+ "store_header.sjs",
+]
+
["browser_origin_trial_coep_credentialless_cache.js"]
support-files = [
"open_credentialless_document.sjs",
diff --git a/dom/fetch/tests/browser_default_credentialless_fetch.js b/dom/fetch/tests/browser_default_credentialless_fetch.js
new file mode 100644
index 0000000000..1c7e820d5f
--- /dev/null
+++ b/dom/fetch/tests/browser_default_credentialless_fetch.js
@@ -0,0 +1,46 @@
+/* Any copyright is dedicated to the Public Domain.
+ https://creativecommons.org/publicdomain/zero/1.0/ */
+
+"use strict";
+
+const RESOURCE_URL =
+ getRootDirectory(gTestPath).replace(
+ "chrome://mochitests/content",
+ "https://example.com"
+ ) + "store_header.sjs";
+
+add_task(async function test_fetch_defaults_to_credentialless() {
+ // Ensure cookie is set up:
+ let expiry = Date.now() / 1000 + 24 * 60 * 60;
+ Services.cookies.add(
+ "example.com",
+ "/",
+ "foo",
+ "bar",
+ false,
+ false,
+ false,
+ expiry,
+ {},
+ Ci.nsICookie.SAMESITE_NONE,
+ Ci.nsICookie.SCHEME_HTTPS
+ );
+
+ // Explicitly send cookie header by using `same-origin` in the init dict, to
+ // ensure cookies are stored correctly and can be sent.
+ await fetch(RESOURCE_URL + "?checkheader", { credentials: "same-origin" });
+
+ Assert.equal(
+ await fetch(RESOURCE_URL + "?getstate").then(r => r.text()),
+ "hasCookie",
+ "Should have cookie when explicitly passing credentials info in 'checkheader' request."
+ );
+
+ // Check the default behaviour.
+ await fetch(RESOURCE_URL + "?checkheader");
+ Assert.equal(
+ await fetch(RESOURCE_URL + "?getstate").then(r => r.text()),
+ "noCookie",
+ "Should not have cookie in the default case (no explicit credentials mode) for chrome privileged requests."
+ );
+});