FtpInitiateConnectionCommand.cc 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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(getRequest()->getProtocol());
  78. if(proxyMethod == V_GET) {
  79. pooledSocket = getDownloadEngine()->popPooledSocket
  80. (getRequest()->getHost(), getRequest()->getPort(),
  81. proxyRequest->getHost(), proxyRequest->getPort());
  82. } else {
  83. pooledSocket = getDownloadEngine()->popPooledSocket
  84. (options, getRequest()->getHost(), getRequest()->getPort(),
  85. getDownloadEngine()->getAuthConfigFactory()->createAuthConfig
  86. (getRequest(), 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. createSocket();
  95. getSocket()->establishConnection(addr, port);
  96. getRequest()->setConnectedAddrInfo(hostname, addr, port);
  97. if(proxyMethod == V_GET) {
  98. // Use GET for FTP via HTTP proxy.
  99. getRequest()->setMethod(Request::METHOD_GET);
  100. SharedHandle<HttpConnection> hc
  101. (new HttpConnection(getCuid(), getSocket()));
  102. HttpRequestCommand* c =
  103. new HttpRequestCommand(getCuid(), getRequest(), getFileEntry(),
  104. getRequestGroup(), hc, getDownloadEngine(),
  105. getSocket());
  106. c->setProxyRequest(proxyRequest);
  107. command = c;
  108. } else if(proxyMethod == V_TUNNEL) {
  109. FtpTunnelRequestCommand* c =
  110. new FtpTunnelRequestCommand(getCuid(), getRequest(), getFileEntry(),
  111. getRequestGroup(), getDownloadEngine(),
  112. proxyRequest, getSocket());
  113. command = c;
  114. } else {
  115. // TODO
  116. throw DL_ABORT_EX("ERROR");
  117. }
  118. } else {
  119. setConnectedAddrInfo(getRequest(), hostname, pooledSocket);
  120. if(proxyMethod == V_TUNNEL) {
  121. command =
  122. new FtpNegotiationCommand(getCuid(), getRequest(), getFileEntry(),
  123. getRequestGroup(), getDownloadEngine(),
  124. pooledSocket,
  125. FtpNegotiationCommand::SEQ_SEND_CWD_PREP,
  126. options["baseWorkingDir"]);
  127. } else if(proxyMethod == V_GET) {
  128. // Use GET for FTP via HTTP proxy.
  129. getRequest()->setMethod(Request::METHOD_GET);
  130. SharedHandle<HttpConnection> hc
  131. (new HttpConnection(getCuid(), pooledSocket));
  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.isNull()) {
  152. if(getLogger()->info()) {
  153. getLogger()->info(MSG_CONNECTING_TO_SERVER,
  154. util::itos(getCuid()).c_str(), addr.c_str(), port);
  155. }
  156. createSocket();
  157. getSocket()->establishConnection(addr, port);
  158. FtpNegotiationCommand* c =
  159. new FtpNegotiationCommand(getCuid(), getRequest(), getFileEntry(),
  160. getRequestGroup(), getDownloadEngine(),
  161. getSocket());
  162. getRequest()->setConnectedAddrInfo(hostname, addr, port);
  163. command = c;
  164. } else {
  165. command =
  166. new FtpNegotiationCommand(getCuid(), getRequest(), getFileEntry(),
  167. getRequestGroup(), getDownloadEngine(),
  168. pooledSocket,
  169. FtpNegotiationCommand::SEQ_SEND_CWD_PREP,
  170. options["baseWorkingDir"]);
  171. setConnectedAddrInfo(getRequest(), hostname, pooledSocket);
  172. }
  173. }
  174. return command;
  175. }
  176. } // namespace aria2