FtpInitiateConnectionCommand.cc 7.2 KB

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