2015-05-03 12:32:37 -07: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-26 16:41:15 -07: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/. */
|
|
|
|
|
|
|
|
#ifndef mozilla_dom_InternalResponse_h
|
|
|
|
#define mozilla_dom_InternalResponse_h
|
|
|
|
|
2014-10-02 10:59:20 -07:00
|
|
|
#include "nsIInputStream.h"
|
2014-09-26 16:41:15 -07:00
|
|
|
#include "nsISupportsImpl.h"
|
|
|
|
|
|
|
|
#include "mozilla/dom/ResponseBinding.h"
|
2015-05-25 11:21:05 -07:00
|
|
|
#include "mozilla/dom/ChannelInfo.h"
|
2015-06-04 21:39:34 -07:00
|
|
|
#include "mozilla/UniquePtr.h"
|
2014-09-26 16:41:15 -07:00
|
|
|
|
|
|
|
namespace mozilla {
|
2015-06-04 21:39:34 -07:00
|
|
|
namespace ipc {
|
|
|
|
class PrincipalInfo;
|
2015-07-13 08:25:42 -07:00
|
|
|
} // namespace ipc
|
2015-06-04 21:39:34 -07:00
|
|
|
|
2014-09-26 16:41:15 -07:00
|
|
|
namespace dom {
|
|
|
|
|
2014-10-02 10:59:20 -07:00
|
|
|
class InternalHeaders;
|
|
|
|
|
2015-03-21 09:28:04 -07:00
|
|
|
class InternalResponse final
|
2014-09-26 16:41:15 -07:00
|
|
|
{
|
|
|
|
friend class FetchDriver;
|
|
|
|
|
|
|
|
public:
|
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(InternalResponse)
|
|
|
|
|
|
|
|
InternalResponse(uint16_t aStatus, const nsACString& aStatusText);
|
|
|
|
|
2015-02-19 17:24:24 -08:00
|
|
|
already_AddRefed<InternalResponse> Clone();
|
|
|
|
|
2014-09-26 16:41:15 -07:00
|
|
|
static already_AddRefed<InternalResponse>
|
|
|
|
NetworkError()
|
|
|
|
{
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<InternalResponse> response = new InternalResponse(0, EmptyCString());
|
2015-03-07 18:54:41 -08:00
|
|
|
ErrorResult result;
|
|
|
|
response->Headers()->SetGuard(HeadersGuardEnum::Immutable, result);
|
|
|
|
MOZ_ASSERT(!result.Failed());
|
2014-09-26 16:41:15 -07:00
|
|
|
response->mType = ResponseType::Error;
|
|
|
|
return response.forget();
|
|
|
|
}
|
|
|
|
|
2015-03-17 08:47:01 -07:00
|
|
|
already_AddRefed<InternalResponse>
|
2015-06-04 21:39:34 -07:00
|
|
|
OpaqueResponse();
|
2014-10-06 11:01:20 -07:00
|
|
|
|
2015-08-31 14:26:29 -07:00
|
|
|
already_AddRefed<InternalResponse>
|
|
|
|
OpaqueRedirectResponse();
|
|
|
|
|
2015-03-17 08:47:01 -07:00
|
|
|
already_AddRefed<InternalResponse>
|
|
|
|
BasicResponse();
|
2014-10-06 11:01:20 -07:00
|
|
|
|
2015-03-17 08:47:01 -07:00
|
|
|
already_AddRefed<InternalResponse>
|
|
|
|
CORSResponse();
|
2014-10-06 11:01:20 -07:00
|
|
|
|
2014-09-26 16:41:15 -07:00
|
|
|
ResponseType
|
|
|
|
Type() const
|
|
|
|
{
|
2015-03-17 08:47:01 -07:00
|
|
|
MOZ_ASSERT_IF(mType == ResponseType::Error, !mWrappedResponse);
|
|
|
|
MOZ_ASSERT_IF(mType == ResponseType::Default, !mWrappedResponse);
|
|
|
|
MOZ_ASSERT_IF(mType == ResponseType::Basic, mWrappedResponse);
|
|
|
|
MOZ_ASSERT_IF(mType == ResponseType::Cors, mWrappedResponse);
|
|
|
|
MOZ_ASSERT_IF(mType == ResponseType::Opaque, mWrappedResponse);
|
2015-08-31 14:26:29 -07:00
|
|
|
MOZ_ASSERT_IF(mType == ResponseType::Opaqueredirect, mWrappedResponse);
|
2014-09-26 16:41:15 -07:00
|
|
|
return mType;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
IsError() const
|
|
|
|
{
|
|
|
|
return Type() == ResponseType::Error;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME(nsm): Return with exclude fragment.
|
2014-10-06 11:01:20 -07:00
|
|
|
void
|
|
|
|
GetUrl(nsCString& aURL) const
|
|
|
|
{
|
|
|
|
aURL.Assign(mURL);
|
|
|
|
}
|
|
|
|
|
2015-09-18 13:41:56 -07:00
|
|
|
void
|
|
|
|
GetUnfilteredUrl(nsCString& aURL) const
|
|
|
|
{
|
|
|
|
if (mWrappedResponse) {
|
|
|
|
return mWrappedResponse->GetUrl(aURL);
|
|
|
|
}
|
|
|
|
|
|
|
|
return GetUrl(aURL);
|
|
|
|
}
|
|
|
|
|
2015-08-13 08:22:19 -07:00
|
|
|
// SetUrl should only be called when the fragment has alredy been stripped
|
2014-10-06 11:01:20 -07:00
|
|
|
void
|
|
|
|
SetUrl(const nsACString& aURL)
|
2014-09-26 16:41:15 -07:00
|
|
|
{
|
2014-10-06 11:01:20 -07:00
|
|
|
mURL.Assign(aURL);
|
2014-09-26 16:41:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t
|
|
|
|
GetStatus() const
|
|
|
|
{
|
|
|
|
return mStatus;
|
|
|
|
}
|
|
|
|
|
2015-08-31 14:26:29 -07:00
|
|
|
uint16_t
|
|
|
|
GetUnfilteredStatus() const
|
|
|
|
{
|
|
|
|
if (mWrappedResponse) {
|
|
|
|
return mWrappedResponse->GetStatus();
|
|
|
|
}
|
|
|
|
|
|
|
|
return GetStatus();
|
|
|
|
}
|
|
|
|
|
2014-09-26 16:41:15 -07:00
|
|
|
const nsCString&
|
|
|
|
GetStatusText() const
|
|
|
|
{
|
|
|
|
return mStatusText;
|
|
|
|
}
|
|
|
|
|
2015-08-31 14:26:29 -07:00
|
|
|
const nsCString&
|
|
|
|
GetUnfilteredStatusText() const
|
|
|
|
{
|
|
|
|
if (mWrappedResponse) {
|
|
|
|
return mWrappedResponse->GetStatusText();
|
|
|
|
}
|
|
|
|
|
|
|
|
return GetStatusText();
|
|
|
|
}
|
|
|
|
|
2014-10-02 10:59:20 -07:00
|
|
|
InternalHeaders*
|
|
|
|
Headers()
|
2014-09-26 16:41:15 -07:00
|
|
|
{
|
|
|
|
return mHeaders;
|
|
|
|
}
|
|
|
|
|
2015-03-17 08:47:01 -07:00
|
|
|
InternalHeaders*
|
|
|
|
UnfilteredHeaders()
|
|
|
|
{
|
|
|
|
if (mWrappedResponse) {
|
|
|
|
return mWrappedResponse->Headers();
|
|
|
|
};
|
|
|
|
|
|
|
|
return Headers();
|
|
|
|
}
|
|
|
|
|
2014-09-26 16:41:15 -07:00
|
|
|
void
|
2015-08-31 14:26:29 -07:00
|
|
|
GetUnfilteredBody(nsIInputStream** aStream)
|
2014-09-26 16:41:15 -07:00
|
|
|
{
|
2015-03-17 08:47:01 -07:00
|
|
|
if (mWrappedResponse) {
|
|
|
|
MOZ_ASSERT(!mBody);
|
|
|
|
return mWrappedResponse->GetBody(aStream);
|
|
|
|
}
|
2014-09-26 16:41:15 -07:00
|
|
|
nsCOMPtr<nsIInputStream> stream = mBody;
|
|
|
|
stream.forget(aStream);
|
|
|
|
}
|
|
|
|
|
2015-06-12 18:59:01 -07:00
|
|
|
void
|
|
|
|
GetBody(nsIInputStream** aStream)
|
|
|
|
{
|
2015-08-31 14:26:29 -07:00
|
|
|
if (Type() == ResponseType::Opaque ||
|
|
|
|
Type() == ResponseType::Opaqueredirect) {
|
2015-06-12 18:59:01 -07:00
|
|
|
*aStream = nullptr;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-08-31 14:26:29 -07:00
|
|
|
return GetUnfilteredBody(aStream);
|
2015-06-12 18:59:01 -07:00
|
|
|
}
|
|
|
|
|
2014-09-26 16:41:15 -07:00
|
|
|
void
|
|
|
|
SetBody(nsIInputStream* aBody)
|
|
|
|
{
|
2015-03-17 08:47:01 -07:00
|
|
|
if (mWrappedResponse) {
|
|
|
|
return mWrappedResponse->SetBody(aBody);
|
|
|
|
}
|
2014-12-10 00:51:59 -08:00
|
|
|
// A request's body may not be reset once set.
|
|
|
|
MOZ_ASSERT(!mBody);
|
2014-09-26 16:41:15 -07:00
|
|
|
mBody = aBody;
|
|
|
|
}
|
|
|
|
|
2015-05-25 11:21:05 -07:00
|
|
|
void
|
|
|
|
InitChannelInfo(nsIChannel* aChannel)
|
2015-03-04 13:05:21 -08:00
|
|
|
{
|
2015-05-25 11:21:05 -07:00
|
|
|
mChannelInfo.InitFromChannel(aChannel);
|
2015-03-04 13:05:21 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-05-25 11:21:05 -07:00
|
|
|
InitChannelInfo(const mozilla::ipc::IPCChannelInfo& aChannelInfo)
|
|
|
|
{
|
|
|
|
mChannelInfo.InitFromIPCChannelInfo(aChannelInfo);
|
|
|
|
}
|
2015-03-04 13:05:21 -08:00
|
|
|
|
|
|
|
void
|
2015-05-25 11:21:05 -07:00
|
|
|
InitChannelInfo(const ChannelInfo& aChannelInfo)
|
|
|
|
{
|
|
|
|
mChannelInfo = aChannelInfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ChannelInfo&
|
|
|
|
GetChannelInfo() const
|
|
|
|
{
|
|
|
|
return mChannelInfo;
|
|
|
|
}
|
2015-03-04 13:05:21 -08:00
|
|
|
|
2015-06-04 21:39:34 -07:00
|
|
|
const UniquePtr<mozilla::ipc::PrincipalInfo>&
|
|
|
|
GetPrincipalInfo() const
|
|
|
|
{
|
|
|
|
return mPrincipalInfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Takes ownership of the principal info.
|
|
|
|
void
|
|
|
|
SetPrincipalInfo(UniquePtr<mozilla::ipc::PrincipalInfo> aPrincipalInfo);
|
|
|
|
|
2015-08-13 08:22:19 -07:00
|
|
|
nsresult
|
|
|
|
StripFragmentAndSetUrl(const nsACString& aUrl);
|
|
|
|
|
2015-10-22 11:07:32 -07:00
|
|
|
LoadTainting
|
|
|
|
GetTainting() const;
|
|
|
|
|
2014-09-26 16:41:15 -07:00
|
|
|
private:
|
2015-06-04 21:39:34 -07:00
|
|
|
~InternalResponse();
|
2014-09-26 16:41:15 -07:00
|
|
|
|
2015-03-17 08:47:01 -07:00
|
|
|
explicit InternalResponse(const InternalResponse& aOther) = delete;
|
|
|
|
InternalResponse& operator=(const InternalResponse&) = delete;
|
|
|
|
|
|
|
|
// Returns an instance of InternalResponse which is a copy of this
|
|
|
|
// InternalResponse, except headers, body and wrapped response (if any) which
|
|
|
|
// are left uninitialized. Used for cloning and filtering.
|
2015-06-04 21:39:34 -07:00
|
|
|
already_AddRefed<InternalResponse> CreateIncompleteCopy();
|
2014-10-06 11:01:20 -07:00
|
|
|
|
2014-09-26 16:41:15 -07:00
|
|
|
ResponseType mType;
|
|
|
|
nsCString mTerminationReason;
|
|
|
|
nsCString mURL;
|
|
|
|
const uint16_t mStatus;
|
|
|
|
const nsCString mStatusText;
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<InternalHeaders> mHeaders;
|
2014-09-26 16:41:15 -07:00
|
|
|
nsCOMPtr<nsIInputStream> mBody;
|
2015-05-25 11:21:05 -07:00
|
|
|
ChannelInfo mChannelInfo;
|
2015-06-04 21:39:34 -07:00
|
|
|
UniquePtr<mozilla::ipc::PrincipalInfo> mPrincipalInfo;
|
2015-03-17 08:47:01 -07:00
|
|
|
|
|
|
|
// For filtered responses.
|
|
|
|
// Cache, and SW interception should always serialize/access the underlying
|
|
|
|
// unfiltered headers and when deserializing, create an InternalResponse
|
|
|
|
// with the unfiltered headers followed by wrapping it.
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<InternalResponse> mWrappedResponse;
|
2014-09-26 16:41:15 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // mozilla_dom_InternalResponse_h
|