HttpResponseCommand.cc 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 "HttpResponseCommand.h"
  36. #include "DlAbortEx.h"
  37. #include "DlRetryEx.h"
  38. #include "HttpDownloadCommand.h"
  39. #include "message.h"
  40. #include "Util.h"
  41. #include "prefs.h"
  42. #include "File.h"
  43. #include "InitiateConnectionCommandFactory.h"
  44. #include <sys/types.h>
  45. #include <unistd.h>
  46. HttpResponseCommand::HttpResponseCommand(int32_t cuid,
  47. const RequestHandle& req,
  48. RequestGroup* requestGroup,
  49. const HttpConnectionHandle& httpConnection,
  50. DownloadEngine* e,
  51. const SocketHandle& s)
  52. :AbstractCommand(cuid, req, requestGroup, e, s),
  53. httpConnection(httpConnection) {}
  54. HttpResponseCommand::~HttpResponseCommand() {}
  55. bool HttpResponseCommand::executeInternal()
  56. {
  57. HttpRequestHandle httpRequest = httpConnection->getFirstHttpRequest();
  58. if(!(httpRequest->getSegment() == segment)) {
  59. logger->info(MSG_SEGMENT_CHANGED, cuid);
  60. return prepareForRetry(0);
  61. }
  62. HttpResponseHandle httpResponse = httpConnection->receiveResponse();
  63. if(httpResponse.isNull()) {
  64. // The server has not responded to our request yet.
  65. e->commands.push_back(this);
  66. return false;
  67. }
  68. // check HTTP status number
  69. httpResponse->validateResponse();
  70. httpResponse->retrieveCookie();
  71. // check whether the server supports persistent connections.
  72. /*
  73. if(Util::toLower(headers.getFirst("Connection")).find("close") != string::npos) {
  74. req->setKeepAlive(false);
  75. }
  76. */
  77. // check whether Location header exists. If it does, update request object
  78. // with redirected URL.
  79. // then establish a connection to the new host and port
  80. if(httpResponse->isRedirect()) {
  81. httpResponse->processRedirect();
  82. logger->info(MSG_REDIRECT, cuid, httpResponse->getRedirectURI().c_str());
  83. e->noWait = true;
  84. return prepareForRetry(0);
  85. }
  86. if(_requestGroup->getSegmentMan()->downloadStarted) {
  87. // TODO validate totalsize
  88. _requestGroup->validateFilename(httpResponse->determinFilename());
  89. _requestGroup->validateTotalLength(httpResponse->getEntityLength());
  90. e->commands.push_back(createHttpDownloadCommand(httpResponse));
  91. return true;
  92. } else {
  93. // TODO validate totalsize against hintTotalSize if it is provided.
  94. _requestGroup->validateFilenameByHint(httpResponse->determinFilename());
  95. _requestGroup->validateTotalLengthByHint(httpResponse->getEntityLength());
  96. if(httpResponse->isTransferEncodingSpecified()) {
  97. return handleOtherEncoding(httpResponse);
  98. } else {
  99. return handleDefaultEncoding(httpResponse);
  100. }
  101. }
  102. }
  103. bool HttpResponseCommand::handleDefaultEncoding(const HttpResponseHandle& httpResponse)
  104. {
  105. HttpRequestHandle httpRequest = httpResponse->getHttpRequest();
  106. // TODO quick and dirty way
  107. if(_requestGroup->isTorrent) {
  108. return doTorrentStuff(httpResponse);
  109. }
  110. int64_t size = httpResponse->getEntityLength();
  111. if(size == INT64_MAX || size < 0) {
  112. throw new DlAbortEx(EX_TOO_LARGE_FILE, size);
  113. }
  114. _requestGroup->getSegmentMan()->isSplittable = !(size == 0);
  115. _requestGroup->getSegmentMan()->filename = httpResponse->determinFilename();
  116. _requestGroup->getSegmentMan()->downloadStarted = true;
  117. _requestGroup->getSegmentMan()->totalSize = size;
  118. // quick hack for method 'head'
  119. if(httpRequest->getMethod() == Request::METHOD_HEAD) {
  120. // TODO because we don't want segment file to be saved.
  121. _requestGroup->getSegmentMan()->isSplittable = false;
  122. return true;
  123. }
  124. if(e->option->get(PREF_CHECK_INTEGRITY) != V_TRUE) {
  125. if(_requestGroup->downloadFinishedByFileLength()) {
  126. logger->notice(MSG_DOWNLOAD_ALREADY_COMPLETED, cuid, _requestGroup->getFilePath().c_str());
  127. return true;
  128. }
  129. }
  130. DownloadCommand* command = 0;
  131. File file(_requestGroup->getFilePath());
  132. if(_requestGroup->getRemainingUris().empty() && !file.exists()) {
  133. command = createHttpDownloadCommand(httpResponse);
  134. }
  135. _requestGroup->loadAndOpenFile();
  136. _requestGroup->prepareForNextAction(cuid, req, e, command);
  137. e->noWait = true;
  138. return true;
  139. }
  140. bool HttpResponseCommand::handleOtherEncoding(const HttpResponseHandle& httpResponse) {
  141. HttpRequestHandle httpRequest = httpResponse->getHttpRequest();
  142. // we ignore content-length when transfer-encoding is set
  143. _requestGroup->getSegmentMan()->downloadStarted = true;
  144. _requestGroup->getSegmentMan()->isSplittable = false;
  145. _requestGroup->getSegmentMan()->filename = httpResponse->determinFilename();
  146. _requestGroup->getSegmentMan()->totalSize = 0;
  147. // quick hack for method 'head'
  148. if(httpRequest->getMethod() == Request::METHOD_HEAD) {
  149. return true;
  150. }
  151. // disable keep-alive
  152. req->setKeepAlive(false);
  153. segment = _requestGroup->getSegmentMan()->getSegment(cuid);
  154. _requestGroup->getSegmentMan()->diskWriter->initAndOpenFile(_requestGroup->getSegmentMan()->getFilePath());
  155. e->commands.push_back(createHttpDownloadCommand(httpResponse));
  156. return true;
  157. }
  158. HttpDownloadCommand* HttpResponseCommand::createHttpDownloadCommand(const HttpResponseHandle& httpResponse)
  159. {
  160. TransferEncodingHandle enc = 0;
  161. if(httpResponse->isTransferEncodingSpecified()) {
  162. enc = httpResponse->getTransferDecoder();
  163. if(enc.isNull()) {
  164. throw new DlAbortEx(EX_TRANSFER_ENCODING_NOT_SUPPORTED,
  165. httpResponse->getTransferEncoding().c_str());
  166. }
  167. enc->init();
  168. }
  169. HttpDownloadCommand* command =
  170. new HttpDownloadCommand(cuid, req, _requestGroup, e, socket);
  171. command->setMaxDownloadSpeedLimit(e->option->getAsInt(PREF_MAX_DOWNLOAD_LIMIT));
  172. command->setStartupIdleTime(e->option->getAsInt(PREF_STARTUP_IDLE_TIME));
  173. command->setLowestDownloadSpeedLimit(e->option->getAsInt(PREF_LOWEST_SPEED_LIMIT));
  174. command->setTransferDecoder(enc);
  175. return command;
  176. }
  177. bool HttpResponseCommand::doTorrentStuff(const HttpResponseHandle& httpResponse)
  178. {
  179. int64_t size = httpResponse->getEntityLength();
  180. _requestGroup->getSegmentMan()->totalSize = size;
  181. if(size > 0) {
  182. _requestGroup->getSegmentMan()->initBitfield(e->option->getAsInt(PREF_SEGMENT_SIZE),
  183. _requestGroup->getSegmentMan()->totalSize);
  184. }
  185. // disable keep-alive
  186. httpResponse->getHttpRequest()->getRequest()->setKeepAlive(false);
  187. _requestGroup->getSegmentMan()->isSplittable = false;
  188. _requestGroup->getSegmentMan()->downloadStarted = true;
  189. _requestGroup->getSegmentMan()->diskWriter->initAndOpenFile("/tmp/aria2"+Util::itos((int32_t)getpid()));
  190. e->commands.push_back(createHttpDownloadCommand(httpResponse));
  191. return true;
  192. }