tons of wxFTP fixes and compilation fix for wxBase

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@8955 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin 2000-12-19 16:56:27 +00:00
parent dda88f5eee
commit 2e907fab47
5 changed files with 587 additions and 298 deletions

View File

@ -74,6 +74,8 @@ enum wxFontEncoding
// types // types
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
#if wxUSE_GUI
// This private structure specifies all the parameters needed to create a font // This private structure specifies all the parameters needed to create a font
// with the given encoding on this platform. // with the given encoding on this platform.
// //
@ -110,4 +112,6 @@ struct WXDLLEXPORT wxNativeEncodingInfo
wxString ToString() const; wxString ToString() const;
}; };
#endif // wxUSE_GUI
#endif // _WX_FONTENC_H_ #endif // _WX_FONTENC_H_

View File

@ -2,7 +2,8 @@
// Name: ftp.h // Name: ftp.h
// Purpose: FTP protocol // Purpose: FTP protocol
// Author: Vadim Zeitlin // Author: Vadim Zeitlin
// Modified by: // Modified by: Mark Johnson, wxWindows@mj10777.de
// 20000917 : RmDir, GetLastResult, GetList
// Created: 07/07/1997 // Created: 07/07/1997
// RCS-ID: $Id$ // RCS-ID: $Id$
// Copyright: (c) 1997, 1998 Guilhem Lavaux // Copyright: (c) 1997, 1998 Guilhem Lavaux
@ -24,58 +25,124 @@
class WXDLLEXPORT wxFTP : public wxProtocol class WXDLLEXPORT wxFTP : public wxProtocol
{ {
public: public:
typedef enum { ASCII, BINARY } wxFTPmode; enum TransferMode
{
ASCII,
BINARY
};
wxFTP(); wxFTP();
virtual ~wxFTP(); virtual ~wxFTP();
bool Connect(wxSockAddress& addr, bool wait = TRUE); // Connecting and disconnecting
bool Connect(const wxString& host); void SetUser(const wxString& user) { m_user = user; }
void SetPassword(const wxString& passwd) { m_passwd = passwd; }
// close the connection bool Connect(wxSockAddress& addr, bool wait = TRUE);
virtual bool Close(); bool Connect(const wxString& host);
void SetUser(const wxString& user) { m_user = user; } // disconnect
void SetPassword(const wxString& passwd) { m_passwd = passwd; } virtual bool Close();
// Filesystem commands // Parameters set up
bool ChDir(const wxString& dir);
bool MkDir(const wxString& dir);
bool RmDir(const wxString& dir);
wxString Pwd();
bool Rename(const wxString& src, const wxString& dst);
bool RmFile(const wxString& path);
// Download methods // set transfer mode now
bool Abort(); bool SetBinary() { return SetTransferMode(BINARY); }
wxInputStream *GetInputStream(const wxString& path); bool SetAscii() { return SetTransferMode(ASCII); }
wxOutputStream *GetOutputStream(const wxString& path); bool SetTransferMode(TransferMode mode);
// List method // Generic FTP interface
bool GetList(wxArrayString& files, const wxString& wildcard = wxEmptyString);
// Low-level methods // the error code
bool SendCommand(const wxString& command, char exp_ret); virtual wxProtocolError GetError() { return m_lastError; }
virtual wxProtocolError GetError() { return m_lastError; }
const wxString& GetLastResult(); // Get the complete return
// deprecated // the last FTP server reply
wxList *GetList(const wxString& wildcard); const wxString& GetLastResult() { return m_lastResult; }
// send any FTP command (should be full FTP command line but without
// trailing "\r\n") and return its return code
char SendCommand(const wxString& command);
// check that the command returned the given code
bool CheckCommand(const wxString& command, char expectedReturn)
{
return SendCommand(command) == expectedReturn;
}
// Filesystem commands
bool ChDir(const wxString& dir);
bool MkDir(const wxString& dir);
bool RmDir(const wxString& dir);
wxString Pwd();
bool Rename(const wxString& src, const wxString& dst);
bool RmFile(const wxString& path);
// Download methods
bool Abort();
virtual wxInputStream *GetInputStream(const wxString& path);
virtual wxOutputStream *GetOutputStream(const wxString& path);
// Directory listing
// get the list of full filenames, the format is fixed: one file name per
// line
bool GetFilesList(wxArrayString& files,
const wxString& wildcard = wxEmptyString)
{
return GetList(files, wildcard, FALSE);
}
// get a directory list in server dependent format - this can be shown
// directly to the user
bool GetDirList(wxArrayString& files,
const wxString& wildcard = wxEmptyString)
{
return GetList(files, wildcard, TRUE);
}
// equivalent to either GetFilesList() (default) or GetDirList()
bool GetList(wxArrayString& files,
const wxString& wildcard = wxEmptyString,
bool details = FALSE);
#ifdef WXWIN_COMPATIBILITY_2
// deprecated
wxList *GetList(const wxString& wildcard, bool details = FALSE);
#endif // WXWIN_COMPATIBILITY_2
protected: protected:
wxString m_user, m_passwd; // this executes a simple ftp command with the given argument and returns
wxString m_lastResult; // TRUE if it its return code starts with '2'
wxProtocolError m_lastError; bool DoSimpleCommand(const wxChar *command,
bool m_streaming; const wxString& arg = wxEmptyString);
friend class wxInputFTPStream; // get the server reply, return the first character of the reply code,
friend class wxOutputFTPStream; // '1'..'5' for normal FTP replies, 0 (*not* '0') if an error occured
char GetResult();
wxSocketClient *GetPort(); // check that the result is equal to expected value
bool GetResult(char exp); bool CheckResult(char ch) { return GetResult() == ch; }
DECLARE_DYNAMIC_CLASS(wxFTP) wxSocketClient *GetPort();
DECLARE_PROTOCOL(wxFTP)
wxString m_user,
m_passwd;
wxString m_lastResult;
wxProtocolError m_lastError;
// true if there is an FTP transfer going on
bool m_streaming;
// true if the user had set the transfer mode
bool m_modeSet;
friend class wxInputFTPStream;
friend class wxOutputFTPStream;
DECLARE_DYNAMIC_CLASS(wxFTP)
DECLARE_PROTOCOL(wxFTP)
}; };
#endif // __WX_FTP_H__ #endif // __WX_FTP_H__

