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"
|
2017-08-11 04:04:55 +03:00
|
|
|
#include "nsNetUtil.h"
|
2014-09-27 03:41:15 +04:00
|
|
|
#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"
|
2017-08-11 04:04:55 +03:00
|
|
|
#include "mozilla/dom/ResponseBinding.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"
|
2014-07-25 04:50:32 +04:00
|
|
|
|
2014-09-27 03:41:15 +04:00
|
|
|
#include "nsDOMString.h"
|
|
|
|
|
2017-08-11 04:04:55 +03:00
|
|
|
#include "BodyExtractor.h"
|
|
|
|
#include "FetchStream.h"
|
2017-08-11 04:04:55 +03:00
|
|
|
#include "FetchStreamReader.h"
|
2014-09-27 03:41:15 +04:00
|
|
|
#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)
|
2017-08-11 04:04:54 +03:00
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(Response)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(Response)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mOwner)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mHeaders)
|
2017-08-29 12:31:06 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mSignal)
|
2017-08-11 04:04:56 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mFetchStreamReader)
|
2017-08-11 04:04:54 +03:00
|
|
|
|
|
|
|
tmp->mReadableStreamBody = nullptr;
|
2017-08-11 04:04:55 +03:00
|
|
|
tmp->mReadableStreamReader = nullptr;
|
2017-08-11 04:04:54 +03:00
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(Response)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mOwner)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mHeaders)
|
2017-08-29 12:31:06 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mSignal)
|
2017-08-11 04:04:56 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mFetchStreamReader)
|
2017-08-11 04:04:54 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(Response)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mReadableStreamBody)
|
2017-08-11 04:04:55 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mReadableStreamReader)
|
2017-08-11 04:04:54 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_END
|
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
|
|
|
|
|
2017-08-29 12:31:06 +03:00
|
|
|
Response::Response(nsIGlobalObject* aGlobal,
|
|
|
|
InternalResponse* aInternalResponse,
|
|
|
|
AbortSignal* aSignal)
|
2017-05-16 13:12:24 +03:00
|
|
|
: FetchBody<Response>(aGlobal)
|
2014-09-27 03:41:15 +04:00
|
|
|
, mInternalResponse(aInternalResponse)
|
2017-08-29 12:31:06 +03:00
|
|
|
, mSignal(aSignal)
|
2014-07-25 04:50:32 +04:00
|
|
|
{
|
2016-06-02 00:02:29 +03:00
|
|
|
MOZ_ASSERT(aInternalResponse->Headers()->Guard() == HeadersGuardEnum::Immutable ||
|
|
|
|
aInternalResponse->Headers()->Guard() == HeadersGuardEnum::Response);
|
2015-02-03 23:59:32 +03:00
|
|
|
SetMimeType();
|
2017-08-11 04:04:54 +03:00
|
|
|
|
|
|
|
mozilla::HoldJSObjects(this);
|
2014-07-25 04:50:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
Response::~Response()
|
|
|
|
{
|
2017-08-11 04:04:54 +03:00
|
|
|
mozilla::DropJSObjects(this);
|
2014-07-25 04:50:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ already_AddRefed<Response>
|
|
|
|
Response::Error(const GlobalObject& aGlobal)
|
|
|
|
{
|
2014-09-27 03:41:15 +04:00
|
|
|
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
|
2017-11-15 22:53:42 +03:00
|
|
|
RefPtr<InternalResponse> error = InternalResponse::NetworkError(NS_ERROR_FAILURE);
|
2017-08-29 12:31:06 +03:00
|
|
|
RefPtr<Response> r = new Response(global, error, nullptr);
|
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;
|
2018-01-23 17:38:21 +03:00
|
|
|
nsCOMPtr<nsPIDOMWindowInner> inner(do_QueryInterface(aGlobal.GetAsSupports()));
|
|
|
|
nsIDocument* doc = inner ? inner->GetExtantDoc() : nullptr;
|
2015-05-07 18:08:34 +03:00
|
|
|
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 {
|
2018-01-31 10:24:08 +03:00
|
|
|
WorkerPrivate* worker = GetCurrentThreadWorkerPrivate();
|
2014-12-26 20:44:09 +03:00
|
|
|
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, aUrl, baseURL, aRv);
|
2014-12-26 20:44:09 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-01-13 06:20:50 +03:00
|
|
|
Optional<Nullable<fetch::ResponseBodyInit>> 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,
|
2018-01-13 06:20:50 +03:00
|
|
|
const Optional<Nullable<fetch::ResponseBodyInit>>& 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;
|
|
|
|
}
|
|
|
|
|
2016-08-06 00:47:43 +03:00
|
|
|
// Check if the status text contains illegal characters
|
|
|
|
nsACString::const_iterator start, end;
|
|
|
|
aInit.mStatusText.BeginReading(start);
|
|
|
|
aInit.mStatusText.EndReading(end);
|
|
|
|
if (FindCharInReadable('\r', start, end)) {
|
|
|
|
aRv.ThrowTypeError<MSG_RESPONSE_INVALID_STATUSTEXT_ERROR>();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
// Reset iterator since FindCharInReadable advances it.
|
|
|
|
aInit.mStatusText.BeginReading(start);
|
|
|
|
if (FindCharInReadable('\n', start, end)) {
|
|
|
|
aRv.ThrowTypeError<MSG_RESPONSE_INVALID_STATUSTEXT_ERROR>();
|
|
|
|
return nullptr;
|
2014-09-27 03:41:15 +04:00
|
|
|
}
|
2014-07-25 04:50:32 +04:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<InternalResponse> internalResponse =
|
2016-08-06 00:47:43 +03:00
|
|
|
new InternalResponse(aInit.mStatus, aInit.mStatusText);
|
2014-09-27 03:41:15 +04:00
|
|
|
|
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 {
|
2018-01-31 10:24:08 +03:00
|
|
|
WorkerPrivate* worker = GetCurrentThreadWorkerPrivate();
|
2015-08-18 01:08:58 +03:00
|
|
|
MOZ_ASSERT(worker);
|
|
|
|
internalResponse->InitChannelInfo(worker->GetChannelInfo());
|
|
|
|
}
|
|
|
|
|
2017-08-29 12:31:06 +03:00
|
|
|
RefPtr<Response> r = new Response(global, internalResponse, nullptr);
|
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
|
|
|
}
|
|
|
|
|
2018-01-13 06:20:50 +03:00
|
|
|
if (aBody.WasPassed() && !aBody.Value().IsNull()) {
|
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;
|
|
|
|
}
|
|
|
|
|
2017-01-24 14:13:31 +03:00
|
|
|
nsCString contentTypeWithCharset;
|
2017-08-11 04:04:55 +03:00
|
|
|
nsCOMPtr<nsIInputStream> bodyStream;
|
2017-08-11 04:04:55 +03:00
|
|
|
int64_t bodySize = InternalResponse::UNKNOWN_BODY_SIZE;
|
2017-08-11 04:04:55 +03:00
|
|
|
|
2018-01-13 06:20:50 +03:00
|
|
|
const fetch::ResponseBodyInit& body = aBody.Value().Value();
|
|
|
|
if (body.IsReadableStream()) {
|
|
|
|
const ReadableStream& readableStream = body.GetAsReadableStream();
|
2017-08-11 04:04:55 +03:00
|
|
|
|
|
|
|
JS::Rooted<JSObject*> readableStreamObj(aGlobal.Context(),
|
|
|
|
readableStream.Obj());
|
|
|
|
|
|
|
|
if (JS::ReadableStreamIsDisturbed(readableStreamObj) ||
|
|
|
|
JS::ReadableStreamIsLocked(readableStreamObj) ||
|
|
|
|
!JS::ReadableStreamIsReadable(readableStreamObj)) {
|
|
|
|
aRv.ThrowTypeError<MSG_FETCH_BODY_CONSUMED_ERROR>();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-08-29 12:31:06 +03:00
|
|
|
r->SetReadableStreamBody(aGlobal.Context(), readableStreamObj);
|
2017-08-11 04:04:55 +03:00
|
|
|
|
2017-08-11 04:04:55 +03:00
|
|
|
if (JS::ReadableStreamGetMode(readableStreamObj) ==
|
|
|
|
JS::ReadableStreamMode::ExternalSource) {
|
|
|
|
// If this is a DOM generated ReadableStream, we can extract the
|
|
|
|
// inputStream directly.
|
|
|
|
void* underlyingSource = nullptr;
|
|
|
|
if (!JS::ReadableStreamGetExternalUnderlyingSource(aGlobal.Context(),
|
|
|
|
readableStreamObj,
|
|
|
|
&underlyingSource)) {
|
|
|
|
aRv.StealExceptionFromJSContext(aGlobal.Context());
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(underlyingSource);
|
|
|
|
|
|
|
|
aRv = FetchStream::RetrieveInputStream(underlyingSource,
|
|
|
|
getter_AddRefs(bodyStream));
|
|
|
|
|
|
|
|
// The releasing of the external source is needed in order to avoid an
|
|
|
|
// extra stream lock.
|
|
|
|
JS::ReadableStreamReleaseExternalUnderlyingSource(readableStreamObj);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// If this is a JS-created ReadableStream, let's create a
|
|
|
|
// FetchStreamReader.
|
|
|
|
aRv = FetchStreamReader::Create(aGlobal.Context(), global,
|
|
|
|
getter_AddRefs(r->mFetchStreamReader),
|
|
|
|
getter_AddRefs(bodyStream));
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2017-08-11 04:04:55 +03:00
|
|
|
}
|
|
|
|
} else {
|
2017-08-11 04:04:55 +03:00
|
|
|
uint64_t size = 0;
|
2018-01-13 06:20:50 +03:00
|
|
|
aRv = ExtractByteStreamFromBody(body,
|
2017-08-11 04:04:55 +03:00
|
|
|
getter_AddRefs(bodyStream),
|
|
|
|
contentTypeWithCharset,
|
2017-08-11 04:04:55 +03:00
|
|
|
size);
|
2017-08-11 04:04:55 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2017-08-11 04:04:55 +03:00
|
|
|
|
|
|
|
bodySize = size;
|
2016-06-02 00:02:29 +03:00
|
|
|
}
|
2017-08-11 04:04:55 +03:00
|
|
|
|
2016-06-02 00:02:29 +03:00
|
|
|
internalResponse->SetBody(bodyStream, bodySize);
|
2014-07-25 04:50:32 +04:00
|
|
|
|
2017-01-24 14:13:31 +03:00
|
|
|
if (!contentTypeWithCharset.IsVoid() &&
|
|
|
|
!internalResponse->Headers()->Has(NS_LITERAL_CSTRING("Content-Type"),
|
|
|
|
aRv)) {
|
2015-10-21 03:05:39 +03:00
|
|
|
// Ignore Append() failing here.
|
|
|
|
ErrorResult error;
|
2017-01-24 14:13:31 +03:00
|
|
|
internalResponse->Headers()->Append(NS_LITERAL_CSTRING("Content-Type"),
|
|
|
|
contentTypeWithCharset, error);
|
2015-10-21 03:05:39 +03:00
|
|
|
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>
|
2017-08-11 04:04:55 +03:00
|
|
|
Response::Clone(JSContext* aCx, ErrorResult& aRv)
|
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;
|
|
|
|
}
|
|
|
|
|
2017-08-11 04:04:55 +03:00
|
|
|
RefPtr<FetchStreamReader> streamReader;
|
|
|
|
nsCOMPtr<nsIInputStream> inputStream;
|
2017-08-11 04:04:55 +03:00
|
|
|
|
|
|
|
JS::Rooted<JSObject*> body(aCx);
|
2017-08-11 04:04:55 +03:00
|
|
|
MaybeTeeReadableStreamBody(aCx, &body,
|
|
|
|
getter_AddRefs(streamReader),
|
|
|
|
getter_AddRefs(inputStream), aRv);
|
2017-08-11 04:04:55 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-08-11 04:04:55 +03:00
|
|
|
MOZ_ASSERT_IF(body, streamReader);
|
|
|
|
MOZ_ASSERT_IF(body, inputStream);
|
|
|
|
|
|
|
|
RefPtr<InternalResponse> ir =
|
|
|
|
mInternalResponse->Clone(body
|
|
|
|
? InternalResponse::eDontCloneInputStream
|
|
|
|
: InternalResponse::eCloneInputStream);
|
|
|
|
|
2017-08-29 12:31:06 +03:00
|
|
|
RefPtr<Response> response = new Response(mOwner, ir, mSignal);
|
2017-08-11 04:04:55 +03:00
|
|
|
|
2017-08-11 04:04:55 +03:00
|
|
|
if (body) {
|
|
|
|
// Maybe we have a body, but we receive null from MaybeTeeReadableStreamBody
|
|
|
|
// if this body is a native stream. In this case the InternalResponse will
|
|
|
|
// have a clone of the native body and the ReadableStream will be created
|
|
|
|
// lazily if needed.
|
2017-08-29 12:31:06 +03:00
|
|
|
response->SetReadableStreamBody(aCx, body);
|
2017-08-11 04:04:55 +03:00
|
|
|
response->mFetchStreamReader = streamReader;
|
|
|
|
ir->SetBody(inputStream, InternalResponse::UNKNOWN_BODY_SIZE);
|
2017-08-11 04:04:55 +03:00
|
|
|
}
|
|
|
|
|
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>
|
2017-08-11 04:04:55 +03:00
|
|
|
Response::CloneUnfiltered(JSContext* aCx, ErrorResult& aRv)
|
2016-01-12 23:15:12 +03:00
|
|
|
{
|
|
|
|
if (BodyUsed()) {
|
|
|
|
aRv.ThrowTypeError<MSG_FETCH_BODY_CONSUMED_ERROR>();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-08-11 04:04:55 +03:00
|
|
|
RefPtr<FetchStreamReader> streamReader;
|
|
|
|
nsCOMPtr<nsIInputStream> inputStream;
|
2017-08-11 04:04:55 +03:00
|
|
|
|
|
|
|
JS::Rooted<JSObject*> body(aCx);
|
2017-08-11 04:04:55 +03:00
|
|
|
MaybeTeeReadableStreamBody(aCx, &body,
|
|
|
|
getter_AddRefs(streamReader),
|
|
|
|
getter_AddRefs(inputStream), aRv);
|
2017-08-11 04:04:55 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-08-11 04:04:55 +03:00
|
|
|
MOZ_ASSERT_IF(body, streamReader);
|
|
|
|
MOZ_ASSERT_IF(body, inputStream);
|
|
|
|
|
|
|
|
RefPtr<InternalResponse> clone =
|
|
|
|
mInternalResponse->Clone(body
|
|
|
|
? InternalResponse::eDontCloneInputStream
|
|
|
|
: InternalResponse::eCloneInputStream);
|
|
|
|
|
|
|
|
RefPtr<InternalResponse> ir = clone->Unfiltered();
|
2017-08-29 12:31:06 +03:00
|
|
|
RefPtr<Response> ref = new Response(mOwner, ir, mSignal);
|
2017-08-11 04:04:55 +03:00
|
|
|
|
2017-08-11 04:04:55 +03:00
|
|
|
if (body) {
|
|
|
|
// Maybe we have a body, but we receive null from MaybeTeeReadableStreamBody
|
|
|
|
// if this body is a native stream. In this case the InternalResponse will
|
|
|
|
// have a clone of the native body and the ReadableStream will be created
|
|
|
|
// lazily if needed.
|
2017-08-29 12:31:06 +03:00
|
|
|
ref->SetReadableStreamBody(aCx, body);
|
2017-08-11 04:04:55 +03:00
|
|
|
ref->mFetchStreamReader = streamReader;
|
|
|
|
ir->SetBody(inputStream, InternalResponse::UNKNOWN_BODY_SIZE);
|
2017-08-11 04:04:55 +03:00
|
|
|
}
|
|
|
|
|
2016-01-12 23:15:12 +03:00
|
|
|
return ref.forget();
|
|
|
|
}
|
|
|
|
|
2014-09-27 03:41:15 +04:00
|
|
|
void
|
2016-06-02 00:02:29 +03:00
|
|
|
Response::SetBody(nsIInputStream* aBody, int64_t aBodySize)
|
2014-07-25 04:50:32 +04:00
|
|
|
{
|
2015-02-20 04:24:24 +03:00
|
|
|
MOZ_ASSERT(!BodyUsed());
|
2016-06-02 00:02:29 +03:00
|
|
|
mInternalResponse->SetBody(aBody, aBodySize);
|
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
|