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/. */
|
|
|
|
|
|
|
|
#ifndef mozilla_dom_InternalResponse_h
|
|
|
|
#define mozilla_dom_InternalResponse_h
|
|
|
|
|
2014-10-02 21:59:20 +04:00
|
|
|
#include "nsIInputStream.h"
|
2014-09-27 03:41:15 +04:00
|
|
|
#include "nsISupportsImpl.h"
|
|
|
|
|
|
|
|
#include "mozilla/dom/ResponseBinding.h"
|
2015-05-25 21:21:05 +03:00
|
|
|
#include "mozilla/dom/ChannelInfo.h"
|
2015-06-05 07:39:34 +03:00
|
|
|
#include "mozilla/UniquePtr.h"
|
2014-09-27 03:41:15 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
2015-06-05 07:39:34 +03:00
|
|
|
namespace ipc {
|
|
|
|
class PrincipalInfo;
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace ipc
|
2015-06-05 07:39:34 +03:00
|
|
|
|
2014-09-27 03:41:15 +04:00
|
|
|
namespace dom {
|
|
|
|
|
2014-10-02 21:59:20 +04:00
|
|
|
class InternalHeaders;
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class InternalResponse final
|
2014-09-27 03:41:15 +04:00
|
|
|
{
|
|
|
|
friend class FetchDriver;
|
|
|
|
|
|
|
|
public:
|
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(InternalResponse)
|
|
|
|
|
|
|
|
InternalResponse(uint16_t aStatus, const nsACString& aStatusText);
|
|
|
|
|
2015-02-20 04:24:24 +03:00
|
|
|
already_AddRefed<InternalResponse> Clone();
|
|
|
|
|
2014-09-27 03:41:15 +04:00
|
|
|
static already_AddRefed<InternalResponse>
|
|
|
|
NetworkError()
|
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<InternalResponse> response = new InternalResponse(0, EmptyCString());
|
2015-03-08 05:54:41 +03:00
|
|
|
ErrorResult result;
|
|
|
|
response->Headers()->SetGuard(HeadersGuardEnum::Immutable, result);
|
|
|
|
MOZ_ASSERT(!result.Failed());
|
2014-09-27 03:41:15 +04:00
|
|
|
response->mType = ResponseType::Error;
|
|
|
|
return response.forget();
|
|
|
|
}
|
|
|
|
|
2015-03-17 18:47:01 +03:00
|
|
|
already_AddRefed<InternalResponse>
|
2015-06-05 07:39:34 +03:00
|
|
|
OpaqueResponse();
|
2014-10-06 22:01:20 +04:00
|
|
|
|
2015-09-01 00:26:29 +03:00
|
|
|
already_AddRefed<InternalResponse>
|
|
|
|
OpaqueRedirectResponse();
|
|
|
|
|
2015-03-17 18:47:01 +03:00
|
|
|
already_AddRefed<InternalResponse>
|
|
|
|
BasicResponse();
|
2014-10-06 22:01:20 +04:00
|
|
|
|
2015-03-17 18:47:01 +03:00
|
|
|
already_AddRefed<InternalResponse>
|
|
|
|
CORSResponse();
|
2014-10-06 22:01:20 +04:00
|
|
|
|
2014-09-27 03:41:15 +04:00
|
|
|
ResponseType
|
|
|
|
Type() const
|
|
|
|
{
|
2015-03-17 18:47:01 +03: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-09-01 00:26:29 +03:00
|
|
|
MOZ_ASSERT_IF(mType == ResponseType::Opaqueredirect, mWrappedResponse);
|
2014-09-27 03:41:15 +04:00
|
|
|
return mType;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
IsError() const
|
|
|
|
{
|
|
|
|
return Type() == ResponseType::Error;
|
|
|
|
}
|
|
|
|
|
2016-05-03 04:48:40 +03:00
|
|
|
// GetUrl should return last fetch URL in response's url list and null if
|
|
|
|
// response's url list is the empty list.
|
2014-10-06 22:01:20 +04:00
|
|
|
void
|
2016-05-03 04:48:40 +03:00
|
|
|
GetURL(nsCString& aURL) const
|
2014-10-06 22:01:20 +04:00
|
|
|
{
|
2016-05-03 04:48:40 +03:00
|
|
|
// Empty urlList when response is a synthetic response.
|
|
|
|
if (mURLList.IsEmpty()) {
|
|
|
|
aURL.Truncate();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
aURL.Assign(mURLList.LastElement());
|
2014-10-06 22:01:20 +04:00
|
|
|
}
|
|
|
|
|
2015-09-18 23:41:56 +03:00
|
|
|
void
|
2016-05-03 04:48:40 +03:00
|
|
|
GetURLList(nsTArray<nsCString>& aURLList) const
|
|
|
|
{
|
|
|
|
aURLList.Assign(mURLList);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GetUnfilteredURL(nsCString& aURL) const
|
2015-09-18 23:41:56 +03:00
|
|
|
{
|
|
|
|
if (mWrappedResponse) {
|
2016-05-03 04:48:40 +03:00
|
|
|
return mWrappedResponse->GetURL(aURL);
|
2015-09-18 23:41:56 +03:00
|
|
|
}
|
|
|
|
|
2016-05-03 04:48:40 +03:00
|
|
|
return GetURL(aURL);
|
2015-09-18 23:41:56 +03:00
|
|
|
}
|
|
|
|
|
2014-10-06 22:01:20 +04:00
|
|
|
void
|
2016-05-03 04:48:40 +03:00
|
|
|
GetUnfilteredURLList(nsTArray<nsCString>& aURLList) const
|
2014-09-27 03:41:15 +04:00
|
|
|
{
|
2016-05-03 04:48:40 +03:00
|
|
|
if (mWrappedResponse) {
|
|
|
|
return mWrappedResponse->GetURLList(aURLList);
|
|
|
|
}
|
|
|
|
|
|
|
|
return GetURLList(aURLList);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SetURLList(const nsTArray<nsCString>& aURLList)
|
|
|
|
{
|
|
|
|
mURLList.Assign(aURLList);
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
for(uint32_t i = 0; i < mURLList.Length(); ++i) {
|
|
|
|
MOZ_ASSERT(mURLList[i].Find(NS_LITERAL_CSTRING("#")) == kNotFound);
|
|
|
|
}
|
|
|
|
#endif
|
2014-09-27 03:41:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t
|
|
|
|
GetStatus() const
|
|
|
|
{
|
|
|
|
return mStatus;
|
|
|
|
}
|
|
|
|
|
2015-09-01 00:26:29 +03:00
|
|
|
uint16_t
|
|
|
|
GetUnfilteredStatus() const
|
|
|
|
{
|
|
|
|
if (mWrappedResponse) {
|
|
|
|
return mWrappedResponse->GetStatus();
|
|
|
|
}
|
|
|
|
|
|
|
|
return GetStatus();
|
|
|
|
}
|
|
|
|
|
2014-09-27 03:41:15 +04:00
|
|
|
const nsCString&
|
|
|
|
GetStatusText() const
|
|
|
|
{
|
|
|
|
return mStatusText;
|
|
|
|
}
|
|
|
|
|
2015-09-01 00:26:29 +03:00
|
|
|
const nsCString&
|
|
|
|
GetUnfilteredStatusText() const
|
|
|
|
{
|
|
|
|
if (mWrappedResponse) {
|
|
|
|
return mWrappedResponse->GetStatusText();
|
|
|
|
}
|
|
|
|
|
|
|
|
return GetStatusText();
|
|
|
|
}
|
|
|
|
|
2014-10-02 21:59:20 +04:00
|
|
|
InternalHeaders*
|
|
|
|
Headers()
|
2014-09-27 03:41:15 +04:00
|
|
|
{
|
|
|
|
return mHeaders;
|
|
|
|
}
|
|
|
|
|
2015-03-17 18:47:01 +03:00
|
|
|
InternalHeaders*
|
|
|
|
UnfilteredHeaders()
|
|
|
|
{
|
|
|
|
if (mWrappedResponse) {
|
|
|
|
return mWrappedResponse->Headers();
|
|
|
|
};
|
|
|
|
|
|
|
|
return Headers();
|
|
|
|
}
|
|
|
|
|
2014-09-27 03:41:15 +04:00
|
|
|
void
|
2015-09-01 00:26:29 +03:00
|
|
|
GetUnfilteredBody(nsIInputStream** aStream)
|
2014-09-27 03:41:15 +04:00
|
|
|
{
|
2015-03-17 18:47:01 +03:00
|
|
|
if (mWrappedResponse) {
|
|
|
|
MOZ_ASSERT(!mBody);
|
|
|
|
return mWrappedResponse->GetBody(aStream);
|
|
|
|
}
|
2014-09-27 03:41:15 +04:00
|
|
|
nsCOMPtr<nsIInputStream> stream = mBody;
|
|
|
|
stream.forget(aStream);
|
|
|
|
}
|
|
|
|
|
2015-06-13 04:59:01 +03:00
|
|
|
void
|
|
|
|
GetBody(nsIInputStream** aStream)
|
|
|
|
{
|
2015-09-01 00:26:29 +03:00
|
|
|
if (Type() == ResponseType::Opaque ||
|
|
|
|
Type() == ResponseType::Opaqueredirect) {
|
2015-06-13 04:59:01 +03:00
|
|
|
*aStream = nullptr;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-09-01 00:26:29 +03:00
|
|
|
return GetUnfilteredBody(aStream);
|
2015-06-13 04:59:01 +03:00
|
|
|
}
|
|
|
|
|
2014-09-27 03:41:15 +04:00
|
|
|
void
|
|
|
|
SetBody(nsIInputStream* aBody)
|
|
|
|
{
|
2015-03-17 18:47:01 +03:00
|
|
|
if (mWrappedResponse) {
|
|
|
|
return mWrappedResponse->SetBody(aBody);
|
|
|
|
}
|
2014-12-10 11:51:59 +03:00
|
|
|
// A request's body may not be reset once set.
|
|
|
|
MOZ_ASSERT(!mBody);
|
2014-09-27 03:41:15 +04:00
|
|
|
mBody = aBody;
|
|
|
|
}
|
|
|
|
|
2015-05-25 21:21:05 +03:00
|
|
|
void
|
|
|
|
InitChannelInfo(nsIChannel* aChannel)
|
2015-03-05 00:05:21 +03:00
|
|
|
{
|
2015-05-25 21:21:05 +03:00
|
|
|
mChannelInfo.InitFromChannel(aChannel);
|
2015-03-05 00:05:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-05-25 21:21:05 +03:00
|
|
|
InitChannelInfo(const mozilla::ipc::IPCChannelInfo& aChannelInfo)
|
|
|
|
{
|
|
|
|
mChannelInfo.InitFromIPCChannelInfo(aChannelInfo);
|
|
|
|
}
|
2015-03-05 00:05:21 +03:00
|
|
|
|
|
|
|
void
|
2015-05-25 21:21:05 +03:00
|
|
|
InitChannelInfo(const ChannelInfo& aChannelInfo)
|
|
|
|
{
|
|
|
|
mChannelInfo = aChannelInfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ChannelInfo&
|
|
|
|
GetChannelInfo() const
|
|
|
|
{
|
|
|
|
return mChannelInfo;
|
|
|
|
}
|
2015-03-05 00:05:21 +03:00
|
|
|
|
2015-06-05 07:39:34 +03:00
|
|
|
const UniquePtr<mozilla::ipc::PrincipalInfo>&
|
|
|
|
GetPrincipalInfo() const
|
|
|
|
{
|
|
|
|
return mPrincipalInfo;
|
|
|
|
}
|
|
|
|
|
2016-05-03 04:48:40 +03:00
|
|
|
bool
|
|
|
|
IsRedirected() const
|
|
|
|
{
|
|
|
|
return mURLList.Length() > 1;
|
|
|
|
}
|
|
|
|
|
2015-06-05 07:39:34 +03:00
|
|
|
// Takes ownership of the principal info.
|
|
|
|
void
|
|
|
|
SetPrincipalInfo(UniquePtr<mozilla::ipc::PrincipalInfo> aPrincipalInfo);
|
|
|
|
|
2015-10-22 21:07:32 +03:00
|
|
|
LoadTainting
|
|
|
|
GetTainting() const;
|
|
|
|
|
2016-01-12 23:15:12 +03:00
|
|
|
already_AddRefed<InternalResponse>
|
|
|
|
Unfiltered();
|
|
|
|
|
2014-09-27 03:41:15 +04:00
|
|
|
private:
|
2015-06-05 07:39:34 +03:00
|
|
|
~InternalResponse();
|
2014-09-27 03:41:15 +04:00
|
|
|
|
2015-03-17 18:47:01 +03: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-05 07:39:34 +03:00
|
|
|
already_AddRefed<InternalResponse> CreateIncompleteCopy();
|
2014-10-06 22:01:20 +04:00
|
|
|
|
2014-09-27 03:41:15 +04:00
|
|
|
ResponseType mType;
|
|
|
|
nsCString mTerminationReason;
|
2016-05-03 04:48:40 +03:00
|
|
|
// A response has an associated url list (a list of zero or more fetch URLs).
|
|
|
|
// Unless stated otherwise, it is the empty list. The current url is the last
|
|
|
|
// element in mURLlist
|
|
|
|
nsTArray<nsCString> mURLList;
|
2014-09-27 03:41:15 +04:00
|
|
|
const uint16_t mStatus;
|
|
|
|
const nsCString mStatusText;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<InternalHeaders> mHeaders;
|
2014-09-27 03:41:15 +04:00
|
|
|
nsCOMPtr<nsIInputStream> mBody;
|
2015-05-25 21:21:05 +03:00
|
|
|
ChannelInfo mChannelInfo;
|
2015-06-05 07:39:34 +03:00
|
|
|
UniquePtr<mozilla::ipc::PrincipalInfo> mPrincipalInfo;
|
2015-03-17 18:47:01 +03: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-18 08:24:48 +03:00
|
|
|
RefPtr<InternalResponse> mWrappedResponse;
|
2014-09-27 03:41:15 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // mozilla_dom_InternalResponse_h
|