FtpInitiateConnectionCommand.cc 5.7 KB

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