2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2014-07-25 04:53:03 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "Request.h"
|
|
|
|
|
|
|
|
#include "nsIURI.h"
|
|
|
|
#include "nsPIDOMWindow.h"
|
|
|
|
|
|
|
|
#include "mozilla/ErrorResult.h"
|
|
|
|
#include "mozilla/dom/Headers.h"
|
2014-09-24 09:03:20 +04:00
|
|
|
#include "mozilla/dom/Fetch.h"
|
2015-09-11 21:26:33 +03:00
|
|
|
#include "mozilla/dom/FetchUtil.h"
|
2014-07-25 04:53:03 +04:00
|
|
|
#include "mozilla/dom/Promise.h"
|
2014-09-24 09:03:20 +04:00
|
|
|
#include "mozilla/dom/URL.h"
|
2015-07-28 00:57:34 +03:00
|
|
|
#include "mozilla/dom/WorkerPrivate.h"
|
2016-08-23 07:09:32 +03:00
|
|
|
#include "mozilla/Unused.h"
|
2014-09-24 09:03:20 +04:00
|
|
|
|
|
|
|
#include "WorkerPrivate.h"
|
2014-07-25 04:53:03 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(Request)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(Request)
|
2014-10-02 21:59:20 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(Request, mOwner, mHeaders)
|
2014-07-25 04:53:03 +04:00
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Request)
|
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2014-09-24 09:03:20 +04:00
|
|
|
Request::Request(nsIGlobalObject* aOwner, InternalRequest* aRequest)
|
2017-05-16 13:12:24 +03:00
|
|
|
: FetchBody<Request>(aOwner)
|
2014-09-24 09:03:20 +04:00
|
|
|
, mRequest(aRequest)
|
2014-07-25 04:53:03 +04:00
|
|
|
{
|
2016-06-02 00:02:29 +03:00
|
|
|
MOZ_ASSERT(aRequest->Headers()->Guard() == HeadersGuardEnum::Immutable ||
|
|
|
|
aRequest->Headers()->Guard() == HeadersGuardEnum::Request ||
|
|
|
|
aRequest->Headers()->Guard() == HeadersGuardEnum::Request_no_cors);
|
2015-02-03 23:59:32 +03:00
|
|
|
SetMimeType();
|
2014-07-25 04:53:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
Request::~Request()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-07-28 00:57:34 +03:00
|
|
|
// static
|
|
|
|
bool
|
|
|
|
Request::RequestContextEnabled(JSContext* aCx, JSObject* aObj)
|
|
|
|
{
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
return Preferences::GetBool("dom.requestcontext.enabled", false);
|
|
|
|
}
|
|
|
|
|
|
|
|
using namespace workers;
|
|
|
|
|
|
|
|
// Otherwise, check the pref via the WorkerPrivate
|
|
|
|
WorkerPrivate* workerPrivate = GetWorkerPrivateFromContext(aCx);
|
|
|
|
if (!workerPrivate) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return workerPrivate->RequestContextEnabled();
|
|
|
|
}
|
|
|
|
|
2014-09-24 09:03:20 +04:00
|
|
|
already_AddRefed<InternalRequest>
|
|
|
|
Request::GetInternalRequest()
|
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<InternalRequest> r = mRequest;
|
2014-09-24 09:03:20 +04:00
|
|
|
return r.forget();
|
|
|
|
}
|
|
|
|
|
2015-03-19 21:41:42 +03:00
|
|
|
namespace {
|
2016-02-26 02:12:20 +03:00
|
|
|
already_AddRefed<nsIURI>
|
|
|
|
ParseURLFromDocument(nsIDocument* aDocument, const nsAString& aInput,
|
|
|
|
ErrorResult& aRv)
|
2015-03-19 21:41:42 +03:00
|
|
|
{
|
2015-05-07 18:08:34 +03:00
|
|
|
MOZ_ASSERT(aDocument);
|
2015-03-19 21:41:42 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2015-05-07 18:08:34 +03:00
|
|
|
nsCOMPtr<nsIURI> baseURI = aDocument->GetBaseURI();
|
|
|
|
nsCOMPtr<nsIURI> resolvedURI;
|
|
|
|
aRv = NS_NewURI(getter_AddRefs(resolvedURI), aInput, nullptr, baseURI);
|
2015-03-19 21:41:42 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
2015-11-20 21:36:46 +03:00
|
|
|
aRv.ThrowTypeError<MSG_INVALID_URL>(aInput);
|
2016-02-26 02:12:20 +03:00
|
|
|
}
|
|
|
|
return resolvedURI.forget();
|
|
|
|
}
|
|
|
|
void
|
|
|
|
GetRequestURLFromDocument(nsIDocument* aDocument, const nsAString& aInput,
|
2016-11-07 05:16:34 +03:00
|
|
|
nsAString& aRequestURL, nsACString& aURLfragment,
|
|
|
|
ErrorResult& aRv)
|
2016-02-26 02:12:20 +03:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIURI> resolvedURI = ParseURLFromDocument(aDocument, aInput, aRv);
|
|
|
|
if (aRv.Failed()) {
|
2015-08-24 23:54:52 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// This fails with URIs with weird protocols, even when they are valid,
|
|
|
|
// so we ignore the failure
|
|
|
|
nsAutoCString credentials;
|
2015-11-02 08:53:26 +03:00
|
|
|
Unused << resolvedURI->GetUserPass(credentials);
|
2015-08-24 23:54:52 +03:00
|
|
|
if (!credentials.IsEmpty()) {
|
2015-11-20 21:36:46 +03:00
|
|
|
aRv.ThrowTypeError<MSG_URL_HAS_CREDENTIALS>(aInput);
|
2015-08-24 23:54:52 +03:00
|
|
|
return;
|
2015-03-19 21:41:42 +03:00
|
|
|
}
|
|
|
|
|
2015-08-13 18:22:19 +03:00
|
|
|
nsCOMPtr<nsIURI> resolvedURIClone;
|
|
|
|
// We use CloneIgnoringRef to strip away the fragment even if the original URI
|
|
|
|
// is immutable.
|
|
|
|
aRv = resolvedURI->CloneIgnoringRef(getter_AddRefs(resolvedURIClone));
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
2015-05-07 18:08:34 +03:00
|
|
|
nsAutoCString spec;
|
2015-08-13 18:22:19 +03:00
|
|
|
aRv = resolvedURIClone->GetSpec(spec);
|
2015-03-19 21:41:42 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
2015-05-07 18:08:34 +03:00
|
|
|
CopyUTF8toUTF16(spec, aRequestURL);
|
2015-03-19 21:41:42 +03:00
|
|
|
|
2016-11-07 05:16:34 +03:00
|
|
|
// Get the fragment from nsIURI.
|
|
|
|
aRv = resolvedURI->GetRef(aURLfragment);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2016-02-26 02:12:20 +03:00
|
|
|
already_AddRefed<nsIURI>
|
|
|
|
ParseURLFromChrome(const nsAString& aInput, ErrorResult& aRv)
|
2015-03-19 21:41:42 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
aRv = NS_NewURI(getter_AddRefs(uri), aInput, nullptr, nullptr);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
2015-11-20 21:36:46 +03:00
|
|
|
aRv.ThrowTypeError<MSG_INVALID_URL>(aInput);
|
2016-02-26 02:12:20 +03:00
|
|
|
}
|
|
|
|
return uri.forget();
|
|
|
|
}
|
|
|
|
void
|
|
|
|
GetRequestURLFromChrome(const nsAString& aInput, nsAString& aRequestURL,
|
2016-11-07 05:16:34 +03:00
|
|
|
nsACString& aURLfragment, ErrorResult& aRv)
|
2016-02-26 02:12:20 +03:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIURI> uri = ParseURLFromChrome(aInput, aRv);
|
|
|
|
if (aRv.Failed()) {
|
2015-08-24 23:54:52 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// This fails with URIs with weird protocols, even when they are valid,
|
|
|
|
// so we ignore the failure
|
|
|
|
nsAutoCString credentials;
|
2015-11-02 08:53:26 +03:00
|
|
|
Unused << uri->GetUserPass(credentials);
|
2015-08-24 23:54:52 +03:00
|
|
|
if (!credentials.IsEmpty()) {
|
2015-11-20 21:36:46 +03:00
|
|
|
aRv.ThrowTypeError<MSG_URL_HAS_CREDENTIALS>(aInput);
|
2015-03-19 21:41:42 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-08-13 18:22:19 +03:00
|
|
|
nsCOMPtr<nsIURI> uriClone;
|
|
|
|
// We use CloneIgnoringRef to strip away the fragment even if the original URI
|
|
|
|
// is immutable.
|
|
|
|
aRv = uri->CloneIgnoringRef(getter_AddRefs(uriClone));
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
2015-03-19 21:41:42 +03:00
|
|
|
nsAutoCString spec;
|
2015-08-13 18:22:19 +03:00
|
|
|
aRv = uriClone->GetSpec(spec);
|
2015-03-19 21:41:42 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
CopyUTF8toUTF16(spec, aRequestURL);
|
|
|
|
|
2016-11-07 05:16:34 +03:00
|
|
|
// Get the fragment from nsIURI.
|
|
|
|
aRv = uri->GetRef(aURLfragment);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2016-06-29 08:43:44 +03:00
|
|
|
already_AddRefed<URL>
|
2016-02-26 02:12:20 +03:00
|
|
|
ParseURLFromWorker(const GlobalObject& aGlobal, const nsAString& aInput,
|
|
|
|
ErrorResult& aRv)
|
2015-03-19 21:41:42 +03:00
|
|
|
{
|
|
|
|
workers::WorkerPrivate* worker = workers::GetCurrentThreadWorkerPrivate();
|
|
|
|
MOZ_ASSERT(worker);
|
|
|
|
worker->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
NS_ConvertUTF8toUTF16 baseURL(worker->GetLocationInfo().mHref);
|
2016-06-29 08:43:44 +03:00
|
|
|
RefPtr<URL> url = URL::WorkerConstructor(aGlobal, aInput, baseURL, aRv);
|
2015-03-19 21:41:42 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
2015-11-20 21:36:46 +03:00
|
|
|
aRv.ThrowTypeError<MSG_INVALID_URL>(aInput);
|
2016-02-26 02:12:20 +03:00
|
|
|
}
|
|
|
|
return url.forget();
|
|
|
|
}
|
|
|
|
void
|
|
|
|
GetRequestURLFromWorker(const GlobalObject& aGlobal, const nsAString& aInput,
|
2016-11-07 05:16:34 +03:00
|
|
|
nsAString& aRequestURL, nsACString& aURLfragment,
|
|
|
|
ErrorResult& aRv)
|
2016-02-26 02:12:20 +03:00
|
|
|
{
|
2016-06-29 08:43:44 +03:00
|
|
|
RefPtr<URL> url = ParseURLFromWorker(aGlobal, aInput, aRv);
|
2016-02-26 02:12:20 +03:00
|
|
|
if (aRv.Failed()) {
|
2015-08-24 23:54:52 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
nsString username;
|
2015-11-24 08:04:21 +03:00
|
|
|
url->GetUsername(username, aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
2015-08-24 23:54:52 +03:00
|
|
|
|
|
|
|
nsString password;
|
2015-11-24 08:04:21 +03:00
|
|
|
url->GetPassword(password, aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
2015-08-24 23:54:52 +03:00
|
|
|
if (!username.IsEmpty() || !password.IsEmpty()) {
|
2015-11-20 21:36:46 +03:00
|
|
|
aRv.ThrowTypeError<MSG_URL_HAS_CREDENTIALS>(aInput);
|
2015-03-19 21:41:42 +03:00
|
|
|
return;
|
|
|
|
}
|
2016-11-07 05:16:34 +03:00
|
|
|
// Get the fragment from URL.
|
|
|
|
nsAutoString fragment;
|
|
|
|
url->GetHash(fragment, aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note: URL::GetHash() includes the "#" and we want the fragment with out
|
|
|
|
// the hash symbol.
|
|
|
|
if (!fragment.IsEmpty()) {
|
|
|
|
CopyUTF16toUTF8(Substring(fragment, 1), aURLfragment);
|
|
|
|
}
|
2015-03-19 21:41:42 +03:00
|
|
|
|
2015-11-24 08:04:21 +03:00
|
|
|
url->SetHash(EmptyString(), aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
url->Stringify(aRequestURL, aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
2015-03-19 21:41:42 +03:00
|
|
|
}
|
|
|
|
|
2016-02-26 02:12:20 +03:00
|
|
|
class ReferrerSameOriginChecker final : public workers::WorkerMainThreadRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ReferrerSameOriginChecker(workers::WorkerPrivate* aWorkerPrivate,
|
|
|
|
const nsAString& aReferrerURL,
|
|
|
|
nsresult& aResult)
|
2016-05-03 10:09:47 +03:00
|
|
|
: workers::WorkerMainThreadRunnable(aWorkerPrivate,
|
|
|
|
NS_LITERAL_CSTRING("Fetch :: Referrer same origin check")),
|
2016-02-26 02:12:20 +03:00
|
|
|
mReferrerURL(aReferrerURL),
|
|
|
|
mResult(aResult)
|
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
MainThreadRun() override
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
if (NS_SUCCEEDED(NS_NewURI(getter_AddRefs(uri), mReferrerURL))) {
|
|
|
|
nsCOMPtr<nsIPrincipal> principal = mWorkerPrivate->GetPrincipal();
|
|
|
|
if (principal) {
|
|
|
|
mResult = principal->CheckMayLoad(uri, /* report */ false,
|
|
|
|
/* allowIfInheritsPrincipal */ false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const nsString mReferrerURL;
|
|
|
|
nsresult& mResult;
|
|
|
|
};
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2015-03-19 21:41:42 +03:00
|
|
|
|
2014-07-25 04:53:03 +04:00
|
|
|
/*static*/ already_AddRefed<Request>
|
2014-09-24 09:03:20 +04:00
|
|
|
Request::Constructor(const GlobalObject& aGlobal,
|
2014-11-20 14:58:00 +03:00
|
|
|
const RequestOrUSVString& aInput,
|
2014-09-24 09:03:20 +04:00
|
|
|
const RequestInit& aInit, ErrorResult& aRv)
|
2014-07-25 04:53:03 +04:00
|
|
|
{
|
2017-03-01 17:59:01 +03:00
|
|
|
bool hasCopiedBody = false;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<InternalRequest> request;
|
2014-09-24 09:03:20 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
|
|
|
|
|
|
|
|
if (aInput.IsRequest()) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Request> inputReq = &aInput.GetAsRequest();
|
2014-12-23 13:25:17 +03:00
|
|
|
nsCOMPtr<nsIInputStream> body;
|
|
|
|
inputReq->GetBody(getter_AddRefs(body));
|
2016-01-14 16:57:00 +03:00
|
|
|
if (inputReq->BodyUsed()) {
|
|
|
|
aRv.ThrowTypeError<MSG_FETCH_BODY_CONSUMED_ERROR>();
|
|
|
|
return nullptr;
|
|
|
|
}
|
2017-03-01 17:59:01 +03:00
|
|
|
|
|
|
|
// The body will be copied when GetRequestConstructorCopy() is executed.
|
2014-12-23 13:25:17 +03:00
|
|
|
if (body) {
|
2017-03-01 17:59:01 +03:00
|
|
|
hasCopiedBody = true;
|
2014-09-24 09:03:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
request = inputReq->GetInternalRequest();
|
|
|
|
} else {
|
2016-05-03 04:48:40 +03:00
|
|
|
// aInput is USVString.
|
|
|
|
// We need to get url before we create a InternalRequest.
|
2015-03-19 21:41:42 +03:00
|
|
|
nsAutoString input;
|
2014-11-20 14:58:00 +03:00
|
|
|
input.Assign(aInput.GetAsUSVString());
|
2015-03-19 21:41:42 +03:00
|
|
|
nsAutoString requestURL;
|
2016-11-07 05:16:34 +03:00
|
|
|
nsCString fragment;
|
2014-09-24 09:03:20 +04:00
|
|
|
if (NS_IsMainThread()) {
|
2015-05-07 18:08:34 +03:00
|
|
|
nsIDocument* doc = GetEntryDocument();
|
|
|
|
if (doc) {
|
2016-11-07 05:16:34 +03:00
|
|
|
GetRequestURLFromDocument(doc, input, requestURL, fragment, aRv);
|
2015-03-19 21:41:42 +03:00
|
|
|
} else {
|
2015-05-07 18:08:34 +03:00
|
|
|
// If we don't have a document, we must assume that this is a full URL.
|
2016-11-07 05:16:34 +03:00
|
|
|
GetRequestURLFromChrome(input, requestURL, fragment, aRv);
|
2014-09-24 09:03:20 +04:00
|
|
|
}
|
|
|
|
} else {
|
2016-11-07 05:16:34 +03:00
|
|
|
GetRequestURLFromWorker(aGlobal, input, requestURL, fragment, aRv);
|
2015-03-19 21:41:42 +03:00
|
|
|
}
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
2014-09-24 09:03:20 +04:00
|
|
|
}
|
2016-11-07 05:16:34 +03:00
|
|
|
request = new InternalRequest(NS_ConvertUTF16toUTF8(requestURL), fragment);
|
2016-05-03 04:48:40 +03:00
|
|
|
}
|
|
|
|
request = request->GetRequestConstructorCopy(global, aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
RequestMode fallbackMode = RequestMode::EndGuard_;
|
|
|
|
RequestCredentials fallbackCredentials = RequestCredentials::EndGuard_;
|
|
|
|
RequestCache fallbackCache = RequestCache::EndGuard_;
|
|
|
|
if (aInput.IsUSVString()) {
|
2014-09-24 09:03:20 +04:00
|
|
|
fallbackMode = RequestMode::Cors;
|
|
|
|
fallbackCredentials = RequestCredentials::Omit;
|
2015-03-22 20:13:18 +03:00
|
|
|
fallbackCache = RequestCache::Default;
|
2014-09-24 09:03:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
RequestMode mode = aInit.mMode.WasPassed() ? aInit.mMode.Value() : fallbackMode;
|
|
|
|
RequestCredentials credentials =
|
|
|
|
aInit.mCredentials.WasPassed() ? aInit.mCredentials.Value()
|
|
|
|
: fallbackCredentials;
|
|
|
|
|
2016-02-24 21:46:38 +03:00
|
|
|
if (mode == RequestMode::Navigate ||
|
|
|
|
(aInit.IsAnyMemberPresent() && request->Mode() == RequestMode::Navigate)) {
|
2017-03-01 13:44:38 +03:00
|
|
|
mode = RequestMode::Same_origin;
|
2016-01-14 02:15:37 +03:00
|
|
|
}
|
|
|
|
|
2016-02-26 02:12:20 +03:00
|
|
|
if (aInit.IsAnyMemberPresent()) {
|
|
|
|
request->SetReferrer(NS_LITERAL_STRING(kFETCH_CLIENT_REFERRER_STR));
|
2016-02-27 01:36:45 +03:00
|
|
|
request->SetReferrerPolicy(ReferrerPolicy::_empty);
|
2016-02-26 02:12:20 +03:00
|
|
|
}
|
|
|
|
if (aInit.mReferrer.WasPassed()) {
|
|
|
|
const nsString& referrer = aInit.mReferrer.Value();
|
|
|
|
if (referrer.IsEmpty()) {
|
|
|
|
request->SetReferrer(NS_LITERAL_STRING(""));
|
|
|
|
} else {
|
|
|
|
nsAutoString referrerURL;
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
nsIDocument* doc = GetEntryDocument();
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
if (doc) {
|
|
|
|
uri = ParseURLFromDocument(doc, referrer, aRv);
|
|
|
|
} else {
|
|
|
|
// If we don't have a document, we must assume that this is a full URL.
|
|
|
|
uri = ParseURLFromChrome(referrer, aRv);
|
|
|
|
}
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
aRv.ThrowTypeError<MSG_INVALID_REFERRER_URL>(referrer);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
nsAutoCString spec;
|
|
|
|
uri->GetSpec(spec);
|
|
|
|
CopyUTF8toUTF16(spec, referrerURL);
|
|
|
|
if (!referrerURL.EqualsLiteral(kFETCH_CLIENT_REFERRER_STR)) {
|
|
|
|
nsCOMPtr<nsIPrincipal> principal = global->PrincipalOrNull();
|
|
|
|
if (principal) {
|
|
|
|
nsresult rv = principal->CheckMayLoad(uri, /* report */ false,
|
|
|
|
/* allowIfInheritsPrincipal */ false);
|
|
|
|
if (NS_FAILED(rv)) {
|
2017-03-01 13:44:38 +03:00
|
|
|
referrerURL.AssignLiteral(kFETCH_CLIENT_REFERRER_STR);
|
2016-02-26 02:12:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2016-06-29 08:43:44 +03:00
|
|
|
RefPtr<URL> url = ParseURLFromWorker(aGlobal, referrer, aRv);
|
2016-02-26 02:12:20 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
aRv.ThrowTypeError<MSG_INVALID_REFERRER_URL>(referrer);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
url->Stringify(referrerURL, aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
aRv.ThrowTypeError<MSG_INVALID_REFERRER_URL>(referrer);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (!referrerURL.EqualsLiteral(kFETCH_CLIENT_REFERRER_STR)) {
|
|
|
|
workers::WorkerPrivate* worker = workers::GetCurrentThreadWorkerPrivate();
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
// ReferrerSameOriginChecker uses a sync loop to get the main thread
|
|
|
|
// to perform the same-origin check. Overall, on Workers this method
|
|
|
|
// can create 3 sync loops (two for constructing URLs and one here) so
|
|
|
|
// in the future we may want to optimize it all by off-loading all of
|
|
|
|
// this work in a single sync loop.
|
|
|
|
RefPtr<ReferrerSameOriginChecker> checker =
|
|
|
|
new ReferrerSameOriginChecker(worker, referrerURL, rv);
|
2017-03-01 13:44:38 +03:00
|
|
|
IgnoredErrorResult error;
|
|
|
|
checker->Dispatch(Terminating, error);
|
|
|
|
if (error.Failed() || NS_FAILED(rv)) {
|
|
|
|
referrerURL.AssignLiteral(kFETCH_CLIENT_REFERRER_STR);
|
2016-02-26 02:12:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
request->SetReferrer(referrerURL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-27 01:36:45 +03:00
|
|
|
if (aInit.mReferrerPolicy.WasPassed()) {
|
|
|
|
request->SetReferrerPolicy(aInit.mReferrerPolicy.Value());
|
|
|
|
}
|
|
|
|
|
2016-04-01 06:52:28 +03:00
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
nsCOMPtr<nsPIDOMWindowInner> window = do_QueryInterface(global);
|
|
|
|
if (window) {
|
|
|
|
nsCOMPtr<nsIDocument> doc;
|
|
|
|
doc = window->GetExtantDoc();
|
|
|
|
if (doc) {
|
|
|
|
request->SetEnvironmentReferrerPolicy(doc->GetReferrerPolicy());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
workers::WorkerPrivate* worker = workers::GetCurrentThreadWorkerPrivate();
|
|
|
|
if (worker) {
|
|
|
|
worker->AssertIsOnWorkerThread();
|
|
|
|
request->SetEnvironmentReferrerPolicy(worker->GetReferrerPolicy());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-24 09:03:20 +04:00
|
|
|
if (mode != RequestMode::EndGuard_) {
|
2015-03-27 13:47:00 +03:00
|
|
|
request->ClearCreatedByFetchEvent();
|
2014-09-24 09:03:20 +04:00
|
|
|
request->SetMode(mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (credentials != RequestCredentials::EndGuard_) {
|
2015-03-27 13:47:00 +03:00
|
|
|
request->ClearCreatedByFetchEvent();
|
2014-09-24 09:03:20 +04:00
|
|
|
request->SetCredentialsMode(credentials);
|
|
|
|
}
|
|
|
|
|
2015-03-22 20:13:18 +03:00
|
|
|
RequestCache cache = aInit.mCache.WasPassed() ?
|
|
|
|
aInit.mCache.Value() : fallbackCache;
|
|
|
|
if (cache != RequestCache::EndGuard_) {
|
2016-05-17 06:20:52 +03:00
|
|
|
if (cache == RequestCache::Only_if_cached &&
|
|
|
|
request->Mode() != RequestMode::Same_origin) {
|
|
|
|
uint32_t t = static_cast<uint32_t>(request->Mode());
|
|
|
|
NS_ConvertASCIItoUTF16 modeString(RequestModeValues::strings[t].value,
|
|
|
|
RequestModeValues::strings[t].length);
|
|
|
|
aRv.ThrowTypeError<MSG_ONLY_IF_CACHED_WITHOUT_SAME_ORIGIN>(modeString);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-03-27 13:47:00 +03:00
|
|
|
request->ClearCreatedByFetchEvent();
|
2015-03-22 20:13:18 +03:00
|
|
|
request->SetCacheMode(cache);
|
|
|
|
}
|
|
|
|
|
2015-09-01 00:26:29 +03:00
|
|
|
if (aInit.mRedirect.WasPassed()) {
|
|
|
|
request->SetRedirectMode(aInit.mRedirect.Value());
|
|
|
|
}
|
|
|
|
|
2016-09-07 05:20:23 +03:00
|
|
|
if (aInit.mIntegrity.WasPassed()) {
|
|
|
|
request->SetIntegrity(aInit.mIntegrity.Value());
|
|
|
|
}
|
|
|
|
|
2015-01-15 23:11:12 +03:00
|
|
|
// Request constructor step 14.
|
2014-09-24 09:03:20 +04:00
|
|
|
if (aInit.mMethod.WasPassed()) {
|
2015-01-15 23:11:12 +03:00
|
|
|
nsAutoCString method(aInit.mMethod.Value());
|
|
|
|
|
|
|
|
// Step 14.1. Disallow forbidden methods, and anything that is not a HTTP
|
|
|
|
// token, since HTTP states that Method may be any of the defined values or
|
|
|
|
// a token (extension method).
|
2015-09-11 21:26:33 +03:00
|
|
|
nsAutoCString outMethod;
|
|
|
|
nsresult rv = FetchUtil::GetValidRequestMethod(method, outMethod);
|
|
|
|
if (NS_FAILED(rv)) {
|
2014-09-24 09:03:20 +04:00
|
|
|
NS_ConvertUTF8toUTF16 label(method);
|
2015-11-20 21:36:46 +03:00
|
|
|
aRv.ThrowTypeError<MSG_INVALID_REQUEST_METHOD>(label);
|
2014-09-24 09:03:20 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-01-15 23:11:12 +03:00
|
|
|
// Step 14.2
|
2015-09-11 21:26:33 +03:00
|
|
|
request->ClearCreatedByFetchEvent();
|
|
|
|
request->SetMethod(outMethod);
|
2014-09-24 09:03:20 +04:00
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<InternalHeaders> requestHeaders = request->Headers();
|
2014-09-24 09:03:20 +04:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<InternalHeaders> headers;
|
2014-09-24 09:03:20 +04:00
|
|
|
if (aInit.mHeaders.WasPassed()) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Headers> h = Headers::Constructor(aGlobal, aInit.mHeaders.Value(), aRv);
|
2014-09-24 09:03:20 +04:00
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-03-27 13:47:00 +03:00
|
|
|
request->ClearCreatedByFetchEvent();
|
2014-10-02 21:59:20 +04:00
|
|
|
headers = h->GetInternalHeaders();
|
2014-09-24 09:03:20 +04:00
|
|
|
} else {
|
2014-10-02 21:59:20 +04:00
|
|
|
headers = new InternalHeaders(*requestHeaders);
|
2014-09-24 09:03:20 +04:00
|
|
|
}
|
|
|
|
|
2014-10-02 21:59:20 +04:00
|
|
|
requestHeaders->Clear();
|
2015-05-08 01:39:13 +03:00
|
|
|
// From "Let r be a new Request object associated with request and a new
|
|
|
|
// Headers object whose guard is "request"."
|
|
|
|
requestHeaders->SetGuard(HeadersGuardEnum::Request, aRv);
|
|
|
|
MOZ_ASSERT(!aRv.Failed());
|
2014-09-24 09:03:20 +04:00
|
|
|
|
2014-10-02 21:59:20 +04:00
|
|
|
if (request->Mode() == RequestMode::No_cors) {
|
2014-10-06 22:01:20 +04:00
|
|
|
if (!request->HasSimpleMethod()) {
|
|
|
|
nsAutoCString method;
|
|
|
|
request->GetMethod(method);
|
2014-09-24 09:03:20 +04:00
|
|
|
NS_ConvertUTF8toUTF16 label(method);
|
2015-11-20 21:36:46 +03:00
|
|
|
aRv.ThrowTypeError<MSG_INVALID_REQUEST_METHOD>(label);
|
2014-09-24 09:03:20 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-09-07 05:20:23 +03:00
|
|
|
if (!request->GetIntegrity().IsEmpty()) {
|
|
|
|
aRv.ThrowTypeError<MSG_REQUEST_INTEGRITY_METADATA_NOT_EMPTY>();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-10-02 21:59:20 +04:00
|
|
|
requestHeaders->SetGuard(HeadersGuardEnum::Request_no_cors, aRv);
|
2014-09-24 09:03:20 +04:00
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-02 21:59:20 +04:00
|
|
|
requestHeaders->Fill(*headers, aRv);
|
2014-09-24 09:03:20 +04:00
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-02-24 19:34:33 +03:00
|
|
|
if ((aInit.mBody.WasPassed() && !aInit.mBody.Value().IsNull()) ||
|
2017-03-01 17:59:01 +03:00
|
|
|
hasCopiedBody) {
|
2015-01-17 00:08:19 +03:00
|
|
|
// HEAD and GET are not allowed to have a body.
|
|
|
|
nsAutoCString method;
|
|
|
|
request->GetMethod(method);
|
|
|
|
// method is guaranteed to be uppercase due to step 14.2 above.
|
|
|
|
if (method.EqualsLiteral("HEAD") || method.EqualsLiteral("GET")) {
|
2015-10-05 19:38:14 +03:00
|
|
|
aRv.ThrowTypeError<MSG_NO_BODY_ALLOWED_FOR_GET_AND_HEAD>();
|
2015-01-17 00:08:19 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
2015-05-14 01:15:23 +03:00
|
|
|
}
|
2015-01-17 00:08:19 +03:00
|
|
|
|
2015-05-14 01:15:23 +03:00
|
|
|
if (aInit.mBody.WasPassed()) {
|
2017-02-09 12:37:54 +03:00
|
|
|
const Nullable<fetch::OwningBodyInit>& bodyInitNullable = aInit.mBody.Value();
|
2016-02-24 19:34:33 +03:00
|
|
|
if (!bodyInitNullable.IsNull()) {
|
2017-02-09 12:37:54 +03:00
|
|
|
const fetch::OwningBodyInit& bodyInit = bodyInitNullable.Value();
|
2016-02-24 19:34:33 +03:00
|
|
|
nsCOMPtr<nsIInputStream> stream;
|
2017-01-24 14:13:31 +03:00
|
|
|
nsAutoCString contentTypeWithCharset;
|
2016-06-02 00:02:29 +03:00
|
|
|
uint64_t contentLengthUnused;
|
2016-02-24 19:34:33 +03:00
|
|
|
aRv = ExtractByteStreamFromBody(bodyInit,
|
2016-06-02 00:02:29 +03:00
|
|
|
getter_AddRefs(stream),
|
2017-01-24 14:13:31 +03:00
|
|
|
contentTypeWithCharset,
|
2016-06-02 00:02:29 +03:00
|
|
|
contentLengthUnused);
|
2016-02-24 19:34:33 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-08-10 22:06:00 +03:00
|
|
|
|
2017-03-01 17:59:01 +03:00
|
|
|
nsCOMPtr<nsIInputStream> temporaryBody = stream;
|
2014-09-24 09:03:20 +04:00
|
|
|
|
2017-01-24 14:13:31 +03:00
|
|
|
if (!contentTypeWithCharset.IsVoid() &&
|
2016-02-24 19:34:33 +03:00
|
|
|
!requestHeaders->Has(NS_LITERAL_CSTRING("Content-Type"), aRv)) {
|
|
|
|
requestHeaders->Append(NS_LITERAL_CSTRING("Content-Type"),
|
2017-01-24 14:13:31 +03:00
|
|
|
contentTypeWithCharset, aRv);
|
2016-02-24 19:34:33 +03:00
|
|
|
}
|
2014-09-24 09:03:20 +04:00
|
|
|
|
2016-02-24 19:34:33 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-08-10 22:06:00 +03:00
|
|
|
|
2016-02-24 19:34:33 +03:00
|
|
|
request->ClearCreatedByFetchEvent();
|
2017-03-01 17:59:01 +03:00
|
|
|
|
|
|
|
if (hasCopiedBody) {
|
|
|
|
request->SetBody(nullptr);
|
|
|
|
}
|
|
|
|
|
2016-02-24 19:34:33 +03:00
|
|
|
request->SetBody(temporaryBody);
|
|
|
|
}
|
2014-09-24 09:03:20 +04:00
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Request> domRequest = new Request(global, request);
|
2015-02-03 23:59:32 +03:00
|
|
|
domRequest->SetMimeType();
|
2015-08-10 22:06:00 +03:00
|
|
|
|
|
|
|
if (aInput.IsRequest()) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Request> inputReq = &aInput.GetAsRequest();
|
2015-08-10 22:06:00 +03:00
|
|
|
nsCOMPtr<nsIInputStream> body;
|
|
|
|
inputReq->GetBody(getter_AddRefs(body));
|
|
|
|
if (body) {
|
|
|
|
inputReq->SetBody(nullptr);
|
|
|
|
inputReq->SetBodyUsed();
|
|
|
|
}
|
|
|
|
}
|
2014-09-24 09:03:20 +04:00
|
|
|
return domRequest.forget();
|
2014-07-25 04:53:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<Request>
|
2015-02-20 04:24:24 +03:00
|
|
|
Request::Clone(ErrorResult& aRv) const
|
2014-07-25 04:53:03 +04:00
|
|
|
{
|
2015-02-20 04:24:24 +03:00
|
|
|
if (BodyUsed()) {
|
2015-10-05 19:38:14 +03:00
|
|
|
aRv.ThrowTypeError<MSG_FETCH_BODY_CONSUMED_ERROR>();
|
2015-02-20 04:24:24 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<InternalRequest> ir = mRequest->Clone();
|
2015-02-20 04:24:24 +03:00
|
|
|
if (!ir) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Request> request = new Request(mOwner, ir);
|
2014-07-25 04:53:03 +04:00
|
|
|
return request.forget();
|
|
|
|
}
|
2014-10-02 21:59:20 +04:00
|
|
|
|
|
|
|
Headers*
|
|
|
|
Request::Headers_()
|
|
|
|
{
|
|
|
|
if (!mHeaders) {
|
|
|
|
mHeaders = new Headers(mOwner, mRequest->Headers());
|
|
|
|
}
|
|
|
|
|
|
|
|
return mHeaders;
|
|
|
|
}
|
|
|
|
|
2014-07-25 04:53:03 +04:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|