/* */ #include "FtpInitiateConnectionCommand.h" #include #include "DownloadEngine.h" #include "Option.h" #include "Request.h" #include "FtpNegotiationCommand.h" #include "HttpRequest.h" #include "Segment.h" #include "HttpRequestCommand.h" #include "FtpTunnelRequestCommand.h" #include "DlAbortEx.h" #include "Logger.h" #include "LogFactory.h" #include "message.h" #include "prefs.h" #include "HttpConnection.h" #include "SocketCore.h" #include "util.h" #include "AuthConfigFactory.h" #include "AuthConfig.h" #include "fmt.h" #include "SocketRecvBuffer.h" #include "BackupIPv4ConnectCommand.h" #include "FtpNegotiationConnectChain.h" #include "FtpTunnelRequestConnectChain.h" #include "HttpRequestConnectChain.h" namespace aria2 { FtpInitiateConnectionCommand::FtpInitiateConnectionCommand (cuid_t cuid, const std::shared_ptr& req, const std::shared_ptr& fileEntry, RequestGroup* requestGroup, DownloadEngine* e) :InitiateConnectionCommand(cuid, req, fileEntry, requestGroup, e) {} FtpInitiateConnectionCommand::~FtpInitiateConnectionCommand() {} std::unique_ptr FtpInitiateConnectionCommand::createNextCommandProxied (const std::string& hostname, const std::string& addr, uint16_t port, const std::vector& resolvedAddresses, const std::shared_ptr& proxyRequest) { std::string options; std::shared_ptr pooledSocket; std::string proxyMethod = resolveProxyMethod(getRequest()->getProtocol()); if(proxyMethod == V_GET) { pooledSocket = getDownloadEngine()->popPooledSocket (getRequest()->getHost(), getRequest()->getPort(), proxyRequest->getHost(), proxyRequest->getPort()); } else { pooledSocket = getDownloadEngine()->popPooledSocket (options, getRequest()->getHost(), getRequest()->getPort(), getDownloadEngine()->getAuthConfigFactory()->createAuthConfig (getRequest(), getOption().get())->getUser(), proxyRequest->getHost(), proxyRequest->getPort()); } if(!pooledSocket) { A2_LOG_INFO(fmt(MSG_CONNECTING_TO_SERVER, getCuid(), addr.c_str(), port)); createSocket(); getSocket()->establishConnection(addr, port); getRequest()->setConnectedAddrInfo(hostname, addr, port); auto c = make_unique(getCuid(), getRequest(), proxyRequest, getFileEntry(), getRequestGroup(), getDownloadEngine(), getSocket()); if(proxyMethod == V_GET) { // Use GET for FTP via HTTP proxy. getRequest()->setMethod(Request::METHOD_GET); std::shared_ptr chain (new HttpRequestConnectChain()); c->setControlChain(chain); } else if(proxyMethod == V_TUNNEL) { std::shared_ptr chain (new FtpTunnelRequestConnectChain()); c->setControlChain(chain); } else { // Unreachable assert(0); return nullptr; } setupBackupConnection(hostname, addr, port, c.get()); return std::move(c); } setConnectedAddrInfo(getRequest(), hostname, pooledSocket); if(proxyMethod == V_TUNNEL) { // options contains "baseWorkingDir" return make_unique (getCuid(), getRequest(), getFileEntry(), getRequestGroup(), getDownloadEngine(), pooledSocket, FtpNegotiationCommand::SEQ_SEND_CWD_PREP, options); } if(proxyMethod != V_GET) { assert(0); return nullptr; } // Use GET for FTP via HTTP proxy. getRequest()->setMethod(Request::METHOD_GET); std::shared_ptr socketRecvBuffer (new SocketRecvBuffer(pooledSocket)); std::shared_ptr hc (new HttpConnection(getCuid(), pooledSocket, socketRecvBuffer)); auto c = make_unique(getCuid(), getRequest(), getFileEntry(), getRequestGroup(), hc, getDownloadEngine(), pooledSocket); c->setProxyRequest(proxyRequest); return std::move(c); } std::unique_ptr FtpInitiateConnectionCommand::createNextCommandPlain (const std::string& hostname, const std::string& addr, uint16_t port, const std::vector& resolvedAddresses) { std::string options; std::shared_ptr pooledSocket = getDownloadEngine()->popPooledSocket (options, resolvedAddresses, getRequest()->getPort(), getDownloadEngine()->getAuthConfigFactory()->createAuthConfig (getRequest(), getOption().get())->getUser()); if(!pooledSocket) { A2_LOG_INFO(fmt(MSG_CONNECTING_TO_SERVER, getCuid(), addr.c_str(), port)); createSocket(); getSocket()->establishConnection(addr, port); getRequest()->setConnectedAddrInfo(hostname, addr, port); auto c = make_unique(getCuid(), getRequest(), nullptr, getFileEntry(), getRequestGroup(), getDownloadEngine(), getSocket()); std::shared_ptr chain (new FtpNegotiationConnectChain()); c->setControlChain(chain); setupBackupConnection(hostname, addr, port, c.get()); return std::move(c); } // options contains "baseWorkingDir" auto command = make_unique (getCuid(), getRequest(), getFileEntry(), getRequestGroup(), getDownloadEngine(), pooledSocket, FtpNegotiationCommand::SEQ_SEND_CWD_PREP, options); setConnectedAddrInfo(getRequest(), hostname, pooledSocket); return std::move(command); } std::unique_ptr FtpInitiateConnectionCommand::createNextCommand (const std::string& hostname, const std::string& addr, uint16_t port, const std::vector& resolvedAddresses, const std::shared_ptr& proxyRequest) { if (proxyRequest) { return createNextCommandProxied(hostname, addr, port, resolvedAddresses, proxyRequest); } return createNextCommandPlain(hostname, addr, port, resolvedAddresses); } } // namespace aria2