]>
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();
77 SendCommand("QUIT", '2');
80 ////////////////////////////////////////////////////////////////
81 ////// wxFTP connect and login methods /////////////////////////
82 ////////////////////////////////////////////////////////////////
83 bool wxFTP::Connect(wxSockAddress
& addr
, bool WXUNUSED(wait
))
85 if (!wxProtocol::Connect(addr
)) {
86 m_lastError
= wxPROTO_NETERR
;
90 if (!m_user
|| !m_passwd
) {
91 m_lastError
= wxPROTO_CONNERR
;
97 if (!GetResult('2')) {
102 command
.sprintf(_T("USER %s"), (const wxChar
*)m_user
);
103 if (!SendCommand(command
, '3')) {
108 command
.sprintf(_T("PASS %s"), (const wxChar
*)m_passwd
);
109 if (!SendCommand(command
, '2')) {
117 bool wxFTP::Connect(const wxString
& host
)
120 wxString my_host
= host
;
122 addr
.Hostname(my_host
);
123 addr
.Service(_T("ftp"));
125 return Connect(addr
);
131 m_lastError
= wxPROTO_STREAMING
;
135 SendCommand(wxString(_T("QUIT")), '2');
136 return wxSocketClient::Close();
139 ////////////////////////////////////////////////////////////////
140 ////// wxFTP low-level methods /////////////////////////////////
141 ////////////////////////////////////////////////////////////////
142 bool wxFTP::SendCommand(const wxString
& command
, char exp_ret
)
147 m_lastError
= wxPROTO_STREAMING
;
150 tmp_str
= command
+ _T("\r\n");
151 const wxWX2MBbuf tmp_buf
= tmp_str
.mb_str();
152 if (Write(MBSTRINGCAST tmp_buf
, strlen(tmp_buf
)).Error()) {
153 m_lastError
= wxPROTO_NETERR
;
156 return GetResult(exp_ret
);
159 bool wxFTP::GetResult(char exp
)
161 m_lastError
= GetLine(this, m_lastResult
);
164 if (m_lastResult
.GetChar(0) != exp
) {
165 m_lastError
= wxPROTO_PROTERR
;
169 if (m_lastResult
.GetChar(3) == '-') {
170 wxString key
= m_lastResult
.Left((size_t)3);
174 while (m_lastResult
.Index(key
) != 0) {
175 m_lastError
= GetLine(this, m_lastResult
);
183 ////////////////////////////////////////////////////////////////
184 ////// wxFTP low-level methods /////////////////////////////////
185 ////////////////////////////////////////////////////////////////
186 bool wxFTP::ChDir(const wxString
& dir
)
190 str
.Prepend(_T("CWD "));
191 return SendCommand(str
, '2');
194 bool wxFTP::MkDir(const wxString
& dir
)
197 str
.Prepend(_T("MKD "));
198 return SendCommand(str
, '2');
201 bool wxFTP::RmDir(const wxString
& dir
)
205 str
.Prepend(_T("PWD "));
206 return SendCommand(str
, '2');
209 wxString
wxFTP::Pwd()
213 if (!SendCommand(_T("PWD"), '2'))
214 return wxString((char *)NULL
);
216 beg
= m_lastResult
.Find(_T('\"'),FALSE
);
217 end
= m_lastResult
.Find(_T('\"'),TRUE
);
219 return wxString(beg
+1, end
);
222 bool wxFTP::Rename(const wxString
& src
, const wxString
& dst
)
226 str
= _T("RNFR ") + src
;
227 if (!SendCommand(str
, '3'))
230 str
= _T("RNTO ") + dst
;
231 return SendCommand(str
, '2');
234 bool wxFTP::RmFile(const wxString
& path
)
240 return SendCommand(str
, '2');
243 ////////////////////////////////////////////////////////////////
244 ////// wxFTP download*upload ///////////////////////////////////
245 ////////////////////////////////////////////////////////////////
247 class wxInputFTPStream
: public wxSocketInputStream
{
252 wxInputFTPStream(wxFTP
*ftp_clt
, wxSocketBase
*sock
)
253 : wxSocketInputStream(*sock
), m_ftp(ftp_clt
) {}
254 size_t GetSize() const { return m_ftpsize
; }
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
;
291 if (!SendCommand(_T("PASV"), '2'))
294 addr_pos
= m_lastResult
.Find(_T('('));
295 if (addr_pos
== -1) {
296 m_lastError
= wxPROTO_PROTERR
;
299 straddr
= m_lastResult(addr_pos
+1, m_lastResult
.Length());
300 wxSscanf((const wxChar
*)straddr
,_T("%d,%d,%d,%d,%d,%d"),&a
[2],&a
[3],&a
[4],&a
[5],&a
[0],&a
[1]);
302 hostaddr
= (wxUint16
)a
[5] << 24 | (wxUint16
)a
[4] << 16 |
303 (wxUint16
)a
[3] << 8 | a
[2];
304 addr
.Hostname(hostaddr
);
306 port
= (wxUint16
)a
[0] << 8 | a
[1];
309 client
= new wxSocketClient();
310 if (!client
->Connect(addr
)) {
314 client
->Notify(FALSE
);
319 bool wxFTP::Abort(void)
322 if (!SendCommand(_T("ABOR"), '4'))
324 return GetResult('2');
327 wxInputStream
*wxFTP::GetInputStream(const wxString
& path
)
331 wxInputFTPStream
*in_stream
;
333 if (!SendCommand(_T("TYPE I"), '2'))
336 wxSocketClient
*sock
= GetPort();
339 m_lastError
= wxPROTO_NETERR
;
343 tmp_str
= _T("RETR ") + path
;
344 if (!SendCommand(tmp_str
, '1'))
347 in_stream
= new wxInputFTPStream(this, sock
);
349 pos_size
= m_lastResult
.Index(_T('('));
350 if (pos_size
!= wxNOT_FOUND
) {
351 wxString str_size
= m_lastResult(pos_size
+1, m_lastResult
.Index(_T(')'))-1);
353 in_stream
->m_ftpsize
= wxAtoi(WXSTRINGCAST str_size
);
355 sock
->SetFlags(WAITALL
);
360 wxOutputStream
*wxFTP::GetOutputStream(const wxString
& path
)
364 if (!SendCommand(_T("TYPE I"), '2'))
367 wxSocketClient
*sock
= GetPort();
369 tmp_str
= _T("STOR ") + path
;
370 if (!SendCommand(tmp_str
, '1'))
373 return new wxOutputFTPStream(this, sock
);
376 wxList
*wxFTP::GetList(const wxString
& wildcard
)
378 wxList
*file_list
= new wxList
;
379 wxSocketBase
*sock
= GetPort();
380 wxString tmp_str
= _T("NLST");
382 if (!wildcard
.IsNull())
385 if (!SendCommand(tmp_str
, '1')) {
391 while (GetLine(sock
, tmp_str
) == wxPROTO_NOERR
) {
392 file_list
->Append((wxObject
*)(new wxString(tmp_str
)));
395 if (!GetResult('2')) {
397 file_list
->DeleteContents(TRUE
);