Merge branch 'poco-1.10.0' into devel

This commit is contained in:
Günter Obiltschnig
2020-01-23 19:11:07 +01:00
32 changed files with 2473 additions and 395 deletions

View File

@@ -51,40 +51,37 @@ public:
{
FTP_PORT = 21
};
enum FileType
{
TYPE_TEXT, // TYPE A (ASCII)
TYPE_BINARY // TYPE I (Image)
TYPE_TEXT, /// TYPE A (ASCII)
TYPE_BINARY /// TYPE I (Image/binary data)
};
FTPClientSession();
/// Creates an FTPClientSession.
///
/// Passive mode will be used for data transfers.
explicit FTPClientSession(const StreamSocket& socket);
FTPClientSession(const StreamSocket& socket, bool readWelcomeMessage = true);
/// Creates an FTPClientSession using the given
/// connected socket for the control connection.
///
/// Passive mode will be used for data transfers.
FTPClientSession(const std::string& host,
Poco::UInt16 port = FTP_PORT,
const std::string& username = "",
const std::string& password = "");
FTPClientSession(const std::string& host, Poco::UInt16 port = FTP_PORT, const std::string& username = "", const std::string& password = "");
/// Creates an FTPClientSession using a socket connected
/// to the given host and port. If username is supplied,
/// login is attempted.
///
/// Passive mode will be used for data transfers.
virtual ~FTPClientSession();
/// Destroys the FTPClientSession.
void setTimeout(const Poco::Timespan& timeout);
/// Sets the timeout for socket operations.
Poco::Timespan getTimeout() const;
/// Returns the timeout for socket operations.
@@ -95,18 +92,15 @@ public:
/// EPSV command is used (with a fallback to PASV if EPSV fails)
/// for switching to passive mode. The same applies to
/// EPRT and PORT for active connections.
bool getPassive() const;
/// Returns true iff passive mode is enabled for this connection.
void open(const std::string& host,
Poco::UInt16 port,
const std::string& username = "",
const std::string& password = "");
virtual void open(const std::string& host, Poco::UInt16 port, const std::string& username = "", const std::string& password = "");
/// Opens the FTP connection to the given host and port.
/// If username is supplied, login is attempted.
void login(const std::string& username, const std::string& password);
virtual void login(const std::string& username, const std::string& password);
/// Authenticates the user against the FTP server. Must be
/// called before any other commands (except QUIT) can be sent.
///
@@ -117,18 +111,21 @@ public:
/// NetException in case of a general network communication failure.
void logout();
/// Logs out from the server by sending a QUIT command. Any transfer
/// that's in progress is ended. The control connection is kept
/// open.
void close();
/// Sends a QUIT command and closes the connection to the server.
///
/// Throws a FTPException in case of a FTP-specific error, or a
/// NetException in case of a general network communication failure.
std::string systemType();
/// Returns the system type of the FTP server.
///
/// Sends a SYST command to the server and returns the result.
void setFileType(FileType type);
/// Sets the file type for transferring files.
///
@@ -144,7 +141,7 @@ public:
void setWorkingDirectory(const std::string& path);
/// Changes the current working directory on the server.
///
/// Sends a CWD command with the given path as argument to the
/// Sends a CWD command with the given path as argument to the
/// server.
///
/// Throws a FTPException in case of a FTP-specific error, or a
@@ -155,7 +152,7 @@ public:
///
/// Throws a FTPException in case of a FTP-specific error, or a
/// NetException in case of a general network communication failure.
void cdup();
/// Moves one directory up from the current working directory
/// on the server.
@@ -164,7 +161,7 @@ public:
///
/// Throws a FTPException in case of a FTP-specific error, or a
/// NetException in case of a general network communication failure.
void rename(const std::string& oldName, const std::string& newName);
/// Renames the file on the server given by oldName to newName.
///
@@ -172,7 +169,7 @@ public:
///
/// Throws a FTPException in case of a FTP-specific error, or a
/// NetException in case of a general network communication failure.
void remove(const std::string& path);
/// Deletes the file specified by path on the server.
///
@@ -217,11 +214,11 @@ public:
/// the native text file format.
/// The InputLineEndingConverter class from the Foundation
/// library can be used for that purpose.
void endDownload();
/// Must be called to complete a download initiated with
/// beginDownload().
std::ostream& beginUpload(const std::string& path);
/// Starts uploading the file with the given name.
/// After all data has been written to the returned stream,
@@ -274,9 +271,9 @@ public:
/// client waits for a connection request from the server.
/// After establishing the data connection, a SocketStream
/// for transferring the data is created.
void endList();
/// Must be called to complete a directory listing download
/// Must be called to complete a directory listing download
/// initiated with beginList().
void abort();
@@ -287,7 +284,7 @@ public:
///
/// A separate call to endDownload() or endUpload() is
/// not necessary.
int sendCommand(const std::string& command, std::string& response);
/// Sends the given command verbatim to the server
/// and waits for a response.
@@ -302,7 +299,15 @@ public:
bool isLoggedIn() const;
/// Returns true if the session is logged in.
bool isSecure() const;
/// Returns true if the session is FTPS.
const std::string& welcomeMessage();
/// Returns the welcome message.
protected:
virtual void receiveServerReadyReply();
enum StatusClass
{
FTP_POSITIVE_PRELIMINARY = 1,
@@ -311,9 +316,10 @@ protected:
FTP_TRANSIENT_NEGATIVE = 4,
FTP_PERMANENT_NEGATIVE = 5
};
enum
{
DEFAULT_TIMEOUT = 30000000 // 30 seconds default timeout for socket operations
DEFAULT_TIMEOUT = 30000000 // 30 seconds default timeout for socket operations
};
static bool isPositivePreliminary(int status);
@@ -322,7 +328,7 @@ protected:
static bool isTransientNegative(int status);
static bool isPermanentNegative(int status);
std::string extractPath(const std::string& response);
StreamSocket establishDataConnection(const std::string& command, const std::string& arg);
virtual StreamSocket establishDataConnection(const std::string& command, const std::string& arg);
StreamSocket activeDataConnection(const std::string& command, const std::string& arg);
StreamSocket passiveDataConnection(const std::string& command, const std::string& arg);
void sendPortCommand(const SocketAddress& addr);
@@ -334,21 +340,24 @@ protected:
void parseAddress(const std::string& str, SocketAddress& addr);
void parseExtAddress(const std::string& str, SocketAddress& addr);
void endTransfer();
DialogSocket* _pControlSocket = nullptr;
SocketStream* _pDataStream = nullptr;
private:
FTPClientSession(const FTPClientSession&);
FTPClientSession& operator = (const FTPClientSession&);
std::string _host;
Poco::UInt16 _port;
DialogSocket* _pControlSocket;
SocketStream* _pDataStream;
bool _passiveMode;
FileType _fileType;
bool _supports1738;
bool _serverReady;
bool _isLoggedIn;
Poco::Timespan _timeout;
std::string _host;
Poco::UInt16 _port = FTP_PORT;
bool _passiveMode = true;
FileType _fileType = TYPE_BINARY;
bool _supports1738 = true;
bool _serverReady = false;
bool _isLoggedIn = false;
Poco::Timespan _timeout = DEFAULT_TIMEOUT;
std::string _welcomeMessage;
Poco::FastMutex _wmMutex;
};
@@ -397,6 +406,19 @@ inline bool FTPClientSession::isLoggedIn() const
}
inline bool FTPClientSession::isSecure() const
{
return false;
}
inline const std::string& FTPClientSession::welcomeMessage()
{
Poco::FastMutex::ScopedLock lock(_wmMutex);
return _welcomeMessage;
}
} } // namespace Poco::Net

