SF [2512027] FTPClientSession reconnect capability

removed unused variable in Socket::poll()
This commit is contained in:
Aleksandar Fabijanic 2009-02-26 02:37:29 +00:00
parent b99f4b6d1f
commit 9bef44cab6
5 changed files with 290 additions and 70 deletions

View File

@ -80,15 +80,24 @@ public:
TYPE_BINARY // TYPE I (Image)
};
FTPClientSession();
/// Creates an FTPClientSession.
///
/// Passive mode will be used for data transfers.
explicit FTPClientSession(const StreamSocket& socket);
/// 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);
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.
/// to the given host and port. If username is supplied,
/// login is attempted.
///
/// Passive mode will be used for data transfers.
@ -106,7 +115,14 @@ public:
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 = "");
/// 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);
/// Authenticates the user against the FTP server. Must be
/// called before any other commands (except QUIT) can be sent.
@ -117,6 +133,8 @@ public:
/// Throws a FTPException in case of a FTP-specific error, or a
/// NetException in case of a general network communication failure.
void logout();
void close();
/// Sends a QUIT command and closes the connection to the server.
///
@ -295,6 +313,12 @@ public:
/// Sends the given command verbatim to the server
/// and waits for a response.
bool isOpen() const;
/// Returns true if the connection with FTP server is opened.
bool isLoggedIn() const;
/// Returns true if the session is logged in.
protected:
enum StatusClass
{
@ -329,16 +353,18 @@ protected:
void endTransfer();
private:
FTPClientSession();
FTPClientSession(const FTPClientSession&);
FTPClientSession& operator = (const FTPClientSession&);
DialogSocket _controlSocket;
std::string _host;
Poco::UInt16 _port;
DialogSocket* _pControlSocket;
SocketStream* _pDataStream;
bool _passiveMode;
FileType _fileType;
bool _supports1738;
bool _isOpen;
bool _serverReady;
bool _isLoggedIn;
Poco::Timespan _timeout;
};
@ -376,6 +402,18 @@ inline bool FTPClientSession::isPermanentNegative(int status)
}
inline bool FTPClientSession::isOpen() const
{
return _pControlSocket != 0;
}
inline bool FTPClientSession::isLoggedIn() const
{
return _isLoggedIn;
}
} } // namespace Poco::Net

View File

