]>
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"
27 #include "wx/string.h"
29 // #include "wx/data.h"
30 #define WXSOCK_INTERNAL
31 #include "wx/sckaddr.h"
32 #undef WXSOCK_INTERNAL
33 #include "wx/socket.h"
35 #include "wx/sckstrm.h"
36 #include "wx/protocol/protocol.h"
37 #include "wx/protocol/ftp.h"
43 #define FTP_BSIZE 1024
45 #if !USE_SHARED_LIBRARY
46 IMPLEMENT_DYNAMIC_CLASS(wxFTP
, wxProtocol
)
47 IMPLEMENT_PROTOCOL(wxFTP
, "ftp", "ftp", TRUE
)
50 ////////////////////////////////////////////////////////////////
51 ////// wxFTP constructor and destructor ////////////////////////
52 ////////////////////////////////////////////////////////////////
59 m_lastError
= wxPROTO_NOERR
;
63 wxGetUserName(tmp
, 256);
64 m_passwd
.sprintf("%s@",tmp
);
65 wxGetHostName(tmp
, 256);
73 SendCommand("QUIT", '2');
76 ////////////////////////////////////////////////////////////////
77 ////// wxFTP connect and login methods /////////////////////////
78 ////////////////////////////////////////////////////////////////
79 bool wxFTP::Connect(wxSockAddress
& addr
)
82 m_lastError
= wxPROTO_NOHNDLR
;
86 if (!wxProtocol::Connect(addr
)) {
87 m_lastError
= wxPROTO_NETERR
;
91 if (!m_user
|| !m_passwd
) {
92 m_lastError
= wxPROTO_CONNERR
;
98 if (!GetResult('2')) {
103 command
.sprintf("USER %s", (const char *)m_user
);
104 if (!SendCommand(command
, '3')) {
109 command
.sprintf("PASS %s", (const char *)m_passwd
);
110 if (!SendCommand(command
, '2')) {
118 bool wxFTP::Connect(const wxString
& host
)
121 wxString my_host
= host
;
123 addr
.Hostname(my_host
);
126 return Connect(addr
);
132 m_lastError
= wxPROTO_STREAMING
;
136 SendCommand(wxString("QUIT"), '2');
137 return wxSocketClient::Close();
140 ////////////////////////////////////////////////////////////////
141 ////// wxFTP low-level methods /////////////////////////////////
142 ////////////////////////////////////////////////////////////////
143 bool wxFTP::SendCommand(const wxString
& command
, char exp_ret
)
148 m_lastError
= wxPROTO_STREAMING
;
151 tmp_str
= command
+ "\r\n";
152 if (Write((char *)tmp_str
.GetData(), tmp_str
.Length()).Error()) {
153 m_lastError
= wxPROTO_NETERR
;
156 return GetResult(exp_ret
);
159 bool wxFTP::GetResult(char exp
)
161 if ((m_lastError
= GetLine(this, m_lastResult
)))
163 if (m_lastResult
.GetChar(0) != exp
) {
164 m_lastError
= wxPROTO_PROTERR
;
168 if (m_lastResult
.GetChar(3) == '-') {
169 wxString key
= m_lastResult
.Left((size_t)3);
173 while (m_lastResult
.Index(key
) != 0) {
174 if ((m_lastError
= GetLine(this, m_lastResult
)))
181 ////////////////////////////////////////////////////////////////
182 ////// wxFTP low-level methods /////////////////////////////////
183 ////////////////////////////////////////////////////////////////
184 bool wxFTP::ChDir(const wxString
& dir
)
189 return SendCommand(str
, '2');
192 bool wxFTP::MkDir(const wxString
& dir
)
196 return SendCommand(str
, '2');
199 bool wxFTP::RmDir(const wxString
& dir
)
204 return SendCommand(str
, '2');
207 wxString
wxFTP::Pwd()
211 if (!SendCommand("PWD", '2'))
212 return wxString((char *)NULL
);
214 beg
= m_lastResult
.Find('\"',FALSE
);
215 end
= m_lastResult
.Find('\"',TRUE
);
217 return wxString(beg
+1, end
);
220 bool wxFTP::Rename(const wxString
& src
, const wxString
& dst
)
225 if (!SendCommand(str
, '3'))
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
{
249 wxInputFTPStream(wxFTP
*ftp_clt
, wxSocketBase
*sock
)
250 : wxSocketInputStream(*sock
), m_ftp(ftp_clt
) {}
251 virtual ~wxInputFTPStream(void)
253 if (LastError() != wxStream_NOERROR
)
254 m_ftp
->GetResult('2');
261 class wxOutputFTPStream
: public wxSocketOutputStream
{
265 wxOutputFTPStream(wxFTP
*ftp_clt
, wxSocketBase
*sock
)
266 : wxSocketOutputStream(*sock
), m_ftp(ftp_clt
) {}
267 virtual ~wxOutputFTPStream(void)
269 if (LastError() != wxStream_NOERROR
)
270 m_ftp
->GetResult('2');
277 wxSocketClient
*wxFTP::GetPort()
280 wxSocketClient
*client
;
286 if (!SendCommand("PASV", '2'))
289 sin
.sa_family
= AF_INET
;
290 addr_pos
= m_lastResult
.Find('(');
291 if (addr_pos
== -1) {
292 m_lastError
= wxPROTO_PROTERR
;
295 straddr
= m_lastResult(addr_pos
+1, m_lastResult
.Length());
296 sscanf((const char *)straddr
,"%d,%d,%d,%d,%d,%d",&a
[2],&a
[3],&a
[4],&a
[5],&a
[0],&a
[1]);
297 sin
.sa_data
[2] = (char)a
[2];
298 sin
.sa_data
[3] = (char)a
[3];
299 sin
.sa_data
[4] = (char)a
[4];
300 sin
.sa_data
[5] = (char)a
[5];
301 sin
.sa_data
[0] = (char)a
[0];
302 sin
.sa_data
[1] = (char)a
[1];
304 addr
.Disassemble(&sin
, sizeof(sin
));
306 client
= m_handler
->CreateClient();
307 if (!client
->Connect(addr
)) {
311 client
->Notify(FALSE
);
316 bool wxFTP::Abort(void)
319 if (!SendCommand("ABOR", '4'))
321 return GetResult('2');
324 wxInputStream
*wxFTP::GetInputStream(const wxString
& path
)
328 if (!SendCommand("TYPE I", '2'))
331 wxSocketClient
*sock
= GetPort();
334 m_lastError
= wxPROTO_NETERR
;
338 tmp_str
= "RETR " + path
;
339 if (!SendCommand(tmp_str
, '1'))
342 return new wxInputFTPStream(this, sock
);
345 wxOutputStream
*wxFTP::GetOutputStream(const wxString
& path
)
349 if (!SendCommand("TYPE I", '2'))
352 wxSocketClient
*sock
= GetPort();
354 tmp_str
= "STOR " + path
;
355 if (!SendCommand(tmp_str
, '1'))
358 return new wxOutputFTPStream(this, sock
);
361 wxList
*wxFTP::GetList(const wxString
& wildcard
)
363 wxList
*file_list
= new wxList
;
364 wxSocketBase
*sock
= GetPort();
365 wxString tmp_str
= "NLST";
367 if (!wildcard
.IsNull())
370 if (!SendCommand(tmp_str
, '1')) {
376 while (GetLine(sock
, tmp_str
) == wxPROTO_NOERR
) {
377 file_list
->Append((wxObject
*)(new wxString(tmp_str
)));
380 if (!GetResult('2')) {
382 file_list
->DeleteContents(TRUE
);