]> git.saurik.com Git - wxWidgets.git/blob - src/common/ftp.cpp
Compile fix for GTK 1.0
[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
359 return in_stream;
360 }
361
362 wxOutputStream *wxFTP::GetOutputStream(const wxString& path)
363 {
364 wxString tmp_str;
365
366 if (!SendCommand(_T("TYPE I"), '2'))
367 return NULL;
368
369 wxSocketClient *sock = GetPort();
370
371 tmp_str = _T("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 = _T("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 // Ininterresting ?!
405 /*
406 sock->SetEventHandler(*GetNextHandler(), m_id);
407 sock->Notify(m_notifyme);
408 sock->SetNotify(m_neededreq);
409 */
410
411 return file_list;
412 }
413 #endif
414 // wxUSE_SOCKETS