2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/* vim:set ts=4 sts=4 sw=4 et cin: */
|
2012-05-21 04:12:37 -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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "nsFTPChannel.h"
|
|
|
|
#include "nsFtpConnectionThread.h" // defines nsFtpState
|
|
|
|
|
|
|
|
#include "nsThreadUtils.h"
|
2012-06-05 20:18:25 -07:00
|
|
|
#include "mozilla/Attributes.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#if defined(PR_LOGGING)
|
|
|
|
extern PRLogModuleInfo* gFTPLog;
|
|
|
|
#endif /* PR_LOGGING */
|
|
|
|
|
|
|
|
// There are two transport connections established for an
|
|
|
|
// ftp connection. One is used for the command channel , and
|
|
|
|
// the other for the data channel. The command channel is the first
|
|
|
|
// connection made and is used to negotiate the second, data, channel.
|
|
|
|
// The data channel is driven by the command channel and is either
|
|
|
|
// initiated by the server (PORT command) or by the client (PASV command).
|
|
|
|
// Client initiation is the most common case and is attempted first.
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2012-10-05 12:53:07 -07:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED4(nsFtpChannel,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsBaseChannel,
|
|
|
|
nsIUploadChannel,
|
|
|
|
nsIResumableChannel,
|
|
|
|
nsIFTPChannel,
|
2012-10-05 12:53:07 -07:00
|
|
|
nsIProxiedChannel)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsFtpChannel::SetUploadStream(nsIInputStream *stream,
|
|
|
|
const nsACString &contentType,
|
2012-09-19 01:09:08 -07:00
|
|
|
int64_t contentLength)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(!IsPending(), NS_ERROR_IN_PROGRESS);
|
|
|
|
|
|
|
|
mUploadStream = stream;
|
|
|
|
|
|
|
|
// NOTE: contentLength is intentionally ignored here.
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsFtpChannel::GetUploadStream(nsIInputStream **stream)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(stream);
|
|
|
|
*stream = mUploadStream;
|
|
|
|
NS_IF_ADDREF(*stream);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsFtpChannel::ResumeAt(uint64_t aStartPos, const nsACString& aEntityID)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(!IsPending(), NS_ERROR_IN_PROGRESS);
|
|
|
|
mEntityID = aEntityID;
|
|
|
|
mStartPos = aStartPos;
|
|
|
|
mResumeRequested = (mStartPos || !mEntityID.IsEmpty());
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsFtpChannel::GetEntityID(nsACString& entityID)
|
|
|
|
{
|
|
|
|
if (mEntityID.IsEmpty())
|
|
|
|
return NS_ERROR_NOT_RESUMABLE;
|
|
|
|
|
|
|
|
entityID = mEntityID;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsFtpChannel::GetProxyInfo(nsIProxyInfo** aProxyInfo)
|
|
|
|
{
|
|
|
|
*aProxyInfo = ProxyInfo();
|
|
|
|
NS_IF_ADDREF(*aProxyInfo);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
nsresult
|
2011-09-28 23:19:26 -07:00
|
|
|
nsFtpChannel::OpenContentStream(bool async, nsIInputStream **result,
|
2008-09-29 14:02:44 -07:00
|
|
|
nsIChannel** channel)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (!async)
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
|
|
|
|
nsFtpState *state = new nsFtpState();
|
|
|
|
if (!state)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
NS_ADDREF(state);
|
|
|
|
|
|
|
|
nsresult rv = state->Init(this);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_RELEASE(state);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
*result = state;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2007-03-22 10:30:00 -07:00
|
|
|
nsFtpChannel::GetStatusArg(nsresult status, nsString &statusArg)
|
|
|
|
{
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString host;
|
2007-03-22 10:30:00 -07:00
|
|
|
URI()->GetHost(host);
|
|
|
|
CopyUTF8toUTF16(host, statusArg);
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsFtpChannel::OnCallbacksChanged()
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
mFTPEventSink = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2011-08-19 08:47:43 -07:00
|
|
|
namespace {
|
|
|
|
|
2012-06-05 20:18:25 -07:00
|
|
|
class FTPEventSinkProxy MOZ_FINAL : public nsIFTPEventSink
|
2011-08-19 08:47:43 -07:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
FTPEventSinkProxy(nsIFTPEventSink* aTarget)
|
|
|
|
: mTarget(aTarget)
|
|
|
|
, mTargetThread(do_GetCurrentThread())
|
|
|
|
{ }
|
|
|
|
|
2013-07-18 19:24:13 -07:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2011-08-19 08:47:43 -07:00
|
|
|
NS_DECL_NSIFTPEVENTSINK
|
|
|
|
|
|
|
|
class OnFTPControlLogRunnable : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
OnFTPControlLogRunnable(nsIFTPEventSink* aTarget,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aServer,
|
2011-08-19 08:47:43 -07:00
|
|
|
const char* aMessage)
|
|
|
|
: mTarget(aTarget)
|
|
|
|
, mServer(aServer)
|
|
|
|
, mMessage(aMessage)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
NS_DECL_NSIRUNNABLE
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsCOMPtr<nsIFTPEventSink> mTarget;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool mServer;
|
2011-08-19 08:47:43 -07:00
|
|
|
nsCString mMessage;
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsCOMPtr<nsIFTPEventSink> mTarget;
|
|
|
|
nsCOMPtr<nsIThread> mTargetThread;
|
|
|
|
};
|
|
|
|
|
2013-07-18 19:24:13 -07:00
|
|
|
NS_IMPL_ISUPPORTS1(FTPEventSinkProxy, nsIFTPEventSink)
|
2011-08-19 08:47:43 -07:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
FTPEventSinkProxy::OnFTPControlLog(bool aServer, const char* aMsg)
|
2011-08-19 08:47:43 -07:00
|
|
|
{
|
|
|
|
nsRefPtr<OnFTPControlLogRunnable> r =
|
|
|
|
new OnFTPControlLogRunnable(mTarget, aServer, aMsg);
|
|
|
|
return mTargetThread->Dispatch(r, NS_DISPATCH_NORMAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
FTPEventSinkProxy::OnFTPControlLogRunnable::Run()
|
|
|
|
{
|
|
|
|
mTarget->OnFTPControlLog(mServer, mMessage.get());
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
void
|
|
|
|
nsFtpChannel::GetFTPEventSink(nsCOMPtr<nsIFTPEventSink> &aResult)
|
|
|
|
{
|
|
|
|
if (!mFTPEventSink) {
|
|
|
|
nsCOMPtr<nsIFTPEventSink> ftpSink;
|
|
|
|
GetCallback(ftpSink);
|
|
|
|
if (ftpSink) {
|
2011-08-19 08:47:43 -07:00
|
|
|
mFTPEventSink = new FTPEventSinkProxy(ftpSink);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
aResult = mFTPEventSink;
|
|
|
|
}
|