2005-11-12 18:17:19 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-05-21 11:12:37 +00: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/. */
|
2005-11-12 18:17:19 +00:00
|
|
|
|
|
|
|
#include "nsBaseChannel.h"
|
|
|
|
#include "nsChannelProperties.h"
|
|
|
|
#include "nsURLHelper.h"
|
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsMimeTypes.h"
|
2006-02-22 14:07:21 +00:00
|
|
|
#include "nsIOService.h"
|
2005-11-12 18:17:19 +00:00
|
|
|
#include "nsIHttpEventSink.h"
|
|
|
|
#include "nsIHttpChannel.h"
|
|
|
|
#include "nsIChannelEventSink.h"
|
|
|
|
#include "nsIStreamConverterService.h"
|
2006-02-17 16:54:27 +00:00
|
|
|
#include "nsIContentSniffer.h"
|
2010-05-21 21:03:02 +00:00
|
|
|
#include "nsChannelClassifier.h"
|
2010-07-28 18:33:06 +00:00
|
|
|
#include "nsAsyncRedirectVerifyHelper.h"
|
2005-11-12 18:17:19 +00:00
|
|
|
|
2008-10-10 15:04:34 +00:00
|
|
|
static PLDHashOperator
|
2005-11-12 18:17:19 +00:00
|
|
|
CopyProperties(const nsAString &key, nsIVariant *data, void *closure)
|
|
|
|
{
|
|
|
|
nsIWritablePropertyBag *bag =
|
2007-07-08 07:08:04 +00:00
|
|
|
static_cast<nsIWritablePropertyBag *>(closure);
|
2005-11-12 18:17:19 +00:00
|
|
|
|
|
|
|
bag->SetProperty(key, data);
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This class is used to suspend a request across a function scope.
|
|
|
|
class ScopedRequestSuspender {
|
|
|
|
public:
|
|
|
|
ScopedRequestSuspender(nsIRequest *request)
|
|
|
|
: mRequest(request) {
|
2008-09-29 21:02:44 +00:00
|
|
|
if (mRequest && NS_FAILED(mRequest->Suspend())) {
|
2006-07-26 00:15:42 +00:00
|
|
|
NS_WARNING("Couldn't suspend pump");
|
2012-07-30 14:20:58 +00:00
|
|
|
mRequest = nullptr;
|
2006-07-26 00:15:42 +00:00
|
|
|
}
|
2005-11-12 18:17:19 +00:00
|
|
|
}
|
|
|
|
~ScopedRequestSuspender() {
|
2006-07-26 00:15:42 +00:00
|
|
|
if (mRequest)
|
|
|
|
mRequest->Resume();
|
2005-11-12 18:17:19 +00:00
|
|
|
}
|
|
|
|
private:
|
|
|
|
nsIRequest *mRequest;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Used to suspend data events from mPump within a function scope. This is
|
|
|
|
// usually needed when a function makes callbacks that could process events.
|
|
|
|
#define SUSPEND_PUMP_FOR_SCOPE() \
|
|
|
|
ScopedRequestSuspender pump_suspender__(mPump)
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsBaseChannel
|
|
|
|
|
|
|
|
nsBaseChannel::nsBaseChannel()
|
2010-08-30 20:20:39 +00:00
|
|
|
: mLoadFlags(LOAD_NORMAL)
|
2011-10-17 14:59:28 +00:00
|
|
|
, mQueriedProgressSink(true)
|
|
|
|
, mSynthProgressEvents(false)
|
|
|
|
, mWasOpened(false)
|
|
|
|
, mWaitingOnAsyncRedirect(false)
|
2010-11-19 23:53:30 +00:00
|
|
|
, mStatus(NS_OK)
|
2005-11-12 18:17:19 +00:00
|
|
|
{
|
|
|
|
mContentType.AssignLiteral(UNKNOWN_CONTENT_TYPE);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-08-22 15:56:38 +00:00
|
|
|
nsBaseChannel::Redirect(nsIChannel *newChannel, uint32_t redirectFlags,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool openNewChannel)
|
2005-11-12 18:17:19 +00:00
|
|
|
{
|
|
|
|
SUSPEND_PUMP_FOR_SCOPE();
|
|
|
|
|
|
|
|
// Transfer properties
|
|
|
|
|
|
|
|
newChannel->SetLoadGroup(mLoadGroup);
|
|
|
|
newChannel->SetNotificationCallbacks(mCallbacks);
|
|
|
|
newChannel->SetLoadFlags(mLoadFlags | LOAD_REPLACE);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIWritablePropertyBag> bag = ::do_QueryInterface(newChannel);
|
|
|
|
if (bag)
|
|
|
|
mPropertyHash.EnumerateRead(CopyProperties, bag.get());
|
|
|
|
|
|
|
|
// Notify consumer, giving chance to cancel redirect. For backwards compat,
|
|
|
|
// we support nsIHttpEventSink if we are an HTTP channel and if this is not
|
|
|
|
// an internal redirect.
|
|
|
|
|
2010-07-28 18:33:06 +00:00
|
|
|
nsRefPtr<nsAsyncRedirectVerifyHelper> redirectCallbackHelper =
|
|
|
|
new nsAsyncRedirectVerifyHelper();
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool checkRedirectSynchronously = !openNewChannel;
|
2010-07-28 18:33:06 +00:00
|
|
|
|
|
|
|
mRedirectChannel = newChannel;
|
|
|
|
mRedirectFlags = redirectFlags;
|
|
|
|
mOpenRedirectChannel = openNewChannel;
|
|
|
|
nsresult rv = redirectCallbackHelper->Init(this, newChannel, redirectFlags,
|
|
|
|
checkRedirectSynchronously);
|
2006-02-22 14:07:21 +00:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
2005-11-12 18:17:19 +00:00
|
|
|
|
2010-07-28 18:33:06 +00:00
|
|
|
if (checkRedirectSynchronously && NS_FAILED(mStatus))
|
|
|
|
return mStatus;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsBaseChannel::ContinueRedirect()
|
|
|
|
{
|
2005-11-12 18:17:19 +00:00
|
|
|
// Backwards compat for non-internal redirects from a HTTP channel.
|
2010-07-28 18:33:06 +00:00
|
|
|
// XXX Is our http channel implementation going to derive from nsBaseChannel?
|
|
|
|
// If not, this code can be removed.
|
|
|
|
if (!(mRedirectFlags & nsIChannelEventSink::REDIRECT_INTERNAL)) {
|
2005-11-12 18:17:19 +00:00
|
|
|
nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface();
|
|
|
|
if (httpChannel) {
|
|
|
|
nsCOMPtr<nsIHttpEventSink> httpEventSink;
|
|
|
|
GetCallback(httpEventSink);
|
|
|
|
if (httpEventSink) {
|
2010-07-28 18:33:06 +00:00
|
|
|
nsresult rv = httpEventSink->OnRedirect(httpChannel, mRedirectChannel);
|
|
|
|
if (NS_FAILED(rv)) {
|
2005-11-12 18:17:19 +00:00
|
|
|
return rv;
|
2010-07-28 18:33:06 +00:00
|
|
|
}
|
2005-11-12 18:17:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-28 18:33:06 +00:00
|
|
|
// Make sure to do this _after_ making all the OnChannelRedirect calls
|
|
|
|
mRedirectChannel->SetOriginalURI(OriginalURI());
|
2008-11-26 01:50:04 +00:00
|
|
|
|
2005-11-12 18:17:19 +00:00
|
|
|
// If we fail to open the new channel, then we want to leave this channel
|
|
|
|
// unaffected, so we defer tearing down our channel until we have succeeded
|
|
|
|
// with the redirect.
|
|
|
|
|
2010-07-28 18:33:06 +00:00
|
|
|
if (mOpenRedirectChannel) {
|
|
|
|
nsresult rv = mRedirectChannel->AsyncOpen(mListener, mListenerContext);
|
2008-09-29 21:02:44 +00:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
}
|
2005-11-12 18:17:19 +00:00
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
mRedirectChannel = nullptr;
|
2010-07-28 18:33:06 +00:00
|
|
|
|
2005-11-12 18:17:19 +00:00
|
|
|
// close down this channel
|
|
|
|
Cancel(NS_BINDING_REDIRECTED);
|
2012-07-30 14:20:58 +00:00
|
|
|
mListener = nullptr;
|
|
|
|
mListenerContext = nullptr;
|
2005-11-12 18:17:19 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool
|
2006-02-14 18:46:12 +00:00
|
|
|
nsBaseChannel::HasContentTypeHint() const
|
|
|
|
{
|
|
|
|
NS_ASSERTION(!IsPending(), "HasContentTypeHint called too late");
|
|
|
|
return !mContentType.EqualsLiteral(UNKNOWN_CONTENT_TYPE);
|
|
|
|
}
|
|
|
|
|
2010-08-30 20:20:39 +00:00
|
|
|
void
|
2012-08-22 15:56:38 +00:00
|
|
|
nsBaseChannel::SetContentLength64(int64_t len)
|
2010-08-30 20:20:39 +00:00
|
|
|
{
|
|
|
|
// XXX: Storing the content-length as a property may not be what we want.
|
|
|
|
// It has the drawback of being copied if we redirect this channel.
|
|
|
|
// Maybe it is time for nsIChannel2.
|
|
|
|
SetPropertyAsInt64(NS_CHANNEL_PROP_CONTENT_LENGTH, len);
|
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t
|
2010-08-30 20:20:39 +00:00
|
|
|
nsBaseChannel::ContentLength64()
|
|
|
|
{
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t len;
|
2010-08-30 20:20:39 +00:00
|
|
|
nsresult rv = GetPropertyAsInt64(NS_CHANNEL_PROP_CONTENT_LENGTH, &len);
|
|
|
|
return NS_SUCCEEDED(rv) ? len : -1;
|
|
|
|
}
|
|
|
|
|
2005-11-12 18:17:19 +00:00
|
|
|
nsresult
|
|
|
|
nsBaseChannel::PushStreamConverter(const char *fromType,
|
|
|
|
const char *toType,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool invalidatesContentLength,
|
2005-11-12 18:17:19 +00:00
|
|
|
nsIStreamListener **result)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mListener, "no listener");
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIStreamConverterService> scs =
|
|
|
|
do_GetService(NS_STREAMCONVERTERSERVICE_CONTRACTID, &rv);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIStreamListener> converter;
|
|
|
|
rv = scs->AsyncConvertData(fromType, toType, mListener, mListenerContext,
|
|
|
|
getter_AddRefs(converter));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
mListener = converter;
|
|
|
|
if (invalidatesContentLength)
|
2010-08-30 20:20:39 +00:00
|
|
|
SetContentLength64(-1);
|
2005-11-12 18:17:19 +00:00
|
|
|
if (result) {
|
2012-07-30 14:20:58 +00:00
|
|
|
*result = nullptr;
|
2005-11-12 18:17:19 +00:00
|
|
|
converter.swap(*result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2006-03-03 16:27:35 +00:00
|
|
|
nsresult
|
|
|
|
nsBaseChannel::BeginPumpingData()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIInputStream> stream;
|
2008-09-29 21:02:44 +00:00
|
|
|
nsCOMPtr<nsIChannel> channel;
|
2011-10-17 14:59:28 +00:00
|
|
|
nsresult rv = OpenContentStream(true, getter_AddRefs(stream),
|
2008-09-29 21:02:44 +00:00
|
|
|
getter_AddRefs(channel));
|
2006-03-03 16:27:35 +00:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2008-09-29 21:02:44 +00:00
|
|
|
NS_ASSERTION(!stream || !channel, "Got both a channel and a stream?");
|
|
|
|
|
2008-10-22 15:42:32 +00:00
|
|
|
if (channel) {
|
|
|
|
rv = NS_DispatchToCurrentThread(new RedirectRunnable(this, channel));
|
|
|
|
if (NS_SUCCEEDED(rv))
|
2011-10-17 14:59:28 +00:00
|
|
|
mWaitingOnAsyncRedirect = true;
|
2008-10-22 15:42:32 +00:00
|
|
|
return rv;
|
|
|
|
}
|
2008-09-29 21:02:44 +00:00
|
|
|
|
2006-03-03 16:27:35 +00:00
|
|
|
// By assigning mPump, we flag this channel as pending (see IsPending). It's
|
|
|
|
// important that the pending flag is set when we call into the stream (the
|
|
|
|
// call to AsyncRead results in the stream's AsyncWait method being called)
|
|
|
|
// and especially when we call into the loadgroup. Our caller takes care to
|
|
|
|
// release mPump if we return an error.
|
|
|
|
|
2006-03-03 21:06:53 +00:00
|
|
|
rv = nsInputStreamPump::Create(getter_AddRefs(mPump), stream, -1, -1, 0, 0,
|
2011-10-17 14:59:28 +00:00
|
|
|
true);
|
2006-03-03 16:27:35 +00:00
|
|
|
if (NS_SUCCEEDED(rv))
|
2012-07-30 14:20:58 +00:00
|
|
|
rv = mPump->AsyncRead(this, nullptr);
|
2006-03-03 16:27:35 +00:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2008-09-29 21:02:44 +00:00
|
|
|
void
|
|
|
|
nsBaseChannel::HandleAsyncRedirect(nsIChannel* newChannel)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(!mPump, "Shouldn't have gotten here");
|
2010-07-28 18:33:06 +00:00
|
|
|
|
|
|
|
nsresult rv = mStatus;
|
2008-10-22 15:42:32 +00:00
|
|
|
if (NS_SUCCEEDED(mStatus)) {
|
2010-07-28 18:33:06 +00:00
|
|
|
rv = Redirect(newChannel,
|
|
|
|
nsIChannelEventSink::REDIRECT_TEMPORARY,
|
2011-10-17 14:59:28 +00:00
|
|
|
true);
|
2010-07-28 18:33:06 +00:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
// OnRedirectVerifyCallback will be called asynchronously
|
|
|
|
return;
|
|
|
|
}
|
2008-10-22 15:42:32 +00:00
|
|
|
}
|
|
|
|
|
2010-07-28 18:33:06 +00:00
|
|
|
ContinueHandleAsyncRedirect(rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsBaseChannel::ContinueHandleAsyncRedirect(nsresult result)
|
|
|
|
{
|
2011-10-17 14:59:28 +00:00
|
|
|
mWaitingOnAsyncRedirect = false;
|
2008-10-22 15:42:32 +00:00
|
|
|
|
2010-07-28 18:33:06 +00:00
|
|
|
if (NS_FAILED(result))
|
|
|
|
Cancel(result);
|
|
|
|
|
|
|
|
if (NS_FAILED(result) && mListener) {
|
2008-09-29 21:02:44 +00:00
|
|
|
// Notify our consumer ourselves
|
|
|
|
mListener->OnStartRequest(this, mListenerContext);
|
|
|
|
mListener->OnStopRequest(this, mListenerContext, mStatus);
|
2012-07-30 14:20:58 +00:00
|
|
|
mListener = nullptr;
|
|
|
|
mListenerContext = nullptr;
|
2008-09-29 21:02:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mLoadGroup)
|
2012-07-30 14:20:58 +00:00
|
|
|
mLoadGroup->RemoveRequest(this, nullptr, mStatus);
|
2008-09-29 21:02:44 +00:00
|
|
|
|
|
|
|
// Drop notification callbacks to prevent cycles.
|
2012-07-30 14:20:58 +00:00
|
|
|
mCallbacks = nullptr;
|
2008-09-29 21:02:44 +00:00
|
|
|
CallbacksChanged();
|
|
|
|
}
|
|
|
|
|
2010-05-21 21:03:02 +00:00
|
|
|
void
|
|
|
|
nsBaseChannel::ClassifyURI()
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
if (mLoadFlags & LOAD_CLASSIFY_URI) {
|
|
|
|
nsRefPtr<nsChannelClassifier> classifier = new nsChannelClassifier();
|
|
|
|
if (classifier) {
|
|
|
|
rv = classifier->Start(this);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
Cancel(rv);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Cancel(NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-21 20:39:20 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsBaseChannel::nsISupports
|
|
|
|
|
2010-07-28 18:33:06 +00:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED7(nsBaseChannel,
|
2008-02-21 20:39:20 +00:00
|
|
|
nsHashPropertyBag,
|
|
|
|
nsIRequest,
|
|
|
|
nsIChannel,
|
|
|
|
nsIInterfaceRequestor,
|
|
|
|
nsITransportEventSink,
|
|
|
|
nsIRequestObserver,
|
2010-07-28 18:33:06 +00:00
|
|
|
nsIStreamListener,
|
|
|
|
nsIAsyncVerifyRedirectCallback)
|
2008-02-21 20:39:20 +00:00
|
|
|
|
2005-11-12 18:17:19 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsBaseChannel::nsIRequest
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBaseChannel::GetName(nsACString &result)
|
|
|
|
{
|
|
|
|
if (!mURI) {
|
|
|
|
result.Truncate();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
return mURI->GetSpec(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 06:19:26 +00:00
|
|
|
nsBaseChannel::IsPending(bool *result)
|
2005-11-12 18:17:19 +00:00
|
|
|
{
|
|
|
|
*result = IsPending();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBaseChannel::GetStatus(nsresult *status)
|
|
|
|
{
|
|
|
|
if (mPump && NS_SUCCEEDED(mStatus)) {
|
|
|
|
mPump->GetStatus(status);
|
|
|
|
} else {
|
|
|
|
*status = mStatus;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBaseChannel::Cancel(nsresult status)
|
|
|
|
{
|
2006-03-09 20:02:54 +00:00
|
|
|
// Ignore redundant cancelation
|
|
|
|
if (NS_FAILED(mStatus))
|
|
|
|
return NS_OK;
|
|
|
|
|
2005-11-12 18:17:19 +00:00
|
|
|
mStatus = status;
|
|
|
|
|
|
|
|
if (mPump)
|
|
|
|
mPump->Cancel(status);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBaseChannel::Suspend()
|
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(mPump, NS_ERROR_NOT_INITIALIZED);
|
|
|
|
return mPump->Suspend();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBaseChannel::Resume()
|
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(mPump, NS_ERROR_NOT_INITIALIZED);
|
|
|
|
return mPump->Resume();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBaseChannel::GetLoadFlags(nsLoadFlags *aLoadFlags)
|
|
|
|
{
|
|
|
|
*aLoadFlags = mLoadFlags;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBaseChannel::SetLoadFlags(nsLoadFlags aLoadFlags)
|
|
|
|
{
|
|
|
|
mLoadFlags = aLoadFlags;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBaseChannel::GetLoadGroup(nsILoadGroup **aLoadGroup)
|
|
|
|
{
|
|
|
|
NS_IF_ADDREF(*aLoadGroup = mLoadGroup);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBaseChannel::SetLoadGroup(nsILoadGroup *aLoadGroup)
|
|
|
|
{
|
|
|
|
mLoadGroup = aLoadGroup;
|
|
|
|
CallbacksChanged();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsBaseChannel::nsIChannel
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBaseChannel::GetOriginalURI(nsIURI **aURI)
|
|
|
|
{
|
|
|
|
*aURI = OriginalURI();
|
2008-10-15 20:05:23 +00:00
|
|
|
NS_ADDREF(*aURI);
|
2005-11-12 18:17:19 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBaseChannel::SetOriginalURI(nsIURI *aURI)
|
|
|
|
{
|
2008-10-15 20:05:23 +00:00
|
|
|
NS_ENSURE_ARG_POINTER(aURI);
|
2005-11-12 18:17:19 +00:00
|
|
|
mOriginalURI = aURI;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBaseChannel::GetURI(nsIURI **aURI)
|
|
|
|
{
|
|
|
|
NS_IF_ADDREF(*aURI = mURI);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBaseChannel::GetOwner(nsISupports **aOwner)
|
|
|
|
{
|
|
|
|
NS_IF_ADDREF(*aOwner = mOwner);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBaseChannel::SetOwner(nsISupports *aOwner)
|
|
|
|
{
|
|
|
|
mOwner = aOwner;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBaseChannel::GetNotificationCallbacks(nsIInterfaceRequestor **aCallbacks)
|
|
|
|
{
|
|
|
|
NS_IF_ADDREF(*aCallbacks = mCallbacks);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBaseChannel::SetNotificationCallbacks(nsIInterfaceRequestor *aCallbacks)
|
|
|
|
{
|
|
|
|
mCallbacks = aCallbacks;
|
|
|
|
CallbacksChanged();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBaseChannel::GetSecurityInfo(nsISupports **aSecurityInfo)
|
|
|
|
{
|
|
|
|
NS_IF_ADDREF(*aSecurityInfo = mSecurityInfo);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBaseChannel::GetContentType(nsACString &aContentType)
|
|
|
|
{
|
|
|
|
aContentType = mContentType;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBaseChannel::SetContentType(const nsACString &aContentType)
|
|
|
|
{
|
|
|
|
// mContentCharset is unchanged if not parsed
|
2011-09-29 06:19:26 +00:00
|
|
|
bool dummy;
|
2005-11-12 18:17:19 +00:00
|
|
|
net_ParseContentType(aContentType, mContentType, mContentCharset, &dummy);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBaseChannel::GetContentCharset(nsACString &aContentCharset)
|
|
|
|
{
|
|
|
|
aContentCharset = mContentCharset;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBaseChannel::SetContentCharset(const nsACString &aContentCharset)
|
|
|
|
{
|
|
|
|
mContentCharset = aContentCharset;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-09 22:41:04 +00:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
nsBaseChannel::GetContentDisposition(uint32_t *aContentDisposition)
|
2011-09-09 22:41:04 +00:00
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBaseChannel::GetContentDispositionFilename(nsAString &aContentDispositionFilename)
|
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBaseChannel::GetContentDispositionHeader(nsACString &aContentDispositionHeader)
|
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
2005-11-12 18:17:19 +00:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
nsBaseChannel::GetContentLength(int32_t *aContentLength)
|
2005-11-12 18:17:19 +00:00
|
|
|
{
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t len = ContentLength64();
|
2010-08-30 20:20:39 +00:00
|
|
|
if (len > PR_INT32_MAX || len < 0)
|
|
|
|
*aContentLength = -1;
|
|
|
|
else
|
2012-08-22 15:56:38 +00:00
|
|
|
*aContentLength = (int32_t) len;
|
2005-11-12 18:17:19 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
nsBaseChannel::SetContentLength(int32_t aContentLength)
|
2005-11-12 18:17:19 +00:00
|
|
|
{
|
2010-08-30 20:20:39 +00:00
|
|
|
SetContentLength64(aContentLength);
|
2005-11-12 18:17:19 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBaseChannel::Open(nsIInputStream **result)
|
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(mURI, NS_ERROR_NOT_INITIALIZED);
|
|
|
|
NS_ENSURE_TRUE(!mPump, NS_ERROR_IN_PROGRESS);
|
2007-03-23 21:56:32 +00:00
|
|
|
NS_ENSURE_TRUE(!mWasOpened, NS_ERROR_IN_PROGRESS);
|
2005-11-12 18:17:19 +00:00
|
|
|
|
2008-09-29 21:02:44 +00:00
|
|
|
nsCOMPtr<nsIChannel> chan;
|
2011-10-17 14:59:28 +00:00
|
|
|
nsresult rv = OpenContentStream(false, result, getter_AddRefs(chan));
|
2008-09-29 21:02:44 +00:00
|
|
|
NS_ASSERTION(!chan || !*result, "Got both a channel and a stream?");
|
|
|
|
if (NS_SUCCEEDED(rv) && chan) {
|
2011-10-17 14:59:28 +00:00
|
|
|
rv = Redirect(chan, nsIChannelEventSink::REDIRECT_INTERNAL, false);
|
2008-09-29 21:02:44 +00:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
rv = chan->Open(result);
|
|
|
|
} else if (rv == NS_ERROR_NOT_IMPLEMENTED)
|
2005-11-12 18:17:19 +00:00
|
|
|
return NS_ImplementChannelOpen(this, result);
|
|
|
|
|
2010-05-21 21:03:02 +00:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2011-10-17 14:59:28 +00:00
|
|
|
mWasOpened = true;
|
2010-05-21 21:03:02 +00:00
|
|
|
ClassifyURI();
|
|
|
|
}
|
2007-03-23 21:56:32 +00:00
|
|
|
|
2005-11-12 18:17:19 +00:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBaseChannel::AsyncOpen(nsIStreamListener *listener, nsISupports *ctxt)
|
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(mURI, NS_ERROR_NOT_INITIALIZED);
|
|
|
|
NS_ENSURE_TRUE(!mPump, NS_ERROR_IN_PROGRESS);
|
2007-03-23 21:56:32 +00:00
|
|
|
NS_ENSURE_TRUE(!mWasOpened, NS_ERROR_ALREADY_OPENED);
|
2005-11-12 18:17:19 +00:00
|
|
|
NS_ENSURE_ARG(listener);
|
|
|
|
|
|
|
|
// Ensure that this is an allowed port before proceeding.
|
2006-06-24 01:25:17 +00:00
|
|
|
nsresult rv = NS_CheckPortSafety(mURI);
|
2008-01-28 23:21:27 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
2012-07-30 14:20:58 +00:00
|
|
|
mCallbacks = nullptr;
|
2005-11-12 18:17:19 +00:00
|
|
|
return rv;
|
2008-01-28 23:21:27 +00:00
|
|
|
}
|
2005-11-12 18:17:19 +00:00
|
|
|
|
2006-03-03 16:27:35 +00:00
|
|
|
// Store the listener and context early so that OpenContentStream and the
|
|
|
|
// stream's AsyncWait method (called by AsyncRead) can have access to them
|
|
|
|
// via PushStreamConverter and the StreamListener methods. However, since
|
|
|
|
// this typically introduces a reference cycle between this and the listener,
|
|
|
|
// we need to be sure to break the reference if this method does not succeed.
|
|
|
|
mListener = listener;
|
|
|
|
mListenerContext = ctxt;
|
2005-11-12 18:17:19 +00:00
|
|
|
|
2006-03-03 16:27:35 +00:00
|
|
|
// This method assigns mPump as a side-effect. We need to clear mPump if
|
|
|
|
// this method fails.
|
|
|
|
rv = BeginPumpingData();
|
2005-11-12 18:17:19 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
2012-07-30 14:20:58 +00:00
|
|
|
mPump = nullptr;
|
|
|
|
mListener = nullptr;
|
|
|
|
mListenerContext = nullptr;
|
|
|
|
mCallbacks = nullptr;
|
2005-11-12 18:17:19 +00:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2006-03-03 16:27:35 +00:00
|
|
|
// At this point, we are going to return success no matter what.
|
2005-11-12 18:17:19 +00:00
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
mWasOpened = true;
|
2007-03-23 21:56:32 +00:00
|
|
|
|
2005-11-12 18:17:19 +00:00
|
|
|
SUSPEND_PUMP_FOR_SCOPE();
|
|
|
|
|
|
|
|
if (mLoadGroup)
|
2012-07-30 14:20:58 +00:00
|
|
|
mLoadGroup->AddRequest(this, nullptr);
|
2005-11-12 18:17:19 +00:00
|
|
|
|
2010-05-21 21:03:02 +00:00
|
|
|
ClassifyURI();
|
|
|
|
|
2005-11-12 18:17:19 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsBaseChannel::nsITransportEventSink
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBaseChannel::OnTransportStatus(nsITransport *transport, nsresult status,
|
2012-08-22 15:56:38 +00:00
|
|
|
uint64_t progress, uint64_t progressMax)
|
2005-11-12 18:17:19 +00:00
|
|
|
{
|
|
|
|
// In some cases, we may wish to suppress transport-layer status events.
|
|
|
|
|
|
|
|
if (!mPump || NS_FAILED(mStatus) || HasLoadFlag(LOAD_BACKGROUND))
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
SUSPEND_PUMP_FOR_SCOPE();
|
|
|
|
|
|
|
|
// Lazily fetch mProgressSink
|
|
|
|
if (!mProgressSink) {
|
|
|
|
if (mQueriedProgressSink)
|
|
|
|
return NS_OK;
|
|
|
|
GetCallback(mProgressSink);
|
2011-10-17 14:59:28 +00:00
|
|
|
mQueriedProgressSink = true;
|
2005-11-12 18:17:19 +00:00
|
|
|
if (!mProgressSink)
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString statusArg;
|
|
|
|
if (GetStatusArg(status, statusArg))
|
|
|
|
mProgressSink->OnStatus(this, mListenerContext, status, statusArg.get());
|
|
|
|
|
|
|
|
if (progress)
|
|
|
|
mProgressSink->OnProgress(this, mListenerContext, progress, progressMax);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsBaseChannel::nsIInterfaceRequestor
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBaseChannel::GetInterface(const nsIID &iid, void **result)
|
|
|
|
{
|
|
|
|
NS_QueryNotificationCallbacks(mCallbacks, mLoadGroup, iid, result);
|
|
|
|
return *result ? NS_OK : NS_ERROR_NO_INTERFACE;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsBaseChannel::nsIRequestObserver
|
|
|
|
|
2006-02-17 16:54:27 +00:00
|
|
|
static void
|
2012-08-22 15:56:38 +00:00
|
|
|
CallTypeSniffers(void *aClosure, const uint8_t *aData, uint32_t aCount)
|
2006-02-17 16:54:27 +00:00
|
|
|
{
|
2007-07-08 07:08:04 +00:00
|
|
|
nsIChannel *chan = static_cast<nsIChannel*>(aClosure);
|
2006-02-17 16:54:27 +00:00
|
|
|
|
2006-03-03 21:06:53 +00:00
|
|
|
const nsCOMArray<nsIContentSniffer>& sniffers =
|
|
|
|
gIOService->GetContentSniffers();
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t length = sniffers.Count();
|
|
|
|
for (uint32_t i = 0; i < length; ++i) {
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString newType;
|
2006-03-03 21:06:53 +00:00
|
|
|
nsresult rv =
|
|
|
|
sniffers[i]->GetMIMETypeFromContent(chan, aData, aCount, newType);
|
|
|
|
if (NS_SUCCEEDED(rv) && !newType.IsEmpty()) {
|
|
|
|
chan->SetContentType(newType);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-08-22 15:56:38 +00:00
|
|
|
CallUnknownTypeSniffer(void *aClosure, const uint8_t *aData, uint32_t aCount)
|
2006-03-03 21:06:53 +00:00
|
|
|
{
|
2007-07-08 07:08:04 +00:00
|
|
|
nsIChannel *chan = static_cast<nsIChannel*>(aClosure);
|
2006-03-03 21:06:53 +00:00
|
|
|
|
2006-02-17 16:54:27 +00:00
|
|
|
nsCOMPtr<nsIContentSniffer> sniffer =
|
|
|
|
do_CreateInstance(NS_GENERIC_CONTENT_SNIFFER);
|
|
|
|
if (!sniffer)
|
|
|
|
return;
|
|
|
|
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString detected;
|
2006-02-17 16:54:27 +00:00
|
|
|
nsresult rv = sniffer->GetMIMETypeFromContent(chan, aData, aCount, detected);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
chan->SetContentType(detected);
|
|
|
|
}
|
|
|
|
|
2005-11-12 18:17:19 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBaseChannel::OnStartRequest(nsIRequest *request, nsISupports *ctxt)
|
|
|
|
{
|
2012-06-26 17:33:00 +00:00
|
|
|
// If our content type is unknown or if the content type is
|
|
|
|
// application/octet-stream and the caller requested it, use the content type
|
|
|
|
// sniffer. If the sniffer is not available for some reason, then we just keep
|
|
|
|
// going as-is.
|
|
|
|
bool shouldSniff = mContentType.EqualsLiteral(UNKNOWN_CONTENT_TYPE) ||
|
|
|
|
((mLoadFlags & LOAD_TREAT_APPLICATION_OCTET_STREAM_AS_UNKNOWN) &&
|
|
|
|
mContentType.EqualsLiteral(APPLICATION_OCTET_STREAM));
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(mStatus) && shouldSniff) {
|
2007-07-08 07:08:04 +00:00
|
|
|
mPump->PeekStream(CallUnknownTypeSniffer, static_cast<nsIChannel*>(this));
|
2006-02-17 16:54:27 +00:00
|
|
|
}
|
2005-11-12 18:17:19 +00:00
|
|
|
|
2006-03-03 21:06:53 +00:00
|
|
|
// Now, the general type sniffers. Skip this if we have none.
|
|
|
|
if ((mLoadFlags & LOAD_CALL_CONTENT_SNIFFERS) &&
|
|
|
|
gIOService->GetContentSniffers().Count() != 0)
|
2007-07-08 07:08:04 +00:00
|
|
|
mPump->PeekStream(CallTypeSniffers, static_cast<nsIChannel*>(this));
|
2006-03-03 21:06:53 +00:00
|
|
|
|
2006-03-03 18:39:40 +00:00
|
|
|
SUSPEND_PUMP_FOR_SCOPE();
|
2006-03-03 16:27:35 +00:00
|
|
|
|
2012-09-17 15:54:09 +00:00
|
|
|
return mListener->OnStartRequest(this, mListenerContext);
|
2005-11-12 18:17:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBaseChannel::OnStopRequest(nsIRequest *request, nsISupports *ctxt,
|
|
|
|
nsresult status)
|
|
|
|
{
|
|
|
|
// If both mStatus and status are failure codes, we keep mStatus as-is since
|
|
|
|
// that is consistent with our GetStatus and Cancel methods.
|
|
|
|
if (NS_SUCCEEDED(mStatus))
|
|
|
|
mStatus = status;
|
|
|
|
|
|
|
|
// Cause IsPending to return false.
|
2012-07-30 14:20:58 +00:00
|
|
|
mPump = nullptr;
|
2005-11-12 18:17:19 +00:00
|
|
|
|
2012-09-17 15:54:09 +00:00
|
|
|
mListener->OnStopRequest(this, mListenerContext, mStatus);
|
2012-07-30 14:20:58 +00:00
|
|
|
mListener = nullptr;
|
|
|
|
mListenerContext = nullptr;
|
2005-11-12 18:17:19 +00:00
|
|
|
|
|
|
|
// No need to suspend pump in this scope since we will not be receiving
|
|
|
|
// any more events from it.
|
|
|
|
|
|
|
|
if (mLoadGroup)
|
2012-07-30 14:20:58 +00:00
|
|
|
mLoadGroup->RemoveRequest(this, nullptr, mStatus);
|
2005-11-12 18:17:19 +00:00
|
|
|
|
|
|
|
// Drop notification callbacks to prevent cycles.
|
2012-07-30 14:20:58 +00:00
|
|
|
mCallbacks = nullptr;
|
2005-11-12 18:17:19 +00:00
|
|
|
CallbacksChanged();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsBaseChannel::nsIStreamListener
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBaseChannel::OnDataAvailable(nsIRequest *request, nsISupports *ctxt,
|
2012-09-06 02:41:02 +00:00
|
|
|
nsIInputStream *stream, uint64_t offset,
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t count)
|
2005-11-12 18:17:19 +00:00
|
|
|
{
|
2006-03-03 18:39:40 +00:00
|
|
|
SUSPEND_PUMP_FOR_SCOPE();
|
2006-03-03 16:27:35 +00:00
|
|
|
|
2005-11-12 18:17:19 +00:00
|
|
|
nsresult rv = mListener->OnDataAvailable(this, mListenerContext, stream,
|
|
|
|
offset, count);
|
|
|
|
if (mSynthProgressEvents && NS_SUCCEEDED(rv)) {
|
2012-09-06 02:41:02 +00:00
|
|
|
uint64_t prog = offset + count;
|
2012-08-22 15:56:38 +00:00
|
|
|
uint64_t progMax = ContentLength64();
|
2012-08-01 08:17:09 +00:00
|
|
|
OnTransportStatus(nullptr, NS_NET_STATUS_READING, prog, progMax);
|
2005-11-12 18:17:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
2010-07-28 18:33:06 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBaseChannel::OnRedirectVerifyCallback(nsresult result)
|
|
|
|
{
|
|
|
|
if (NS_SUCCEEDED(result))
|
|
|
|
result = ContinueRedirect();
|
|
|
|
|
|
|
|
if (NS_FAILED(result) && !mWaitingOnAsyncRedirect) {
|
|
|
|
if (NS_SUCCEEDED(mStatus))
|
|
|
|
mStatus = result;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mWaitingOnAsyncRedirect)
|
|
|
|
ContinueHandleAsyncRedirect(result);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|