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:50:32 +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 "Response.h"
|
2014-09-27 03:41:15 +04:00
|
|
|
|
2014-07-25 04:50:32 +04:00
|
|
|
#include "nsISupportsImpl.h"
|
2014-09-27 03:41:15 +04:00
|
|
|
#include "nsIURI.h"
|
|
|
|
#include "nsPIDOMWindow.h"
|
2014-07-25 04:50:32 +04:00
|
|
|
|
|
|
|
#include "mozilla/ErrorResult.h"
|
2014-10-01 22:43:26 +04:00
|
|
|
#include "mozilla/dom/FetchBinding.h"
|
2014-07-25 04:50:32 +04:00
|
|
|
#include "mozilla/dom/Headers.h"
|
|
|
|
#include "mozilla/dom/Promise.h"
|
2015-01-15 01:01:02 +03:00
|
|
|
#include "mozilla/dom/URL.h"
|
|
|
|
#include "mozilla/dom/workers/bindings/URL.h"
|
2014-07-25 04:50:32 +04:00
|
|
|
|
2014-09-27 03:41:15 +04:00
|
|
|
#include "nsDOMString.h"
|
|
|
|
|
|
|
|
#include "InternalResponse.h"
|
2015-01-15 01:01:02 +03:00
|
|
|
#include "WorkerPrivate.h"
|
2014-09-27 03:41:15 +04:00
|
|
|
|
2014-07-25 04:50:32 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(Response)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(Response)
|
2014-10-02 21:59:20 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(Response, mOwner, mHeaders)
|
2014-07-25 04:50:32 +04:00
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Response)
|
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2014-09-27 03:41:15 +04:00
|
|
|
Response::Response(nsIGlobalObject* aGlobal, InternalResponse* aInternalResponse)
|
|
|
|
: FetchBody<Response>()
|
|
|
|
, mOwner(aGlobal)
|
|
|
|
, mInternalResponse(aInternalResponse)
|
2014-07-25 04:50:32 +04:00
|
|
|
{
|
2015-02-03 23:59:32 +03:00
|
|
|
SetMimeType();
|
2014-07-25 04:50:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
Response::~Response()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ already_AddRefed<Response>
|
|
|
|
Response::Error(const GlobalObject& aGlobal)
|
|
|
|
{
|
2014-09-27 03:41:15 +04:00
|
|
|
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<InternalResponse> error = InternalResponse::NetworkError();
|
|
|
|
RefPtr<Response> r = new Response(global, error);
|
2014-07-25 04:50:32 +04:00
|
|
|
return r.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ already_AddRefed<Response>
|
|
|
|
Response::Redirect(const GlobalObject& aGlobal, const nsAString& aUrl,
|
2014-12-26 20:44:09 +03:00
|
|
|
uint16_t aStatus, ErrorResult& aRv)
|
2014-07-25 04:50:32 +04:00
|
|
|
{
|
2014-12-26 20:44:09 +03:00
|
|
|
nsAutoString parsedURL;
|
|
|
|
|
|
|
|
if (NS_IsMainThread()) {
|
2015-05-07 18:08:34 +03:00
|
|
|
nsCOMPtr<nsIURI> baseURI;
|
|
|
|
nsIDocument* doc = GetEntryDocument();
|
|
|
|
if (doc) {
|
|
|
|
baseURI = doc->GetBaseURI();
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsIURI> resolvedURI;
|
|
|
|
aRv = NS_NewURI(getter_AddRefs(resolvedURI), aUrl, nullptr, baseURI);
|
2014-12-26 20:44:09 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-05-07 18:08:34 +03:00
|
|
|
nsAutoCString spec;
|
|
|
|
aRv = resolvedURI->GetSpec(spec);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
2014-12-26 20:44:09 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-05-07 18:08:34 +03:00
|
|
|
CopyUTF8toUTF16(spec, parsedURL);
|
2014-12-26 20:44:09 +03:00
|
|
|
} else {
|
|
|
|
workers::WorkerPrivate* worker = workers::GetCurrentThreadWorkerPrivate();
|
|
|
|
MOZ_ASSERT(worker);
|
|
|
|
worker->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
NS_ConvertUTF8toUTF16 baseURL(worker->GetLocationInfo().mHref);
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<workers::URL> url =
|
2014-12-26 20:44:09 +03:00
|
|
|
workers::URL::Constructor(aGlobal, aUrl, baseURL, aRv);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-11-24 08:04:21 +03:00
|
|
|
url->Stringify(parsedURL, aRv);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
2014-12-26 20:44:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (aStatus != 301 && aStatus != 302 && aStatus != 303 && aStatus != 307 && aStatus != 308) {
|
2015-10-05 19:38:14 +03:00
|
|
|
aRv.ThrowRangeError<MSG_INVALID_REDIRECT_STATUSCODE_ERROR>();
|
2014-12-26 20:44:09 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-03-17 18:32:04 +03:00
|
|
|
Optional<ArrayBufferOrArrayBufferViewOrBlobOrFormDataOrUSVStringOrURLSearchParams> body;
|
2014-12-26 20:44:09 +03:00
|
|
|
ResponseInit init;
|
|
|
|
init.mStatus = aStatus;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Response> r = Response::Constructor(aGlobal, body, init, aRv);
|
2014-12-26 20:44:09 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
r->GetInternalHeaders()->Set(NS_LITERAL_CSTRING("Location"),
|
|
|
|
NS_ConvertUTF16toUTF8(parsedURL), aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-03-08 05:54:41 +03:00
|
|
|
r->GetInternalHeaders()->SetGuard(HeadersGuardEnum::Immutable, aRv);
|
|
|
|
MOZ_ASSERT(!aRv.Failed());
|
2014-12-26 20:44:09 +03:00
|
|
|
|
2014-07-25 04:50:32 +04:00
|
|
|
return r.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*static*/ already_AddRefed<Response>
|
2014-09-27 03:41:15 +04:00
|
|
|
Response::Constructor(const GlobalObject& aGlobal,
|
2015-03-17 18:32:04 +03:00
|
|
|
const Optional<ArrayBufferOrArrayBufferViewOrBlobOrFormDataOrUSVStringOrURLSearchParams>& aBody,
|
2014-09-27 03:41:15 +04:00
|
|
|
const ResponseInit& aInit, ErrorResult& aRv)
|
2014-07-25 04:50:32 +04:00
|
|
|
{
|
2015-03-19 21:41:42 +03:00
|
|
|
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
|
|
|
|
|
2014-09-27 03:41:15 +04:00
|
|
|
if (aInit.mStatus < 200 || aInit.mStatus > 599) {
|
2015-10-05 19:38:14 +03:00
|
|
|
aRv.ThrowRangeError<MSG_INVALID_RESPONSE_STATUSCODE_ERROR>();
|
2014-07-25 04:50:32 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-09-27 03:41:15 +04:00
|
|
|
nsCString statusText;
|
|
|
|
if (aInit.mStatusText.WasPassed()) {
|
|
|
|
statusText = aInit.mStatusText.Value();
|
|
|
|
nsACString::const_iterator start, end;
|
|
|
|
statusText.BeginReading(start);
|
|
|
|
statusText.EndReading(end);
|
|
|
|
if (FindCharInReadable('\r', start, end)) {
|
2015-10-05 19:38:14 +03:00
|
|
|
aRv.ThrowTypeError<MSG_RESPONSE_INVALID_STATUSTEXT_ERROR>();
|
2014-09-27 03:41:15 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
// Reset iterator since FindCharInReadable advances it.
|
|
|
|
statusText.BeginReading(start);
|
|
|
|
if (FindCharInReadable('\n', start, end)) {
|
2015-10-05 19:38:14 +03:00
|
|
|
aRv.ThrowTypeError<MSG_RESPONSE_INVALID_STATUSTEXT_ERROR>();
|
2014-09-27 03:41:15 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Since we don't support default values for ByteString.
|
|
|
|
statusText = NS_LITERAL_CSTRING("OK");
|
|
|
|
}
|
2014-07-25 04:50:32 +04:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<InternalResponse> internalResponse =
|
2014-09-27 03:41:15 +04:00
|
|
|
new InternalResponse(aInit.mStatus, statusText);
|
|
|
|
|
2015-08-18 01:08:58 +03:00
|
|
|
// Grab a valid channel info from the global so this response is 'valid' for
|
|
|
|
// interception.
|
|
|
|
if (NS_IsMainThread()) {
|
2015-09-01 01:36:48 +03:00
|
|
|
ChannelInfo info;
|
2016-01-30 20:05:36 +03:00
|
|
|
nsCOMPtr<nsPIDOMWindowInner> window = do_QueryInterface(global);
|
2015-09-01 00:26:30 +03:00
|
|
|
if (window) {
|
|
|
|
nsIDocument* doc = window->GetExtantDoc();
|
|
|
|
MOZ_ASSERT(doc);
|
|
|
|
info.InitFromDocument(doc);
|
|
|
|
} else {
|
|
|
|
info.InitFromChromeGlobal(global);
|
|
|
|
}
|
2015-08-18 01:08:58 +03:00
|
|
|
internalResponse->InitChannelInfo(info);
|
|
|
|
} else {
|
|
|
|
workers::WorkerPrivate* worker = workers::GetCurrentThreadWorkerPrivate();
|
|
|
|
MOZ_ASSERT(worker);
|
|
|
|
internalResponse->InitChannelInfo(worker->GetChannelInfo());
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Response> r = new Response(global, internalResponse);
|
2014-09-27 03:41:15 +04:00
|
|
|
|
|
|
|
if (aInit.mHeaders.WasPassed()) {
|
2014-10-02 21:59:20 +04:00
|
|
|
internalResponse->Headers()->Clear();
|
2014-09-27 03:41:15 +04:00
|
|
|
|
|
|
|
// Instead of using Fill, create an object to allow the constructor to
|
|
|
|
// unwrap the HeadersInit.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Headers> headers =
|
2015-03-19 21:41:42 +03:00
|
|
|
Headers::Create(global, aInit.mHeaders.Value(), aRv);
|
2014-09-27 03:41:15 +04:00
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-10-02 21:59:20 +04:00
|
|
|
internalResponse->Headers()->Fill(*headers->GetInternalHeaders(), aRv);
|
2014-09-27 03:41:15 +04:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2014-07-25 04:50:32 +04:00
|
|
|
}
|
|
|
|
|
2014-09-27 03:41:15 +04:00
|
|
|
if (aBody.WasPassed()) {
|
2015-09-30 21:50:10 +03:00
|
|
|
if (aInit.mStatus == 204 || aInit.mStatus == 205 || aInit.mStatus == 304) {
|
2015-10-05 19:38:14 +03:00
|
|
|
aRv.ThrowTypeError<MSG_RESPONSE_NULL_STATUS_WITH_BODY>();
|
2015-09-30 21:50:10 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-09-27 03:41:15 +04:00
|
|
|
nsCOMPtr<nsIInputStream> bodyStream;
|
|
|
|
nsCString contentType;
|
|
|
|
aRv = ExtractByteStreamFromBody(aBody.Value(), getter_AddRefs(bodyStream), contentType);
|
|
|
|
internalResponse->SetBody(bodyStream);
|
2014-07-25 04:50:32 +04:00
|
|
|
|
2014-09-27 03:41:15 +04:00
|
|
|
if (!contentType.IsVoid() &&
|
2014-10-02 21:59:20 +04:00
|
|
|
!internalResponse->Headers()->Has(NS_LITERAL_CSTRING("Content-Type"), aRv)) {
|
2015-10-21 03:05:39 +03:00
|
|
|
// Ignore Append() failing here.
|
|
|
|
ErrorResult error;
|
|
|
|
internalResponse->Headers()->Append(NS_LITERAL_CSTRING("Content-Type"), contentType, error);
|
|
|
|
error.SuppressException();
|
2014-09-27 03:41:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2014-07-25 04:50:32 +04:00
|
|
|
}
|
|
|
|
|
2015-02-03 23:59:32 +03:00
|
|
|
r->SetMimeType();
|
2014-09-27 03:41:15 +04:00
|
|
|
return r.forget();
|
2014-07-25 04:50:32 +04:00
|
|
|
}
|
|
|
|
|
2014-09-27 03:41:15 +04:00
|
|
|
already_AddRefed<Response>
|
2015-02-20 04:24:24 +03:00
|
|
|
Response::Clone(ErrorResult& aRv) const
|
2014-07-25 04:50:32 +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<InternalResponse> ir = mInternalResponse->Clone();
|
|
|
|
RefPtr<Response> response = new Response(mOwner, ir);
|
2014-09-27 03:41:15 +04:00
|
|
|
return response.forget();
|
2014-07-25 04:50:32 +04:00
|
|
|
}
|
|
|
|
|
2016-01-12 23:15:12 +03:00
|
|
|
already_AddRefed<Response>
|
|
|
|
Response::CloneUnfiltered(ErrorResult& aRv) const
|
|
|
|
{
|
|
|
|
if (BodyUsed()) {
|
|
|
|
aRv.ThrowTypeError<MSG_FETCH_BODY_CONSUMED_ERROR>();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<InternalResponse> clone = mInternalResponse->Clone();
|
|
|
|
RefPtr<InternalResponse> ir = clone->Unfiltered();
|
|
|
|
RefPtr<Response> ref = new Response(mOwner, ir);
|
|
|
|
return ref.forget();
|
|
|
|
}
|
|
|
|
|
2014-09-27 03:41:15 +04:00
|
|
|
void
|
|
|
|
Response::SetBody(nsIInputStream* aBody)
|
2014-07-25 04:50:32 +04:00
|
|
|
{
|
2015-02-20 04:24:24 +03:00
|
|
|
MOZ_ASSERT(!BodyUsed());
|
2014-09-27 03:41:15 +04:00
|
|
|
mInternalResponse->SetBody(aBody);
|
2014-07-25 04:50:32 +04:00
|
|
|
}
|
2014-10-02 21:59:20 +04:00
|
|
|
|
2015-03-02 16:08:00 +03:00
|
|
|
already_AddRefed<InternalResponse>
|
|
|
|
Response::GetInternalResponse() const
|
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<InternalResponse> ref = mInternalResponse;
|
2015-03-02 16:08:00 +03:00
|
|
|
return ref.forget();
|
|
|
|
}
|
|
|
|
|
2014-10-02 21:59:20 +04:00
|
|
|
Headers*
|
|
|
|
Response::Headers_()
|
|
|
|
{
|
|
|
|
if (!mHeaders) {
|
|
|
|
mHeaders = new Headers(mOwner, mInternalResponse->Headers());
|
|
|
|
}
|
|
|
|
|
|
|
|
return mHeaders;
|
|
|
|
}
|
2015-07-13 18:25:42 +03:00
|
|
|
|
2014-07-25 04:50:32 +04:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|