FtpInitiateConnectionCommand.cc 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 "LogFactory.h"
  48. #include "message.h"
  49. #include "prefs.h"
  50. #include "HttpConnection.h"
  51. #include "Socket.h"
  52. #include "util.h"
  53. #include "AuthConfigFactory.h"
  54. #include "AuthConfig.h"
  55. #include "fmt.h"
  56. namespace aria2 {
  57. FtpInitiateConnectionCommand::FtpInitiateConnectionCommand
  58. (cuid_t cuid,
  59. const SharedHandle<Request>& req,
  60. const SharedHandle<FileEntry>& fileEntry,
  61. RequestGroup* requestGroup,
  62. DownloadEngine* e)
  63. :InitiateConnectionCommand(cuid, req, fileEntry, requestGroup, e) {}
  64. FtpInitiateConnectionCommand::~FtpInitiateConnectionCommand() {}
  65. Command* FtpInitiateConnectionCommand::createNextCommand
  66. (const std::string& hostname, const std::string& addr, uint16_t port,
  67. const std::vector<std::string>& resolvedAddresses,
  68. const SharedHandle<Request>& proxyRequest)
  69. {
  70. Command* command;
  71. if(proxyRequest) {
  72. std::map<std::string, std::string> options;
  73. SharedHandle<SocketCore> pooledSocket;
  74. std::string proxyMethod = resolveProxyMethod(getRequest()->getProtocol());
  75. if(proxyMethod == V_GET) {
  76. pooledSocket = getDownloadEngine()->popPooledSocket
  77. (getRequest()->getHost(), getRequest()->getPort(),
  78. proxyRequest->getHost(), proxyRequest->getPort());
  79. } else {
  80. pooledSocket = getDownloadEngine()->popPooledSocket
  81. (options, getRequest()->getHost(), getRequest()->getPort(),
  82. getDownloadEngine()->getAuthConfigFactory()->createAuthConfig
  83. (getRequest(), getOption().get())->getUser(),
  84. proxyRequest->getHost(), proxyRequest->getPort());
  85. }
  86. if(!pooledSocket) {
  87. A2_LOG_INFO(fmt(MSG_CONNECTING_TO_SERVER,
  88. getCuid(), addr.c_str(), port));
  89. createSocket();
  90. getSocket()->establishConnection(addr, port);
  91. getRequest()->setConnectedAddrInfo(hostname, addr, port);
  92. if(proxyMethod == V_GET) {
  93. // Use GET for FTP via HTTP proxy.
  94. getRequest()->setMethod(Request::METHOD_GET);
  95. SharedHandle<HttpConnection> hc
  96. (new HttpConnection(getCuid(), getSocket()));
  97. HttpRequestCommand* c =
  98. new HttpRequestCommand(getCuid(), getRequest(), getFileEntry(),
  99. getRequestGroup(), hc, getDownloadEngine(),
  100. getSocket());
  101. c->setProxyRequest(proxyRequest);
  102. command = c;
  103. } else if(proxyMethod == V_TUNNEL) {
  104. FtpTunnelRequestCommand* c =
  105. new FtpTunnelRequestCommand(getCuid(), getRequest(), getFileEntry(),
  106. getRequestGroup(), getDownloadEngine(),
  107. proxyRequest, getSocket());
  108. command = c;
  109. } else {
  110. // TODO
  111. throw DL_ABORT_EX("ERROR");
  112. }
  113. } else {
  114. setConnectedAddrInfo(getRequest(), hostname, pooledSocket);
  115. if(proxyMethod == V_TUNNEL) {
  116. command =
  117. new FtpNegotiationCommand(getCuid(), getRequest(), getFileEntry(),
  118. getRequestGroup(), getDownloadEngine(),
  119. pooledSocket,
  120. FtpNegotiationCommand::SEQ_SEND_CWD_PREP,
  121. options["baseWorkingDir"]);
  122. } else if(proxyMethod == V_GET) {
  123. // Use GET for FTP via HTTP proxy.
  124. getRequest()->setMethod(Request::METHOD_GET);
  125. SharedHandle<HttpConnection> hc
  126. (new HttpConnection(getCuid(), pooledSocket));
  127. HttpRequestCommand* c =
  128. new HttpRequestCommand(getCuid(), getRequest(), getFileEntry(),
  129. getRequestGroup(), hc, getDownloadEngine(),
  130. pooledSocket);
  131. c->setProxyRequest(proxyRequest);
  132. command = c;
  133. } else {
  134. // TODO
  135. throw DL_ABORT_EX("ERROR");
  136. }
  137. }
  138. } else {
  139. std::map<std::string, std::string> options;
  140. SharedHandle<SocketCore> pooledSocket =
  141. getDownloadEngine()->popPooledSocket
  142. (options, resolvedAddresses,
  143. getRequest()->getPort(),
  144. getDownloadEngine()->getAuthConfigFactory()->createAuthConfig
  145. (getRequest(), getOption().get())->getUser());
  146. if(!pooledSocket) {
  147. A2_LOG_INFO(fmt(MSG_CONNECTING_TO_SERVER,
  148. getCuid(), addr.c_str(), port));
  149. createSocket();
  150. getSocket()->establishConnection(addr, port);
  151. FtpNegotiationCommand* c =
  152. new FtpNegotiationCommand(getCuid(), getRequest(), getFileEntry(),
  153. getRequestGroup(), getDownloadEngine(),
  154. getSocket());
  155. getRequest()->setConnectedAddrInfo(hostname, addr, port);
  156. command = c;
  157. } else {
  158. command =
  159. new FtpNegotiationCommand(getCuid(), getRequest(), getFileEntry(),
  160. getRequestGroup(), getDownloadEngine(),
  161. pooledSocket,
  162. FtpNegotiationCommand::SEQ_SEND_CWD_PREP,
  163. options["baseWorkingDir"]);
  164. setConnectedAddrInfo(getRequest(), hostname, pooledSocket);
  165. }
  166. }
  167. return command;
  168. }
  169. } // namespace aria2