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"
|
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))
|
2014-09-27 03:41:15 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-06-05 07:39:34 +03:00
|
|
|
InternalResponse::~InternalResponse()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
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-08-13 18:22:19 +03:00
|
|
|
nsresult
|
|
|
|
InternalResponse::StripFragmentAndSetUrl(const nsACString& aUrl)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> iuri;
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
rv = NS_NewURI(getter_AddRefs(iuri), aUrl);
|
|
|
|
if(NS_WARN_IF(NS_FAILED(rv))){
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> iuriClone;
|
|
|
|
// We use CloneIgnoringRef to strip away the fragment even if the original URI
|
|
|
|
// is immutable.
|
|
|
|
rv = iuri->CloneIgnoringRef(getter_AddRefs(iuriClone));
|
|
|
|
if(NS_WARN_IF(NS_FAILED(rv))){
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCString spec;
|
|
|
|
rv = iuriClone->GetSpec(spec);
|
|
|
|
if(NS_WARN_IF(NS_FAILED(rv))){
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
SetUrl(spec);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
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");
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<InternalResponse> response = OpaqueResponse();
|
2015-09-01 00:26:29 +03:00
|
|
|
response->mType = ResponseType::Opaqueredirect;
|
|
|
|
response->mURL = mURL;
|
|
|
|
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;
|
|
|
|
copy->mURL = mURL;
|
|
|
|
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
|