]>
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"
28 #if defined(__WXMAC__)
29 #include "/wx/mac/macsock.h"
33 #include "wx/string.h"
35 // #include "wx/data.h"
36 #define WXSOCK_INTERNAL
37 #include "wx/sckaddr.h"
38 #undef WXSOCK_INTERNAL
39 #include "wx/socket.h"
41 #include "wx/sckstrm.h"
42 #include "wx/protocol/protocol.h"
43 #include "wx/protocol/ftp.h"
49 #define FTP_BSIZE 1024
51 #if !USE_SHARED_LIBRARY
52 IMPLEMENT_DYNAMIC_CLASS(wxFTP
, wxProtocol
)
53 IMPLEMENT_PROTOCOL(wxFTP
, _T("ftp"), _T("ftp"), TRUE
)
56 ////////////////////////////////////////////////////////////////
57 ////// wxFTP constructor and destructor ////////////////////////
58 ////////////////////////////////////////////////////////////////
63 m_lastError
= wxPROTO_NOERR
;
66 m_user
= _T("anonymous");
67 m_passwd
= wxGetUserId();
69 m_passwd
+= wxGetHostName();
76 SendCommand("QUIT", '2');
79 ////////////////////////////////////////////////////////////////
80 ////// wxFTP connect and login methods /////////////////////////
81 ////////////////////////////////////////////////////////////////
82 bool wxFTP::Connect(wxSockAddress
& addr
, bool WXUNUSED(wait
))
85 m_lastError
= wxPROTO_NOHNDLR
;
89 if (!wxProtocol::Connect(addr
)) {
90 m_lastError
= wxPROTO_NETERR
;
94 if (!m_user
|| !m_passwd
) {
95 m_lastError
= wxPROTO_CONNERR
;
101 if (!GetResult('2')) {
106 command
.sprintf(_T("USER %s"), (const wxChar
*)m_user
);
107 if (!SendCommand(command
, '3')) {
112 command
.sprintf(_T("PASS %s"), (const wxChar
*)m_passwd
);
113 if (!SendCommand(command
, '2')) {
121 bool wxFTP::Connect(const wxString
& host
)
124 wxString my_host
= host
;
126 addr
.Hostname(my_host
);
127 addr
.Service(_T("ftp"));
129 return Connect(addr
);
135 m_lastError
= wxPROTO_STREAMING
;
139 SendCommand(wxString(_T("QUIT")), '2');
140 return wxSocketClient::Close();
143 ////////////////////////////////////////////////////////////////
144 ////// wxFTP low-level methods /////////////////////////////////
145 ////////////////////////////////////////////////////////////////
146 bool wxFTP::SendCommand(const wxString
& command
, char exp_ret
)
151 m_lastError
= wxPROTO_STREAMING
;
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
;
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
)
192 str
.Prepend(_T("CWD "));
193 return SendCommand(str
, '2');
196 bool wxFTP::MkDir(const wxString
& dir
)
199 str
.Prepend(_T("MKD "));
200 return SendCommand(str
, '2');
203 bool wxFTP::RmDir(const wxString
& dir
)
207 str
.Prepend(_T("PWD "));
208 return SendCommand(str
, '2');
211 wxString
wxFTP::Pwd()
215 if (!SendCommand(_T("PWD"), '2'))
216 return wxString((char *)NULL
);
218 beg
= m_lastResult
.Find(_T('\"'),FALSE
);
219 end
= m_lastResult
.Find(_T('\"'),TRUE
);
221 return wxString(beg
+1, end
);
224 bool wxFTP::Rename(const wxString
& src
, const wxString
& dst
)
228 str
= _T("RNFR ") + src
;
229 if (!SendCommand(str
, '3'))
232 str
= _T("RNTO ") + dst
;
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
{
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)
259 if (LastError() != wxStream_NOERROR
)
260 m_ftp
->GetResult('2');
267 class wxOutputFTPStream
: public wxSocketOutputStream
{
271 wxOutputFTPStream(wxFTP
*ftp_clt
, wxSocketBase
*sock
)
272 : wxSocketOutputStream(*sock
), m_ftp(ftp_clt
) {}
273 virtual ~wxOutputFTPStream(void)
275 if (LastError() != wxStream_NOERROR
)
276 m_ftp
->GetResult('2');
283 wxSocketClient
*wxFTP::GetPort()
286 wxSocketClient
*client
;
292 if (!SendCommand(_T("PASV"), '2'))
295 sin
.sa_family
= AF_INET
;
296 addr_pos
= m_lastResult
.Find(_T('('));
297 if (addr_pos
== -1) {
298 m_lastError
= wxPROTO_PROTERR
;
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];
310 addr
.Disassemble(&sin
, sizeof(sin
));
312 client
= m_handler
->CreateClient();
313 if (!client
->Connect(addr
)) {
317 client
->Notify(FALSE
);
322 bool wxFTP::Abort(void)
325 if (!SendCommand(_T("ABOR"), '4'))
327 return GetResult('2');
330 wxInputStream
*wxFTP::GetInputStream(const wxString
& path
)
334 wxInputFTPStream
*in_stream
;
336 if (!SendCommand(_T("TYPE I"), '2'))
339 wxSocketClient
*sock
= GetPort();
342 m_lastError
= wxPROTO_NETERR
;
346 tmp_str
= _T("RETR ") + path
;
347 if (!SendCommand(tmp_str
, '1'))
350 in_stream
= new wxInputFTPStream(this, sock
);
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);
356 in_stream
->m_ftpsize
= wxAtoi(WXSTRINGCAST str_size
);
358 sock
->SetFlags(WAITALL
);
363 wxOutputStream
*wxFTP::GetOutputStream(const wxString
& path
)
367 if (!SendCommand(_T("TYPE I"), '2'))
370 wxSocketClient
*sock
= GetPort();
372 tmp_str
= _T("STOR ") + path
;
373 if (!SendCommand(tmp_str
, '1'))
376 return new wxOutputFTPStream(this, sock
);
379 wxList
*wxFTP::GetList(const wxString
& wildcard
)
381 wxList
*file_list
= new wxList
;
382 wxSocketBase
*sock
= GetPort();
383 wxString tmp_str
= _T("NLST");
385 if (!wildcard
.IsNull())
388 if (!SendCommand(tmp_str
, '1')) {
394 while (GetLine(sock
, tmp_str
) == wxPROTO_NOERR
) {
395 file_list
->Append((wxObject
*)(new wxString(tmp_str
)));
398 if (!GetResult('2')) {
400 file_list
->DeleteContents(TRUE
);