2014-07-24 17:50:32 -07: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 "Response.h"
|
2014-09-26 16:41:15 -07:00
|
|
|
|
2014-07-24 17:50:32 -07:00
|
|
|
#include "nsISupportsImpl.h"
|
2014-09-26 16:41:15 -07:00
|
|
|
#include "nsIURI.h"
|
|
|
|
#include "nsPIDOMWindow.h"
|
2014-07-24 17:50:32 -07:00
|
|
|
|
|
|
|
#include "mozilla/ErrorResult.h"
|
2014-10-01 11:43:26 -07:00
|
|
|
#include "mozilla/dom/FetchBinding.h"
|
2014-07-24 17:50:32 -07:00
|
|
|
#include "mozilla/dom/Headers.h"
|
|
|
|
#include "mozilla/dom/Promise.h"
|
|
|
|
|
2014-09-26 16:41:15 -07:00
|
|
|
#include "nsDOMString.h"
|
|
|
|
|
|
|
|
#include "InternalResponse.h"
|
|
|
|
|
2014-07-24 17:50:32 -07:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(Response)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(Response)
|
2014-10-02 10:59:20 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(Response, mOwner, mHeaders)
|
2014-07-24 17:50:32 -07: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 16:41:15 -07:00
|
|
|
Response::Response(nsIGlobalObject* aGlobal, InternalResponse* aInternalResponse)
|
|
|
|
: FetchBody<Response>()
|
|
|
|
, mOwner(aGlobal)
|
|
|
|
, mInternalResponse(aInternalResponse)
|
2014-07-24 17:50:32 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Response::~Response()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ already_AddRefed<Response>
|
|
|
|
Response::Error(const GlobalObject& aGlobal)
|
|
|
|
{
|
2014-09-26 16:41:15 -07:00
|
|
|
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
|
|
|
|
nsRefPtr<InternalResponse> error = InternalResponse::NetworkError();
|
|
|
|
nsRefPtr<Response> r = new Response(global, error);
|
2014-07-24 17:50:32 -07:00
|
|
|
return r.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ already_AddRefed<Response>
|
|
|
|
Response::Redirect(const GlobalObject& aGlobal, const nsAString& aUrl,
|
|
|
|
uint16_t aStatus)
|
|
|
|
{
|
|
|
|
ErrorResult result;
|
|
|
|
ResponseInit init;
|
2014-10-09 10:52:10 -07:00
|
|
|
Optional<ArrayBufferOrArrayBufferViewOrBlobOrScalarValueStringOrURLSearchParams> body;
|
2014-07-24 17:50:32 -07:00
|
|
|
nsRefPtr<Response> r = Response::Constructor(aGlobal, body, init, result);
|
|
|
|
return r.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*static*/ already_AddRefed<Response>
|
2014-09-26 16:41:15 -07:00
|
|
|
Response::Constructor(const GlobalObject& aGlobal,
|
2014-10-09 10:52:10 -07:00
|
|
|
const Optional<ArrayBufferOrArrayBufferViewOrBlobOrScalarValueStringOrURLSearchParams>& aBody,
|
2014-09-26 16:41:15 -07:00
|
|
|
const ResponseInit& aInit, ErrorResult& aRv)
|
2014-07-24 17:50:32 -07:00
|
|
|
{
|
2014-09-26 16:41:15 -07:00
|
|
|
if (aInit.mStatus < 200 || aInit.mStatus > 599) {
|
|
|
|
aRv.Throw(NS_ERROR_RANGE_ERR);
|
2014-07-24 17:50:32 -07:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-09-26 16:41:15 -07: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-24 17:50:32 -07:00
|
|
|
|
2014-09-26 16:41:15 -07:00
|
|
|
nsRefPtr<InternalResponse> internalResponse =
|
|
|
|
new InternalResponse(aInit.mStatus, statusText);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
|
|
|
|
nsRefPtr<Response> r = new Response(global, internalResponse);
|
|
|
|
|
|
|
|
if (aInit.mHeaders.WasPassed()) {
|
2014-10-02 10:59:20 -07:00
|
|
|
internalResponse->Headers()->Clear();
|
2014-09-26 16:41:15 -07:00
|
|
|
|
|
|
|
// Instead of using Fill, create an object to allow the constructor to
|
|
|
|
// unwrap the HeadersInit.
|
|
|
|
nsRefPtr<Headers> headers =
|
|
|
|
Headers::Constructor(aGlobal, aInit.mHeaders.Value(), aRv);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-10-02 10:59:20 -07:00
|
|
|
internalResponse->Headers()->Fill(*headers->GetInternalHeaders(), aRv);
|
2014-09-26 16:41:15 -07:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2014-07-24 17:50:32 -07:00
|
|
|
}
|
|
|
|
|
2014-09-26 16:41:15 -07:00
|
|
|
if (aBody.WasPassed()) {
|
|
|
|
nsCOMPtr<nsIInputStream> bodyStream;
|
|
|
|
nsCString contentType;
|
|
|
|
aRv = ExtractByteStreamFromBody(aBody.Value(), getter_AddRefs(bodyStream), contentType);
|
|
|
|
internalResponse->SetBody(bodyStream);
|
2014-07-24 17:50:32 -07:00
|
|
|
|
2014-09-26 16:41:15 -07:00
|
|
|
if (!contentType.IsVoid() &&
|
2014-10-02 10:59:20 -07:00
|
|
|
!internalResponse->Headers()->Has(NS_LITERAL_CSTRING("Content-Type"), aRv)) {
|
|
|
|
internalResponse->Headers()->Append(NS_LITERAL_CSTRING("Content-Type"), contentType, aRv);
|
2014-09-26 16:41:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2014-07-24 17:50:32 -07:00
|
|
|
}
|
|
|
|
|
2014-09-26 16:41:15 -07:00
|
|
|
r->SetMimeType(aRv);
|
|
|
|
return r.forget();
|
2014-07-24 17:50:32 -07:00
|
|
|
}
|
|
|
|
|
2014-09-26 16:41:15 -07:00
|
|
|
// FIXME(nsm): Bug 1073231: This is currently unspecced!
|
|
|
|
already_AddRefed<Response>
|
|
|
|
Response::Clone()
|
2014-07-24 17:50:32 -07:00
|
|
|
{
|
2014-09-26 16:41:15 -07:00
|
|
|
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(mOwner);
|
|
|
|
nsRefPtr<Response> response = new Response(global, mInternalResponse);
|
|
|
|
return response.forget();
|
2014-07-24 17:50:32 -07:00
|
|
|
}
|
|
|
|
|
2014-09-26 16:41:15 -07:00
|
|
|
void
|
|
|
|
Response::SetBody(nsIInputStream* aBody)
|
2014-07-24 17:50:32 -07:00
|
|
|
{
|
2014-09-26 16:41:15 -07:00
|
|
|
// FIXME(nsm): Do we flip bodyUsed here?
|
|
|
|
mInternalResponse->SetBody(aBody);
|
2014-07-24 17:50:32 -07:00
|
|
|
}
|
2014-10-02 10:59:20 -07:00
|
|
|
|
|
|
|
Headers*
|
|
|
|
Response::Headers_()
|
|
|
|
{
|
|
|
|
if (!mHeaders) {
|
|
|
|
mHeaders = new Headers(mOwner, mInternalResponse->Headers());
|
|
|
|
}
|
|
|
|
|
|
|
|
return mHeaders;
|
|
|
|
}
|
2014-07-24 17:50:32 -07:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|