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"
|
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"
|
2014-09-24 09:03:20 +04:00
|
|
|
#include "mozilla/dom/workers/bindings/URL.h"
|
2015-08-24 23:54:52 +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)
|
2014-09-27 03:41:15 +04:00
|
|
|
: FetchBody<Request>()
|
|
|
|
, mOwner(aOwner)
|
2014-09-24 09:03:20 +04:00
|
|
|
, mRequest(aRequest)
|
2014-07-25 04:53:03 +04:00
|
|
|
{
|
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()
|
|
|
|
{
|
|
|
|
nsRefPtr<InternalRequest> r = mRequest;
|
|
|
|
return r.forget();
|
|
|
|
}
|
|
|
|
|
2015-03-19 21:41:42 +03:00
|
|
|
namespace {
|
|
|
|
void
|
2015-05-07 18:08:34 +03:00
|
|
|
GetRequestURLFromDocument(nsIDocument* aDocument, const nsAString& aInput,
|
|
|
|
nsAString& aRequestURL, 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-08-24 23:54:52 +03:00
|
|
|
aRv.ThrowTypeError(MSG_INVALID_URL, &aInput);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This fails with URIs with weird protocols, even when they are valid,
|
|
|
|
// so we ignore the failure
|
|
|
|
nsAutoCString credentials;
|
|
|
|
unused << resolvedURI->GetUserPass(credentials);
|
|
|
|
if (!credentials.IsEmpty()) {
|
|
|
|
aRv.ThrowTypeError(MSG_URL_HAS_CREDENTIALS, &aInput);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GetRequestURLFromChrome(const nsAString& aInput, nsAString& aRequestURL,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
aRv = NS_NewURI(getter_AddRefs(uri), aInput, nullptr, nullptr);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
2015-08-24 23:54:52 +03:00
|
|
|
aRv.ThrowTypeError(MSG_INVALID_URL, &aInput);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This fails with URIs with weird protocols, even when they are valid,
|
|
|
|
// so we ignore the failure
|
|
|
|
nsAutoCString credentials;
|
|
|
|
unused << uri->GetUserPass(credentials);
|
|
|
|
if (!credentials.IsEmpty()) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GetRequestURLFromWorker(const GlobalObject& aGlobal, const nsAString& aInput,
|
|
|
|
nsAString& aRequestURL, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
workers::WorkerPrivate* worker = workers::GetCurrentThreadWorkerPrivate();
|
|
|
|
MOZ_ASSERT(worker);
|
|
|
|
worker->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
NS_ConvertUTF8toUTF16 baseURL(worker->GetLocationInfo().mHref);
|
|
|
|
nsRefPtr<workers::URL> url =
|
|
|
|
workers::URL::Constructor(aGlobal, aInput, baseURL, aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
2015-08-24 23:54:52 +03:00
|
|
|
aRv.ThrowTypeError(MSG_INVALID_URL, &aInput);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsString username;
|
|
|
|
url->GetUsername(username, aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsString password;
|
|
|
|
url->GetPassword(password, aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!username.IsEmpty() || !password.IsEmpty()) {
|
|
|
|
aRv.ThrowTypeError(MSG_URL_HAS_CREDENTIALS, &aInput);
|
2015-03-19 21:41:42 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-08-13 18:22:19 +03:00
|
|
|
url->SetHash(EmptyString(), aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-19 21:41:42 +03:00
|
|
|
url->Stringify(aRequestURL, aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2015-08-10 22:06:00 +03:00
|
|
|
nsCOMPtr<nsIInputStream> temporaryBody;
|
2014-09-24 09:03:20 +04:00
|
|
|
nsRefPtr<InternalRequest> request;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
|
|
|
|
|
|
|
|
if (aInput.IsRequest()) {
|
|
|
|
nsRefPtr<Request> inputReq = &aInput.GetAsRequest();
|
2014-12-23 13:25:17 +03:00
|
|
|
nsCOMPtr<nsIInputStream> body;
|
|
|
|
inputReq->GetBody(getter_AddRefs(body));
|
|
|
|
if (body) {
|
|
|
|
if (inputReq->BodyUsed()) {
|
2015-02-20 04:24:24 +03:00
|
|
|
aRv.ThrowTypeError(MSG_FETCH_BODY_CONSUMED_ERROR);
|
2014-12-23 13:25:17 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
2015-08-10 22:06:00 +03:00
|
|
|
temporaryBody = body;
|
2014-09-24 09:03:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
request = inputReq->GetInternalRequest();
|
|
|
|
} else {
|
|
|
|
request = new InternalRequest();
|
|
|
|
}
|
|
|
|
|
|
|
|
request = request->GetRequestConstructorCopy(global, aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
RequestMode fallbackMode = RequestMode::EndGuard_;
|
|
|
|
RequestCredentials fallbackCredentials = RequestCredentials::EndGuard_;
|
2015-03-22 20:13:18 +03:00
|
|
|
RequestCache fallbackCache = RequestCache::EndGuard_;
|
2014-11-20 14:58:00 +03:00
|
|
|
if (aInput.IsUSVString()) {
|
2015-03-19 21:41:42 +03:00
|
|
|
nsAutoString input;
|
2014-11-20 14:58:00 +03:00
|
|
|
input.Assign(aInput.GetAsUSVString());
|
2014-09-24 09:03:20 +04:00
|
|
|
|
2015-03-19 21:41:42 +03:00
|
|
|
nsAutoString requestURL;
|
2014-09-24 09:03:20 +04:00
|
|
|
if (NS_IsMainThread()) {
|
2015-05-07 18:08:34 +03:00
|
|
|
nsIDocument* doc = GetEntryDocument();
|
|
|
|
if (doc) {
|
|
|
|
GetRequestURLFromDocument(doc, input, requestURL, 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.
|
2015-03-19 21:41:42 +03:00
|
|
|
GetRequestURLFromChrome(input, requestURL, aRv);
|
2014-09-24 09:03:20 +04:00
|
|
|
}
|
|
|
|
} else {
|
2015-03-19 21:41:42 +03:00
|
|
|
GetRequestURLFromWorker(aGlobal, input, requestURL, aRv);
|
|
|
|
}
|
2014-09-24 09:03:20 +04:00
|
|
|
|
2015-03-19 21:41:42 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
2014-09-24 09:03:20 +04:00
|
|
|
}
|
2015-03-19 21:41:42 +03:00
|
|
|
|
2014-09-24 09:03:20 +04:00
|
|
|
request->SetURL(NS_ConvertUTF16toUTF8(requestURL));
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-01-08 03:24:40 +03:00
|
|
|
// CORS-with-forced-preflight is not publicly exposed and should not be
|
|
|
|
// considered a valid value.
|
|
|
|
if (aInit.mMode.WasPassed() &&
|
|
|
|
aInit.mMode.Value() == RequestMode::Cors_with_forced_preflight) {
|
|
|
|
NS_NAMED_LITERAL_STRING(sourceDescription, "'mode' member of RequestInit");
|
|
|
|
NS_NAMED_LITERAL_STRING(value, "cors-with-forced-preflight");
|
|
|
|
NS_NAMED_LITERAL_STRING(type, "RequestMode");
|
|
|
|
aRv.ThrowTypeError(MSG_INVALID_ENUM_VALUE, &sourceDescription, &value, &type);
|
|
|
|
return nullptr;
|
|
|
|
}
|
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;
|
|
|
|
|
|
|
|
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_) {
|
2015-03-27 13:47:00 +03:00
|
|
|
request->ClearCreatedByFetchEvent();
|
2015-03-22 20:13:18 +03:00
|
|
|
request->SetCacheMode(cache);
|
|
|
|
}
|
|
|
|
|
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());
|
|
|
|
nsAutoCString upperCaseMethod = method;
|
|
|
|
ToUpperCase(upperCaseMethod);
|
|
|
|
|
|
|
|
// 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).
|
|
|
|
if (upperCaseMethod.EqualsLiteral("CONNECT") ||
|
|
|
|
upperCaseMethod.EqualsLiteral("TRACE") ||
|
|
|
|
upperCaseMethod.EqualsLiteral("TRACK") ||
|
|
|
|
!NS_IsValidHTTPToken(method)) {
|
2014-09-24 09:03:20 +04:00
|
|
|
NS_ConvertUTF8toUTF16 label(method);
|
|
|
|
aRv.ThrowTypeError(MSG_INVALID_REQUEST_METHOD, &label);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-01-15 23:11:12 +03:00
|
|
|
// Step 14.2
|
|
|
|
if (upperCaseMethod.EqualsLiteral("DELETE") ||
|
|
|
|
upperCaseMethod.EqualsLiteral("GET") ||
|
|
|
|
upperCaseMethod.EqualsLiteral("HEAD") ||
|
|
|
|
upperCaseMethod.EqualsLiteral("POST") ||
|
|
|
|
upperCaseMethod.EqualsLiteral("PUT") ||
|
|
|
|
upperCaseMethod.EqualsLiteral("OPTIONS")) {
|
2015-03-27 13:47:00 +03:00
|
|
|
request->ClearCreatedByFetchEvent();
|
2015-01-15 23:11:12 +03:00
|
|
|
request->SetMethod(upperCaseMethod);
|
|
|
|
} else {
|
2015-03-27 13:47:00 +03:00
|
|
|
request->ClearCreatedByFetchEvent();
|
2015-01-15 23:11:12 +03:00
|
|
|
request->SetMethod(method);
|
|
|
|
}
|
2014-09-24 09:03:20 +04:00
|
|
|
}
|
|
|
|
|
2014-10-02 21:59:20 +04:00
|
|
|
nsRefPtr<InternalHeaders> requestHeaders = request->Headers();
|
2014-09-24 09:03:20 +04:00
|
|
|
|
2014-10-02 21:59:20 +04:00
|
|
|
nsRefPtr<InternalHeaders> headers;
|
2014-09-24 09:03:20 +04:00
|
|
|
if (aInit.mHeaders.WasPassed()) {
|
2014-10-02 21:59:20 +04:00
|
|
|
nsRefPtr<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);
|
|
|
|
aRv.ThrowTypeError(MSG_INVALID_REQUEST_METHOD, &label);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-08-10 22:06:00 +03:00
|
|
|
if (aInit.mBody.WasPassed() || temporaryBody) {
|
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")) {
|
|
|
|
aRv.ThrowTypeError(MSG_NO_BODY_ALLOWED_FOR_GET_AND_HEAD);
|
|
|
|
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()) {
|
2015-03-17 18:32:04 +03:00
|
|
|
const OwningArrayBufferOrArrayBufferViewOrBlobOrFormDataOrUSVStringOrURLSearchParams& bodyInit = aInit.mBody.Value();
|
2014-09-24 09:03:20 +04:00
|
|
|
nsCOMPtr<nsIInputStream> stream;
|
2015-03-19 21:41:42 +03:00
|
|
|
nsAutoCString contentType;
|
2014-09-24 09:03:20 +04:00
|
|
|
aRv = ExtractByteStreamFromBody(bodyInit,
|
|
|
|
getter_AddRefs(stream), contentType);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-08-10 22:06:00 +03:00
|
|
|
|
|
|
|
temporaryBody = stream;
|
2014-09-24 09:03:20 +04:00
|
|
|
|
|
|
|
if (!contentType.IsVoid() &&
|
2014-10-02 21:59:20 +04:00
|
|
|
!requestHeaders->Has(NS_LITERAL_CSTRING("Content-Type"), aRv)) {
|
|
|
|
requestHeaders->Append(NS_LITERAL_CSTRING("Content-Type"),
|
|
|
|
contentType, aRv);
|
2014-09-24 09:03:20 +04:00
|
|
|
}
|
|
|
|
|
2015-08-10 22:06:00 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
2014-09-24 09:03:20 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
2015-08-10 22:06:00 +03:00
|
|
|
|
|
|
|
request->ClearCreatedByFetchEvent();
|
|
|
|
request->SetBody(temporaryBody);
|
2014-09-24 09:03:20 +04:00
|
|
|
}
|
|
|
|
|
2014-10-02 21:59:20 +04:00
|
|
|
nsRefPtr<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()) {
|
|
|
|
nsRefPtr<Request> inputReq = &aInput.GetAsRequest();
|
|
|
|
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()) {
|
|
|
|
aRv.ThrowTypeError(MSG_FETCH_BODY_CONSUMED_ERROR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<InternalRequest> ir = mRequest->Clone();
|
|
|
|
if (!ir) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<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
|