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-09-27 03:41:15 +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 "InternalResponse.h"
|
|
|
|
|
2015-08-13 18:22:19 +03:00
|
|
|
#include "mozilla/Assertions.h"
|
2014-10-02 21:59:20 +04:00
|
|
|
#include "mozilla/dom/InternalHeaders.h"
|
2015-06-05 07:39:34 +03:00
|
|
|
#include "mozilla/dom/cache/CacheTypes.h"
|
|
|
|
#include "mozilla/ipc/PBackgroundSharedTypes.h"
|
2015-08-13 18:22:19 +03:00
|
|
|
#include "nsIURI.h"
|
2015-02-20 04:24:24 +03:00
|
|
|
#include "nsStreamUtils.h"
|
2016-06-07 12:46:03 +03:00
|
|
|
#include "mozilla/dom/FetchTypes.h"
|
2014-09-27 03:41:15 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
InternalResponse::InternalResponse(uint16_t aStatus, const nsACString& aStatusText)
|
|
|
|
: mType(ResponseType::Default)
|
|
|
|
, mStatus(aStatus)
|
|
|
|
, mStatusText(aStatusText)
|
2014-10-02 21:59:20 +04:00
|
|
|
, mHeaders(new InternalHeaders(HeadersGuardEnum::Response))
|
2016-06-02 00:02:29 +03:00
|
|
|
, mBodySize(UNKNOWN_BODY_SIZE)
|
2014-09-27 03:41:15 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-06-07 12:46:03 +03:00
|
|
|
already_AddRefed<InternalResponse>
|
|
|
|
InternalResponse::FromIPC(const IPCInternalResponse& aIPCResponse)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!aIPCResponse.urlList().IsEmpty());
|
|
|
|
|
|
|
|
if (aIPCResponse.type() == ResponseType::Error) {
|
|
|
|
return InternalResponse::NetworkError();
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<InternalResponse> response =
|
|
|
|
new InternalResponse(aIPCResponse.status(),
|
|
|
|
aIPCResponse.statusText());
|
|
|
|
|
|
|
|
response->SetURLList(aIPCResponse.urlList());
|
|
|
|
|
|
|
|
response->mHeaders = new InternalHeaders(aIPCResponse.headers(),
|
|
|
|
aIPCResponse.headersGuard());
|
|
|
|
|
|
|
|
response->InitChannelInfo(aIPCResponse.channelInfo());
|
|
|
|
if (aIPCResponse.principalInfo().type() == mozilla::ipc::OptionalPrincipalInfo::TPrincipalInfo) {
|
|
|
|
UniquePtr<mozilla::ipc::PrincipalInfo> info(new mozilla::ipc::PrincipalInfo(aIPCResponse.principalInfo().get_PrincipalInfo()));
|
|
|
|
response->SetPrincipalInfo(Move(info));
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (aIPCResponse.type())
|
|
|
|
{
|
|
|
|
case ResponseType::Basic:
|
|
|
|
response = response->BasicResponse();
|
|
|
|
break;
|
|
|
|
case ResponseType::Cors:
|
|
|
|
response = response->CORSResponse();
|
|
|
|
break;
|
|
|
|
case ResponseType::Default:
|
|
|
|
break;
|
|
|
|
case ResponseType::Opaque:
|
|
|
|
response = response->OpaqueResponse();
|
|
|
|
break;
|
|
|
|
case ResponseType::Opaqueredirect:
|
|
|
|
response = response->OpaqueRedirectResponse();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
MOZ_CRASH("Unexpected ResponseType!");
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(response);
|
|
|
|
|
|
|
|
return response.forget();
|
|
|
|
}
|
|
|
|
|
2015-06-05 07:39:34 +03:00
|
|
|
InternalResponse::~InternalResponse()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-06-07 12:46:03 +03:00
|
|
|
void
|
|
|
|
InternalResponse::ToIPC(IPCInternalResponse* aIPCResponse)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aIPCResponse);
|
|
|
|
MOZ_ASSERT(!mURLList.IsEmpty());
|
|
|
|
aIPCResponse->type() = mType;
|
|
|
|
aIPCResponse->urlList() = mURLList;
|
|
|
|
aIPCResponse->status() = GetUnfilteredStatus();
|
|
|
|
aIPCResponse->statusText() = GetUnfilteredStatusText();
|
|
|
|
|
|
|
|
mHeaders->ToIPC(aIPCResponse->headers(), aIPCResponse->headersGuard());
|
|
|
|
|
|
|
|
aIPCResponse->channelInfo() = mChannelInfo.AsIPCChannelInfo();
|
|
|
|
if (mPrincipalInfo) {
|
|
|
|
aIPCResponse->principalInfo() = *mPrincipalInfo;
|
|
|
|
} else {
|
|
|
|
aIPCResponse->principalInfo() = void_t();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-20 04:24:24 +03:00
|
|
|
already_AddRefed<InternalResponse>
|
|
|
|
InternalResponse::Clone()
|
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<InternalResponse> clone = CreateIncompleteCopy();
|
2015-03-17 18:47:01 +03:00
|
|
|
|
2015-02-20 04:24:24 +03:00
|
|
|
clone->mHeaders = new InternalHeaders(*mHeaders);
|
2015-03-17 18:47:01 +03:00
|
|
|
if (mWrappedResponse) {
|
|
|
|
clone->mWrappedResponse = mWrappedResponse->Clone();
|
|
|
|
MOZ_ASSERT(!mBody);
|
|
|
|
return clone.forget();
|
|
|
|
}
|
2015-02-20 04:24:24 +03:00
|
|
|
|
|
|
|
if (!mBody) {
|
|
|
|
return clone.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStream> clonedBody;
|
|
|
|
nsCOMPtr<nsIInputStream> replacementBody;
|
|
|
|
|
|
|
|
nsresult rv = NS_CloneInputStream(mBody, getter_AddRefs(clonedBody),
|
|
|
|
getter_AddRefs(replacementBody));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) { return nullptr; }
|
|
|
|
|
|
|
|
clone->mBody.swap(clonedBody);
|
|
|
|
if (replacementBody) {
|
|
|
|
mBody.swap(replacementBody);
|
|
|
|
}
|
|
|
|
|
|
|
|
return clone.forget();
|
|
|
|
}
|
|
|
|
|
2014-10-06 22:01:20 +04:00
|
|
|
already_AddRefed<InternalResponse>
|
2015-03-17 18:47:01 +03:00
|
|
|
InternalResponse::BasicResponse()
|
2014-10-06 22:01:20 +04:00
|
|
|
{
|
2015-03-17 18:47:01 +03:00
|
|
|
MOZ_ASSERT(!mWrappedResponse, "Can't BasicResponse a already wrapped response");
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<InternalResponse> basic = CreateIncompleteCopy();
|
2014-10-06 22:01:20 +04:00
|
|
|
basic->mType = ResponseType::Basic;
|
2015-03-17 18:47:01 +03:00
|
|
|
basic->mHeaders = InternalHeaders::BasicHeaders(Headers());
|
|
|
|
basic->mWrappedResponse = this;
|
2014-10-06 22:01:20 +04:00
|
|
|
return basic.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<InternalResponse>
|
2015-03-17 18:47:01 +03:00
|
|
|
InternalResponse::CORSResponse()
|
2014-10-06 22:01:20 +04:00
|
|
|
{
|
2015-03-17 18:47:01 +03:00
|
|
|
MOZ_ASSERT(!mWrappedResponse, "Can't CORSResponse a already wrapped response");
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<InternalResponse> cors = CreateIncompleteCopy();
|
2014-10-06 22:01:20 +04:00
|
|
|
cors->mType = ResponseType::Cors;
|
2015-03-17 18:47:01 +03:00
|
|
|
cors->mHeaders = InternalHeaders::CORSHeaders(Headers());
|
|
|
|
cors->mWrappedResponse = this;
|
2014-10-06 22:01:20 +04:00
|
|
|
return cors.forget();
|
|
|
|
}
|
|
|
|
|
2015-06-05 07:39:34 +03:00
|
|
|
void
|
|
|
|
InternalResponse::SetPrincipalInfo(UniquePtr<mozilla::ipc::PrincipalInfo> aPrincipalInfo)
|
|
|
|
{
|
|
|
|
mPrincipalInfo = Move(aPrincipalInfo);
|
|
|
|
}
|
|
|
|
|
2015-10-22 21:07:32 +03:00
|
|
|
LoadTainting
|
|
|
|
InternalResponse::GetTainting() const
|
|
|
|
{
|
|
|
|
switch (mType) {
|
|
|
|
case ResponseType::Cors:
|
|
|
|
return LoadTainting::CORS;
|
|
|
|
case ResponseType::Opaque:
|
|
|
|
return LoadTainting::Opaque;
|
|
|
|
default:
|
|
|
|
return LoadTainting::Basic;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-12 23:15:12 +03:00
|
|
|
already_AddRefed<InternalResponse>
|
|
|
|
InternalResponse::Unfiltered()
|
|
|
|
{
|
|
|
|
RefPtr<InternalResponse> ref = mWrappedResponse;
|
|
|
|
if (!ref) {
|
|
|
|
ref = this;
|
|
|
|
}
|
|
|
|
return ref.forget();
|
|
|
|
}
|
|
|
|
|
2015-06-05 07:39:34 +03:00
|
|
|
already_AddRefed<InternalResponse>
|
|
|
|
InternalResponse::OpaqueResponse()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mWrappedResponse, "Can't OpaqueResponse a already wrapped response");
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<InternalResponse> response = new InternalResponse(0, EmptyCString());
|
2015-06-05 07:39:34 +03:00
|
|
|
response->mType = ResponseType::Opaque;
|
|
|
|
response->mTerminationReason = mTerminationReason;
|
|
|
|
response->mChannelInfo = mChannelInfo;
|
|
|
|
if (mPrincipalInfo) {
|
|
|
|
response->mPrincipalInfo = MakeUnique<mozilla::ipc::PrincipalInfo>(*mPrincipalInfo);
|
|
|
|
}
|
|
|
|
response->mWrappedResponse = this;
|
|
|
|
return response.forget();
|
|
|
|
}
|
|
|
|
|
2015-09-01 00:26:29 +03:00
|
|
|
already_AddRefed<InternalResponse>
|
|
|
|
InternalResponse::OpaqueRedirectResponse()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mWrappedResponse, "Can't OpaqueRedirectResponse a already wrapped response");
|
2016-05-03 04:48:40 +03:00
|
|
|
MOZ_ASSERT(!mURLList.IsEmpty(), "URLList should not be emtpy for internalResponse");
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<InternalResponse> response = OpaqueResponse();
|
2015-09-01 00:26:29 +03:00
|
|
|
response->mType = ResponseType::Opaqueredirect;
|
2016-05-03 04:48:40 +03:00
|
|
|
response->mURLList = mURLList;
|
2015-09-01 00:26:29 +03:00
|
|
|
return response.forget();
|
|
|
|
}
|
|
|
|
|
2015-06-05 07:39:34 +03:00
|
|
|
already_AddRefed<InternalResponse>
|
|
|
|
InternalResponse::CreateIncompleteCopy()
|
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<InternalResponse> copy = new InternalResponse(mStatus, mStatusText);
|
2015-06-05 07:39:34 +03:00
|
|
|
copy->mType = mType;
|
|
|
|
copy->mTerminationReason = mTerminationReason;
|
2016-05-03 04:48:40 +03:00
|
|
|
copy->mURLList = mURLList;
|
2015-06-05 07:39:34 +03:00
|
|
|
copy->mChannelInfo = mChannelInfo;
|
|
|
|
if (mPrincipalInfo) {
|
|
|
|
copy->mPrincipalInfo = MakeUnique<mozilla::ipc::PrincipalInfo>(*mPrincipalInfo);
|
|
|
|
}
|
|
|
|
return copy.forget();
|
|
|
|
}
|
|
|
|
|
2014-09-27 03:41:15 +04:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|