FtpInitiateConnectionCommand.cc 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2006 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #include "FtpInitiateConnectionCommand.h"
  36. #include <map>
  37. #include "DownloadEngine.h"
  38. #include "Option.h"
  39. #include "Request.h"
  40. #include "FtpNegotiationCommand.h"
  41. #include "HttpRequest.h"
  42. #include "Segment.h"
  43. #include "HttpRequestCommand.h"
  44. #include "FtpTunnelRequestCommand.h"
  45. #include "DlAbortEx.h"
  46. #include "Logger.h"
  47. #include "message.h"
  48. #include "prefs.h"
  49. #include "HttpConnection.h"
  50. #include "Socket.h"
  51. #include "DownloadContext.h"
  52. #include "util.h"
  53. #include "AuthConfigFactory.h"
  54. #include "AuthConfig.h"
  55. #include "RequestGroupMan.h"
  56. #include "FileAllocationEntry.h"
  57. #include "CheckIntegrityEntry.h"
  58. #include "ServerStatMan.h"
  59. namespace aria2 {
  60. FtpInitiateConnectionCommand::FtpInitiateConnectionCommand
  61. (cuid_t cuid,
  62. const SharedHandle<Request>& req,
  63. const SharedHandle<FileEntry>& fileEntry,
  64. RequestGroup* requestGroup,
  65. DownloadEngine* e)
  66. :InitiateConnectionCommand(cuid, req, fileEntry, requestGroup, e) {}
  67. FtpInitiateConnectionCommand::~FtpInitiateConnectionCommand() {}
  68. Command* FtpInitiateConnectionCommand::createNextCommand
  69. (const std::string& hostname, const std::string& addr, uint16_t port,
  70. const std::vector<std::string>& resolvedAddresses,
  71. const SharedHandle<Request>& proxyRequest)
  72. {
  73. Command* command;
  74. if(!proxyRequest.isNull()) {
  75. std::map<std::string, std::string> options;
  76. SharedHandle<SocketCore> pooledSocket;
  77. std::string proxyMethod = resolveProxyMethod(req->getProtocol());
  78. if(proxyMethod == V_GET) {
  79. pooledSocket = e->popPooledSocket
  80. (req->getHost(), req->getPort(),
  81. proxyRequest->getHost(), proxyRequest->getPort());
  82. } else {
  83. pooledSocket = e->popPooledSocket
  84. (options, req->getHost(), req->getPort(),
  85. e->getAuthConfigFactory()->createAuthConfig
  86. (req, getOption().get())->getUser(),
  87. proxyRequest->getHost(), proxyRequest->getPort());
  88. }
  89. if(pooledSocket.isNull()) {
  90. if(getLogger()->info()) {
  91. getLogger()->info(MSG_CONNECTING_TO_SERVER,
  92. util::itos(getCuid()).c_str(), addr.c_str(), port);
  93. }
  94. socket.reset(new SocketCore());
  95. socket->establishConnection(addr, port);
  96. if(proxyMethod == V_GET) {
  97. // Use GET for FTP via HTTP proxy.
  98. req->setMethod(Request::METHOD_GET);
  99. SharedHandle<HttpConnection> hc
  100. (new HttpConnection(getCuid(), socket, getOption().get()));
  101. HttpRequestCommand* c =
  102. new HttpRequestCommand(getCuid(), req, _fileEntry,
  103. _requestGroup, hc, e, socket);
  104. c->setConnectedAddr(hostname, addr, port);
  105. c->setProxyRequest(proxyRequest);
  106. command = c;
  107. } else if(proxyMethod == V_TUNNEL) {
  108. FtpTunnelRequestCommand* c =
  109. new FtpTunnelRequestCommand(getCuid(), req, _fileEntry,
  110. _requestGroup, e,
  111. proxyRequest, socket);
  112. c->setConnectedAddr(hostname, addr, port);
  113. command = c;
  114. } else {
  115. // TODO
  116. throw DL_ABORT_EX("ERROR");
  117. }
  118. } else {
  119. if(proxyMethod == V_TUNNEL) {
  120. command =
  121. new FtpNegotiationCommand(getCuid(), req, _fileEntry,
  122. _requestGroup, e, pooledSocket,
  123. FtpNegotiationCommand::SEQ_SEND_CWD,
  124. options["baseWorkingDir"]);
  125. } else if(proxyMethod == V_GET) {
  126. // Use GET for FTP via HTTP proxy.
  127. req->setMethod(Request::METHOD_GET);
  128. SharedHandle<HttpConnection> hc
  129. (new HttpConnection(getCuid(), pooledSocket, getOption().get()));
  130. HttpRequestCommand* c =
  131. new HttpRequestCommand(getCuid(), req, _fileEntry,
  132. _requestGroup, hc, e, pooledSocket);
  133. c->setProxyRequest(proxyRequest);
  134. command = c;
  135. } else {
  136. // TODO
  137. throw DL_ABORT_EX("ERROR");
  138. }
  139. }
  140. } else {
  141. std::map<std::string, std::string> options;
  142. SharedHandle<SocketCore> pooledSocket =
  143. e->popPooledSocket(options, resolvedAddresses, req->getPort(),
  144. e->getAuthConfigFactory()->createAuthConfig
  145. (req, getOption().get())->getUser());
  146. if(pooledSocket.isNull()) {
  147. if(getLogger()->info()) {
  148. getLogger()->info(MSG_CONNECTING_TO_SERVER,
  149. util::itos(getCuid()).c_str(), addr.c_str(), port);
  150. }
  151. socket.reset(new SocketCore());
  152. socket->establishConnection(addr, port);
  153. FtpNegotiationCommand* c =
  154. new FtpNegotiationCommand(getCuid(), req, _fileEntry,
  155. _requestGroup, e, socket);
  156. c->setConnectedAddr(hostname, addr, port);
  157. command = c;
  158. } else {
  159. command =
  160. new FtpNegotiationCommand(getCuid(), req, _fileEntry,
  161. _requestGroup, e, pooledSocket,
  162. FtpNegotiationCommand::SEQ_SEND_CWD,
  163. options["baseWorkingDir"]);
  164. }
  165. }
  166. return command;
  167. }
  168. } // namespace aria2