]>
git.saurik.com Git - wxWidgets.git/blob - src/common/ftp.cpp
62161ada7aa6b8a90c60954f3f6b94f3b238fb31
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"
21 #include "wx/string.h"
23 // #include "wx/data.h"
24 #define WXSOCK_INTERNAL
25 #include "wx/sckaddr.h"
26 #undef WXSOCK_INTERNAL
27 #include "wx/socket.h"
29 #include "wx/sckstrm.h"
30 #include "wx/protocol/protocol.h"
31 #include "wx/protocol/ftp.h"
37 #define FTP_BSIZE 1024
39 #if !USE_SHARED_LIBRARY
40 IMPLEMENT_DYNAMIC_CLASS(wxFTP
, wxProtocol
)
41 IMPLEMENT_PROTOCOL(wxFTP
, "ftp", "ftp", TRUE
)
44 ////////////////////////////////////////////////////////////////
45 ////// wxFTP constructor and destructor ////////////////////////
46 ////////////////////////////////////////////////////////////////
53 m_lastError
= wxPROTO_NOERR
;
57 wxGetUserName(tmp
, 256);
58 m_passwd
.sprintf("%s@",tmp
);
59 wxGetHostName(tmp
, 256);
67 SendCommand("QUIT", '2');
70 ////////////////////////////////////////////////////////////////
71 ////// wxFTP connect and login methods /////////////////////////
72 ////////////////////////////////////////////////////////////////
73 bool wxFTP::Connect(wxSockAddress
& addr
)
76 m_lastError
= wxPROTO_NOHNDLR
;
80 if (!wxProtocol::Connect(addr
)) {
81 m_lastError
= wxPROTO_NETERR
;
85 if (!m_user
|| !m_passwd
) {
86 m_lastError
= wxPROTO_CONNERR
;
92 if (!GetResult('2')) {
97 command
.sprintf("USER %s", (const char *)m_user
);
98 if (!SendCommand(command
, '3')) {
103 command
.sprintf("PASS %s", (const char *)m_passwd
);
104 if (!SendCommand(command
, '2')) {
112 bool wxFTP::Connect(const wxString
& host
)
115 wxString my_host
= host
;
117 addr
.Hostname(my_host
);
120 return Connect(addr
);
126 m_lastError
= wxPROTO_STREAMING
;
130 SendCommand(wxString("QUIT"), '2');
131 return wxSocketClient::Close();
134 ////////////////////////////////////////////////////////////////
135 ////// wxFTP low-level methods /////////////////////////////////
136 ////////////////////////////////////////////////////////////////
137 bool wxFTP::SendCommand(const wxString
& command
, char exp_ret
)
142 m_lastError
= wxPROTO_STREAMING
;
145 tmp_str
= command
+ "\r\n";
146 if (Write((char *)tmp_str
.GetData(), tmp_str
.Length()).Error()) {
147 m_lastError
= wxPROTO_NETERR
;
150 return GetResult(exp_ret
);
153 bool wxFTP::GetResult(char exp
)
155 if ((m_lastError
= GetLine(this, m_lastResult
)))
157 if (m_lastResult
[0] != exp
) {
158 m_lastError
= wxPROTO_PROTERR
;
162 if (m_lastResult
[3] == '-') {
163 wxString key
= m_lastResult
.Left((size_t)3);
167 while (m_lastResult
.Index(key
) != 0) {
168 if ((m_lastError
= GetLine(this, m_lastResult
)))
175 ////////////////////////////////////////////////////////////////
176 ////// wxFTP low-level methods /////////////////////////////////
177 ////////////////////////////////////////////////////////////////
178 bool wxFTP::ChDir(const wxString
& dir
)
183 return SendCommand(str
, '2');
186 bool wxFTP::MkDir(const wxString
& dir
)
190 return SendCommand(str
, '2');
193 bool wxFTP::RmDir(const wxString
& dir
)
198 return SendCommand(str
, '2');
201 wxString
wxFTP::Pwd()
205 if (!SendCommand("PWD", '2'))
206 return wxString((char *)NULL
);
208 beg
= m_lastResult
.Find('\"',FALSE
);
209 end
= m_lastResult
.Find('\"',TRUE
);
211 return wxString(beg
+1, end
);
214 bool wxFTP::Rename(const wxString
& src
, const wxString
& dst
)
219 if (!SendCommand(str
, '3'))
223 return SendCommand(str
, '2');
226 bool wxFTP::RmFile(const wxString
& path
)
232 return SendCommand(str
, '2');
235 ////////////////////////////////////////////////////////////////
236 ////// wxFTP download*upload ///////////////////////////////////
237 ////////////////////////////////////////////////////////////////
239 class wxInputFTPStream
: public wxSocketInputStream
{
243 wxInputFTPStream(wxFTP
*ftp_clt
, wxSocketBase
*sock
)
244 : wxSocketInputStream(*sock
), m_ftp(ftp_clt
) {}
245 virtual ~wxInputFTPStream(void)
247 if (LastError() != wxStream_NOERROR
)
248 m_ftp
->GetResult('2');
255 class wxOutputFTPStream
: public wxSocketOutputStream
{
259 wxOutputFTPStream(wxFTP
*ftp_clt
, wxSocketBase
*sock
)
260 : wxSocketOutputStream(*sock
), m_ftp(ftp_clt
) {}
261 virtual ~wxOutputFTPStream(void)
263 if (LastError() != wxStream_NOERROR
)
264 m_ftp
->GetResult('2');
271 wxSocketClient
*wxFTP::GetPort()
274 wxSocketClient
*client
;
280 if (!SendCommand("PASV", '2'))
283 sin
.sa_family
= AF_INET
;
284 addr_pos
= m_lastResult
.Find('(');
285 if (addr_pos
== -1) {
286 m_lastError
= wxPROTO_PROTERR
;
289 straddr
= m_lastResult(addr_pos
+1, m_lastResult
.Length());
290 sscanf((const char *)straddr
,"%d,%d,%d,%d,%d,%d",&a
[2],&a
[3],&a
[4],&a
[5],&a
[0],&a
[1]);
291 sin
.sa_data
[2] = (char)a
[2];
292 sin
.sa_data
[3] = (char)a
[3];
293 sin
.sa_data
[4] = (char)a
[4];
294 sin
.sa_data
[5] = (char)a
[5];
295 sin
.sa_data
[0] = (char)a
[0];
296 sin
.sa_data
[1] = (char)a
[1];
298 addr
.Disassemble(&sin
, sizeof(sin
));
300 client
= m_handler
->CreateClient();
301 if (!client
->Connect(addr
)) {
305 client
->Notify(FALSE
);
310 bool wxFTP::Abort(void)
313 if (!SendCommand("ABOR", '4'))
315 return GetResult('2');
318 wxInputStream
*wxFTP::GetInputStream(const wxString
& path
)
322 if (!SendCommand("TYPE I", '2'))
325 wxSocketClient
*sock
= GetPort();
328 m_lastError
= wxPROTO_NETERR
;
332 tmp_str
= "RETR " + path
;
333 if (!SendCommand(tmp_str
, '1'))
336 return new wxInputFTPStream(this, sock
);
339 wxOutputStream
*wxFTP::GetOutputStream(const wxString
& path
)
343 if (!SendCommand("TYPE I", '2'))
346 wxSocketClient
*sock
= GetPort();
348 tmp_str
= "STOR " + path
;
349 if (!SendCommand(tmp_str
, '1'))
352 return new wxOutputFTPStream(this, sock
);
355 wxList
*wxFTP::GetList(const wxString
& wildcard
)
357 wxList
*file_list
= new wxList
;
358 wxSocketBase
*sock
= GetPort();
359 wxString tmp_str
= "NLST";
361 if (!wildcard
.IsNull())
364 if (!SendCommand(tmp_str
, '1')) {
370 while (GetLine(sock
, tmp_str
) == wxPROTO_NOERR
) {
371 file_list
->Append((wxObject
*)(new wxString(tmp_str
)));
374 if (!GetResult('2')) {
376 file_list
->DeleteContents(TRUE
);