]>
git.saurik.com Git - wxWidgets.git/blob - src/common/ftp.cpp
00d44539cdfc93781605a0f16ed1570ff0910343
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: FTP protocol
4 // Author: Guilhem Lavaux
8 // Copyright: (c) 1997, 1998 Guilhem Lavaux
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "ftp.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
25 #include "wx/string.h"
27 // #include "wx/data.h"
28 #define WXSOCK_INTERNAL
29 #include "wx/sckaddr.h"
30 #undef WXSOCK_INTERNAL
31 #include "wx/socket.h"
33 #include "wx/sckstrm.h"
34 #include "wx/protocol/protocol.h"
35 #include "wx/protocol/ftp.h"
41 #define FTP_BSIZE 1024
43 #if !USE_SHARED_LIBRARY
44 IMPLEMENT_DYNAMIC_CLASS(wxFTP
, wxProtocol
)
45 IMPLEMENT_PROTOCOL(wxFTP
, "ftp", "ftp", TRUE
)
48 ////////////////////////////////////////////////////////////////
49 ////// wxFTP constructor and destructor ////////////////////////
50 ////////////////////////////////////////////////////////////////
57 m_lastError
= wxPROTO_NOERR
;
61 wxGetUserName(tmp
, 256);
62 m_passwd
.sprintf("%s@",tmp
);
63 wxGetHostName(tmp
, 256);
71 SendCommand("QUIT", '2');
74 ////////////////////////////////////////////////////////////////
75 ////// wxFTP connect and login methods /////////////////////////
76 ////////////////////////////////////////////////////////////////
77 bool wxFTP::Connect(wxSockAddress
& addr
)
80 m_lastError
= wxPROTO_NOHNDLR
;
84 if (!wxProtocol::Connect(addr
)) {
85 m_lastError
= wxPROTO_NETERR
;
89 if (!m_user
|| !m_passwd
) {
90 m_lastError
= wxPROTO_CONNERR
;
96 if (!GetResult('2')) {
101 command
.sprintf("USER %s", (const char *)m_user
);
102 if (!SendCommand(command
, '3')) {
107 command
.sprintf("PASS %s", (const char *)m_passwd
);
108 if (!SendCommand(command
, '2')) {
116 bool wxFTP::Connect(const wxString
& host
)
119 wxString my_host
= host
;
121 addr
.Hostname(my_host
);
124 return Connect(addr
);
130 m_lastError
= wxPROTO_STREAMING
;
134 SendCommand(wxString("QUIT"), '2');
135 return wxSocketClient::Close();
138 ////////////////////////////////////////////////////////////////
139 ////// wxFTP low-level methods /////////////////////////////////
140 ////////////////////////////////////////////////////////////////
141 bool wxFTP::SendCommand(const wxString
& command
, char exp_ret
)
146 m_lastError
= wxPROTO_STREAMING
;
149 tmp_str
= command
+ "\r\n";
150 if (Write((char *)tmp_str
.GetData(), tmp_str
.Length()).Error()) {
151 m_lastError
= wxPROTO_NETERR
;
154 return GetResult(exp_ret
);
157 bool wxFTP::GetResult(char exp
)
159 if ((m_lastError
= GetLine(this, m_lastResult
)))
161 if (m_lastResult
[0] != exp
) {
162 m_lastError
= wxPROTO_PROTERR
;
166 if (m_lastResult
[3] == '-') {
167 wxString key
= m_lastResult
.Left((size_t)3);
171 while (m_lastResult
.Index(key
) != 0) {
172 if ((m_lastError
= GetLine(this, m_lastResult
)))
179 ////////////////////////////////////////////////////////////////
180 ////// wxFTP low-level methods /////////////////////////////////
181 ////////////////////////////////////////////////////////////////
182 bool wxFTP::ChDir(const wxString
& dir
)
187 return SendCommand(str
, '2');
190 bool wxFTP::MkDir(const wxString
& dir
)
194 return SendCommand(str
, '2');
197 bool wxFTP::RmDir(const wxString
& dir
)
202 return SendCommand(str
, '2');
205 wxString
wxFTP::Pwd()
209 if (!SendCommand("PWD", '2'))
210 return wxString((char *)NULL
);
212 beg
= m_lastResult
.Find('\"',FALSE
);
213 end
= m_lastResult
.Find('\"',TRUE
);
215 return wxString(beg
+1, end
);
218 bool wxFTP::Rename(const wxString
& src
, const wxString
& dst
)
223 if (!SendCommand(str
, '3'))
227 return SendCommand(str
, '2');
230 bool wxFTP::RmFile(const wxString
& path
)
236 return SendCommand(str
, '2');
239 ////////////////////////////////////////////////////////////////
240 ////// wxFTP download*upload ///////////////////////////////////
241 ////////////////////////////////////////////////////////////////
243 class wxInputFTPStream
: public wxSocketInputStream
{
247 wxInputFTPStream(wxFTP
*ftp_clt
, wxSocketBase
*sock
)
248 : wxSocketInputStream(*sock
), m_ftp(ftp_clt
) {}
249 virtual ~wxInputFTPStream(void)
251 if (LastError() != wxStream_NOERROR
)
252 m_ftp
->GetResult('2');
259 class wxOutputFTPStream
: public wxSocketOutputStream
{
263 wxOutputFTPStream(wxFTP
*ftp_clt
, wxSocketBase
*sock
)
264 : wxSocketOutputStream(*sock
), m_ftp(ftp_clt
) {}
265 virtual ~wxOutputFTPStream(void)
267 if (LastError() != wxStream_NOERROR
)
268 m_ftp
->GetResult('2');
275 wxSocketClient
*wxFTP::GetPort()
278 wxSocketClient
*client
;
284 if (!SendCommand("PASV", '2'))
287 sin
.sa_family
= AF_INET
;
288 addr_pos
= m_lastResult
.Find('(');
289 if (addr_pos
== -1) {
290 m_lastError
= wxPROTO_PROTERR
;
293 straddr
= m_lastResult(addr_pos
+1, m_lastResult
.Length());
294 sscanf((const char *)straddr
,"%d,%d,%d,%d,%d,%d",&a
[2],&a
[3],&a
[4],&a
[5],&a
[0],&a
[1]);
295 sin
.sa_data
[2] = (char)a
[2];
296 sin
.sa_data
[3] = (char)a
[3];
297 sin
.sa_data
[4] = (char)a
[4];
298 sin
.sa_data
[5] = (char)a
[5];
299 sin
.sa_data
[0] = (char)a
[0];
300 sin
.sa_data
[1] = (char)a
[1];
302 addr
.Disassemble(&sin
, sizeof(sin
));
304 client
= m_handler
->CreateClient();
305 if (!client
->Connect(addr
)) {
309 client
->Notify(FALSE
);
314 bool wxFTP::Abort(void)
317 if (!SendCommand("ABOR", '4'))
319 return GetResult('2');
322 wxInputStream
*wxFTP::GetInputStream(const wxString
& path
)
326 if (!SendCommand("TYPE I", '2'))
329 wxSocketClient
*sock
= GetPort();
332 m_lastError
= wxPROTO_NETERR
;
336 tmp_str
= "RETR " + path
;
337 if (!SendCommand(tmp_str
, '1'))
340 return new wxInputFTPStream(this, sock
);
343 wxOutputStream
*wxFTP::GetOutputStream(const wxString
& path
)
347 if (!SendCommand("TYPE I", '2'))
350 wxSocketClient
*sock
= GetPort();
352 tmp_str
= "STOR " + path
;
353 if (!SendCommand(tmp_str
, '1'))
356 return new wxOutputFTPStream(this, sock
);
359 wxList
*wxFTP::GetList(const wxString
& wildcard
)
361 wxList
*file_list
= new wxList
;
362 wxSocketBase
*sock
= GetPort();
363 wxString tmp_str
= "NLST";
365 if (!wildcard
.IsNull())
368 if (!SendCommand(tmp_str
, '1')) {
374 while (GetLine(sock
, tmp_str
) == wxPROTO_NOERR
) {
375 file_list
->Append((wxObject
*)(new wxString(tmp_str
)));
378 if (!GetResult('2')) {
380 file_list
->DeleteContents(TRUE
);