1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=4 sts=2 sw=2 et cin: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "nsFTPChannel.h"
8 #include "nsFtpConnectionThread.h" // defines nsFtpState
10 #include "nsThreadUtils.h"
11 #include "mozilla/Attributes.h"
13 using namespace mozilla
;
14 using namespace mozilla::net
;
15 extern LazyLogModule gFTPLog
;
17 // There are two transport connections established for an
18 // ftp connection. One is used for the command channel , and
19 // the other for the data channel. The command channel is the first
20 // connection made and is used to negotiate the second, data, channel.
21 // The data channel is driven by the command channel and is either
22 // initiated by the server (PORT command) or by the client (PASV command).
23 // Client initiation is the most common case and is attempted first.
25 //-----------------------------------------------------------------------------
27 NS_IMPL_ISUPPORTS_INHERITED(nsFtpChannel
, nsBaseChannel
, nsIUploadChannel
,
28 nsIResumableChannel
, nsIFTPChannel
,
29 nsIProxiedChannel
, nsIForcePendingChannel
,
30 nsIChannelWithDivertableParentListener
)
32 //-----------------------------------------------------------------------------
35 nsFtpChannel::SetUploadStream(nsIInputStream
*stream
,
36 const nsACString
&contentType
,
37 int64_t contentLength
) {
38 NS_ENSURE_TRUE(!Pending(), NS_ERROR_IN_PROGRESS
);
40 mUploadStream
= stream
;
42 // NOTE: contentLength is intentionally ignored here.
48 nsFtpChannel::GetUploadStream(nsIInputStream
**stream
) {
49 NS_ENSURE_ARG_POINTER(stream
);
50 *stream
= mUploadStream
;
51 NS_IF_ADDREF(*stream
);
55 //-----------------------------------------------------------------------------
58 nsFtpChannel::ResumeAt(uint64_t aStartPos
, const nsACString
&aEntityID
) {
59 NS_ENSURE_TRUE(!Pending(), NS_ERROR_IN_PROGRESS
);
60 mEntityID
= aEntityID
;
61 mStartPos
= aStartPos
;
62 mResumeRequested
= (mStartPos
|| !mEntityID
.IsEmpty());
67 nsFtpChannel::GetEntityID(nsACString
&entityID
) {
68 if (mEntityID
.IsEmpty()) return NS_ERROR_NOT_RESUMABLE
;
74 //-----------------------------------------------------------------------------
76 nsFtpChannel::GetProxyInfo(nsIProxyInfo
**aProxyInfo
) {
77 *aProxyInfo
= ProxyInfo();
78 NS_IF_ADDREF(*aProxyInfo
);
82 //-----------------------------------------------------------------------------
84 nsresult
nsFtpChannel::OpenContentStream(bool async
, nsIInputStream
**result
,
85 nsIChannel
**channel
) {
86 if (!async
) return NS_ERROR_NOT_IMPLEMENTED
;
88 nsFtpState
*state
= new nsFtpState();
89 if (!state
) return NS_ERROR_OUT_OF_MEMORY
;
92 nsresult rv
= state
->Init(this);
102 bool nsFtpChannel::GetStatusArg(nsresult status
, nsString
&statusArg
) {
104 URI()->GetHost(host
);
105 CopyUTF8toUTF16(host
, statusArg
);
109 void nsFtpChannel::OnCallbacksChanged() { mFTPEventSink
= nullptr; }
111 //-----------------------------------------------------------------------------
115 class FTPEventSinkProxy final
: public nsIFTPEventSink
{
116 ~FTPEventSinkProxy() = default;
119 explicit FTPEventSinkProxy(nsIFTPEventSink
*aTarget
)
120 : mTarget(aTarget
), mEventTarget(GetCurrentThreadEventTarget()) {}
122 NS_DECL_THREADSAFE_ISUPPORTS
123 NS_DECL_NSIFTPEVENTSINK
125 class OnFTPControlLogRunnable
: public Runnable
{
127 OnFTPControlLogRunnable(nsIFTPEventSink
*aTarget
, bool aServer
,
128 const char *aMessage
)
129 : mozilla::Runnable("FTPEventSinkProxy::OnFTPControlLogRunnable"),
132 mMessage(aMessage
) {}
137 nsCOMPtr
<nsIFTPEventSink
> mTarget
;
143 nsCOMPtr
<nsIFTPEventSink
> mTarget
;
144 nsCOMPtr
<nsIEventTarget
> mEventTarget
;
147 NS_IMPL_ISUPPORTS(FTPEventSinkProxy
, nsIFTPEventSink
)
150 FTPEventSinkProxy::OnFTPControlLog(bool aServer
, const char *aMsg
) {
151 RefPtr
<OnFTPControlLogRunnable
> r
=
152 new OnFTPControlLogRunnable(mTarget
, aServer
, aMsg
);
153 return mEventTarget
->Dispatch(r
, NS_DISPATCH_NORMAL
);
157 FTPEventSinkProxy::OnFTPControlLogRunnable::Run() {
158 mTarget
->OnFTPControlLog(mServer
, mMessage
.get());
164 void nsFtpChannel::GetFTPEventSink(nsCOMPtr
<nsIFTPEventSink
> &aResult
) {
165 if (!mFTPEventSink
) {
166 nsCOMPtr
<nsIFTPEventSink
> ftpSink
;
167 GetCallback(ftpSink
);
169 mFTPEventSink
= new FTPEventSinkProxy(ftpSink
);
172 aResult
= mFTPEventSink
;
176 nsFtpChannel::ForcePending(bool aForcePending
) {
177 // Set true here so IsPending will return true.
178 // Required for callback diversion from child back to parent. In such cases
179 // OnStopRequest can be called in the parent before callbacks are diverted
180 // back from the child to the listener in the parent.
181 mForcePending
= aForcePending
;
187 nsFtpChannel::IsPending(bool *result
) {
192 bool nsFtpChannel::Pending() const {
193 return nsBaseChannel::Pending() || mForcePending
;
197 nsFtpChannel::Suspend() {
198 LOG(("nsFtpChannel::Suspend [this=%p]\n", this));
200 nsresult rv
= SuspendInternal();
202 nsresult rvParentChannel
= NS_OK
;
203 if (mParentChannel
) {
204 rvParentChannel
= mParentChannel
->SuspendMessageDiversion();
207 return NS_FAILED(rv
) ? rv
: rvParentChannel
;
211 nsFtpChannel::Resume() {
212 LOG(("nsFtpChannel::Resume [this=%p]\n", this));
214 nsresult rv
= ResumeInternal();
216 nsresult rvParentChannel
= NS_OK
;
217 if (mParentChannel
) {
218 rvParentChannel
= mParentChannel
->ResumeMessageDiversion();
221 return NS_FAILED(rv
) ? rv
: rvParentChannel
;
224 //-----------------------------------------------------------------------------
225 // AChannelHasDivertableParentChannelAsListener internal functions
226 //-----------------------------------------------------------------------------
229 nsFtpChannel::MessageDiversionStarted(
230 ADivertableParentChannel
*aParentChannel
) {
231 MOZ_ASSERT(!mParentChannel
);
232 mParentChannel
= aParentChannel
;
233 // If the channel is suspended, propagate that info to the parent's mEventQ.
234 uint32_t suspendCount
= mSuspendCount
;
235 while (suspendCount
--) {
236 mParentChannel
->SuspendMessageDiversion();
242 nsFtpChannel::MessageDiversionStop() {
243 LOG(("nsFtpChannel::MessageDiversionStop [this=%p]", this));
244 MOZ_ASSERT(mParentChannel
);
245 mParentChannel
= nullptr;
250 nsFtpChannel::SuspendInternal() {
251 LOG(("nsFtpChannel::SuspendInternal [this=%p]\n", this));
253 return nsBaseChannel::Suspend();
257 nsFtpChannel::ResumeInternal() {
258 LOG(("nsFtpChannel::ResumeInternal [this=%p]\n", this));
259 NS_ENSURE_TRUE(mSuspendCount
> 0, NS_ERROR_UNEXPECTED
);
261 return nsBaseChannel::Resume();