]> git.saurik.com Git - wxWidgets.git/blob - src/common/ftp.cpp
Fixed compile errors.
[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 #if wxUSE_SOCKETS
24
25 #ifndef __MWERKS__
26 #include <memory.h>
27 #endif
28 #if defined(__WXMAC__)
29 #include "/wx/mac/macsock.h"
30 #endif
31
32 #include <stdlib.h>
33 #include "wx/string.h"
34 #include "wx/utils.h"
35 // #include "wx/data.h"
36 #define WXSOCK_INTERNAL
37 #include "wx/sckaddr.h"
38 #undef WXSOCK_INTERNAL
39 #include "wx/socket.h"
40 #include "wx/url.h"
41 #include "wx/sckstrm.h"
42 #include "wx/protocol/protocol.h"
43 #include "wx/protocol/ftp.h"
44
45 #ifdef __BORLANDC__
46 #pragma hdrstop
47 #endif
48
49 #define FTP_BSIZE 1024
50
51 #if !USE_SHARED_LIBRARY
52 IMPLEMENT_DYNAMIC_CLASS(wxFTP, wxProtocol)
53 IMPLEMENT_PROTOCOL(wxFTP, _T("ftp"), _T("ftp"), TRUE)
54 #endif
55
56 ////////////////////////////////////////////////////////////////
57 ////// wxFTP constructor and destructor ////////////////////////
58 ////////////////////////////////////////////////////////////////
59
60 wxFTP::wxFTP()
61 : wxProtocol()
62 {
63 m_lastError = wxPROTO_NOERR;
64 m_streaming = FALSE;
65
66 m_user = _T("anonymous");
67 m_passwd = wxGetUserId();
68 m_passwd += '@';
69 m_passwd += wxGetHostName();
70
71 SetNotify(0);
72 }
73
74 wxFTP::~wxFTP()
75 {
76 SendCommand("QUIT", '2');
77 }
78
79 ////////////////////////////////////////////////////////////////
80 ////// wxFTP connect and login methods /////////////////////////
81 ////////////////////////////////////////////////////////////////
82 bool wxFTP::Connect(wxSockAddress& addr, bool WXUNUSED(wait))
83 {
84 if (!m_handler) {
85 m_lastError = wxPROTO_NOHNDLR;
86 return FALSE;
87 }
88
89 if (!wxProtocol::Connect(addr)) {
90 m_lastError = wxPROTO_NETERR;
91 return FALSE;
92 }
93
94 if (!m_user || !m_passwd) {
95 m_lastError = wxPROTO_CONNERR;
96 return FALSE;
97 }
98
99 wxString command;
100
101 if (!GetResult('2')) {
102 Close();
103 return FALSE;
104 }
105
106 command.sprintf(_T("USER %s"), (const wxChar *)m_user);
107 if (!SendCommand(command, '3')) {
108 Close();
109 return FALSE;
110 }
111
112 command.sprintf(_T("PASS %s"), (const wxChar *)m_passwd);
113 if (!SendCommand(command, '2')) {
114 Close();
115 return FALSE;
116 }
117
118 return TRUE;
119 }
120
121 bool wxFTP::Connect(const wxString& host)
122 {
123 wxIPV4address addr;
124 wxString my_host = host;
125
126 addr.Hostname(my_host);
127 addr.Service(_T("ftp"));
128
129 return Connect(addr);
130 }
131
132 bool wxFTP::Close()
133 {
134 if (m_streaming) {
135 m_lastError = wxPROTO_STREAMING;
136 return FALSE;
137 }
138 if (m_connected)
139 SendCommand(wxString(_T("QUIT")), '2');
140 return wxSocketClient::Close();
141 }
142
143 ////////////////////////////////////////////////////////////////
144 ////// wxFTP low-level methods /////////////////////////////////
145 ////////////////////////////////////////////////////////////////
146 bool wxFTP::SendCommand(const wxString& command, char exp_ret)
147 {
148 wxString tmp_str;
149
150 if (m_streaming) {
151 m_lastError = wxPROTO_STREAMING;
152 return FALSE;
153 }
154 tmp_str = command + _T("\r\n");
155 const wxWX2MBbuf tmp_buf = tmp_str.mb_str();
156 if (Write(MBSTRINGCAST tmp_buf, strlen(tmp_buf)).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 += _T(' ');
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(_T("CWD "));
193 return SendCommand(str, '2');
194 }
195
196 bool wxFTP::MkDir(const wxString& dir)
197 {
198 wxString str = dir;
199 str.Prepend(_T("MKD "));
200 return SendCommand(str, '2');
201 }
202
203 bool wxFTP::RmDir(const wxString& dir)
204 {
205 wxString str = dir;
206
207 str.Prepend(_T("PWD "));
208 return SendCommand(str, '2');
209 }
210
211 wxString wxFTP::Pwd()
212 {
213 int beg, end;
214
215 if (!SendCommand(_T("PWD"), '2'))
216 return wxString((char *)NULL);
217
218 beg = m_lastResult.Find(_T('\"'),FALSE);
219 end = m_lastResult.Find(_T('\"'),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 = _T("RNFR ") + src;
229 if (!SendCommand(str, '3'))
230 return FALSE;
231
232 str = _T("RNTO ") + dst;
233 return SendCommand(str, '2');
234 }
235
236 bool wxFTP::RmFile(const wxString& path)
237 {
238 wxString str;
239
240 str = _T("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() const { 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(_T("PASV"), '2'))
293 return NULL;
294
295 sin.sa_family = AF_INET;
296 addr_pos = m_lastResult.Find(_T('('));
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 wxSscanf((const wxChar *)straddr,_T("%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(_T("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(_T("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 = _T("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(_T('('));
353 if (pos_size != wxNOT_FOUND) {
354 wxString str_size = m_lastResult(pos_size+1, m_lastResult.Index(_T(')'))-1);
355
356 in_stream->m_ftpsize = wxAtoi(WXSTRINGCAST str_size);
357 }
358 sock->SetFlags(WAITALL);
359
360 return in_stream;
361 }
362
363 wxOutputStream *wxFTP::GetOutputStream(const wxString& path)
364 {
365 wxString tmp_str;
366
367 if (!SendCommand(_T("TYPE I"), '2'))
368 return NULL;
369
370 wxSocketClient *sock = GetPort();
371
372 tmp_str = _T("STOR ") + path;
373 if (!SendCommand(tmp_str, '1'))
374 return FALSE;
375
376 return new wxOutputFTPStream(this, sock);
377 }
378
379 wxList *wxFTP::GetList(const wxString& wildcard)
380 {
381 wxList *file_list = new wxList;
382 wxSocketBase *sock = GetPort();
383 wxString tmp_str = _T("NLST");
384
385 if (!wildcard.IsNull())
386 tmp_str += wildcard;
387
388 if (!SendCommand(tmp_str, '1')) {
389 delete sock;
390 delete file_list;
391 return NULL;
392 }
393
394 while (GetLine(sock, tmp_str) == wxPROTO_NOERR) {
395 file_list->Append((wxObject *)(new wxString(tmp_str)));
396 }
397
398 if (!GetResult('2')) {
399 delete sock;
400 file_list->DeleteContents(TRUE);
401 delete file_list;
402 return NULL;
403 }
404
405 return file_list;
406 }
407 #endif
408 // wxUSE_SOCKETS