View File

@ -44,11 +44,12 @@
//#define TEST_EXECUTE //#define TEST_EXECUTE
//#define TEST_FILE //#define TEST_FILE
//#define TEST_FILECONF //#define TEST_FILECONF
#define TEST_FTP
//#define TEST_HASH //#define TEST_HASH
//#define TEST_LIST //#define TEST_LIST
//#define TEST_LOG //#define TEST_LOG
//#define TEST_LONGLONG //#define TEST_LONGLONG
#define TEST_MIME //#define TEST_MIME
//#define TEST_INFO_FUNCTIONS //#define TEST_INFO_FUNCTIONS
//#define TEST_REGISTRY //#define TEST_REGISTRY
//#define TEST_SOCKETS //#define TEST_SOCKETS
@ -1241,7 +1242,6 @@ static void TestRegistryAssociation()
#include <wx/socket.h> #include <wx/socket.h>
#include <wx/protocol/protocol.h> #include <wx/protocol/protocol.h>
#include <wx/protocol/ftp.h>
#include <wx/protocol/http.h> #include <wx/protocol/http.h>
static void TestSocketServer() static void TestSocketServer()
@ -1364,17 +1364,33 @@ static void TestSocketClient()
} }
} }
#endif // TEST_SOCKETS
#ifdef TEST_FTP
#include <wx/protocol/ftp.h>
static void TestProtocolFtp() static void TestProtocolFtp()
{ {
puts("*** Testing wxFTP download ***\n"); puts("*** Testing wxFTP download ***\n");
wxLog::AddTraceMask(_T("ftp"));
static const char *hostname = "ftp.wxwindows.org";
printf("--- Attempting to connect to %s:21...\n", hostname);
wxFTP ftp; wxFTP ftp;
#if 1
static const char *hostname = "ftp.wxwindows.org";
static const char *directory = "pub";
printf("--- Attempting to connect to %s:21 anonymously...\n", hostname);
#else
static const char *hostname = "localhost";
static const char *user = "zeitlin";
static const char *directory = "/tmp";
ftp.SetUser(user);
ftp.SetPassword("password");
printf("--- Attempting to connect to %s:21 as %s...\n", hostname, user);
#endif
if ( !ftp.Connect(hostname) ) if ( !ftp.Connect(hostname) )
{ {
printf("ERROR: failed to connect to %s\n", hostname); printf("ERROR: failed to connect to %s\n", hostname);
@ -1383,19 +1399,37 @@ static void TestProtocolFtp()
{ {
printf("--- Connected to %s, current directory is '%s'\n", printf("--- Connected to %s, current directory is '%s'\n",
hostname, ftp.Pwd().c_str()); hostname, ftp.Pwd().c_str());
if ( !ftp.ChDir(_T("pub")) )
// test CWD
if ( !ftp.ChDir(directory) )
{ {
puts("ERROR: failed to cd to pub"); printf("ERROR: failed to cd to %s\n", directory);
} }
// test NLIST and LIST
wxArrayString files; wxArrayString files;
if ( !ftp.GetList(files) ) if ( !ftp.GetFilesList(files) )
{ {
puts("ERROR: failed to get list of files"); puts("ERROR: failed to get NLIST of files");
} }
else else
{ {
printf("List of files under '%s':\n", ftp.Pwd().c_str()); printf("Brief list of files under '%s':\n", ftp.Pwd().c_str());
size_t count = files.GetCount();
for ( size_t n = 0; n < count; n++ )
{
printf("\t%s\n", files[n].c_str());
}
puts("End of the file list");
}
if ( !ftp.GetDirList(files) )
{
puts("ERROR: failed to get LIST of files");
}
else
{
printf("Detailed list of files under '%s':\n", ftp.Pwd().c_str());
size_t count = files.GetCount(); size_t count = files.GetCount();
for ( size_t n = 0; n < count; n++ ) for ( size_t n = 0; n < count; n++ )
{ {
@ -1409,11 +1443,12 @@ static void TestProtocolFtp()
puts("ERROR: failed to cd to .."); puts("ERROR: failed to cd to ..");
} }
// test RETR
static const char *filename = "welcome.msg"; static const char *filename = "welcome.msg";
wxInputStream *in = ftp.GetInputStream(filename); wxInputStream *in = ftp.GetInputStream(filename);
if ( !in ) if ( !in )
{ {
puts("ERROR: couldn't get input stream"); printf("ERROR: couldn't get input stream for %s\n", filename);
} }
else else
{ {
@ -1433,6 +1468,26 @@ static void TestProtocolFtp()
delete [] data; delete [] data;
delete in; delete in;
} }
// test some other FTP commands
if ( ftp.SendCommand("STAT") != '2' )
{
puts("ERROR: STAT failed");
}
else
{
printf("STAT returned:\n\n%s\n", ftp.GetLastResult().c_str());
}
if ( ftp.SendCommand("HELP SITE") != '2' )
{
puts("ERROR: HELP SITE failed");
}
else
{
printf("The list of site-specific commands:\n\n%s\n",
ftp.GetLastResult().c_str());
}
} }
} }
@ -1440,15 +1495,13 @@ static void TestProtocolFtpUpload()
{ {
puts("*** Testing wxFTP uploading ***\n"); puts("*** Testing wxFTP uploading ***\n");
wxLog::AddTraceMask(_T("ftp"));
static const char *hostname = "localhost"; static const char *hostname = "localhost";
printf("--- Attempting to connect to %s:21...\n", hostname); printf("--- Attempting to connect to %s:21...\n", hostname);
wxFTP ftp; wxFTP ftp;
ftp.SetUser("zeitlin"); ftp.SetUser("zeitlin");
ftp.SetPassword("insert your password here"); ftp.SetPassword("password");
if ( !ftp.Connect(hostname) ) if ( !ftp.Connect(hostname) )
{ {
printf("ERROR: failed to connect to %s\n", hostname); printf("ERROR: failed to connect to %s\n", hostname);
@ -1469,6 +1522,17 @@ static void TestProtocolFtpUpload()
delete out; delete out;
} }
// send a command to check the remote file
if ( ftp.SendCommand(wxString("STAT ") + file1) != '2' )
{
printf("ERROR: STAT %s failed\n", file1);
}
else
{
printf("STAT %s returned:\n\n%s\n",
file1, ftp.GetLastResult().c_str());
}
out = ftp.GetOutputStream(file2); out = ftp.GetOutputStream(file2);
if ( out ) if ( out )
{ {
@ -1479,7 +1543,7 @@ static void TestProtocolFtpUpload()
} }
} }
#endif // TEST_SOCKETS #endif // TEST_FTP
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// streams // streams
@ -3858,12 +3922,17 @@ int main(int argc, char **argv)
if ( 0 ) if ( 0 )
{ {
TestSocketServer(); TestSocketServer();
TestSocketClient();
TestProtocolFtp();
} }
TestProtocolFtpUpload(); TestSocketClient();
#endif // TEST_SOCKETS #endif // TEST_SOCKETS
#ifdef TEST_FTP
wxLog::AddTraceMask(_T("ftp"));
TestProtocolFtp();
if ( 0 )
TestProtocolFtpUpload();
#endif // TEST_FTP
#ifdef TEST_STREAMS #ifdef TEST_STREAMS
TestMemoryStream(); TestMemoryStream();
#endif // TEST_STREAMS #endif // TEST_STREAMS

View File

@ -34,7 +34,6 @@
#include "wx/intl.h" #include "wx/intl.h"
#endif // PCH #endif // PCH
#include "wx/fontutil.h"
#include "wx/fontmap.h" #include "wx/fontmap.h"
#if wxUSE_CONFIG #if wxUSE_CONFIG
@ -43,6 +42,7 @@
#endif #endif
#if wxUSE_GUI #if wxUSE_GUI
#include "wx/fontutil.h"
#include "wx/msgdlg.h" #include "wx/msgdlg.h"
#include "wx/fontdlg.h" #include "wx/fontdlg.h"
#include "wx/choicdlg.h" #include "wx/choicdlg.h"

View File

@ -2,17 +2,29 @@
// Name: ftp.cpp // Name: ftp.cpp
// Purpose: FTP protocol // Purpose: FTP protocol
// Author: Guilhem Lavaux // Author: Guilhem Lavaux
// Modified by: // Modified by: Mark Johnson, wxWindows@mj10777.de
// 20000917 : RmDir, GetLastResult, GetList
// Vadim Zeitlin (numerous fixes and rewrites to all part of the
// code, support ASCII/Binary modes, better error reporting, more
// robust Abort(), support for arbitrary FTP commands, ...)
// Created: 07/07/1997 // Created: 07/07/1997
// RCS-ID: $Id$ // RCS-ID: $Id$
// Copyright: (c) 1997, 1998 Guilhem Lavaux // Copyright: (c) 1997, 1998 Guilhem Lavaux
// Licence: wxWindows license // Licence: wxWindows license
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
// ============================================================================
// declarations
// ============================================================================
#ifdef __GNUG__ #ifdef __GNUG__
#pragma implementation "ftp.h" #pragma implementation "ftp.h"
#endif #endif
// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------
// For compilers that support precompilation, includes "wx.h". // For compilers that support precompilation, includes "wx.h".
#include "wx/wxprec.h" #include "wx/wxprec.h"
@ -20,49 +32,58 @@
#pragma hdrstop #pragma hdrstop
#endif #endif
#include "wx/setup.h"
#if wxUSE_SOCKETS && wxUSE_STREAMS #if wxUSE_SOCKETS && wxUSE_STREAMS
#ifndef __MWERKS__ #ifndef WX_PRECOMP
#include <memory.h> #include <stdlib.h>
#endif #include "wx/string.h"
#if defined(__WXMAC__) #include "wx/utils.h"
#include "/wx/mac/macsock.h" #include "wx/log.h"
#endif #include "wx/intl.h"
#endif // WX_PRECOMP
#include <stdlib.h>
#include "wx/string.h"
#include "wx/utils.h"
#include "wx/sckaddr.h" #include "wx/sckaddr.h"
#include "wx/socket.h" #include "wx/socket.h"
#include "wx/url.h" #include "wx/url.h"
#include "wx/sckstrm.h" #include "wx/sckstrm.h"
#include "wx/protocol/protocol.h" #include "wx/protocol/protocol.h"
#include "wx/protocol/ftp.h" #include "wx/protocol/ftp.h"
#include "wx/log.h"
#ifdef __BORLANDC__ #if defined(__WXMAC__)
#pragma hdrstop #include "/wx/mac/macsock.h"
#endif #endif
#ifndef __MWERKS__
#include <memory.h>
#endif
// ----------------------------------------------------------------------------
// constants
// ----------------------------------------------------------------------------
// the length of FTP status code (3 digits) // the length of FTP status code (3 digits)
static const size_t LEN_CODE = 3; static const size_t LEN_CODE = 3;
#define FTP_BSIZE 1024 // ----------------------------------------------------------------------------
// macros
// ----------------------------------------------------------------------------
IMPLEMENT_DYNAMIC_CLASS(wxFTP, wxProtocol) IMPLEMENT_DYNAMIC_CLASS(wxFTP, wxProtocol)
IMPLEMENT_PROTOCOL(wxFTP, wxT("ftp"), wxT("ftp"), TRUE) IMPLEMENT_PROTOCOL(wxFTP, wxT("ftp"), wxT("ftp"), TRUE)
//////////////////////////////////////////////////////////////// // ============================================================================
////// wxFTP constructor and destructor //////////////////////// // implementation
//////////////////////////////////////////////////////////////// // ============================================================================
// ----------------------------------------------------------------------------
// wxFTP constructor and destructor
// ----------------------------------------------------------------------------
wxFTP::wxFTP() wxFTP::wxFTP()
: wxProtocol()
{ {
m_lastError = wxPROTO_NOERR; m_lastError = wxPROTO_NOERR;
m_streaming = FALSE; m_streaming = FALSE;
m_modeSet = FALSE;
m_user = wxT("anonymous"); m_user = wxT("anonymous");
m_passwd << wxGetUserId() << wxT('@') << wxGetFullHostName(); m_passwd << wxGetUserId() << wxT('@') << wxGetFullHostName();
@ -81,52 +102,63 @@ wxFTP::~wxFTP()
Close(); Close();
} }
//////////////////////////////////////////////////////////////// // ----------------------------------------------------------------------------
////// wxFTP connect and login methods ///////////////////////// // wxFTP connect and login methods
//////////////////////////////////////////////////////////////// // ----------------------------------------------------------------------------
bool wxFTP::Connect(wxSockAddress& addr, bool WXUNUSED(wait)) bool wxFTP::Connect(wxSockAddress& addr, bool WXUNUSED(wait))
{ {
if (!wxProtocol::Connect(addr)) { if ( !wxProtocol::Connect(addr) )
m_lastError = wxPROTO_NETERR; {
return FALSE; m_lastError = wxPROTO_NETERR;
} return FALSE;
}
if (!m_user || !m_passwd) { if ( !m_user )
m_lastError = wxPROTO_CONNERR; {
return FALSE; m_lastError = wxPROTO_CONNERR;
} return FALSE;
}
wxString command; // we should have 220 welcome message
if ( !CheckResult('2') )
{
Close();
return FALSE;
}
if (!GetResult('2')) { wxString command;
Close(); command.Printf(wxT("USER %s"), m_user.c_str());
return FALSE; char rc = SendCommand(command);
} if ( rc == '2' )
{
// 230 return: user accepted without password
return TRUE;
}
command.sprintf(wxT("USER %s"), (const wxChar *)m_user); if ( rc != '3' )
if (!SendCommand(command, '3')) { {
Close(); Close();
return FALSE; return FALSE;
} }
command.sprintf(wxT("PASS %s"), (const wxChar *)m_passwd); command.Printf(wxT("PASS %s"), m_passwd.c_str());
if (!SendCommand(command, '2')) { if ( !CheckCommand(command, '2') )
Close(); {
return FALSE; Close();
} return FALSE;
}
return TRUE; return TRUE;
} }
bool wxFTP::Connect(const wxString& host) bool wxFTP::Connect(const wxString& host)
{ {
wxIPV4address addr; wxIPV4address addr;
wxString my_host = host; addr.Hostname(host);
addr.Service(wxT("ftp"));
addr.Hostname(my_host); return Connect(addr);
addr.Service(wxT("ftp"));
return Connect(addr);
} }
bool wxFTP::Close() bool wxFTP::Close()
@ -138,7 +170,12 @@ bool wxFTP::Close()
} }
if ( IsConnected() ) if ( IsConnected() )
SendCommand(wxT("QUIT"), '2'); {
if ( !CheckCommand(wxT("QUIT"), '2') )
{
wxLogDebug(_T("Failed to close connection gracefully."));
}
}
return wxSocketClient::Close(); return wxSocketClient::Close();
} }
@ -151,37 +188,39 @@ bool wxFTP::Close()
// Send command to FTP server // Send command to FTP server
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
bool wxFTP::SendCommand(const wxString& command, char exp_ret) char wxFTP::SendCommand(const wxString& command)
{ {
wxString tmp_str; if ( m_streaming )
if (m_streaming)
{ {
m_lastError = wxPROTO_STREAMING; m_lastError = wxPROTO_STREAMING;
return FALSE; return 0;
} }
tmp_str = command + wxT("\r\n"); wxString tmp_str = command + wxT("\r\n");
const wxWX2MBbuf tmp_buf = tmp_str.mb_str(); const wxWX2MBbuf tmp_buf = tmp_str.mb_str();
if ( Write(wxMBSTRINGCAST tmp_buf, strlen(tmp_buf)).Error()) if ( Write(wxMBSTRINGCAST tmp_buf, strlen(tmp_buf)).Error())
{ {
m_lastError = wxPROTO_NETERR; m_lastError = wxPROTO_NETERR;
return FALSE; return 0;
} }
wxLogTrace(_T("ftp"), _T("==> %s"), command.c_str()); wxLogTrace(_T("ftp"), _T("==> %s"), command.c_str());
return GetResult(exp_ret); return GetResult();
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// Recieve servers reply // Recieve servers reply
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
bool wxFTP::GetResult(char exp) char wxFTP::GetResult()
{ {
wxString code; wxString code;
// m_lastResult will contain the entire server response, possibly on
// multiple lines
m_lastResult.clear();
// we handle multiline replies here according to RFC 959: it says that a // we handle multiline replies here according to RFC 959: it says that a
// reply may either be on 1 line of the form "xyz ..." or on several lines // reply may either be on 1 line of the form "xyz ..." or on several lines
// in whuch case it looks like // in whuch case it looks like
@ -194,13 +233,22 @@ bool wxFTP::GetResult(char exp)
bool endOfReply = FALSE; bool endOfReply = FALSE;
while ( !endOfReply && !badReply ) while ( !endOfReply && !badReply )
{ {
m_lastError = ReadLine(m_lastResult); wxString line;
m_lastError = ReadLine(line);
if ( m_lastError ) if ( m_lastError )
return FALSE; return 0;
if ( !m_lastResult.empty() )
{
// separate from last line
m_lastResult += _T('\n');
}
m_lastResult += line;
// unless this is an intermediate line of a multiline reply, it must // unless this is an intermediate line of a multiline reply, it must
// contain the code in the beginning and '-' or ' ' following it // contain the code in the beginning and '-' or ' ' following it
if ( m_lastResult.Len() < LEN_CODE + 1 ) if ( line.Len() < LEN_CODE + 1 )
{ {
if ( firstLine ) if ( firstLine )
{ {
@ -209,19 +257,19 @@ bool wxFTP::GetResult(char exp)
else else
{ {
wxLogTrace(_T("ftp"), _T("<== %s %s"), wxLogTrace(_T("ftp"), _T("<== %s %s"),
code.c_str(), m_lastResult.c_str()); code.c_str(), line.c_str());
} }
} }
else // line has at least 4 chars else // line has at least 4 chars
{ {
// this is the char which tells us what we're dealing with // this is the char which tells us what we're dealing with
wxChar chMarker = m_lastResult.GetChar(LEN_CODE); wxChar chMarker = line.GetChar(LEN_CODE);
if ( firstLine ) if ( firstLine )
{ {
code = wxString(m_lastResult, LEN_CODE); code = wxString(line, LEN_CODE);
wxLogTrace(_T("ftp"), _T("<== %s %s"), wxLogTrace(_T("ftp"), _T("<== %s %s"),
code.c_str(), m_lastResult.c_str() + LEN_CODE + 1); code.c_str(), line.c_str() + LEN_CODE + 1);
switch ( chMarker ) switch ( chMarker )
{ {
@ -240,7 +288,7 @@ bool wxFTP::GetResult(char exp)
} }
else // subsequent line of multiline reply else // subsequent line of multiline reply
{ {
if ( wxStrncmp(m_lastResult, code, LEN_CODE) == 0 ) if ( wxStrncmp(line, code, LEN_CODE) == 0 )
{ {
if ( chMarker == _T(' ') ) if ( chMarker == _T(' ') )
{ {
@ -248,13 +296,13 @@ bool wxFTP::GetResult(char exp)
} }
wxLogTrace(_T("ftp"), _T("<== %s %s"), wxLogTrace(_T("ftp"), _T("<== %s %s"),
code.c_str(), m_lastResult.c_str() + LEN_CODE + 1); code.c_str(), line.c_str() + LEN_CODE + 1);
} }
else else
{ {
// just part of reply // just part of reply
wxLogTrace(_T("ftp"), _T("<== %s %s"), wxLogTrace(_T("ftp"), _T("<== %s %s"),
code.c_str(), m_lastResult.c_str()); code.c_str(), line.c_str());
} }
} }
} }
@ -267,12 +315,59 @@ bool wxFTP::GetResult(char exp)
m_lastError = wxPROTO_PROTERR; m_lastError = wxPROTO_PROTERR;
return 0;
}
// if we got here we must have a non empty code string
return code[0u];
}
// ----------------------------------------------------------------------------
// wxFTP simple commands
// ----------------------------------------------------------------------------
bool wxFTP::SetTransferMode(TransferMode transferMode)
{
wxString mode;
switch ( transferMode )
{
default:
wxFAIL_MSG(_T("unknown FTP transfer mode"));
// fall through
case BINARY:
mode = _T('I');
break;
case ASCII:
mode = _T('A');
break;
}
if ( !DoSimpleCommand(_T("TYPE"), mode) )
{
wxLogError(_("Failed to set FTP transfer mode to %s."),
transferMode == ASCII ? _("ASCII") : _("binary"));
return FALSE; return FALSE;
} }
if ( code.GetChar(0) != exp ) m_modeSet = TRUE;
return TRUE;
}
bool wxFTP::DoSimpleCommand(const wxChar *command, const wxString& arg)
{
wxString fullcmd = command;
if ( !arg.empty() )
{ {
m_lastError = wxPROTO_PROTERR; fullcmd << _T(' ') << arg;
}
if ( !CheckCommand(fullcmd, '2') )
{
wxLogDebug(_T("FTP command '%s' failed."), fullcmd.c_str());
return FALSE; return FALSE;
} }
@ -280,39 +375,32 @@ bool wxFTP::GetResult(char exp)
return TRUE; return TRUE;
} }
////////////////////////////////////////////////////////////////
////// wxFTP low-level methods /////////////////////////////////
////////////////////////////////////////////////////////////////
bool wxFTP::ChDir(const wxString& dir) bool wxFTP::ChDir(const wxString& dir)
{ {
wxString str = dir; // some servers might not understand ".." if they use different directory
// tree conventions, but they always understand CDUP - should we use it if
// dir == ".."? OTOH, do such servers (still) exist?
str.Prepend(wxT("CWD ")); return DoSimpleCommand(_T("CWD"), dir);
return SendCommand(str, '2');
} }
bool wxFTP::MkDir(const wxString& dir) bool wxFTP::MkDir(const wxString& dir)
{ {
wxString str = dir; return DoSimpleCommand(_T("MKD"), dir);
str.Prepend(wxT("MKD "));
return SendCommand(str, '2');
} }
bool wxFTP::RmDir(const wxString& dir) bool wxFTP::RmDir(const wxString& dir)
{ {
wxString str = dir; return DoSimpleCommand(_T("RMD"), dir);
str.Prepend(wxT("PWD "));
return SendCommand(str, '2');
} }
wxString wxFTP::Pwd() wxString wxFTP::Pwd()
{ {
wxString path; wxString path;
if ( SendCommand(wxT("PWD"), '2') ) if ( CheckCommand(wxT("PWD"), '2') )
{ {
// the result is at least that long if SendCommand() succeeded // the result is at least that long if CheckCommand() succeeded
const wxChar *p = m_lastResult.c_str() + LEN_CODE + 1; const wxChar *p = m_lastResult.c_str() + LEN_CODE + 1;
if ( *p != _T('"') ) if ( *p != _T('"') )
{ {
@ -345,243 +433,304 @@ wxString wxFTP::Pwd()
} }
} }
} }
else
{
wxLogDebug(_T("FTP PWD command failed."));
}
return path; return path;
} }
bool wxFTP::Rename(const wxString& src, const wxString& dst) bool wxFTP::Rename(const wxString& src, const wxString& dst)
{ {
wxString str; wxString str;
str = wxT("RNFR ") + src; str = wxT("RNFR ") + src;
if (!SendCommand(str, '3')) if ( !CheckCommand(str, '3') )
return FALSE; return FALSE;
str = wxT("RNTO ") + dst; str = wxT("RNTO ") + dst;
return SendCommand(str, '2');
return CheckCommand(str, '2');
} }
bool wxFTP::RmFile(const wxString& path) bool wxFTP::RmFile(const wxString& path)
{ {
wxString str; wxString str;
str = wxT("DELE ") + path;
str = wxT("DELE "); return CheckCommand(str, '2');
str += path;
return SendCommand(str, '2');
} }
//////////////////////////////////////////////////////////////// // ----------------------------------------------------------------------------
////// wxFTP download*upload /////////////////////////////////// // wxFTP download and upload
//////////////////////////////////////////////////////////////// // ----------------------------------------------------------------------------
class wxInputFTPStream : public wxSocketInputStream { class wxInputFTPStream : public wxSocketInputStream
{
public: public:
wxFTP *m_ftp; wxInputFTPStream(wxFTP *ftp_clt, wxSocketBase *sock)
size_t m_ftpsize; : wxSocketInputStream(*sock), m_ftp(ftp_clt)
{
}
wxInputFTPStream(wxFTP *ftp_clt, wxSocketBase *sock) size_t GetSize() const { return m_ftpsize; }
: wxSocketInputStream(*sock), m_ftp(ftp_clt) {}
size_t GetSize() const { return m_ftpsize; } virtual ~wxInputFTPStream()
virtual ~wxInputFTPStream(void) {
{ if ( LastError() == wxStream_NOERROR )
if (LastError() == wxStream_NOERROR) {
m_ftp->GetResult('2'); // wait for "226 transfer completed"
else m_ftp->CheckResult('2');
m_ftp->Abort();
delete m_i_socket; m_ftp->m_streaming = FALSE;
} }
else
{
m_ftp->Abort();
}
delete m_i_socket;
}
wxFTP *m_ftp;
size_t m_ftpsize;
}; };
class wxOutputFTPStream : public wxSocketOutputStream { class wxOutputFTPStream : public wxSocketOutputStream
{
public: public:
wxFTP *m_ftp; wxOutputFTPStream(wxFTP *ftp_clt, wxSocketBase *sock)
: wxSocketOutputStream(*sock), m_ftp(ftp_clt)
{
}
wxOutputFTPStream(wxFTP *ftp_clt, wxSocketBase *sock) virtual ~wxOutputFTPStream(void)
: wxSocketOutputStream(*sock), m_ftp(ftp_clt) {} {
virtual ~wxOutputFTPStream(void) if ( IsOk() )
{ {
if ( IsOk() ) // close data connection first, this will generate "transfer
{ // completed" reply
// close data connection first, this will generate "transfer delete m_o_socket;
// completed" reply
delete m_o_socket;
// read this reply // read this reply
m_ftp->GetResult('2'); m_ftp->CheckResult('2');
}
else
{
// abort data connection first
m_ftp->Abort();
// and close it after m_ftp->m_streaming = FALSE;
delete m_o_socket; }
} else
} {
// abort data connection first
m_ftp->Abort();
// and close it after
delete m_o_socket;
}
}
wxFTP *m_ftp;
}; };
wxSocketClient *wxFTP::GetPort() wxSocketClient *wxFTP::GetPort()
{ {
wxIPV4address addr; int a[6];
wxSocketClient *client;
int a[6];
wxString straddr;
int addr_pos;
wxUint16 port;
wxUint32 hostaddr;
if (!SendCommand(wxT("PASV"), '2')) if ( !DoSimpleCommand(_T("PASV")) )
return NULL; {
wxLogError(_("The FTP server doesn't support passive mode."));
addr_pos = m_lastResult.Find(wxT('(')); return NULL;
if (addr_pos == -1) { }
m_lastError = wxPROTO_PROTERR;
return NULL;
}
straddr = m_lastResult(addr_pos+1, m_lastResult.Length());
wxSscanf((const wxChar *)straddr,wxT("%d,%d,%d,%d,%d,%d"),&a[2],&a[3],&a[4],&a[5],&a[0],&a[1]);
hostaddr = (wxUint16)a[5] << 24 | (wxUint16)a[4] << 16 | const char *addrStart = wxStrchr(m_lastResult, _T('('));
(wxUint16)a[3] << 8 | a[2]; if ( !addrStart )
addr.Hostname(hostaddr); {
m_lastError = wxPROTO_PROTERR;
port = (wxUint16)a[0] << 8 | a[1]; return NULL;
addr.Service(port); }
client = new wxSocketClient(); const char *addrEnd = wxStrchr(addrStart, _T(')'));
if (!client->Connect(addr)) { if ( !addrEnd )
delete client; {
return NULL; m_lastError = wxPROTO_PROTERR;
}
client->Notify(FALSE);
return client; return NULL;
}
wxString straddr(addrStart + 1, addrEnd);
wxSscanf(straddr, wxT("%d,%d,%d,%d,%d,%d"),
&a[2],&a[3],&a[4],&a[5],&a[0],&a[1]);
wxUint32 hostaddr = (wxUint16)a[5] << 24 |
(wxUint16)a[4] << 16 |
(wxUint16)a[3] << 8 |
a[2];
wxUint16 port = (wxUint16)a[0] << 8 | a[1];
wxIPV4address addr;
addr.Hostname(hostaddr);
addr.Service(port);
wxSocketClient *client = new wxSocketClient();
if ( !client->Connect(addr) )
{
delete client;
return NULL;
}
client->Notify(FALSE);
return client;
} }
bool wxFTP::Abort() bool wxFTP::Abort()
{ {
if ( !m_streaming ) if ( !m_streaming )
return TRUE; return TRUE;
m_streaming = FALSE; m_streaming = FALSE;
if ( !SendCommand(wxT("ABOR"), '4') ) if ( !CheckCommand(wxT("ABOR"), '4') )
return FALSE; return FALSE;
return GetResult('2'); return CheckResult('2');
} }
wxInputStream *wxFTP::GetInputStream(const wxString& path) wxInputStream *wxFTP::GetInputStream(const wxString& path)
{ {
wxString tmp_str; int pos_size;
int pos_size; wxInputFTPStream *in_stream;
wxInputFTPStream *in_stream;
if (!SendCommand(wxT("TYPE I"), '2')) if ( !m_modeSet && !SetTransferMode(BINARY) )
return NULL; return NULL;
wxSocketClient *sock = GetPort(); wxSocketClient *sock = GetPort();
if (!sock) { if ( !sock )
m_lastError = wxPROTO_NETERR; {
return NULL; m_lastError = wxPROTO_NETERR;
} return NULL;
}
tmp_str = wxT("RETR ") + wxURL::ConvertFromURI(path); wxString tmp_str = wxT("RETR ") + wxURL::ConvertFromURI(path);
if (!SendCommand(tmp_str, '1')) if ( !CheckCommand(tmp_str, '1') )
return NULL; return NULL;
in_stream = new wxInputFTPStream(this, sock); m_streaming = TRUE;
pos_size = m_lastResult.Index(wxT('(')); in_stream = new wxInputFTPStream(this, sock);
if (pos_size != wxNOT_FOUND) {
wxString str_size = m_lastResult(pos_size+1, m_lastResult.Index(wxT(')'))-1);
in_stream->m_ftpsize = wxAtoi(WXSTRINGCAST str_size); pos_size = m_lastResult.Index(wxT('('));
} if ( pos_size != wxNOT_FOUND )
sock->SetFlags(wxSOCKET_WAITALL); {
wxString str_size = m_lastResult(pos_size+1, m_lastResult.Index(wxT(')'))-1);
return in_stream; in_stream->m_ftpsize = wxAtoi(WXSTRINGCAST str_size);
}
sock->SetFlags(wxSOCKET_WAITALL);
return in_stream;
} }
wxOutputStream *wxFTP::GetOutputStream(const wxString& path) wxOutputStream *wxFTP::GetOutputStream(const wxString& path)
{ {
wxString tmp_str; if ( !m_modeSet && !SetTransferMode(BINARY) )
return NULL;
if (!SendCommand(wxT("TYPE I"), '2')) wxSocketClient *sock = GetPort();
return NULL;
wxSocketClient *sock = GetPort(); wxString tmp_str = wxT("STOR ") + path;
if ( !CheckCommand(tmp_str, '1') )
return FALSE;
tmp_str = wxT("STOR ") + path; m_streaming = TRUE;
if (!SendCommand(tmp_str, '1'))
return FALSE;
return new wxOutputFTPStream(this, sock); return new wxOutputFTPStream(this, sock);
} }
bool wxFTP::GetList(wxArrayString& files, const wxString& wildcard) // ----------------------------------------------------------------------------
// FTP directory listing
// ----------------------------------------------------------------------------
bool wxFTP::GetList(wxArrayString& files,
const wxString& wildcard,
bool details)
{ {
wxSocketBase *sock = GetPort(); wxSocketBase *sock = GetPort();
if ( !sock ) if (!sock)
{
return FALSE; return FALSE;
}
wxString line = _T("NLST"); // NLST : List of Filenames (including Directory's !)
// LIST : depending on BS of FTP-Server
// - Unix : result like "ls" command
// - Windows : like "dir" command
// - others : ?
wxString line(details ? _T("LIST") : _T("NLST"));
if ( !!wildcard ) if ( !!wildcard )
{ {
// notice that there is no space here line << _T(' ') << wildcard;
line += wildcard;
} }
if ( !SendCommand(line, '1') ) if (!CheckCommand(line, '1'))
{ {
return FALSE; return FALSE;
} }
files.Empty(); files.Empty();
while ( ReadLine(sock, line) == wxPROTO_NOERR ) while ( ReadLine(sock, line) == wxPROTO_NOERR )
{ {
files.Add(line); files.Add(line);
} }
delete sock; delete sock;
// the file list should be terminated by "226 Transfer complete"" // the file list should be terminated by "226 Transfer complete""
if ( !GetResult('2') ) if ( !CheckResult('2') )
return FALSE; return FALSE;
return TRUE; return TRUE;
} }
wxList *wxFTP::GetList(const wxString& wildcard) #ifdef WXWIN_COMPATIBILITY_2
// deprecated
wxList *wxFTP::GetList(const wxString& wildcard, bool details)
{ {
wxList *file_list = new wxList; wxSocketBase *sock = GetPort();
wxSocketBase *sock = GetPort(); if (!sock)
wxString tmp_str = wxT("NLST"); return FALSE;
wxList *file_list = new wxList;
if (!wildcard.IsNull()) wxString line;
tmp_str += wildcard; // NLST : List of Filenames (including Directory's !)
// LIST : depending on BS of FTP-Server
if (!SendCommand(tmp_str, '1')) { // - Unix : result like "ls" command
delete sock; // - Windows : like "dir" command
delete file_list; // - others : ?
return NULL; if (!details)
} line = _T("NLST"); // Default
else
while (GetLine(sock, tmp_str) == wxPROTO_NOERR) { line = _T("LIST");
file_list->Append((wxObject *)(new wxString(tmp_str))); if (!wildcard.IsNull())
} line += wildcard;
if (!CheckCommand(line, '1'))
if (!GetResult('2')) { {
delete sock; delete sock;
file_list->DeleteContents(TRUE); delete file_list;
delete file_list; return NULL;
return NULL; }
} while (GetLine(sock, line) == wxPROTO_NOERR)
{
return file_list; file_list->Append((wxObject *)(new wxString(line)));
}
if (!CheckResult('2'))
{
delete sock;
file_list->DeleteContents(TRUE);
delete file_list;
return NULL;
}
return file_list;
} }
#endif // WXWIN_COMPATIBILITY_2
#endif #endif
// wxUSE_SOCKETS // wxUSE_SOCKETS