HttpInitiateConnectionCommand.cc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 "HttpInitiateConnectionCommand.h"
  36. #include "Request.h"
  37. #include "DownloadEngine.h"
  38. #include "HttpConnection.h"
  39. #include "HttpRequest.h"
  40. #include "Segment.h"
  41. #include "HttpRequestCommand.h"
  42. #include "HttpProxyRequestCommand.h"
  43. #include "DlAbortEx.h"
  44. #include "Option.h"
  45. #include "Logger.h"
  46. #include "LogFactory.h"
  47. #include "SocketCore.h"
  48. #include "message.h"
  49. #include "prefs.h"
  50. #include "A2STR.h"
  51. #include "util.h"
  52. #include "fmt.h"
  53. #include "SocketRecvBuffer.h"
  54. #include "BackupIPv4ConnectCommand.h"
  55. #include "ConnectCommand.h"
  56. #include "HttpRequestConnectChain.h"
  57. #include "HttpProxyRequestConnectChain.h"
  58. namespace aria2 {
  59. HttpInitiateConnectionCommand::HttpInitiateConnectionCommand(
  60. cuid_t cuid, const std::shared_ptr<Request>& req,
  61. const std::shared_ptr<FileEntry>& fileEntry, RequestGroup* requestGroup,
  62. DownloadEngine* e)
  63. : InitiateConnectionCommand(cuid, req, fileEntry, requestGroup, e)
  64. {
  65. }
  66. HttpInitiateConnectionCommand::~HttpInitiateConnectionCommand() {}
  67. std::unique_ptr<Command> HttpInitiateConnectionCommand::createNextCommand(
  68. const std::string& hostname, const std::string& addr, uint16_t port,
  69. const std::vector<std::string>& resolvedAddresses,
  70. const std::shared_ptr<Request>& proxyRequest)
  71. {
  72. if (proxyRequest) {
  73. std::shared_ptr<SocketCore> pooledSocket =
  74. getDownloadEngine()->popPooledSocket(
  75. getRequest()->getHost(), getRequest()->getPort(),
  76. proxyRequest->getHost(), proxyRequest->getPort());
  77. std::string proxyMethod = resolveProxyMethod(getRequest()->getProtocol());
  78. if (!pooledSocket) {
  79. A2_LOG_INFO(fmt(MSG_CONNECTING_TO_SERVER, getCuid(), addr.c_str(), port));
  80. createSocket();
  81. getSocket()->establishConnection(addr, port);
  82. getRequest()->setConnectedAddrInfo(hostname, addr, port);
  83. auto c = make_unique<ConnectCommand>(
  84. getCuid(), getRequest(), proxyRequest, getFileEntry(),
  85. getRequestGroup(), getDownloadEngine(), getSocket());
  86. if (proxyMethod == V_TUNNEL) {
  87. c->setControlChain(std::make_shared<HttpProxyRequestConnectChain>());
  88. }
  89. else if (proxyMethod == V_GET) {
  90. c->setControlChain(std::make_shared<HttpRequestConnectChain>());
  91. }
  92. else {
  93. // Unreachable
  94. assert(0);
  95. }
  96. setupBackupConnection(hostname, addr, port, c.get());
  97. return std::move(c);
  98. }
  99. else {
  100. setConnectedAddrInfo(getRequest(), hostname, pooledSocket);
  101. auto c = make_unique<HttpRequestCommand>(
  102. getCuid(), getRequest(), getFileEntry(), getRequestGroup(),
  103. std::make_shared<HttpConnection>(
  104. getCuid(), pooledSocket,
  105. std::make_shared<SocketRecvBuffer>(pooledSocket)),
  106. getDownloadEngine(), pooledSocket);
  107. if (proxyMethod == V_GET) {
  108. c->setProxyRequest(proxyRequest);
  109. }
  110. return std::move(c);
  111. }
  112. }
  113. else {
  114. std::shared_ptr<SocketCore> pooledSocket =
  115. getDownloadEngine()->popPooledSocket(resolvedAddresses,
  116. getRequest()->getPort());
  117. if (!pooledSocket) {
  118. A2_LOG_INFO(fmt(MSG_CONNECTING_TO_SERVER, getCuid(), addr.c_str(), port));
  119. createSocket();
  120. getSocket()->establishConnection(addr, port);
  121. getRequest()->setConnectedAddrInfo(hostname, addr, port);
  122. auto c = make_unique<ConnectCommand>(getCuid(), getRequest(),
  123. proxyRequest, // must be null
  124. getFileEntry(), getRequestGroup(),
  125. getDownloadEngine(), getSocket());
  126. c->setControlChain(std::make_shared<HttpRequestConnectChain>());
  127. setupBackupConnection(hostname, addr, port, c.get());
  128. return std::move(c);
  129. }
  130. else {
  131. setSocket(pooledSocket);
  132. setConnectedAddrInfo(getRequest(), hostname, pooledSocket);
  133. return make_unique<HttpRequestCommand>(
  134. getCuid(), getRequest(), getFileEntry(), getRequestGroup(),
  135. std::make_shared<HttpConnection>(
  136. getCuid(), getSocket(),
  137. std::make_shared<SocketRecvBuffer>(getSocket())),
  138. getDownloadEngine(), getSocket());
  139. }
  140. }
  141. }
  142. } // namespace aria2