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