HttpResponseCommand.cc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 "DownloadEngine.h"
  37. #include "HttpResponse.h"
  38. #include "HttpConnection.h"
  39. #include "SegmentMan.h"
  40. #include "DlAbortEx.h"
  41. #include "DlRetryEx.h"
  42. #include "HttpDownloadCommand.h"
  43. #include "message.h"
  44. #include "Util.h"
  45. #include "prefs.h"
  46. #include "File.h"
  47. #include "InitiateConnectionCommandFactory.h"
  48. #include "SingleFileDownloadContext.h"
  49. #include "DiskAdaptor.h"
  50. #include "PieceStorage.h"
  51. #include "DefaultBtProgressInfoFile.h"
  52. #include "RequestGroupMan.h"
  53. #include "DownloadFailureException.h"
  54. #include <sys/types.h>
  55. #include <unistd.h>
  56. HttpResponseCommand::HttpResponseCommand(int32_t cuid,
  57. const RequestHandle& req,
  58. RequestGroup* requestGroup,
  59. const HttpConnectionHandle& httpConnection,
  60. DownloadEngine* e,
  61. const SocketHandle& s)
  62. :AbstractCommand(cuid, req, requestGroup, e, s),
  63. httpConnection(httpConnection) {}
  64. HttpResponseCommand::~HttpResponseCommand() {}
  65. bool HttpResponseCommand::executeInternal()
  66. {
  67. HttpRequestHandle httpRequest = httpConnection->getFirstHttpRequest();
  68. HttpResponseHandle httpResponse = httpConnection->receiveResponse();
  69. if(httpResponse.isNull()) {
  70. // The server has not responded to our request yet.
  71. e->commands.push_back(this);
  72. return false;
  73. }
  74. // check HTTP status number
  75. httpResponse->validateResponse();
  76. httpResponse->retrieveCookie();
  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->getPieceStorage().isNull()) {
  87. int64_t totalLength = httpResponse->getEntityLength();
  88. SingleFileDownloadContextHandle dctx = _requestGroup->getDownloadContext();
  89. dctx->setTotalLength(totalLength);
  90. dctx->setFilename(httpResponse->determinFilename());
  91. dctx->setContentType(httpResponse->getContentType());
  92. _requestGroup->preDownloadProcessing();
  93. if(e->_requestGroupMan->isSameFileBeingDownloaded(_requestGroup)) {
  94. throw new DownloadFailureException(EX_DUPLICATE_FILE_DOWNLOAD,
  95. _requestGroup->getFilePath().c_str());
  96. }
  97. if(totalLength == 0 || httpResponse->isTransferEncodingSpecified()) {
  98. // we ignore content-length when transfer-encoding is set
  99. dctx->setTotalLength(0);
  100. return handleOtherEncoding(httpResponse);
  101. } else {
  102. return handleDefaultEncoding(httpResponse);
  103. }
  104. } else {
  105. // validate totalsize
  106. _requestGroup->validateTotalLength(httpResponse->getEntityLength());
  107. e->commands.push_back(createHttpDownloadCommand(httpResponse));
  108. return true;
  109. }
  110. }
  111. bool HttpResponseCommand::handleDefaultEncoding(const HttpResponseHandle& httpResponse)
  112. {
  113. HttpRequestHandle httpRequest = httpResponse->getHttpRequest();
  114. _requestGroup->initPieceStorage();
  115. // quick hack for method 'head',, is it necessary?
  116. if(httpRequest->getMethod() == Request::METHOD_HEAD) {
  117. // TODO because we don't want segment file to be saved.
  118. return true;
  119. }
  120. BtProgressInfoFileHandle infoFile = new DefaultBtProgressInfoFile(_requestGroup->getDownloadContext(), _requestGroup->getPieceStorage(), e->option);
  121. if(!infoFile->exists() && _requestGroup->downloadFinishedByFileLength()) {
  122. return true;
  123. }
  124. DownloadCommand* command = 0;
  125. try {
  126. _requestGroup->loadAndOpenFile(infoFile);
  127. File file(_requestGroup->getFilePath());
  128. if(_requestGroup->getRemainingUris().empty() && !file.exists()) {
  129. command = createHttpDownloadCommand(httpResponse);
  130. }
  131. prepareForNextAction(command);
  132. e->noWait = true;
  133. } catch(Exception* e) {
  134. delete command;
  135. throw;
  136. }
  137. return true;
  138. }
  139. bool HttpResponseCommand::handleOtherEncoding(const HttpResponseHandle& httpResponse) {
  140. HttpRequestHandle httpRequest = httpResponse->getHttpRequest();
  141. // quick hack for method 'head',, is it necessary?
  142. if(httpRequest->getMethod() == Request::METHOD_HEAD) {
  143. return true;
  144. }
  145. // disable keep-alive
  146. req->setKeepAlive(false);
  147. _requestGroup->initPieceStorage();
  148. _requestGroup->shouldCancelDownloadForSafety();
  149. _requestGroup->getPieceStorage()->getDiskAdaptor()->initAndOpenFile();
  150. e->commands.push_back(createHttpDownloadCommand(httpResponse));
  151. return true;
  152. }
  153. HttpDownloadCommand* HttpResponseCommand::createHttpDownloadCommand(const HttpResponseHandle& httpResponse)
  154. {
  155. TransferEncodingHandle enc = 0;
  156. if(httpResponse->isTransferEncodingSpecified()) {
  157. enc = httpResponse->getTransferDecoder();
  158. if(enc.isNull()) {
  159. throw new DlAbortEx(EX_TRANSFER_ENCODING_NOT_SUPPORTED,
  160. httpResponse->getTransferEncoding().c_str());
  161. }
  162. enc->init();
  163. }
  164. HttpDownloadCommand* command =
  165. new HttpDownloadCommand(cuid, req, _requestGroup, httpConnection, e, socket);
  166. command->setMaxDownloadSpeedLimit(e->option->getAsInt(PREF_MAX_DOWNLOAD_LIMIT));
  167. command->setStartupIdleTime(e->option->getAsInt(PREF_STARTUP_IDLE_TIME));
  168. command->setLowestDownloadSpeedLimit(e->option->getAsInt(PREF_LOWEST_SPEED_LIMIT));
  169. command->setTransferDecoder(enc);
  170. return command;
  171. }