@ -50,48 +50,72 @@ namespace Poco {
namespace Net {
FTPClientSession::FTPClientSession(const StreamSocket& socket):
_controlSocket(socket),
FTPClientSession::FTPClientSession():
_port(0),
_pControlSocket(0),
_pDataStream(0),
_passiveMode(true),
_fileType(TYPE_BINARY),
_supports1738(true),
_isOpen(true),
_serverReady(false),
_isLoggedIn(false),
_timeout(DEFAULT_TIMEOUT)
{
_controlSocket.setReceiveTimeout(_timeout);
}
FTPClientSession::FTPClientSession(const StreamSocket& socket):
_host(socket.address().host().toString()),
_port(socket.address().port()),
_pControlSocket(new DialogSocket(socket)),
_pDataStream(0),
_passiveMode(true),
_fileType(TYPE_BINARY),
_supports1738(true),
_serverReady(false),
_isLoggedIn(false),
_timeout(DEFAULT_TIMEOUT)
{
_pControlSocket->setReceiveTimeout(_timeout);
}
FTPClientSession::FTPClientSession(const std::string& host, Poco::UInt16 port):
_controlSocket(SocketAddress(host, port)),
_pDataStream(0),
_passiveMode(true),
_fileType(TYPE_BINARY),
_supports1738(true),
_isOpen(true),
_timeout(DEFAULT_TIMEOUT)
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),
_passiveMode(true),
_fileType(TYPE_BINARY),
_supports1738(true),
_serverReady(false),
_isLoggedIn(false),
_timeout(DEFAULT_TIMEOUT)
{
_controlSocket.setReceiveTimeout(_timeout);
if (!username.empty())
login(username, password);
else
_pControlSocket->setReceiveTimeout(_timeout);
}
FTPClientSession::~FTPClientSession()
{
try
{
close();
}
catch (...)
{
}
try { close(); }
catch (...) { }
}
void FTPClientSession::setTimeout(const Poco::Timespan& timeout)
{
if (!isOpen())
throw FTPException("Connection is closed.");
_timeout = timeout;
_controlSocket.setReceiveTimeout(timeout);
_pControlSocket->setReceiveTimeout(timeout);
}
@ -113,35 +137,78 @@ bool FTPClientSession::getPassive() const
}
void FTPClientSession::open(const std::string& host,
Poco::UInt16 port,
const std::string& username,
const std::string& password)
{
_host = host;
_port = port;
if (!username.empty())
login(username, password);
else
{
_pControlSocket = new DialogSocket(SocketAddress(_host, _port));
_pControlSocket->setReceiveTimeout(_timeout);
}
}
void FTPClientSession::login(const std::string& username, const std::string& password)
{
if (_isLoggedIn) logout();
int status = FTP_POSITIVE_COMPLETION * 100;
std::string response;
int status = _controlSocket.receiveStatusMessage(response);
if (!isPositiveCompletion(status)) throw FTPException("Cannot login to server", response);
if (!_pControlSocket)
{
_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);
_serverReady = true;
}
status = sendCommand("USER", username, response);
if (isPositiveIntermediate(status))
status = sendCommand("PASS", password, response);
if (!isPositiveCompletion(status)) throw FTPException("Login denied", response);
if (!isPositiveCompletion(status))
throw FTPException("Login denied", response);
setFileType(_fileType);
_isLoggedIn = true;
}
void FTPClientSession::logout()
{
if (!isOpen())
throw FTPException("Connection is closed.");
if (_isLoggedIn)
{
try { endTransfer(); }
catch (...) { }
std::string response;
sendCommand("QUIT", response);
_isLoggedIn = false;
}
}
void FTPClientSession::close()
{
if (_isOpen)
{
try
{
endTransfer();
}
catch (...)
{
}
std::string response;
sendCommand("QUIT", response);
_controlSocket.close();
_isOpen = false;
}
logout();
_pControlSocket->close();
delete _pControlSocket;
_pControlSocket = 0;
_serverReady = false;
}
@ -175,7 +242,8 @@ void FTPClientSession::setWorkingDirectory(const std::string& path)
{
std::string response;
int status = sendCommand("CWD", path, response);
if (!isPositiveCompletion(status)) throw FTPException("Cannot change directory", response);
if (!isPositiveCompletion(status))
throw FTPException("Cannot change directory", response);
}
@ -194,7 +262,8 @@ void FTPClientSession::cdup()
{
std::string response;
int status = sendCommand("CDUP", response);
if (!isPositiveCompletion(status)) throw FTPException("Cannot change directory", response);
if (!isPositiveCompletion(status))
throw FTPException("Cannot change directory", response);
}
@ -202,9 +271,11 @@ void FTPClientSession::rename(const std::string& oldName, const std::string& new
{
std::string response;
int status = sendCommand("RNFR", oldName, response);
if (!isPositiveIntermediate(status)) throw FTPException(std::string("Cannot rename ") + oldName, response);
if (!isPositiveIntermediate(status))
throw FTPException(std::string("Cannot rename ") + oldName, response);
status = sendCommand("RNTO", newName, response);
if (!isPositiveCompletion(status)) throw FTPException(std::string("Cannot rename to ") + newName, response);
if (!isPositiveCompletion(status))
throw FTPException(std::string("Cannot rename to ") + newName, response);
}
@ -212,7 +283,8 @@ void FTPClientSession::remove(const std::string& path)
{
std::string response;
int status = sendCommand("DELE", path, response);
if (!isPositiveCompletion(status)) throw FTPException(std::string("Cannot remove " + path), response);
if (!isPositiveCompletion(status))
throw FTPException(std::string("Cannot remove " + path), response);
}
@ -220,7 +292,8 @@ void FTPClientSession::createDirectory(const std::string& path)
{
std::string response;
int status = sendCommand("MKD", path, response);
if (!isPositiveCompletion(status)) throw FTPException(std::string("Cannot create directory ") + path, response);
if (!isPositiveCompletion(status))
throw FTPException(std::string("Cannot create directory ") + path, response);
}
@ -228,12 +301,16 @@ void FTPClientSession::removeDirectory(const std::string& path)
{
std::string response;
int status = sendCommand("RMD", path, response);
if (!isPositiveCompletion(status)) throw FTPException(std::string("Cannot remove directory ") + path, response);
if (!isPositiveCompletion(status))
throw FTPException(std::string("Cannot remove directory ") + path, response);
}
std::istream& FTPClientSession::beginDownload(const std::string& path)
{
if (!isOpen())
throw FTPException("Connection is closed.");
delete _pDataStream;
_pDataStream = 0;
_pDataStream = new SocketStream(establishDataConnection("RETR", path));
@ -249,6 +326,9 @@ void FTPClientSession::endDownload()
std::ostream& FTPClientSession::beginUpload(const std::string& path)
{
if (!isOpen())
throw FTPException("Connection is closed.");
delete _pDataStream;
_pDataStream = 0;
_pDataStream = new SocketStream(establishDataConnection("STOR", path));
@ -264,6 +344,9 @@ void FTPClientSession::endUpload()
std::istream& FTPClientSession::beginList(const std::string& path, bool extended)
{
if (!isOpen())
throw FTPException("Connection is closed.");
delete _pDataStream;
_pDataStream = 0;
_pDataStream = new SocketStream(establishDataConnection(extended ? "LIST" : "NLST", path));
@ -279,27 +362,36 @@ void FTPClientSession::endList()
void FTPClientSession::abort()
{
_controlSocket.sendByte(DialogSocket::TELNET_IP);
_controlSocket.synch();
if (!isOpen())
throw FTPException("Connection is closed.");
_pControlSocket->sendByte(DialogSocket::TELNET_IP);
_pControlSocket->synch();
std::string response;
int status = sendCommand("ABOR", response);
if (status == 426)
status = _controlSocket.receiveStatusMessage(response);
status = _pControlSocket->receiveStatusMessage(response);
if (status != 226) throw FTPException("Cannot abort transfer", response);
}
int FTPClientSession::sendCommand(const std::string& command, std::string& response)
{
_controlSocket.sendMessage(command);
return _controlSocket.receiveStatusMessage(response);
if (!isOpen())
throw FTPException("Connection is closed.");
_pControlSocket->sendMessage(command);
return _pControlSocket->receiveStatusMessage(response);
}
int FTPClientSession::sendCommand(const std::string& command, const std::string& arg, std::string& response)
{
_controlSocket.sendMessage(command, arg);
return _controlSocket.receiveStatusMessage(response);
if (!isOpen())
throw FTPException("Connection is closed.");
_pControlSocket->sendMessage(command, arg);
return _pControlSocket->receiveStatusMessage(response);
}
@ -337,7 +429,10 @@ StreamSocket FTPClientSession::establishDataConnection(const std::string& comman
StreamSocket FTPClientSession::activeDataConnection(const std::string& command, const std::string& arg)
{
ServerSocket server(SocketAddress(_controlSocket.address().host(), 0));
if (!isOpen())
throw FTPException("Connection is closed.");
ServerSocket server(SocketAddress(_pControlSocket->address().host(), 0));
sendPortCommand(server.address());
std::string response;
int status = sendCommand(command, arg, response);
@ -488,18 +583,21 @@ void FTPClientSession::parseExtAddress(const std::string& str, SocketAddress& ad
if (it != end && *it == delim) ++it;
Poco::UInt16 port = 0;
while (it != end && std::isdigit(*it)) { port *= 10; port += *it++ - '0'; }
addr = SocketAddress(_controlSocket.peerAddress().host(), port);
addr = SocketAddress(_pControlSocket->peerAddress().host(), port);
}
void FTPClientSession::endTransfer()
{
if (!isOpen())
throw FTPException("Connection is closed.");
if (_pDataStream)
{
delete _pDataStream;
_pDataStream = 0;
std::string response;
int status = _controlSocket.receiveStatusMessage(response);
int status = _pControlSocket->receiveStatusMessage(response);
if (!isPositiveCompletion(status)) throw FTPException("Data transfer failed", response);
}
}

View File

@ -160,7 +160,6 @@ int Socket::poll(SocketList& readList, SocketList& writeList, SocketList& except
SocketList readyWriteList;
SocketList readyExceptList;
int count = 0;
SocketList::iterator begE = exceptList.begin();
SocketList::iterator endE = exceptList.end();
for (int idx = 0; idx < nfd; ++idx)

View File

@ -93,14 +93,11 @@ FTPClientSessionTest::~FTPClientSessionTest()
}
void FTPClientSessionTest::testLogin()
void FTPClientSessionTest::login(DialogServer& server, FTPClientSession& session)
{
DialogServer server;
server.addResponse("220 localhost FTP ready");
server.addResponse("331 Password required");
server.addResponse("230 Welcome");
server.addResponse("200 Type set to I");
FTPClientSession session("localhost", server.port());
session.login("user", "password");
std::string cmd = server.popCommand();
assert (cmd == "USER user");
@ -110,12 +107,86 @@ void FTPClientSessionTest::testLogin()
assert (cmd == "TYPE I");
assert (session.getFileType() == FTPClientSession::TYPE_BINARY);
}
void FTPClientSessionTest::testLogin1()
{
DialogServer server;
server.addResponse("220 localhost FTP ready");
FTPClientSession session("localhost", server.port());
assert (session.isOpen());
assert (!session.isLoggedIn());
login(server, session);
assert (session.isOpen());
assert (session.isLoggedIn());
server.addResponse("221 Good Bye");
session.logout();
assert (session.isOpen());
assert (!session.isLoggedIn());
server.clearCommands();
server.clearResponses();
login(server, session);
assert (session.isOpen());
assert (session.isLoggedIn());
server.addResponse("221 Good Bye");
session.close();
assert (!session.isOpen());
assert (!session.isLoggedIn());
}
void FTPClientSessionTest::testLogin2()
{
DialogServer server;
server.addResponse("220 localhost FTP ready");
server.addResponse("331 Password required");
server.addResponse("230 Welcome");
server.addResponse("200 Type set to I");
FTPClientSession session("localhost", server.port(), "user", "password");
assert (session.isOpen());
assert (session.isLoggedIn());
server.addResponse("221 Good Bye");
session.close();
assert (!session.isOpen());
assert (!session.isLoggedIn());
server.clearCommands();
server.clearResponses();
server.addResponse("220 localhost FTP ready");
server.addResponse("331 Password required");
server.addResponse("230 Welcome");
server.addResponse("200 Type set to I");
session.open("localhost", server.port(), "user", "password");
assert (session.isOpen());
assert (session.isLoggedIn());
server.addResponse("221 Good Bye");
session.close();
assert (!session.isOpen());
assert (!session.isLoggedIn());
}
void FTPClientSessionTest::testLogin3()
{
DialogServer server;
server.addResponse("220 localhost FTP ready");
server.addResponse("331 Password required");
server.addResponse("230 Welcome");
server.addResponse("200 Type set to I");
FTPClientSession session;
assert (!session.isOpen());
assert (!session.isLoggedIn());
session.open("localhost", server.port(), "user", "password");
server.addResponse("221 Good Bye");
session.close();
assert (!session.isOpen());
assert (!session.isLoggedIn());
}
void FTPClientSessionTest::testLoginFailed1()
{
DialogServer server;
@ -535,7 +606,9 @@ CppUnit::Test* FTPClientSessionTest::suite()
{
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("FTPClientSessionTest");
CppUnit_addTest(pSuite, FTPClientSessionTest, testLogin);
CppUnit_addTest(pSuite, FTPClientSessionTest, testLogin1);
CppUnit_addTest(pSuite, FTPClientSessionTest, testLogin2);
CppUnit_addTest(pSuite, FTPClientSessionTest, testLogin3);
CppUnit_addTest(pSuite, FTPClientSessionTest, testLoginFailed1);
CppUnit_addTest(pSuite, FTPClientSessionTest, testLoginFailed2);
CppUnit_addTest(pSuite, FTPClientSessionTest, testCommands);

View File

@ -40,13 +40,24 @@
#include "CppUnit/TestCase.h"
namespace Poco {
namespace Net {
class FTPClientSession;
} }
class DialogServer;
class FTPClientSessionTest: public CppUnit::TestCase
{
public:
FTPClientSessionTest(const std::string& name);
~FTPClientSessionTest();
void testLogin();
void testLogin1();
void testLogin2();
void testLogin3();
void testLoginFailed1();
void testLoginFailed2();
void testCommands();
@ -63,6 +74,7 @@ public:
static CppUnit::Test* suite();
private:
void login(DialogServer& server, Poco::Net::FTPClientSession& session);
};