]>
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
, "ftp", "ftp", TRUE
)
56 ////////////////////////////////////////////////////////////////
57 ////// wxFTP constructor and destructor ////////////////////////
58 ////////////////////////////////////////////////////////////////
65 m_lastError
= wxPROTO_NOERR
;
69 wxGetUserName(tmp
, 256);
70 m_passwd
.sprintf("%s@",tmp
);
71 wxGetHostName(tmp
, 256);
79 SendCommand("QUIT", '2');
82 ////////////////////////////////////////////////////////////////
83 ////// wxFTP connect and login methods /////////////////////////
84 ////////////////////////////////////////////////////////////////
85 bool wxFTP::Connect(wxSockAddress
& addr
, bool WXUNUSED(wait
))
88 m_lastError
= wxPROTO_NOHNDLR
;
92 if (!wxProtocol::Connect(addr
)) {
93 m_lastError
= wxPROTO_NETERR
;
97 if (!m_user
|| !m_passwd
) {
98 m_lastError
= wxPROTO_CONNERR
;
104 if (!GetResult('2')) {
109 command
.sprintf("USER %s", (const char *)m_user
);
110 if (!SendCommand(command
, '3')) {
115 command
.sprintf("PASS %s", (const char *)m_passwd
);
116 if (!SendCommand(command
, '2')) {
124 bool wxFTP::Connect(const wxString
& host
)
127 wxString my_host
= host
;
129 addr
.Hostname(my_host
);
132 return Connect(addr
);
138 m_lastError
= wxPROTO_STREAMING
;
142 SendCommand(wxString("QUIT"), '2');
143 return wxSocketClient::Close();
146 ////////////////////////////////////////////////////////////////
147 ////// wxFTP low-level methods /////////////////////////////////
148 ////////////////////////////////////////////////////////////////
149 bool wxFTP::SendCommand(const wxString
& command
, char exp_ret
)
154 m_lastError
= wxPROTO_STREAMING
;
157 tmp_str
= command
+ "\r\n";
158 if (Write((char *)tmp_str
.GetData(), tmp_str
.Length()).Error()) {
159 m_lastError
= wxPROTO_NETERR
;
162 return GetResult(exp_ret
);
165 bool wxFTP::GetResult(char exp
)
167 if ((m_lastError
= GetLine(this, m_lastResult
)))
169 if (m_lastResult
.GetChar(0) != exp
) {
170 m_lastError
= wxPROTO_PROTERR
;
174 if (m_lastResult
.GetChar(3) == '-') {
175 wxString key
= m_lastResult
.Left((size_t)3);
179 while (m_lastResult
.Index(key
) != 0) {
180 if ((m_lastError
= GetLine(this, m_lastResult
)))
187 ////////////////////////////////////////////////////////////////
188 ////// wxFTP low-level methods /////////////////////////////////
189 ////////////////////////////////////////////////////////////////
190 bool wxFTP::ChDir(const wxString
& dir
)
195 return SendCommand(str
, '2');
198 bool wxFTP::MkDir(const wxString
& dir
)
202 return SendCommand(str
, '2');
205 bool wxFTP::RmDir(const wxString
& dir
)
210 return SendCommand(str
, '2');
213 wxString
wxFTP::Pwd()
217 if (!SendCommand("PWD", '2'))
218 return wxString((char *)NULL
);
220 beg
= m_lastResult
.Find('\"',FALSE
);
221 end
= m_lastResult
.Find('\"',TRUE
);
223 return wxString(beg
+1, end
);
226 bool wxFTP::Rename(const wxString
& src
, const wxString
& dst
)
231 if (!SendCommand(str
, '3'))
235 return SendCommand(str
, '2');
238 bool wxFTP::RmFile(const wxString
& path
)
244 return SendCommand(str
, '2');
247 ////////////////////////////////////////////////////////////////
248 ////// wxFTP download*upload ///////////////////////////////////
249 ////////////////////////////////////////////////////////////////
251 class wxInputFTPStream
: public wxSocketInputStream
{
256 wxInputFTPStream(wxFTP
*ftp_clt
, wxSocketBase
*sock
)
257 : wxSocketInputStream(*sock
), m_ftp(ftp_clt
) {}
258 size_t StreamSize() const { return m_ftpsize
; }
259 virtual ~wxInputFTPStream(void)
261 if (LastError() != wxStream_NOERROR
)
262 m_ftp
->GetResult('2');
269 class wxOutputFTPStream
: public wxSocketOutputStream
{
273 wxOutputFTPStream(wxFTP
*ftp_clt
, wxSocketBase
*sock
)
274 : wxSocketOutputStream(*sock
), m_ftp(ftp_clt
) {}
275 virtual ~wxOutputFTPStream(void)
277 if (LastError() != wxStream_NOERROR
)
278 m_ftp
->GetResult('2');
285 wxSocketClient
*wxFTP::GetPort()
288 wxSocketClient
*client
;
294 if (!SendCommand("PASV", '2'))
297 sin
.sa_family
= AF_INET
;
298 addr_pos
= m_lastResult
.Find('(');
299 if (addr_pos
== -1) {
300 m_lastError
= wxPROTO_PROTERR
;
303 straddr
= m_lastResult(addr_pos
+1, m_lastResult
.Length());
304 sscanf((const char *)straddr
,"%d,%d,%d,%d,%d,%d",&a
[2],&a
[3],&a
[4],&a
[5],&a
[0],&a
[1]);
305 sin
.sa_data
[2] = (char)a
[2];
306 sin
.sa_data
[3] = (char)a
[3];
307 sin
.sa_data
[4] = (char)a
[4];
308 sin
.sa_data
[5] = (char)a
[5];
309 sin
.sa_data
[0] = (char)a
[0];
310 sin
.sa_data
[1] = (char)a
[1];
312 addr
.Disassemble(&sin
, sizeof(sin
));
314 client
= m_handler
->CreateClient();
315 if (!client
->Connect(addr
)) {
319 client
->Notify(FALSE
);
324 bool wxFTP::Abort(void)
327 if (!SendCommand("ABOR", '4'))
329 return GetResult('2');
332 wxInputStream
*wxFTP::GetInputStream(const wxString
& path
)
336 wxInputFTPStream
*in_stream
;
338 if (!SendCommand("TYPE I", '2'))
341 wxSocketClient
*sock
= GetPort();
344 m_lastError
= wxPROTO_NETERR
;
348 tmp_str
= "RETR " + path
;
349 if (!SendCommand(tmp_str
, '1'))
352 in_stream
= new wxInputFTPStream(this, sock
);
354 pos_size
= m_lastResult
.Index('(');
355 if (pos_size
!= wxNOT_FOUND
) {
356 wxString str_size
= m_lastResult(pos_size
+1, m_lastResult
.Index(')')-1);
358 in_stream
->m_ftpsize
= atoi(WXSTRINGCAST str_size
);
364 wxOutputStream
*wxFTP::GetOutputStream(const wxString
& path
)
368 if (!SendCommand("TYPE I", '2'))
371 wxSocketClient
*sock
= GetPort();
373 tmp_str
= "STOR " + path
;
374 if (!SendCommand(tmp_str
, '1'))
377 return new wxOutputFTPStream(this, sock
);
380 wxList
*wxFTP::GetList(const wxString
& wildcard
)
382 wxList
*file_list
= new wxList
;
383 wxSocketBase
*sock
= GetPort();
384 wxString tmp_str
= "NLST";
386 if (!wildcard
.IsNull())
389 if (!SendCommand(tmp_str
, '1')) {
395 while (GetLine(sock
, tmp_str
) == wxPROTO_NOERR
) {
396 file_list
->Append((wxObject
*)(new wxString(tmp_str
)));
399 if (!GetResult('2')) {
401 file_list
->DeleteContents(TRUE
);
406 sock
->SetEventHandler(*GetNextHandler(), m_id
);
407 sock
->Notify(m_notifyme
);
408 sock
->SetNotify(m_neededreq
);