]> git.saurik.com Git - wxWidgets.git/blob - src/common/ftp.cpp
d0b2a80211366cb6a46014c6f99d8231fa83a051
[wxWidgets.git] / src / common / ftp.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: ftp.cpp
3 // Purpose: FTP protocol
4 // Author: Guilhem Lavaux
5 // Modified by:
6 // Created: 07/07/1997
7 // RCS-ID: $Id$
8 // Copyright: (c) 1997, 1998 Guilhem Lavaux
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "ftp.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef __MWERKS__
24 #include <memory.h>
25 #endif
26 #if defined(__WXMAC__)
27 #include "/wx/mac/macsock.h"
28 #endif
29
30 #include <stdlib.h>
31 #include "wx/string.h"
32 #include "wx/utils.h"
33 // #include "wx/data.h"
34 #define WXSOCK_INTERNAL
35 #include "wx/sckaddr.h"
36 #undef WXSOCK_INTERNAL
37 #include "wx/socket.h"
38 #include "wx/url.h"
39 #include "wx/sckstrm.h"
40 #include "wx/protocol/protocol.h"
41 #include "wx/protocol/ftp.h"
42
43 #ifdef __BORLANDC__
44 #pragma hdrstop
45 #endif
46
47 #define FTP_BSIZE 1024
48
49 #if !USE_SHARED_LIBRARY
50 IMPLEMENT_DYNAMIC_CLASS(wxFTP, wxProtocol)
51 IMPLEMENT_PROTOCOL(wxFTP, "ftp", "ftp", TRUE)
52 #endif
53
54 ////////////////////////////////////////////////////////////////
55 ////// wxFTP constructor and destructor ////////////////////////
56 ////////////////////////////////////////////////////////////////
57
58 wxFTP::wxFTP()
59 : wxProtocol()
60 {
61 char tmp[256];
62
63 m_lastError = wxPROTO_NOERR;
64 m_streaming = FALSE;
65
66 m_user = "anonymous";
67 wxGetUserName(tmp, 256);
68 m_passwd.sprintf("%s@",tmp);
69 wxGetHostName(tmp, 256);
70 m_passwd += tmp;
71
72 SetNotify(0);
73 }
74
75 wxFTP::~wxFTP()
76 {
77 SendCommand("QUIT", '2');
78 }
79
80 ////////////////////////////////////////////////////////////////
81 ////// wxFTP connect and login methods /////////////////////////
82 ////////////////////////////////////////////////////////////////
83 bool wxFTP::Connect(wxSockAddress& addr, bool WXUNUSED(wait))
84 {
85 if (!m_handler) {
86 m_lastError = wxPROTO_NOHNDLR;
87 return FALSE;
88 }
89
90 if (!wxProtocol::Connect(addr)) {
91 m_lastError = wxPROTO_NETERR;
92 return FALSE;
93 }
94
95 if (!m_user || !m_passwd) {
96 m_lastError = wxPROTO_CONNERR;
97 return FALSE;
98 }
99
100 wxString command;
101
102 if (!GetResult('2')) {
103 Close();
104 return FALSE;
105 }
106
107 command.sprintf("USER %s", (const char *)m_user);
108 if (!SendCommand(command, '3')) {
109 Close();
110 return FALSE;
111 }
112
113 command.sprintf("PASS %s", (const char *)m_passwd);
114 if (!SendCommand(command, '2')) {
115 Close();
116 return FALSE;
117 }
118
119 return TRUE;
120 }
121
122 bool wxFTP::Connect(const wxString& host)
123 {
124 wxIPV4address addr;
125 wxString my_host = host;
126
127 addr.Hostname(my_host);
128 addr.Service("ftp");
129
130 return Connect(addr);
131 }
132
133 bool wxFTP::Close()
134 {
135 if (m_streaming) {
136 m_lastError = wxPROTO_STREAMING;
137 return FALSE;
138 }
139 if (m_connected)
140 SendCommand(wxString("QUIT"), '2');
141 return wxSocketClient::Close();
142 }
143
144 ////////////////////////////////////////////////////////////////
145 ////// wxFTP low-level methods /////////////////////////////////
146 ////////////////////////////////////////////////////////////////
147 bool wxFTP::SendCommand(const wxString& command, char exp_ret)
148 {
149 wxString tmp_str;
150
151 if (m_streaming) {
152 m_lastError = wxPROTO_STREAMING;
153 return FALSE;
154 }
155 tmp_str = command + "\r\n";
156 if (Write((char *)tmp_str.GetData(), tmp_str.Length()).Error()) {
157 m_lastError = wxPROTO_NETERR;
158 return FALSE;
159 }
160 return GetResult(exp_ret);
161 }
162
163 bool wxFTP::GetResult(char exp)
164 {
165 if ((m_lastError = GetLine(this, m_lastResult)))
166 return FALSE;
167 if (m_lastResult.GetChar(0) != exp) {
168 m_lastError = wxPROTO_PROTERR;
169 return FALSE;
170 }
171
172 if (m_lastResult.GetChar(3) == '-') {
173 wxString key = m_lastResult.Left((size_t)3);
174
175 key += ' ';
176
177 while (m_lastResult.Index(key) != 0) {
178 if ((m_lastError = GetLine(this, m_lastResult)))
179 return FALSE;
180 }
181 }
182 return TRUE;
183 }
184
185 ////////////////////////////////////////////////////////////////
186 ////// wxFTP low-level methods /////////////////////////////////
187 ////////////////////////////////////////////////////////////////
188 bool wxFTP::ChDir(const wxString& dir)
189 {
190 wxString str = dir;
191
192 str.Prepend("CWD ");
193 return SendCommand(str, '2');
194 }
195
196 bool wxFTP::MkDir(const wxString& dir)
197 {
198 wxString str = dir;
199 str.Prepend("MKD ");
200 return SendCommand(str, '2');
201 }
202
203 bool wxFTP::RmDir(const wxString& dir)
204 {
205 wxString str = dir;
206
207 str.Prepend("PWD ");
208 return SendCommand(str, '2');
209 }
210
211 wxString wxFTP::Pwd()
212 {
213 int beg, end;
214
215 if (!SendCommand("PWD", '2'))
216 return wxString((char *)NULL);
217
218 beg = m_lastResult.Find('\"',FALSE);
219 end = m_lastResult.Find('\"',TRUE);
220
221 return wxString(beg+1, end);
222 }
223
224 bool wxFTP::Rename(const wxString& src, const wxString& dst)
225 {
226 wxString str;
227
228 str = "RNFR " + src;
229 if (!SendCommand(str, '3'))
230 return FALSE;
231
232 str = "RNTO " + dst;
233 return SendCommand(str, '2');
234 }
235
236 bool wxFTP::RmFile(const wxString& path)
237 {
238 wxString str;
239
240 str = "DELE ";
241 str += path;
242 return SendCommand(str, '2');
243 }
244
245 ////////////////////////////////////////////////////////////////
246 ////// wxFTP download*upload ///////////////////////////////////
247 ////////////////////////////////////////////////////////////////
248
249 class wxInputFTPStream : public wxSocketInputStream {
250 public:
251 wxFTP *m_ftp;
252 size_t m_ftpsize;
253
254 wxInputFTPStream(wxFTP *ftp_clt, wxSocketBase *sock)
255 : wxSocketInputStream(*sock), m_ftp(ftp_clt) {}
256 size_t StreamSize() { return m_ftpsize; }
257 virtual ~wxInputFTPStream(void)
258 {
259 if (LastError() != wxStream_NOERROR)
260 m_ftp->GetResult('2');
261 else
262 m_ftp->Abort();
263 delete m_i_socket;
264 }
265 };
266
267 class wxOutputFTPStream : public wxSocketOutputStream {
268 public:
269 wxFTP *m_ftp;
270
271 wxOutputFTPStream(wxFTP *ftp_clt, wxSocketBase *sock)
272 : wxSocketOutputStream(*sock), m_ftp(ftp_clt) {}
273 virtual ~wxOutputFTPStream(void)
274 {
275 if (LastError() != wxStream_NOERROR)
276 m_ftp->GetResult('2');
277 else
278 m_ftp->Abort();
279 delete m_o_socket;
280 }
281 };
282
283 wxSocketClient *wxFTP::GetPort()
284 {
285 wxIPV4address addr;
286 wxSocketClient *client;
287 struct sockaddr sin;
288 int a[6];
289 wxString straddr;
290 int addr_pos;
291
292 if (!SendCommand("PASV", '2'))
293 return NULL;
294
295 sin.sa_family = AF_INET;
296 addr_pos = m_lastResult.Find('(');
297 if (addr_pos == -1) {
298 m_lastError = wxPROTO_PROTERR;
299 return NULL;
300 }
301 straddr = m_lastResult(addr_pos+1, m_lastResult.Length());
302 sscanf((const char *)straddr,"%d,%d,%d,%d,%d,%d",&a[2],&a[3],&a[4],&a[5],&a[0],&a[1]);
303 sin.sa_data[2] = (char)a[2];
304 sin.sa_data[3] = (char)a[3];
305 sin.sa_data[4] = (char)a[4];
306 sin.sa_data[5] = (char)a[5];
307 sin.sa_data[0] = (char)a[0];
308 sin.sa_data[1] = (char)a[1];
309
310 addr.Disassemble(&sin, sizeof(sin));
311
312 client = m_handler->CreateClient();
313 if (!client->Connect(addr)) {
314 delete client;
315 return NULL;
316 }
317 client->Notify(FALSE);
318
319 return client;
320 }
321
322 bool wxFTP::Abort(void)
323 {
324 m_streaming = FALSE;
325 if (!SendCommand("ABOR", '4'))
326 return FALSE;
327 return GetResult('2');
328 }
329
330 wxInputStream *wxFTP::GetInputStream(const wxString& path)
331 {
332 wxString tmp_str;
333 int pos_size;
334 wxInputFTPStream *in_stream;
335
336 if (!SendCommand("TYPE I", '2'))
337 return NULL;
338
339 wxSocketClient *sock = GetPort();
340
341 if (!sock) {
342 m_lastError = wxPROTO_NETERR;
343 return NULL;
344 }
345
346 tmp_str = "RETR " + path;
347 if (!SendCommand(tmp_str, '1'))
348 return NULL;
349
350 in_stream = new wxInputFTPStream(this, sock);
351
352 pos_size = m_lastResult.Index('(');
353 if (pos_size != wxNOT_FOUND) {
354 wxString str_size = m_lastResult(pos_size, m_lastResult.Index(')'));
355
356 in_stream->m_ftpsize = atoi(WXSTRINGCAST str_size);
357 }
358
359 return in_stream;
360 }
361
362 wxOutputStream *wxFTP::GetOutputStream(const wxString& path)
363 {
364 wxString tmp_str;
365
366 if (!SendCommand("TYPE I", '2'))
367 return NULL;
368
369 wxSocketClient *sock = GetPort();
370
371 tmp_str = "STOR " + path;
372 if (!SendCommand(tmp_str, '1'))
373 return FALSE;
374
375 return new wxOutputFTPStream(this, sock);
376 }
377
378 wxList *wxFTP::GetList(const wxString& wildcard)
379 {
380 wxList *file_list = new wxList;
381 wxSocketBase *sock = GetPort();
382 wxString tmp_str = "NLST";
383
384 if (!wildcard.IsNull())
385 tmp_str += wildcard;
386
387 if (!SendCommand(tmp_str, '1')) {
388 delete sock;
389 delete file_list;
390 return NULL;
391 }
392
393 while (GetLine(sock, tmp_str) == wxPROTO_NOERR) {
394 file_list->Append((wxObject *)(new wxString(tmp_str)));
395 }
396
397 if (!GetResult('2')) {
398 delete sock;
399 file_list->DeleteContents(TRUE);
400 delete file_list;
401 return NULL;
402 }
403
404 return file_list;
405 }