]>
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 IMPLEMENT_DYNAMIC_CLASS(wxFTP
, wxProtocol
)
52 IMPLEMENT_PROTOCOL(wxFTP
, wxT("ftp"), wxT("ftp"), TRUE
)
54 ////////////////////////////////////////////////////////////////
55 ////// wxFTP constructor and destructor ////////////////////////
56 ////////////////////////////////////////////////////////////////
61 m_lastError
= wxPROTO_NOERR
;
64 m_user
= wxT("anonymous");
65 m_passwd
= wxGetUserId();
67 m_passwd
+= wxGetHostName();
75 SendCommand("QUIT", '2');
78 ////////////////////////////////////////////////////////////////
79 ////// wxFTP connect and login methods /////////////////////////
80 ////////////////////////////////////////////////////////////////
81 bool wxFTP::Connect(wxSockAddress
& addr
, bool WXUNUSED(wait
))
83 if (!wxProtocol::Connect(addr
)) {
84 m_lastError
= wxPROTO_NETERR
;
88 if (!m_user
|| !m_passwd
) {
89 m_lastError
= wxPROTO_CONNERR
;
95 if (!GetResult('2')) {
100 command
.sprintf(wxT("USER %s"), (const wxChar
*)m_user
);
101 if (!SendCommand(command
, '3')) {
106 command
.sprintf(wxT("PASS %s"), (const wxChar
*)m_passwd
);
107 if (!SendCommand(command
, '2')) {
115 bool wxFTP::Connect(const wxString
& host
)
118 wxString my_host
= host
;
120 addr
.Hostname(my_host
);
121 addr
.Service(wxT("ftp"));
123 return Connect(addr
);
129 m_lastError
= wxPROTO_STREAMING
;
133 SendCommand(wxString(wxT("QUIT")), '2');
134 return wxSocketClient::Close();
137 ////////////////////////////////////////////////////////////////
138 ////// wxFTP low-level methods /////////////////////////////////
139 ////////////////////////////////////////////////////////////////
140 bool wxFTP::SendCommand(const wxString
& command
, char exp_ret
)
145 m_lastError
= wxPROTO_STREAMING
;
148 tmp_str
= command
+ wxT("\r\n");
149 const wxWX2MBbuf tmp_buf
= tmp_str
.mb_str();
150 if (Write(wxMBSTRINGCAST tmp_buf
, strlen(tmp_buf
)).Error()) {
151 m_lastError
= wxPROTO_NETERR
;
154 return GetResult(exp_ret
);
157 bool wxFTP::GetResult(char exp
)
159 m_lastError
= GetLine(this, m_lastResult
);
162 if (m_lastResult
.GetChar(0) != exp
) {
163 m_lastError
= wxPROTO_PROTERR
;
167 if (m_lastResult
.GetChar(3) == '-') {
168 wxString key
= m_lastResult
.Left((size_t)3);
172 while (m_lastResult
.Index(key
) != 0) {
173 m_lastError
= GetLine(this, m_lastResult
);
181 ////////////////////////////////////////////////////////////////
182 ////// wxFTP low-level methods /////////////////////////////////
183 ////////////////////////////////////////////////////////////////
184 bool wxFTP::ChDir(const wxString
& dir
)
188 str
.Prepend(wxT("CWD "));
189 return SendCommand(str
, '2');
192 bool wxFTP::MkDir(const wxString
& dir
)
195 str
.Prepend(wxT("MKD "));
196 return SendCommand(str
, '2');
199 bool wxFTP::RmDir(const wxString
& dir
)
203 str
.Prepend(wxT("PWD "));
204 return SendCommand(str
, '2');
207 wxString
wxFTP::Pwd()
211 if (!SendCommand(wxT("PWD"), '2'))
212 return wxString((char *)NULL
);
214 beg
= m_lastResult
.Find(wxT('\"'),FALSE
);
215 end
= m_lastResult
.Find(wxT('\"'),TRUE
);
217 return wxString(beg
+1, end
);
220 bool wxFTP::Rename(const wxString
& src
, const wxString
& dst
)
224 str
= wxT("RNFR ") + src
;
225 if (!SendCommand(str
, '3'))
228 str
= wxT("RNTO ") + dst
;
229 return SendCommand(str
, '2');
232 bool wxFTP::RmFile(const wxString
& path
)
238 return SendCommand(str
, '2');
241 ////////////////////////////////////////////////////////////////
242 ////// wxFTP download*upload ///////////////////////////////////
243 ////////////////////////////////////////////////////////////////
245 class wxInputFTPStream
: public wxSocketInputStream
{
250 wxInputFTPStream(wxFTP
*ftp_clt
, wxSocketBase
*sock
)
251 : wxSocketInputStream(*sock
), m_ftp(ftp_clt
) {}
252 size_t GetSize() const { return m_ftpsize
; }
253 virtual ~wxInputFTPStream(void)
255 if (LastError() == wxStream_NOERROR
)
256 m_ftp
->GetResult('2');
263 class wxOutputFTPStream
: public wxSocketOutputStream
{
267 wxOutputFTPStream(wxFTP
*ftp_clt
, wxSocketBase
*sock
)
268 : wxSocketOutputStream(*sock
), m_ftp(ftp_clt
) {}
269 virtual ~wxOutputFTPStream(void)
271 if (LastError() != wxStream_NOERROR
)
272 m_ftp
->GetResult('2');
279 wxSocketClient
*wxFTP::GetPort()
282 wxSocketClient
*client
;
289 if (!SendCommand(wxT("PASV"), '2'))
292 addr_pos
= m_lastResult
.Find(wxT('('));
293 if (addr_pos
== -1) {
294 m_lastError
= wxPROTO_PROTERR
;
297 straddr
= m_lastResult(addr_pos
+1, m_lastResult
.Length());
298 wxSscanf((const wxChar
*)straddr
,wxT("%d,%d,%d,%d,%d,%d"),&a
[2],&a
[3],&a
[4],&a
[5],&a
[0],&a
[1]);
300 hostaddr
= (wxUint16
)a
[5] << 24 | (wxUint16
)a
[4] << 16 |
301 (wxUint16
)a
[3] << 8 | a
[2];
302 addr
.Hostname(hostaddr
);
304 port
= (wxUint16
)a
[0] << 8 | a
[1];
307 client
= new wxSocketClient();
308 if (!client
->Connect(addr
)) {
312 client
->Notify(FALSE
);
317 bool wxFTP::Abort(void)
320 if (!SendCommand(wxT("ABOR"), '4'))
322 return GetResult('2');
325 wxInputStream
*wxFTP::GetInputStream(const wxString
& path
)
329 wxInputFTPStream
*in_stream
;
331 if (!SendCommand(wxT("TYPE I"), '2'))
334 wxSocketClient
*sock
= GetPort();
337 m_lastError
= wxPROTO_NETERR
;
341 tmp_str
= wxT("RETR ") + wxURL::ConvertFromURI(path
);
342 if (!SendCommand(tmp_str
, '1'))
345 in_stream
= new wxInputFTPStream(this, sock
);
347 pos_size
= m_lastResult
.Index(wxT('('));
348 if (pos_size
!= wxNOT_FOUND
) {
349 wxString str_size
= m_lastResult(pos_size
+1, m_lastResult
.Index(wxT(')'))-1);
351 in_stream
->m_ftpsize
= wxAtoi(WXSTRINGCAST str_size
);
353 sock
->SetFlags(WAITALL
);
358 wxOutputStream
*wxFTP::GetOutputStream(const wxString
& path
)
362 if (!SendCommand(wxT("TYPE I"), '2'))
365 wxSocketClient
*sock
= GetPort();
367 tmp_str
= wxT("STOR ") + path
;
368 if (!SendCommand(tmp_str
, '1'))
371 return new wxOutputFTPStream(this, sock
);
374 wxList
*wxFTP::GetList(const wxString
& wildcard
)
376 wxList
*file_list
= new wxList
;
377 wxSocketBase
*sock
= GetPort();
378 wxString tmp_str
= wxT("NLST");
380 if (!wildcard
.IsNull())
383 if (!SendCommand(tmp_str
, '1')) {
389 while (GetLine(sock
, tmp_str
) == wxPROTO_NOERR
) {
390 file_list
->Append((wxObject
*)(new wxString(tmp_str
)));
393 if (!GetResult('2')) {
395 file_list
->DeleteContents(TRUE
);