View File

@@ -30,8 +30,8 @@ namespace Net {
class Net_API FTPPasswordProvider
/// The base class for all password providers.
/// An instance of a subclass of this class can be
/// registered with the FTPStreamFactory to
/// provide a password
/// registered with the FTPStreamFactory to
/// provide a password.
{
public:
virtual std::string password(const std::string& username, const std::string& host) = 0;
@@ -56,7 +56,7 @@ class Net_API FTPStreamFactory: public Poco::URIStreamFactory
/// the FTP URL format specified in RFC 1738.
///
/// If the URI does not contain a username and password, the
/// username "anonymous" and the password "
/// username "anonymous" and the password "poco@localhost".
{
public:
FTPStreamFactory();
@@ -64,13 +64,13 @@ public:
~FTPStreamFactory();
/// Destroys the FTPStreamFactory.
std::istream* open(const Poco::URI& uri);
/// Creates and opens a HTTP stream for the given URI.
/// The URI must be a ftp://... URI.
///
/// Throws a NetException if anything goes wrong.
static void setAnonymousPassword(const std::string& password);
/// Sets the password used for anonymous FTP.
///
@@ -78,10 +78,10 @@ public:
/// thread-safe, so it's best to call this method
/// during application initialization, before the
/// FTPStreamFactory is used for the first time.
static const std::string& getAnonymousPassword();
/// Returns the password used for anonymous FTP.
static void setPasswordProvider(FTPPasswordProvider* pProvider);
/// Sets the FTPPasswordProvider. If NULL is given,
/// no password provider is used.
@@ -90,7 +90,7 @@ public:
/// thread-safe, so it's best to call this method
/// during application initialization, before the
/// FTPStreamFactory is used for the first time.
static FTPPasswordProvider* getPasswordProvider();
/// Returns the FTPPasswordProvider currently in use,
/// or NULL if no one has been set.
@@ -107,7 +107,7 @@ protected:
static void splitUserInfo(const std::string& userInfo, std::string& username, std::string& password);
static void getUserInfo(const Poco::URI& uri, std::string& username, std::string& password);
static void getPathAndType(const Poco::URI& uri, std::string& path, char& type);
private:
static std::string _anonymousPassword;
static FTPPasswordProvider* _pPasswordProvider;

View File

@@ -29,9 +29,9 @@ namespace Net {
FTPClientSession::FTPClientSession():
_port(0),
_pControlSocket(0),
_pDataStream(0),
_port(FTP_PORT),
_passiveMode(true),
_fileType(TYPE_BINARY),
_supports1738(true),
@@ -41,12 +41,12 @@ FTPClientSession::FTPClientSession():
{
}
FTPClientSession::FTPClientSession(const StreamSocket& socket):
_host(socket.address().host().toString()),
_port(socket.address().port()),
FTPClientSession::FTPClientSession(const StreamSocket& socket, bool readWelcomeMessage):
_pControlSocket(new DialogSocket(socket)),
_pDataStream(0),
_host(socket.address().host().toString()),
_port(socket.address().port()),
_passiveMode(true),
_fileType(TYPE_BINARY),
_supports1738(true),
@@ -55,6 +55,14 @@ FTPClientSession::FTPClientSession(const StreamSocket& socket):
_timeout(DEFAULT_TIMEOUT)
{
_pControlSocket->setReceiveTimeout(_timeout);
if (readWelcomeMessage)
{
receiveServerReadyReply();
}
else
{
_serverReady = true;
}
}
@@ -62,10 +70,10 @@ FTPClientSession::FTPClientSession(const std::string& host,
Poco::UInt16 port,
const std::string& username,
const std::string& password):
_host(host),
_port(port),
_pControlSocket(new DialogSocket(SocketAddress(host, port))),
_pDataStream(0),
_host(host),
_port(port),
_passiveMode(true),
_fileType(TYPE_BINARY),
_supports1738(true),
@@ -73,10 +81,9 @@ FTPClientSession::FTPClientSession(const std::string& host,
_isLoggedIn(false),
_timeout(DEFAULT_TIMEOUT)
{
_pControlSocket->setReceiveTimeout(_timeout);
if (!username.empty())
login(username, password);
else
_pControlSocket->setReceiveTimeout(_timeout);
}
@@ -86,7 +93,7 @@ FTPClientSession::~FTPClientSession()
{
close();
}
catch (...)
catch (...)
{
}
}
@@ -101,7 +108,7 @@ void FTPClientSession::setTimeout(const Poco::Timespan& timeout)
_pControlSocket->setReceiveTimeout(timeout);
}
Poco::Timespan FTPClientSession::getTimeout() const
{
return _timeout;
@@ -114,7 +121,7 @@ void FTPClientSession::setPassive(bool flag, bool useRFC1738)
_supports1738 = useRFC1738;
}
bool FTPClientSession::getPassive() const
{
return _passiveMode;
@@ -134,12 +141,32 @@ void FTPClientSession::open(const std::string& host,
}
else
{
_pControlSocket = new DialogSocket(SocketAddress(_host, _port));
_pControlSocket->setReceiveTimeout(_timeout);
if (!_pControlSocket)
{
_pControlSocket = new DialogSocket(SocketAddress(_host, _port));
_pControlSocket->setReceiveTimeout(_timeout);
}
receiveServerReadyReply();
}
}
void FTPClientSession::receiveServerReadyReply()
{
if (_serverReady) return;
std::string response;
int status = _pControlSocket->receiveStatusMessage(response);
if (!isPositiveCompletion(status))
throw FTPException("Cannot receive status message", response, status);
{
Poco::FastMutex::ScopedLock lock(_wmMutex);
_welcomeMessage = response;
}
_serverReady = true;
}
void FTPClientSession::login(const std::string& username, const std::string& password)
{
if (_isLoggedIn) logout();
@@ -151,20 +178,12 @@ void FTPClientSession::login(const std::string& username, const std::string& pas
_pControlSocket = new DialogSocket(SocketAddress(_host, _port));
_pControlSocket->setReceiveTimeout(_timeout);
}
if (!_serverReady)
{
status = _pControlSocket->receiveStatusMessage(response);
if (!isPositiveCompletion(status))
throw FTPException("Cannot login to server", response, status);
_serverReady = true;
}
receiveServerReadyReply();
status = sendCommand("USER", username, response);
if (isPositiveIntermediate(status))
status = sendCommand("PASS", password, response);
if (!isPositiveCompletion(status))
if (!isPositiveCompletion(status))
throw FTPException("Login denied", response, status);
setFileType(_fileType);
@@ -179,8 +198,13 @@ void FTPClientSession::logout()
if (_isLoggedIn)
{
try { endTransfer(); }
catch (...) { }
try
{
endTransfer();
}
catch (...)
{
}
_isLoggedIn = false;
std::string response;
sendCommand("QUIT", response);
@@ -190,8 +214,13 @@ void FTPClientSession::logout()
void FTPClientSession::close()
{
try { logout(); }
catch (...) {}
try
{
logout();
}
catch (...)
{
}
_serverReady = false;
if (_pControlSocket)
{
@@ -256,24 +285,24 @@ void FTPClientSession::cdup()
throw FTPException("Cannot change directory", response, status);
}
void FTPClientSession::rename(const std::string& oldName, const std::string& newName)
{
std::string response;
int status = sendCommand("RNFR", oldName, response);
if (!isPositiveIntermediate(status))
if (!isPositiveIntermediate(status))
throw FTPException(std::string("Cannot rename ") + oldName, response, status);
status = sendCommand("RNTO", newName, response);
if (!isPositiveCompletion(status))
if (!isPositiveCompletion(status))
throw FTPException(std::string("Cannot rename to ") + newName, response, status);
}
void FTPClientSession::remove(const std::string& path)
{
std::string response;
int status = sendCommand("DELE", path, response);
if (!isPositiveCompletion(status))
if (!isPositiveCompletion(status))
throw FTPException(std::string("Cannot remove " + path), response, status);
}
@@ -282,7 +311,7 @@ void FTPClientSession::createDirectory(const std::string& path)
{
std::string response;
int status = sendCommand("MKD", path, response);
if (!isPositiveCompletion(status))
if (!isPositiveCompletion(status))
throw FTPException(std::string("Cannot create directory ") + path, response, status);
}
@@ -291,7 +320,7 @@ void FTPClientSession::removeDirectory(const std::string& path)
{
std::string response;
int status = sendCommand("RMD", path, response);
if (!isPositiveCompletion(status))
if (!isPositiveCompletion(status))
throw FTPException(std::string("Cannot remove directory ") + path, response, status);
}
@@ -307,13 +336,13 @@ std::istream& FTPClientSession::beginDownload(const std::string& path)
return *_pDataStream;
}
void FTPClientSession::endDownload()
{
endTransfer();
}
std::ostream& FTPClientSession::beginUpload(const std::string& path)
{
if (!isOpen())
@@ -349,7 +378,7 @@ void FTPClientSession::endList()
endTransfer();
}
void FTPClientSession::abort()
{
if (!isOpen())
@@ -361,7 +390,7 @@ void FTPClientSession::abort()
int status = sendCommand("ABOR", response);
if (status == 426)
status = _pControlSocket->receiveStatusMessage(response);
if (status != 226)
if (status != 226)
throw FTPException("Cannot abort transfer", response, status);
}
@@ -427,7 +456,7 @@ StreamSocket FTPClientSession::activeDataConnection(const std::string& command,
sendPortCommand(server.address());
std::string response;
int status = sendCommand(command, arg, response);
if (!isPositivePreliminary(status))
if (!isPositivePreliminary(status))
throw FTPException(command + " command failed", response, status);
if (server.poll(_timeout, Socket::SELECT_READ))
return server.acceptConnection();
@@ -445,7 +474,7 @@ StreamSocket FTPClientSession::passiveDataConnection(const std::string& command,
sock.setSendTimeout(_timeout);
std::string response;
int status = sendCommand(command, arg, response);
if (!isPositivePreliminary(status))
if (!isPositivePreliminary(status))
throw FTPException(command + " command failed", response, status);
return sock;
}
@@ -513,7 +542,7 @@ void FTPClientSession::sendPORT(const SocketAddress& addr)
arg += NumberFormatter::format(port % 256);
std::string response;
int status = sendCommand("PORT", arg, response);
if (!isPositiveCompletion(status))
if (!isPositiveCompletion(status))
throw FTPException("PORT command failed", response, status);
}
@@ -539,7 +568,7 @@ void FTPClientSession::sendPASV(SocketAddress& addr)
{
std::string response;
int status = sendCommand("PASV", response);
if (!isPositiveCompletion(status))
if (!isPositiveCompletion(status))
throw FTPException("PASV command failed", response, status);
parseAddress(response, addr);
}
@@ -580,8 +609,8 @@ void FTPClientSession::parseExtAddress(const std::string& str, SocketAddress& ad
if (it != end && *it == delim) ++it;
if (it != end && *it == delim) ++it;
Poco::UInt16 port = 0;
while (it != end && Poco::Ascii::isDigit(*it)) { port *= 10; port += *it++ - '0'; }
addr = SocketAddress(_pControlSocket->peerAddress().host(), port);
while (it != end && Poco::Ascii::isDigit(*it)) { port *= 10; port += *it++ - '0'; }
addr = SocketAddress(_pControlSocket->peerAddress().host(), port);
}
@@ -593,7 +622,7 @@ void FTPClientSession::endTransfer()
_pDataStream = 0;
std::string response;
int status = _pControlSocket->receiveStatusMessage(response);
if (!isPositiveCompletion(status))
if (!isPositiveCompletion(status))
throw FTPException("Data transfer failed", response, status);
}
}