2014-10-17 13:55:09 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: -*- */
|
|
|
|
/* vim:set expandtab ts=2 sw=2 sts=2 cin: */
|
|
|
|
/* 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 "HttpLog.h"
|
|
|
|
|
|
|
|
#include "InterceptedChannel.h"
|
|
|
|
#include "nsInputStreamPump.h"
|
|
|
|
#include "nsIPipe.h"
|
|
|
|
#include "nsIStreamListener.h"
|
|
|
|
#include "nsHttpChannel.h"
|
|
|
|
#include "HttpChannelChild.h"
|
|
|
|
#include "nsHttpResponseHead.h"
|
2015-05-25 18:21:05 +00:00
|
|
|
#include "mozilla/dom/ChannelInfo.h"
|
2014-10-17 13:55:09 +00:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace net {
|
|
|
|
|
2015-06-12 14:20:21 +00:00
|
|
|
extern bool
|
|
|
|
WillRedirect(const nsHttpResponseHead * response);
|
|
|
|
|
2014-10-17 13:55:09 +00:00
|
|
|
extern nsresult
|
|
|
|
DoAddCacheEntryHeaders(nsHttpChannel *self,
|
|
|
|
nsICacheEntry *entry,
|
|
|
|
nsHttpRequestHead *requestHead,
|
|
|
|
nsHttpResponseHead *responseHead,
|
|
|
|
nsISupports *securityInfo);
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(InterceptedChannelBase, nsIInterceptedChannel)
|
|
|
|
|
2015-02-19 01:34:29 +00:00
|
|
|
InterceptedChannelBase::InterceptedChannelBase(nsINetworkInterceptController* aController,
|
|
|
|
bool aIsNavigation)
|
2014-10-17 13:55:09 +00:00
|
|
|
: mController(aController)
|
2015-02-19 01:34:29 +00:00
|
|
|
, mIsNavigation(aIsNavigation)
|
2014-10-17 13:55:09 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
InterceptedChannelBase::~InterceptedChannelBase()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-02-19 01:34:29 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
InterceptedChannelBase::GetResponseBody(nsIOutputStream** aStream)
|
|
|
|
{
|
|
|
|
NS_IF_ADDREF(*aStream = mResponseBody);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-10-17 13:55:09 +00:00
|
|
|
void
|
|
|
|
InterceptedChannelBase::EnsureSynthesizedResponse()
|
|
|
|
{
|
|
|
|
if (mSynthesizedResponseHead.isNothing()) {
|
2015-02-19 01:34:29 +00:00
|
|
|
mSynthesizedResponseHead.emplace(new nsHttpResponseHead());
|
2014-10-17 13:55:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-02-19 01:34:29 +00:00
|
|
|
InterceptedChannelBase::DoNotifyController()
|
2014-10-17 13:55:09 +00:00
|
|
|
{
|
2015-02-19 01:34:29 +00:00
|
|
|
nsresult rv = mController->ChannelIntercepted(this);
|
2015-03-31 01:36:26 +00:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
rv = ResetInterception();
|
|
|
|
NS_WARN_IF_FALSE(NS_SUCCEEDED(rv), "Failed to resume intercepted network request");
|
|
|
|
}
|
2015-02-19 01:34:29 +00:00
|
|
|
mController = nullptr;
|
2014-10-17 13:55:09 +00:00
|
|
|
}
|
|
|
|
|
2015-02-19 01:34:29 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
InterceptedChannelBase::GetIsNavigation(bool* aIsNavigation)
|
|
|
|
{
|
|
|
|
*aIsNavigation = mIsNavigation;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-03-17 15:48:30 +00:00
|
|
|
nsresult
|
|
|
|
InterceptedChannelBase::DoSynthesizeStatus(uint16_t aStatus, const nsACString& aReason)
|
|
|
|
{
|
|
|
|
EnsureSynthesizedResponse();
|
|
|
|
|
|
|
|
// Always assume HTTP 1.1 for synthesized responses.
|
|
|
|
nsAutoCString statusLine;
|
|
|
|
statusLine.AppendLiteral("HTTP/1.1 ");
|
|
|
|
statusLine.AppendInt(aStatus);
|
|
|
|
statusLine.AppendLiteral(" ");
|
|
|
|
statusLine.Append(aReason);
|
|
|
|
|
|
|
|
(*mSynthesizedResponseHead)->ParseStatusLine(statusLine.get());
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-10-17 13:55:09 +00:00
|
|
|
nsresult
|
|
|
|
InterceptedChannelBase::DoSynthesizeHeader(const nsACString& aName, const nsACString& aValue)
|
|
|
|
{
|
|
|
|
EnsureSynthesizedResponse();
|
|
|
|
|
|
|
|
nsAutoCString header = aName + NS_LITERAL_CSTRING(": ") + aValue;
|
|
|
|
// Overwrite any existing header.
|
2015-02-19 01:34:29 +00:00
|
|
|
nsresult rv = (*mSynthesizedResponseHead)->ParseHeaderLine(header.get());
|
2014-10-17 13:55:09 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
InterceptedChannelChrome::InterceptedChannelChrome(nsHttpChannel* aChannel,
|
|
|
|
nsINetworkInterceptController* aController,
|
|
|
|
nsICacheEntry* aEntry)
|
2015-02-19 01:34:29 +00:00
|
|
|
: InterceptedChannelBase(aController, aChannel->IsNavigation())
|
2014-10-17 13:55:09 +00:00
|
|
|
, mChannel(aChannel)
|
|
|
|
, mSynthesizedCacheEntry(aEntry)
|
|
|
|
{
|
2015-03-17 15:48:32 +00:00
|
|
|
nsresult rv = mChannel->GetApplyConversion(&mOldApplyConversion);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
mOldApplyConversion = false;
|
|
|
|
}
|
2014-10-17 13:55:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
InterceptedChannelChrome::NotifyController()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIOutputStream> out;
|
|
|
|
|
2015-03-17 15:48:32 +00:00
|
|
|
// Intercepted responses should already be decoded.
|
|
|
|
mChannel->SetApplyConversion(false);
|
|
|
|
|
2015-02-19 01:34:29 +00:00
|
|
|
nsresult rv = mSynthesizedCacheEntry->OpenOutputStream(0, getter_AddRefs(mResponseBody));
|
2014-10-17 13:55:09 +00:00
|
|
|
NS_ENSURE_SUCCESS_VOID(rv);
|
|
|
|
|
2015-02-19 01:34:29 +00:00
|
|
|
DoNotifyController();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
InterceptedChannelChrome::GetChannel(nsIChannel** aChannel)
|
|
|
|
{
|
|
|
|
NS_IF_ADDREF(*aChannel = mChannel);
|
|
|
|
return NS_OK;
|
2014-10-17 13:55:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
InterceptedChannelChrome::ResetInterception()
|
|
|
|
{
|
|
|
|
if (!mChannel) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
mSynthesizedCacheEntry->AsyncDoom(nullptr);
|
|
|
|
mSynthesizedCacheEntry = nullptr;
|
|
|
|
|
2015-03-17 15:48:32 +00:00
|
|
|
mChannel->SetApplyConversion(mOldApplyConversion);
|
|
|
|
|
2014-10-17 13:55:09 +00:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
mChannel->GetURI(getter_AddRefs(uri));
|
|
|
|
|
|
|
|
nsresult rv = mChannel->StartRedirectChannelToURI(uri, nsIChannelEventSink::REDIRECT_INTERNAL);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
mChannel = nullptr;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-03-17 15:48:30 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
InterceptedChannelChrome::SynthesizeStatus(uint16_t aStatus, const nsACString& aReason)
|
|
|
|
{
|
|
|
|
if (!mSynthesizedCacheEntry) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return DoSynthesizeStatus(aStatus, aReason);
|
|
|
|
}
|
|
|
|
|
2014-10-17 13:55:09 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
InterceptedChannelChrome::SynthesizeHeader(const nsACString& aName, const nsACString& aValue)
|
|
|
|
{
|
|
|
|
if (!mSynthesizedCacheEntry) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return DoSynthesizeHeader(aName, aValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
InterceptedChannelChrome::FinishSynthesizedResponse()
|
|
|
|
{
|
|
|
|
if (!mChannel) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
EnsureSynthesizedResponse();
|
|
|
|
|
2015-06-12 14:20:21 +00:00
|
|
|
// If the synthesized response is a redirect, then we want to respect
|
|
|
|
// the encoding of whatever is loaded as a result.
|
|
|
|
if (WillRedirect(mSynthesizedResponseHead.ref())) {
|
|
|
|
nsresult rv = mChannel->SetApplyConversion(mOldApplyConversion);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
2014-10-17 13:55:09 +00:00
|
|
|
mChannel->MarkIntercepted();
|
|
|
|
|
|
|
|
// First we ensure the appropriate metadata is set on the synthesized cache entry
|
|
|
|
// (i.e. the flattened response head)
|
|
|
|
|
|
|
|
nsCOMPtr<nsISupports> securityInfo;
|
|
|
|
nsresult rv = mChannel->GetSecurityInfo(getter_AddRefs(securityInfo));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = DoAddCacheEntryHeaders(mChannel, mSynthesizedCacheEntry,
|
|
|
|
mChannel->GetRequestHead(),
|
2015-02-19 01:34:29 +00:00
|
|
|
mSynthesizedResponseHead.ref(), securityInfo);
|
2014-10-17 13:55:09 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
mChannel->GetURI(getter_AddRefs(uri));
|
|
|
|
|
|
|
|
bool usingSSL = false;
|
|
|
|
uri->SchemeIs("https", &usingSSL);
|
|
|
|
|
|
|
|
// Then we open a real cache entry to read the synthesized response from.
|
|
|
|
rv = mChannel->OpenCacheEntry(usingSSL);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
mSynthesizedCacheEntry = nullptr;
|
|
|
|
|
|
|
|
if (!mChannel->AwaitingCacheCallbacks()) {
|
|
|
|
rv = mChannel->ContinueConnect();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
mChannel = nullptr;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-02-19 01:34:29 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
InterceptedChannelChrome::Cancel()
|
|
|
|
{
|
|
|
|
if (!mChannel) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// we need to use AsyncAbort instead of Cancel since there's no active pump
|
|
|
|
// to cancel which will provide OnStart/OnStopRequest to the channel.
|
|
|
|
nsresult rv = mChannel->AsyncAbort(NS_BINDING_ABORTED);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-03-04 23:27:33 +00:00
|
|
|
NS_IMETHODIMP
|
2015-05-25 18:21:05 +00:00
|
|
|
InterceptedChannelChrome::SetChannelInfo(dom::ChannelInfo* aChannelInfo)
|
2015-03-04 23:27:33 +00:00
|
|
|
{
|
2015-03-22 19:42:12 +00:00
|
|
|
if (!mChannel) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2015-05-25 18:21:05 +00:00
|
|
|
return aChannelInfo->ResurrectInfoOnChannel(mChannel);
|
2015-03-04 23:27:33 +00:00
|
|
|
}
|
|
|
|
|
2014-10-17 13:55:09 +00:00
|
|
|
InterceptedChannelContent::InterceptedChannelContent(HttpChannelChild* aChannel,
|
|
|
|
nsINetworkInterceptController* aController,
|
|
|
|
nsIStreamListener* aListener)
|
2015-02-19 01:34:29 +00:00
|
|
|
: InterceptedChannelBase(aController, aChannel->IsNavigation())
|
2014-10-17 13:55:09 +00:00
|
|
|
, mChannel(aChannel)
|
|
|
|
, mStreamListener(aListener)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
InterceptedChannelContent::NotifyController()
|
|
|
|
{
|
|
|
|
nsresult rv = NS_NewPipe(getter_AddRefs(mSynthesizedInput),
|
2015-02-19 01:34:29 +00:00
|
|
|
getter_AddRefs(mResponseBody),
|
2014-10-17 13:55:09 +00:00
|
|
|
0, UINT32_MAX, true, true);
|
|
|
|
NS_ENSURE_SUCCESS_VOID(rv);
|
|
|
|
|
2015-02-19 01:34:29 +00:00
|
|
|
DoNotifyController();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
InterceptedChannelContent::GetChannel(nsIChannel** aChannel)
|
|
|
|
{
|
|
|
|
NS_IF_ADDREF(*aChannel = mChannel);
|
|
|
|
return NS_OK;
|
2014-10-17 13:55:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
InterceptedChannelContent::ResetInterception()
|
|
|
|
{
|
|
|
|
if (!mChannel) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
2015-02-19 01:34:29 +00:00
|
|
|
mResponseBody = nullptr;
|
2014-10-17 13:55:09 +00:00
|
|
|
mSynthesizedInput = nullptr;
|
|
|
|
|
|
|
|
mChannel->ResetInterception();
|
|
|
|
mChannel = nullptr;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-03-17 15:48:30 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
InterceptedChannelContent::SynthesizeStatus(uint16_t aStatus, const nsACString& aReason)
|
|
|
|
{
|
|
|
|
if (!mResponseBody) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return DoSynthesizeStatus(aStatus, aReason);
|
|
|
|
}
|
|
|
|
|
2014-10-17 13:55:09 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
InterceptedChannelContent::SynthesizeHeader(const nsACString& aName, const nsACString& aValue)
|
|
|
|
{
|
2015-02-19 01:34:29 +00:00
|
|
|
if (!mResponseBody) {
|
2014-10-17 13:55:09 +00:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return DoSynthesizeHeader(aName, aValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
InterceptedChannelContent::FinishSynthesizedResponse()
|
|
|
|
{
|
2015-02-19 01:34:29 +00:00
|
|
|
if (NS_WARN_IF(!mChannel)) {
|
2014-10-17 13:55:09 +00:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
EnsureSynthesizedResponse();
|
|
|
|
|
2015-04-17 22:59:10 +00:00
|
|
|
mChannel->OverrideWithSynthesizedResponse(mSynthesizedResponseHead.ref(),
|
|
|
|
mSynthesizedInput,
|
|
|
|
mStreamListener);
|
2014-10-17 13:55:09 +00:00
|
|
|
|
2015-02-19 01:34:29 +00:00
|
|
|
mResponseBody = nullptr;
|
2014-10-17 13:55:09 +00:00
|
|
|
mChannel = nullptr;
|
2015-02-19 01:34:29 +00:00
|
|
|
mStreamListener = nullptr;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
InterceptedChannelContent::Cancel()
|
|
|
|
{
|
|
|
|
if (!mChannel) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// we need to use AsyncAbort instead of Cancel since there's no active pump
|
|
|
|
// to cancel which will provide OnStart/OnStopRequest to the channel.
|
|
|
|
nsresult rv = mChannel->AsyncAbort(NS_BINDING_ABORTED);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
mChannel = nullptr;
|
|
|
|
mStreamListener = nullptr;
|
2014-10-17 13:55:09 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-03-04 23:27:33 +00:00
|
|
|
NS_IMETHODIMP
|
2015-05-25 18:21:05 +00:00
|
|
|
InterceptedChannelContent::SetChannelInfo(dom::ChannelInfo* aChannelInfo)
|
2015-03-04 23:27:33 +00:00
|
|
|
{
|
2015-03-22 19:42:12 +00:00
|
|
|
if (!mChannel) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2015-05-25 18:21:05 +00:00
|
|
|
return aChannelInfo->ResurrectInfoOnChannel(mChannel);
|
2015-03-04 23:27:33 +00:00
|
|
|
}
|
|
|
|
|
2014-10-17 13:55:09 +00:00
|
|
|
} // namespace net
|
|
|
|
} // namespace mozilla
|