void FTPClientSession::login(const std::string& username, const std::string& password) { if (_isLoggedIn) logout(); int status = FTP_POSITIVE_COMPLETION * 100; std::string 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, status); _serverReady = true; } status = sendCommand("USER", username, response); if (isPositiveIntermediate(status)) status = sendCommand("PASS", password, response); if (!isPositiveCompletion(status)) throw FTPException("Login denied", response, status); setFileType(_fileType); _isLoggedIn = true; }
void FTPClientSession::rename(const std::string& oldName, const std::string& newName) { std::string response; int status = sendCommand("RNFR", oldName, response); if (!isPositiveIntermediate(status)) throw FTPException(std::string("Cannot rename ") + oldName, response, status); status = sendCommand("RNTO", newName, response); if (!isPositiveCompletion(status)) throw FTPException(std::string("Cannot rename to ") + newName, response, status); }
void FTPClientSession::login(const std::string& username, const std::string& password) { std::string response; int status = _controlSocket.receiveStatusMessage(response); if (!isPositiveCompletion(status)) throw FTPException("Cannot login to server", response, status); status = sendCommand("USER", username, response); if (isPositiveIntermediate(status)) status = sendCommand("PASS", password, response); if (!isPositiveCompletion(status)) throw FTPException("Login denied", response, status); setFileType(_fileType); }
StreamSocket FTPClientSession::activeDataConnection(const std::string& command, const std::string& arg) { ServerSocket server(SocketAddress(_controlSocket.address().host(), 0)); sendPortCommand(server.address()); std::string response; int status = sendCommand(command, arg, response); if (!isPositivePreliminary(status)) throw FTPException(command + " command failed", response, status); if (server.poll(_timeout, Socket::SELECT_READ)) return server.acceptConnection(); else throw FTPException("The server has not initiated a data connection"); }
void FTPClientSession::abort() { 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 = _pControlSocket->receiveStatusMessage(response); if (status != 226) throw FTPException("Cannot abort transfer", response, status); }
void FTPClientSession::setFileType(FTPClientSession::FileType type) { std::string response; int status = sendCommand("TYPE", (type == TYPE_TEXT ? "A" : "I"), response); if (!isPositiveCompletion(status)) throw FTPException("Cannot set file type", response, status); _fileType = type; }
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, status); }
void FTPClientSession::cdup() { std::string response; int status = sendCommand("CDUP", response); if (!isPositiveCompletion(status)) throw FTPException("Cannot change directory", response, status); }
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, status); }
void FTPClientSession::sendPASV(SocketAddress& addr) { std::string response; int status = sendCommand("PASV", response); if (!isPositiveCompletion(status)) throw FTPException("PASV command failed", response, status); parseAddress(response, addr); }
void FTPClientSession::setTimeout(const Poco::Timespan& timeout) { if (!isOpen()) throw FTPException("Connection is closed."); _timeout = timeout; _pControlSocket->setReceiveTimeout(timeout); }
int FTPClientSession::sendCommand(const std::string& command, const std::string& arg, std::string& response) { if (!isOpen()) throw FTPException("Connection is closed."); _pControlSocket->sendMessage(command, arg); return _pControlSocket->receiveStatusMessage(response); }
std::string FTPClientSession::getWorkingDirectory() { std::string response; int status = sendCommand("PWD", response); if (isPositiveCompletion(status)) return extractPath(response); else throw FTPException("Cannot get current working directory", response, status); }
std::string FTPClientSession::systemType() { std::string response; int status = sendCommand("SYST", response); if (isPositiveCompletion(status)) return response.substr(4); else throw FTPException("Cannot get remote system type", response, status); }
StreamSocket FTPClientSession::passiveDataConnection(const std::string& command, const std::string& arg) { SocketAddress sa(sendPassiveCommand()); StreamSocket sock(sa); std::string response; int status = sendCommand(command, arg, response); if (!isPositivePreliminary(status)) throw FTPException(command + " command failed", response, status); return sock; }
void FTPClientSession::abort() { _controlSocket.sendByte(DialogSocket::TELNET_IP); _controlSocket.synch(); std::string response; int status = sendCommand("ABOR", response); if (status == 426) status = _controlSocket.receiveStatusMessage(response); if (status != 226) throw FTPException("Cannot abort transfer", response, status); }
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)); return *_pDataStream; }
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)); return *_pDataStream; }
void FTPClientSession::endTransfer() { if (_pDataStream) { delete _pDataStream; _pDataStream = 0; std::string response; int status = _controlSocket.receiveStatusMessage(response); if (!isPositiveCompletion(status)) throw FTPException("Data transfer failed", response, status); } }
void FTPClientSession::logout() { if (!isOpen()) throw FTPException("Connection is closed."); if (_isLoggedIn) { try { endTransfer(); } catch (...) { } _isLoggedIn = false; std::string response; sendCommand("QUIT", response); } }
bool FTPClientSession::sendEPSV(SocketAddress& addr) { std::string response; int status = sendCommand("EPSV", response); if (isPositiveCompletion(status)) { parseExtAddress(response, addr); return true; } else if (isPermanentNegative(status)) { return false; } else throw FTPException("EPSV command failed", response, status); }
void FTPClientSession::sendPORT(const SocketAddress& addr) { std::string arg(addr.host().toString()); for (std::string::iterator it = arg.begin(); it != arg.end(); ++it) { if (*it == '.') *it = ','; } arg += ','; Poco::UInt16 port = addr.port(); arg += NumberFormatter::format(port/256); arg += ','; arg += NumberFormatter::format(port % 256); std::string response; int status = sendCommand("PORT", arg, response); if (!isPositiveCompletion(status)) throw FTPException("PORT command failed", response, status); }
void FTPStreamFactory::getUserInfo(const URI& uri, std::string& username, std::string& password) { splitUserInfo(uri.getUserInfo(), username, password); if (username.empty()) { username = "******"; password = _anonymousPassword; } else if (password.empty()) { if (_pPasswordProvider) password = _pPasswordProvider->password(username, uri.getHost()); else throw FTPException(std::string("Password required for ") + username + "@" + uri.getHost()); } }
bool FTPClientSession::sendEPRT(const SocketAddress& addr) { std::string arg("|"); arg += addr.af() == AF_INET ? '1' : '2'; arg += '|'; arg += addr.host().toString(); arg += '|'; arg += NumberFormatter::format(addr.port()); arg += '|'; std::string response; int status = sendCommand("EPRT", arg, response); if (isPositiveCompletion(status)) return true; else if (isPermanentNegative(status)) return false; else throw FTPException("EPRT command failed", response, status); }
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); }