FtpInitiateConnectionCommand.cc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. namespace aria2 {
  54. FtpInitiateConnectionCommand::FtpInitiateConnectionCommand
  55. (cuid_t cuid,
  56. const SharedHandle<Request>& req,
  57. const SharedHandle<FileEntry>& fileEntry,
  58. RequestGroup* requestGroup,
  59. DownloadEngine* e)
  60. :InitiateConnectionCommand(cuid, req, fileEntry, requestGroup, e) {}
  61. FtpInitiateConnectionCommand::~FtpInitiateConnectionCommand() {}
  62. Command* FtpInitiateConnectionCommand::createNextCommand
  63. (const std::string& hostname, const std::string& addr, uint16_t port,
  64. const std::vector<std::string>& resolvedAddresses,
  65. const SharedHandle<Request>& proxyRequest)
  66. {
  67. Command* command;
  68. if(!proxyRequest.isNull()) {
  69. std::map<std::string, std::string> options;
  70. SharedHandle<SocketCore> pooledSocket =
  71. e->popPooledSocket(options, req->getHost(), req->getPort());
  72. std::string proxyMethod = resolveProxyMethod(req->getProtocol());
  73. if(pooledSocket.isNull()) {
  74. if(logger->info()) {
  75. logger->info(MSG_CONNECTING_TO_SERVER,
  76. util::itos(cuid).c_str(), addr.c_str(), port);
  77. }
  78. socket.reset(new SocketCore());
  79. socket->establishConnection(addr, port);
  80. if(proxyMethod == V_GET) {
  81. // Use GET for FTP via HTTP proxy.
  82. req->setMethod(Request::METHOD_GET);
  83. SharedHandle<HttpConnection> hc
  84. (new HttpConnection(cuid, socket, getOption().get()));
  85. HttpRequestCommand* c =
  86. new HttpRequestCommand(cuid, req, _fileEntry,
  87. _requestGroup, hc, e, socket);
  88. c->setConnectedAddr(hostname, addr, port);
  89. c->setProxyRequest(proxyRequest);
  90. command = c;
  91. } else if(proxyMethod == V_TUNNEL) {
  92. FtpTunnelRequestCommand* c =
  93. new FtpTunnelRequestCommand(cuid, req, _fileEntry,
  94. _requestGroup, e,
  95. proxyRequest, socket);
  96. c->setConnectedAddr(hostname, addr, port);
  97. command = c;
  98. } else {
  99. // TODO
  100. throw DL_ABORT_EX("ERROR");
  101. }
  102. } else {
  103. if(proxyMethod == V_TUNNEL) {
  104. command =
  105. new FtpNegotiationCommand(cuid, req, _fileEntry,
  106. _requestGroup, e, pooledSocket,
  107. FtpNegotiationCommand::SEQ_SEND_CWD,
  108. options["baseWorkingDir"]);
  109. } else if(proxyMethod == V_GET) {
  110. // Use GET for FTP via HTTP proxy.
  111. req->setMethod(Request::METHOD_GET);
  112. SharedHandle<HttpConnection> hc
  113. (new HttpConnection(cuid, pooledSocket, getOption().get()));
  114. HttpRequestCommand* c =
  115. new HttpRequestCommand(cuid, req, _fileEntry,
  116. _requestGroup, hc, e, pooledSocket);
  117. c->setProxyRequest(proxyRequest);
  118. command = c;
  119. } else {
  120. // TODO
  121. throw DL_ABORT_EX("ERROR");
  122. }
  123. }
  124. } else {
  125. std::map<std::string, std::string> options;
  126. SharedHandle<SocketCore> pooledSocket =
  127. e->popPooledSocket(options, resolvedAddresses, req->getPort());
  128. if(pooledSocket.isNull()) {
  129. if(logger->info()) {
  130. logger->info(MSG_CONNECTING_TO_SERVER,
  131. util::itos(cuid).c_str(), addr.c_str(), port);
  132. }
  133. socket.reset(new SocketCore());
  134. socket->establishConnection(addr, port);
  135. FtpNegotiationCommand* c =
  136. new FtpNegotiationCommand(cuid, req, _fileEntry,
  137. _requestGroup, e, socket);
  138. c->setConnectedAddr(hostname, addr, port);
  139. command = c;
  140. } else {
  141. command =
  142. new FtpNegotiationCommand(cuid, req, _fileEntry,
  143. _requestGroup, e, pooledSocket,
  144. FtpNegotiationCommand::SEQ_SEND_CWD,
  145. options["baseWorkingDir"]);
  146. }
  147. }
  148. return command;
  149. }
  150. } // namespace aria2