1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
/* ***** BEGIN LICENSE BLOCK *****
3
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
5
* The contents of this file are subject to the Mozilla Public License Version
6
* 1.1 (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
8
* http://www.mozilla.org/MPL/
10
* Software distributed under the License is distributed on an "AS IS" basis,
11
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12
* for the specific language governing rights and limitations under the
15
* The Original Code is mozilla.org code.
17
* The Initial Developer of the Original Code is
18
* Netscape Communications Corporation.
19
* Portions created by the Initial Developer are Copyright (C) 1998
20
* the Initial Developer. All Rights Reserved.
24
* Alternatively, the contents of this file may be used under the terms of
25
* either the GNU General Public License Version 2 or later (the "GPL"), or
26
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27
* in which case the provisions of the GPL or the LGPL are applicable instead
28
* of those above. If you wish to allow use of your version of this file only
29
* under the terms of either the GPL or the LGPL, and not to allow others to
30
* use your version of this file under the terms of the MPL, indicate your
31
* decision by deleting the provisions above and replace them with the notice
32
* and other provisions required by the GPL or the LGPL. If you do not delete
33
* the provisions above, a recipient may use your version of this file under
34
* the terms of any one of the MPL, the GPL or the LGPL.
36
* ***** END LICENSE BLOCK ***** */
39
#include "nsFTPChannel.h"
40
#include "nsFtpControlConnection.h"
43
#include "nsIInputStream.h"
44
#include "nsISocketTransportService.h"
45
#include "nsISocketTransport.h"
46
#include "nsNetUtil.h"
47
#include "nsThreadUtils.h"
50
#if defined(PR_LOGGING)
51
extern PRLogModuleInfo* gFTPLog;
53
#define LOG(args) PR_LOG(gFTPLog, PR_LOG_DEBUG, args)
54
#define LOG_ALWAYS(args) PR_LOG(gFTPLog, PR_LOG_ALWAYS, args)
57
// nsFtpControlConnection implementation ...
60
NS_IMPL_ISUPPORTS1(nsFtpControlConnection, nsIInputStreamCallback)
63
nsFtpControlConnection::OnInputStreamReady(nsIAsyncInputStream *stream)
67
// Consume data whether we have a listener or not.
69
nsresult rv = stream->Available(&avail);
70
if (NS_SUCCEEDED(rv)) {
71
if (avail > sizeof(data))
75
rv = stream->Read(data, avail, &n);
76
if (NS_SUCCEEDED(rv) && n != avail)
80
// It's important that we null out mListener before calling one of its
81
// methods as it may call WaitData, which would queue up another read.
83
nsRefPtr<nsFtpControlConnectionListener> listener;
84
listener.swap(mListener);
90
listener->OnControlError(rv);
92
listener->OnControlDataAvailable(data, avail);
98
nsFtpControlConnection::nsFtpControlConnection(const nsCSubstring& host, PRUint32 port)
99
: mServerType(0), mHost(host), mPort(port)
101
LOG_ALWAYS(("FTP:CC created @%p", this));
104
nsFtpControlConnection::~nsFtpControlConnection()
106
LOG_ALWAYS(("FTP:CC destroyed @%p", this));
110
nsFtpControlConnection::IsAlive()
115
PRBool isAlive = PR_FALSE;
116
mSocket->IsAlive(&isAlive);
120
nsFtpControlConnection::Connect(nsIProxyInfo* proxyInfo,
121
nsITransportEventSink* eventSink)
128
nsCOMPtr<nsISocketTransportService> sts =
129
do_GetService(NS_SOCKETTRANSPORTSERVICE_CONTRACTID, &rv);
133
rv = sts->CreateTransport(nsnull, 0, mHost, mPort, proxyInfo,
134
getter_AddRefs(mSocket)); // the command transport
138
// proxy transport events back to current thread
140
mSocket->SetEventSink(eventSink, NS_GetCurrentThread());
142
// open buffered, blocking output stream to socket. so long as commands
143
// do not exceed 1024 bytes in length, the writing thread (the main thread)
144
// will not block. this should be OK.
145
rv = mSocket->OpenOutputStream(nsITransport::OPEN_BLOCKING, 1024, 1,
146
getter_AddRefs(mSocketOutput));
150
// open buffered, non-blocking/asynchronous input stream to socket.
151
nsCOMPtr<nsIInputStream> inStream;
152
rv = mSocket->OpenInputStream(0,
153
FTP_COMMAND_CHANNEL_SEG_SIZE,
154
FTP_COMMAND_CHANNEL_SEG_COUNT,
155
getter_AddRefs(inStream));
156
if (NS_SUCCEEDED(rv))
157
mSocketInput = do_QueryInterface(inStream);
163
nsFtpControlConnection::WaitData(nsFtpControlConnectionListener *listener)
165
LOG(("FTP:(%p) wait data [listener=%p]\n", this, listener));
167
// If listener is null, then simply disconnect the listener. Otherwise,
168
// ensure that we are listening.
174
NS_ENSURE_STATE(mSocketInput);
176
mListener = listener;
177
return mSocketInput->AsyncWait(this, 0, 0, NS_GetCurrentThread());
181
nsFtpControlConnection::Disconnect(nsresult status)
184
return NS_OK; // already disconnected
186
LOG_ALWAYS(("FTP:(%p) CC disconnecting (%x)", this, status));
188
if (NS_FAILED(status)) {
189
// break cyclic reference!
190
mSocket->Close(status);
192
mSocketInput->AsyncWait(nsnull, 0, 0, nsnull); // clear any observer
193
mSocketInput = nsnull;
194
mSocketOutput = nsnull;
201
nsFtpControlConnection::Write(const nsCSubstring& command)
203
NS_ENSURE_STATE(mSocketOutput);
205
PRUint32 len = command.Length();
207
nsresult rv = mSocketOutput->Write(command.Data(), len, &cnt);
213
return NS_ERROR_FAILURE;