summaryrefslogtreecommitdiffstats
path: root/dom/fetch/Request.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'dom/fetch/Request.cpp')
-rw-r--r--dom/fetch/Request.cpp130
1 files changed, 57 insertions, 73 deletions
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();
}