]>
git.saurik.com Git - wxWidgets.git/blob - src/common/ftp.cpp
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"
26 #if defined(__WXMAC__)
27 #include "/wx/mac/macsock.h"
31 #include "wx/string.h"
33 // #include "wx/data.h"
34 #define WXSOCK_INTERNAL
35 #include "wx/sckaddr.h"
36 #undef WXSOCK_INTERNAL
37 #include "wx/socket.h"
39 #include "wx/sckstrm.h"
40 #include "wx/protocol/protocol.h"
41 #include "wx/protocol/ftp.h"
47 #define FTP_BSIZE 1024
49 #if !USE_SHARED_LIBRARY
50 IMPLEMENT_DYNAMIC_CLASS(wxFTP
, wxProtocol
)
51 IMPLEMENT_PROTOCOL(wxFTP
, "ftp", "ftp", TRUE
)
54 ////////////////////////////////////////////////////////////////
55 ////// wxFTP constructor and destructor ////////////////////////
56 ////////////////////////////////////////////////////////////////
63 m_lastError
= wxPROTO_NOERR
;
67 wxGetUserName(tmp
, 256);
68 m_passwd
.sprintf("%s@",tmp
);
69 wxGetHostName(tmp
, 256);
77 SendCommand("QUIT", '2');
80 ////////////////////////////////////////////////////////////////
81 ////// wxFTP connect and login methods /////////////////////////
82 ////////////////////////////////////////////////////////////////
83 bool wxFTP::Connect(wxSockAddress
& addr
, bool WXUNUSED(wait
))
86 m_lastError
= wxPROTO_NOHNDLR
;
90 if (!wxProtocol::Connect(addr
)) {
91 m_lastError
= wxPROTO_NETERR
;
95 if (!m_user
|| !m_passwd
) {
96 m_lastError
= wxPROTO_CONNERR
;
102 if (!GetResult('2')) {
107 command
.sprintf("USER %s", (const char *)m_user
);
108 if (!SendCommand(command
, '3')) {
113 command
.sprintf("PASS %s", (const char *)m_passwd
);
114 if (!SendCommand(command
, '2')) {
122 bool wxFTP::Connect(const wxString
& host
)
125 wxString my_host
= host
;
127 addr
.Hostname(my_host
);
130 return Connect(addr
);
136 m_lastError
= wxPROTO_STREAMING
;
140 SendCommand(wxString("QUIT"), '2');
141 return wxSocketClient::Close();
144 ////////////////////////////////////////////////////////////////
145 ////// wxFTP low-level methods /////////////////////////////////
146 ////////////////////////////////////////////////////////////////
147 bool wxFTP::SendCommand(const wxString
& command
, char exp_ret
)
152 m_lastError
= wxPROTO_STREAMING
;
155 tmp_str
= command
+ "\r\n";
156 if (Write((char *)tmp_str
.GetData(), tmp_str
.Length()).Error()) {
157 m_lastError
= wxPROTO_NETERR
;
160 return GetResult(exp_ret
);
163 bool wxFTP::GetResult(char exp
)
165 if ((m_lastError
= GetLine(this, m_lastResult
)))
167 if (m_lastResult
.GetChar(0) != exp
) {
168 m_lastError
= wxPROTO_PROTERR
;
172 if (m_lastResult
.GetChar(3) == '-') {
173 wxString key
= m_lastResult
.Left((size_t)3);
177 while (m_lastResult
.Index(key
) != 0) {
178 if ((m_lastError
= GetLine(this, m_lastResult
)))
185 ////////////////////////////////////////////////////////////////
186 ////// wxFTP low-level methods /////////////////////////////////
187 ////////////////////////////////////////////////////////////////
188 bool wxFTP::ChDir(const wxString
& dir
)
193 return SendCommand(str
, '2');
196 bool wxFTP::MkDir(const wxString
& dir
)
200 return SendCommand(str
, '2');
203 bool wxFTP::RmDir(const wxString
& dir
)
208 return SendCommand(str
, '2');
211 wxString
wxFTP::Pwd()
215 if (!SendCommand("PWD", '2'))
216 return wxString((char *)NULL
);
218 beg
= m_lastResult
.Find('\"',FALSE
);
219 end
= m_lastResult
.Find('\"',TRUE
);
221 return wxString(beg
+1, end
);
224 bool wxFTP::Rename(const wxString
& src
, const wxString
& dst
)
229 if (!SendCommand(str
, '3'))
233 return SendCommand(str
, '2');
236 bool wxFTP::RmFile(const wxString
& path
)
242 return SendCommand(str
, '2');
245 ////////////////////////////////////////////////////////////////
246 ////// wxFTP download*upload ///////////////////////////////////
247 ////////////////////////////////////////////////////////////////
249 class wxInputFTPStream
: public wxSocketInputStream
{
253 wxInputFTPStream(wxFTP
*ftp_clt
, wxSocketBase
*sock
)
254 : wxSocketInputStream(*sock
), m_ftp(ftp_clt
) {}
255 virtual ~wxInputFTPStream(void)
257 if (LastError() != wxStream_NOERROR
)
258 m_ftp
->GetResult('2');
265 class wxOutputFTPStream
: public wxSocketOutputStream
{
269 wxOutputFTPStream(wxFTP
*ftp_clt
, wxSocketBase
*sock
)
270 : wxSocketOutputStream(*sock
), m_ftp(ftp_clt
) {}
271 virtual ~wxOutputFTPStream(void)
273 if (LastError() != wxStream_NOERROR
)
274 m_ftp
->GetResult('2');
281 wxSocketClient
*wxFTP::GetPort()
284 wxSocketClient
*client
;
290 if (!SendCommand("PASV", '2'))
293 sin
.sa_family
= AF_INET
;
294 addr_pos
= m_lastResult
.Find('(');
295 if (addr_pos
== -1) {
296 m_lastError
= wxPROTO_PROTERR
;
299 straddr
= m_lastResult(addr_pos
+1, m_lastResult
.Length());
300 sscanf((const char *)straddr
,"%d,%d,%d,%d,%d,%d",&a
[2],&a
[3],&a
[4],&a
[5],&a
[0],&a
[1]);
301 sin
.sa_data
[2] = (char)a
[2];
302 sin
.sa_data
[3] = (char)a
[3];
303 sin
.sa_data
[4] = (char)a
[4];
304 sin
.sa_data
[5] = (char)a
[5];
305 sin
.sa_data
[0] = (char)a
[0];
306 sin
.sa_data
[1] = (char)a
[1];
308 addr
.Disassemble(&sin
, sizeof(sin
));
310 client
= m_handler
->CreateClient();
311 if (!client
->Connect(addr
)) {
315 client
->Notify(FALSE
);
320 bool wxFTP::Abort(void)
323 if (!SendCommand("ABOR", '4'))
325 return GetResult('2');
328 wxInputStream
*wxFTP::GetInputStream(const wxString
& path
)
332 if (!SendCommand("TYPE I", '2'))
335 wxSocketClient
*sock
= GetPort();
338 m_lastError
= wxPROTO_NETERR
;
342 tmp_str
= "RETR " + path
;
343 if (!SendCommand(tmp_str
, '1'))
346 return new wxInputFTPStream(this, sock
);
349 wxOutputStream
*wxFTP::GetOutputStream(const wxString
& path
)
353 if (!SendCommand("TYPE I", '2'))
356 wxSocketClient
*sock
= GetPort();
358 tmp_str
= "STOR " + path
;
359 if (!SendCommand(tmp_str
, '1'))
362 return new wxOutputFTPStream(this, sock
);
365 wxList
*wxFTP::GetList(const wxString
& wildcard
)
367 wxList
*file_list
= new wxList
;
368 wxSocketBase
*sock
= GetPort();
369 wxString tmp_str
= "NLST";
371 if (!wildcard
.IsNull())
374 if (!SendCommand(tmp_str
, '1')) {
380 while (GetLine(sock
, tmp_str
) == wxPROTO_NOERR
) {
381 file_list
->Append((wxObject
*)(new wxString(tmp_str
)));
384 if (!GetResult('2')) {
386 file_list
->DeleteContents(TRUE
);