HttpInitiateConnectionCommand.cc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 "Socket.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. namespace aria2 {
  55. HttpInitiateConnectionCommand::HttpInitiateConnectionCommand
  56. (cuid_t cuid,
  57. const SharedHandle<Request>& req,
  58. const SharedHandle<FileEntry>& fileEntry,
  59. RequestGroup* requestGroup,
  60. DownloadEngine* e)
  61. : InitiateConnectionCommand(cuid, req, fileEntry, requestGroup, e)
  62. {}
  63. HttpInitiateConnectionCommand::~HttpInitiateConnectionCommand() {}
  64. Command* HttpInitiateConnectionCommand::createNextCommand
  65. (const std::string& hostname, const std::string& addr, uint16_t port,
  66. const std::vector<std::string>& resolvedAddresses,
  67. const SharedHandle<Request>& proxyRequest)
  68. {
  69. Command* command;
  70. if(proxyRequest) {
  71. SharedHandle<SocketCore> pooledSocket =
  72. getDownloadEngine()->popPooledSocket
  73. (getRequest()->getHost(), getRequest()->getPort(),
  74. proxyRequest->getHost(), proxyRequest->getPort());
  75. std::string proxyMethod = resolveProxyMethod(getRequest()->getProtocol());
  76. if(!pooledSocket) {
  77. A2_LOG_INFO(fmt(MSG_CONNECTING_TO_SERVER,
  78. getCuid(), addr.c_str(), port));
  79. createSocket();
  80. getSocket()->establishConnection(addr, port);
  81. getRequest()->setConnectedAddrInfo(hostname, addr, port);
  82. if(proxyMethod == V_TUNNEL) {
  83. HttpProxyRequestCommand* c =
  84. new HttpProxyRequestCommand(getCuid(),
  85. getRequest(),
  86. getFileEntry(),
  87. getRequestGroup(),
  88. getDownloadEngine(),
  89. proxyRequest,
  90. getSocket());
  91. command = c;
  92. } else if(proxyMethod == V_GET) {
  93. SharedHandle<SocketRecvBuffer> socketRecvBuffer
  94. (new SocketRecvBuffer(getSocket()));
  95. SharedHandle<HttpConnection> httpConnection
  96. (new HttpConnection(getCuid(), getSocket(), socketRecvBuffer));
  97. HttpRequestCommand* c = new HttpRequestCommand(getCuid(),
  98. getRequest(),
  99. getFileEntry(),
  100. getRequestGroup(),
  101. httpConnection,
  102. getDownloadEngine(),
  103. getSocket());
  104. c->setProxyRequest(proxyRequest);
  105. command = c;
  106. } else {
  107. // TODO
  108. throw DL_ABORT_EX("ERROR");
  109. }
  110. } else {
  111. setConnectedAddrInfo(getRequest(), hostname, pooledSocket);
  112. SharedHandle<SocketRecvBuffer> socketRecvBuffer
  113. (new SocketRecvBuffer(pooledSocket));
  114. SharedHandle<HttpConnection> httpConnection
  115. (new HttpConnection(getCuid(), pooledSocket, socketRecvBuffer));
  116. HttpRequestCommand* c = new HttpRequestCommand(getCuid(),
  117. getRequest(),
  118. getFileEntry(),
  119. getRequestGroup(),
  120. httpConnection,
  121. getDownloadEngine(),
  122. pooledSocket);
  123. if(proxyMethod == V_GET) {
  124. c->setProxyRequest(proxyRequest);
  125. }
  126. command = c;
  127. }
  128. } else {
  129. SharedHandle<SocketCore> pooledSocket =
  130. getDownloadEngine()->popPooledSocket
  131. (resolvedAddresses, getRequest()->getPort());
  132. if(!pooledSocket) {
  133. A2_LOG_INFO(fmt(MSG_CONNECTING_TO_SERVER,
  134. getCuid(), addr.c_str(), port));
  135. createSocket();
  136. getSocket()->establishConnection(addr, port);
  137. getRequest()->setConnectedAddrInfo(hostname, addr, port);
  138. } else {
  139. setSocket(pooledSocket);
  140. setConnectedAddrInfo(getRequest(), hostname, pooledSocket);
  141. }
  142. SharedHandle<SocketRecvBuffer> socketRecvBuffer
  143. (new SocketRecvBuffer(getSocket()));
  144. SharedHandle<HttpConnection> httpConnection
  145. (new HttpConnection(getCuid(), getSocket(), socketRecvBuffer));
  146. HttpRequestCommand* c =
  147. new HttpRequestCommand(getCuid(), getRequest(), getFileEntry(),
  148. getRequestGroup(),
  149. httpConnection,
  150. getDownloadEngine(),
  151. getSocket());
  152. command = c;
  153. }
  154. return command;
  155. }
  156. } // namespace aria2