2014-07-25 04:53:03 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* 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"
|
|
|
|
#include "mozilla/dom/workers/bindings/URL.h"
|
|
|
|
|
|
|
|
#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
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Request::~Request()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-09-24 09:03:20 +04:00
|
|
|
already_AddRefed<InternalRequest>
|
|
|
|
Request::GetInternalRequest()
|
|
|
|
{
|
|
|
|
nsRefPtr<InternalRequest> r = mRequest;
|
|
|
|
return r.forget();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
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();
|
|
|
|
if (inputReq->BodyUsed()) {
|
|
|
|
aRv.ThrowTypeError(MSG_REQUEST_BODY_CONSUMED_ERROR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
inputReq->SetBodyUsed();
|
|
|
|
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_;
|
2014-11-20 14:58:00 +03:00
|
|
|
if (aInput.IsUSVString()) {
|
2014-09-24 09:03:20 +04:00
|
|
|
nsString input;
|
2014-11-20 14:58:00 +03:00
|
|
|
input.Assign(aInput.GetAsUSVString());
|
2014-09-24 09:03:20 +04:00
|
|
|
|
|
|
|
nsString requestURL;
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(global);
|
|
|
|
MOZ_ASSERT(window);
|
|
|
|
nsCOMPtr<nsIURI> docURI = window->GetDocumentURI();
|
|
|
|
nsCString spec;
|
|
|
|
aRv = docURI->GetSpec(spec);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<mozilla::dom::URL> url =
|
|
|
|
dom::URL::Constructor(aGlobal, input, NS_ConvertUTF8toUTF16(spec), aRv);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
url->Stringify(requestURL, aRv);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
workers::WorkerPrivate* worker = workers::GetCurrentThreadWorkerPrivate();
|
|
|
|
MOZ_ASSERT(worker);
|
|
|
|
worker->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
nsString baseURL = NS_ConvertUTF8toUTF16(worker->GetLocationInfo().mHref);
|
|
|
|
nsRefPtr<workers::URL> url =
|
|
|
|
workers::URL::Constructor(aGlobal, input, baseURL, aRv);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
url->Stringify(requestURL, aRv);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
request->SetURL(NS_ConvertUTF16toUTF8(requestURL));
|
|
|
|
fallbackMode = RequestMode::Cors;
|
|
|
|
fallbackCredentials = RequestCredentials::Omit;
|
|
|
|
}
|
|
|
|
|
|
|
|
RequestMode mode = aInit.mMode.WasPassed() ? aInit.mMode.Value() : fallbackMode;
|
|
|
|
RequestCredentials credentials =
|
|
|
|
aInit.mCredentials.WasPassed() ? aInit.mCredentials.Value()
|
|
|
|
: fallbackCredentials;
|
|
|
|
|
|
|
|
if (mode != RequestMode::EndGuard_) {
|
|
|
|
request->SetMode(mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (credentials != RequestCredentials::EndGuard_) {
|
|
|
|
request->SetCredentialsMode(credentials);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aInit.mMethod.WasPassed()) {
|
|
|
|
nsCString method = aInit.mMethod.Value();
|
|
|
|
ToLowerCase(method);
|
|
|
|
|
|
|
|
if (!method.EqualsASCII("options") &&
|
|
|
|
!method.EqualsASCII("get") &&
|
|
|
|
!method.EqualsASCII("head") &&
|
|
|
|
!method.EqualsASCII("post") &&
|
|
|
|
!method.EqualsASCII("put") &&
|
|
|
|
!method.EqualsASCII("delete")) {
|
|
|
|
NS_ConvertUTF8toUTF16 label(method);
|
|
|
|
aRv.ThrowTypeError(MSG_INVALID_REQUEST_METHOD, &label);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
ToUpperCase(method);
|
|
|
|
request->SetMethod(method);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
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();
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aInit.mBody.WasPassed()) {
|
2014-11-20 14:58:00 +03:00
|
|
|
const OwningArrayBufferOrArrayBufferViewOrBlobOrUSVStringOrURLSearchParams& bodyInit = aInit.mBody.Value();
|
2014-09-24 09:03:20 +04:00
|
|
|
nsCOMPtr<nsIInputStream> stream;
|
|
|
|
nsCString contentType;
|
|
|
|
aRv = ExtractByteStreamFromBody(bodyInit,
|
|
|
|
getter_AddRefs(stream), contentType);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
request->SetBody(stream);
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-02 21:59:20 +04:00
|
|
|
nsRefPtr<Request> domRequest = new Request(global, request);
|
2014-09-27 03:41:15 +04:00
|
|
|
domRequest->SetMimeType(aRv);
|
2014-09-24 09:03:20 +04:00
|
|
|
return domRequest.forget();
|
2014-07-25 04:53:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<Request>
|
|
|
|
Request::Clone() const
|
|
|
|
{
|
2014-09-24 09:03:20 +04:00
|
|
|
// FIXME(nsm): Bug 1073231. This is incorrect, but the clone method isn't
|
|
|
|
// well defined yet.
|
|
|
|
nsRefPtr<Request> request = new Request(mOwner,
|
|
|
|
new InternalRequest(*mRequest));
|
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
|