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
4 : *
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/
9 : *
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
13 : * License.
14 : *
15 : * The Original Code is mozilla.org code.
16 : *
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.
21 : *
22 : * Contributor(s):
23 : *
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.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #include "nsIOService.h"
39 : #include "nsFTPChannel.h"
40 : #include "nsFtpControlConnection.h"
41 : #include "nsFtpProtocolHandler.h"
42 : #include "prlog.h"
43 : #include "nsIPipe.h"
44 : #include "nsIInputStream.h"
45 : #include "nsISocketTransportService.h"
46 : #include "nsISocketTransport.h"
47 : #include "nsNetUtil.h"
48 : #include "nsThreadUtils.h"
49 : #include "nsCRT.h"
50 :
51 : #if defined(PR_LOGGING)
52 : extern PRLogModuleInfo* gFTPLog;
53 : #endif
54 : #define LOG(args) PR_LOG(gFTPLog, PR_LOG_DEBUG, args)
55 : #define LOG_ALWAYS(args) PR_LOG(gFTPLog, PR_LOG_ALWAYS, args)
56 :
57 : //
58 : // nsFtpControlConnection implementation ...
59 : //
60 :
61 0 : NS_IMPL_ISUPPORTS1(nsFtpControlConnection, nsIInputStreamCallback)
62 :
63 : NS_IMETHODIMP
64 0 : nsFtpControlConnection::OnInputStreamReady(nsIAsyncInputStream *stream)
65 : {
66 : char data[4096];
67 :
68 : // Consume data whether we have a listener or not.
69 : PRUint32 avail;
70 0 : nsresult rv = stream->Available(&avail);
71 0 : if (NS_SUCCEEDED(rv)) {
72 0 : if (avail > sizeof(data))
73 0 : avail = sizeof(data);
74 :
75 : PRUint32 n;
76 0 : rv = stream->Read(data, avail, &n);
77 0 : if (NS_SUCCEEDED(rv) && n != avail)
78 0 : avail = n;
79 : }
80 :
81 : // It's important that we null out mListener before calling one of its
82 : // methods as it may call WaitData, which would queue up another read.
83 :
84 0 : nsRefPtr<nsFtpControlConnectionListener> listener;
85 0 : listener.swap(mListener);
86 :
87 0 : if (!listener)
88 0 : return NS_OK;
89 :
90 0 : if (NS_FAILED(rv)) {
91 0 : listener->OnControlError(rv);
92 : } else {
93 0 : listener->OnControlDataAvailable(data, avail);
94 : }
95 :
96 0 : return NS_OK;
97 : }
98 :
99 0 : nsFtpControlConnection::nsFtpControlConnection(const nsCSubstring& host,
100 : PRUint32 port)
101 0 : : mServerType(0), mSessionId(gFtpHandler->GetSessionId()), mHost(host)
102 0 : , mPort(port)
103 : {
104 0 : LOG_ALWAYS(("FTP:CC created @%p", this));
105 0 : }
106 :
107 0 : nsFtpControlConnection::~nsFtpControlConnection()
108 : {
109 0 : LOG_ALWAYS(("FTP:CC destroyed @%p", this));
110 0 : }
111 :
112 : bool
113 0 : nsFtpControlConnection::IsAlive()
114 : {
115 0 : if (!mSocket)
116 0 : return false;
117 :
118 0 : bool isAlive = false;
119 0 : mSocket->IsAlive(&isAlive);
120 0 : return isAlive;
121 : }
122 : nsresult
123 0 : nsFtpControlConnection::Connect(nsIProxyInfo* proxyInfo,
124 : nsITransportEventSink* eventSink)
125 : {
126 0 : if (mSocket)
127 0 : return NS_OK;
128 :
129 : // build our own
130 : nsresult rv;
131 : nsCOMPtr<nsISocketTransportService> sts =
132 0 : do_GetService(NS_SOCKETTRANSPORTSERVICE_CONTRACTID, &rv);
133 0 : if (NS_FAILED(rv))
134 0 : return rv;
135 :
136 0 : rv = sts->CreateTransport(nsnull, 0, mHost, mPort, proxyInfo,
137 0 : getter_AddRefs(mSocket)); // the command transport
138 0 : if (NS_FAILED(rv))
139 0 : return rv;
140 :
141 0 : mSocket->SetQoSBits(gFtpHandler->GetControlQoSBits());
142 :
143 : // proxy transport events back to current thread
144 0 : if (eventSink)
145 0 : mSocket->SetEventSink(eventSink, NS_GetCurrentThread());
146 :
147 : // open buffered, blocking output stream to socket. so long as commands
148 : // do not exceed 1024 bytes in length, the writing thread (the main thread)
149 : // will not block. this should be OK.
150 0 : rv = mSocket->OpenOutputStream(nsITransport::OPEN_BLOCKING, 1024, 1,
151 0 : getter_AddRefs(mSocketOutput));
152 0 : if (NS_FAILED(rv))
153 0 : return rv;
154 :
155 : // open buffered, non-blocking/asynchronous input stream to socket.
156 0 : nsCOMPtr<nsIInputStream> inStream;
157 0 : rv = mSocket->OpenInputStream(0,
158 : nsIOService::gDefaultSegmentSize,
159 : nsIOService::gDefaultSegmentCount,
160 0 : getter_AddRefs(inStream));
161 0 : if (NS_SUCCEEDED(rv))
162 0 : mSocketInput = do_QueryInterface(inStream);
163 :
164 0 : return rv;
165 : }
166 :
167 : nsresult
168 0 : nsFtpControlConnection::WaitData(nsFtpControlConnectionListener *listener)
169 : {
170 0 : LOG(("FTP:(%p) wait data [listener=%p]\n", this, listener));
171 :
172 : // If listener is null, then simply disconnect the listener. Otherwise,
173 : // ensure that we are listening.
174 0 : if (!listener) {
175 0 : mListener = nsnull;
176 0 : return NS_OK;
177 : }
178 :
179 0 : NS_ENSURE_STATE(mSocketInput);
180 :
181 0 : mListener = listener;
182 0 : return mSocketInput->AsyncWait(this, 0, 0, NS_GetCurrentThread());
183 : }
184 :
185 : nsresult
186 0 : nsFtpControlConnection::Disconnect(nsresult status)
187 : {
188 0 : if (!mSocket)
189 0 : return NS_OK; // already disconnected
190 :
191 0 : LOG_ALWAYS(("FTP:(%p) CC disconnecting (%x)", this, status));
192 :
193 0 : if (NS_FAILED(status)) {
194 : // break cyclic reference!
195 0 : mSocket->Close(status);
196 0 : mSocket = 0;
197 0 : mSocketInput->AsyncWait(nsnull, 0, 0, nsnull); // clear any observer
198 0 : mSocketInput = nsnull;
199 0 : mSocketOutput = nsnull;
200 : }
201 :
202 0 : return NS_OK;
203 : }
204 :
205 : nsresult
206 0 : nsFtpControlConnection::Write(const nsCSubstring& command)
207 : {
208 0 : NS_ENSURE_STATE(mSocketOutput);
209 :
210 0 : PRUint32 len = command.Length();
211 : PRUint32 cnt;
212 0 : nsresult rv = mSocketOutput->Write(command.Data(), len, &cnt);
213 :
214 0 : if (NS_FAILED(rv))
215 0 : return rv;
216 :
217 0 : if (len != cnt)
218 0 : return NS_ERROR_FAILURE;
219 :
220 0 : return NS_OK;
221 : }
|