HttpInitiateConnectionCommand.cc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 "Socket.h"
  47. #include "message.h"
  48. #include "prefs.h"
  49. #include "A2STR.h"
  50. #include "DownloadContext.h"
  51. namespace aria2 {
  52. HttpInitiateConnectionCommand::HttpInitiateConnectionCommand
  53. (int cuid,
  54. const RequestHandle& req,
  55. const SharedHandle<FileEntry>& fileEntry,
  56. RequestGroup* requestGroup,
  57. DownloadEngine* e):
  58. InitiateConnectionCommand(cuid, req, fileEntry, requestGroup, e) {}
  59. HttpInitiateConnectionCommand::~HttpInitiateConnectionCommand() {}
  60. Command* HttpInitiateConnectionCommand::createNextCommand
  61. (const std::string& hostname, const std::string& addr, uint16_t port,
  62. const std::deque<std::string>& resolvedAddresses,
  63. const SharedHandle<Request>& proxyRequest)
  64. {
  65. Command* command;
  66. if(!proxyRequest.isNull()) {
  67. SharedHandle<SocketCore> pooledSocket =
  68. e->popPooledSocket(req->getHost(), req->getPort());
  69. std::string proxyMethod = resolveProxyMethod(req->getProtocol());
  70. if(pooledSocket.isNull()) {
  71. logger->info(MSG_CONNECTING_TO_SERVER, cuid, addr.c_str(), port);
  72. socket.reset(new SocketCore());
  73. socket->establishConnection(addr, port);
  74. if(proxyMethod == V_TUNNEL) {
  75. HttpProxyRequestCommand* c =
  76. new HttpProxyRequestCommand(cuid, req, _fileEntry,
  77. _requestGroup, e,
  78. proxyRequest, socket);
  79. c->setConnectedAddr(hostname, addr, port);
  80. command = c;
  81. } else if(proxyMethod == V_GET) {
  82. SharedHandle<HttpConnection> httpConnection
  83. (new HttpConnection(cuid, socket, getOption().get()));
  84. HttpRequestCommand* c = new HttpRequestCommand(cuid, req,
  85. _fileEntry,
  86. _requestGroup,
  87. httpConnection, e,
  88. socket);
  89. c->setConnectedAddr(hostname, addr, port);
  90. c->setProxyRequest(proxyRequest);
  91. command = c;
  92. } else {
  93. // TODO
  94. throw DL_ABORT_EX("ERROR");
  95. }
  96. } else {
  97. SharedHandle<HttpConnection> httpConnection
  98. (new HttpConnection(cuid, pooledSocket, getOption().get()));
  99. HttpRequestCommand* c = new HttpRequestCommand(cuid, req,
  100. _fileEntry,
  101. _requestGroup,
  102. httpConnection, e,
  103. pooledSocket);
  104. if(proxyMethod == V_GET) {
  105. c->setProxyRequest(proxyRequest);
  106. }
  107. command = c;
  108. }
  109. } else {
  110. SharedHandle<SocketCore> pooledSocket =
  111. e->popPooledSocket(resolvedAddresses, req->getPort());
  112. if(pooledSocket.isNull()) {
  113. logger->info(MSG_CONNECTING_TO_SERVER, cuid, addr.c_str(), port);
  114. socket.reset(new SocketCore());
  115. socket->establishConnection(addr, port);
  116. } else {
  117. socket = pooledSocket;
  118. }
  119. SharedHandle<HttpConnection> httpConnection(new HttpConnection(cuid, socket, getOption().get()));
  120. HttpRequestCommand* c =
  121. new HttpRequestCommand(cuid, req, _fileEntry, _requestGroup,
  122. httpConnection, e, socket);
  123. if(pooledSocket.isNull()) {
  124. c->setConnectedAddr(hostname, addr, port);
  125. }
  126. command = c;
  127. }
  128. return command;
  129. }
  130. } // namespace aria2