HttpResponseCommand.cc 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 "HttpDownloadCommand.h"
  42. #include "message.h"
  43. #include "Util.h"
  44. #include "prefs.h"
  45. #include "File.h"
  46. #include "InitiateConnectionCommandFactory.h"
  47. #include "SingleFileDownloadContext.h"
  48. #include "DiskAdaptor.h"
  49. #include "PieceStorage.h"
  50. #include "DefaultBtProgressInfoFile.h"
  51. #include "RequestGroupMan.h"
  52. #include "DownloadFailureException.h"
  53. #include "ServerHost.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->isSingleHostMultiConnectionEnabled()) {
  87. _requestGroup->removeURIWhoseHostnameIs(_requestGroup->searchServerHost(cuid)->getHostname());
  88. }
  89. if(_requestGroup->getPieceStorage().isNull()) {
  90. int64_t totalLength = httpResponse->getEntityLength();
  91. SingleFileDownloadContextHandle dctx = _requestGroup->getDownloadContext();
  92. dctx->setTotalLength(totalLength);
  93. dctx->setFilename(httpResponse->determinFilename());
  94. dctx->setContentType(httpResponse->getContentType());
  95. _requestGroup->preDownloadProcessing();
  96. if(e->_requestGroupMan->isSameFileBeingDownloaded(_requestGroup)) {
  97. throw new DownloadFailureException(EX_DUPLICATE_FILE_DOWNLOAD,
  98. _requestGroup->getFilePath().c_str());
  99. }
  100. if(totalLength == 0 || httpResponse->isTransferEncodingSpecified()) {
  101. // we ignore content-length when transfer-encoding is set
  102. dctx->setTotalLength(0);
  103. return handleOtherEncoding(httpResponse);
  104. } else {
  105. return handleDefaultEncoding(httpResponse);
  106. }
  107. } else {
  108. // validate totalsize
  109. _requestGroup->validateTotalLength(httpResponse->getEntityLength());
  110. e->commands.push_back(createHttpDownloadCommand(httpResponse));
  111. return true;
  112. }
  113. }
  114. bool HttpResponseCommand::handleDefaultEncoding(const HttpResponseHandle& httpResponse)
  115. {
  116. HttpRequestHandle httpRequest = httpResponse->getHttpRequest();
  117. _requestGroup->initPieceStorage();
  118. // quick hack for method 'head',, is it necessary?
  119. if(httpRequest->getMethod() == Request::METHOD_HEAD) {
  120. // TODO because we don't want segment file to be saved.
  121. return true;
  122. }
  123. BtProgressInfoFileHandle infoFile = new DefaultBtProgressInfoFile(_requestGroup->getDownloadContext(), _requestGroup->getPieceStorage(), e->option);
  124. if(!infoFile->exists() && _requestGroup->downloadFinishedByFileLength()) {
  125. return true;
  126. }
  127. DownloadCommand* command = 0;
  128. try {
  129. _requestGroup->loadAndOpenFile(infoFile);
  130. File file(_requestGroup->getFilePath());
  131. SegmentHandle segment = _requestGroup->getSegmentMan()->getSegment(cuid, 0);
  132. if(!segment.isNull() && segment->getPositionToWrite() == 0) {
  133. command = createHttpDownloadCommand(httpResponse);
  134. } else {
  135. _requestGroup->getSegmentMan()->cancelSegment(cuid);
  136. }
  137. prepareForNextAction(command);
  138. e->noWait = true;
  139. } catch(Exception* e) {
  140. delete command;
  141. throw;
  142. }
  143. return true;
  144. }
  145. bool HttpResponseCommand::handleOtherEncoding(const HttpResponseHandle& httpResponse) {
  146. HttpRequestHandle httpRequest = httpResponse->getHttpRequest();
  147. // quick hack for method 'head',, is it necessary?
  148. if(httpRequest->getMethod() == Request::METHOD_HEAD) {
  149. return true;
  150. }
  151. // disable keep-alive
  152. req->setKeepAlive(false);
  153. _requestGroup->initPieceStorage();
  154. _requestGroup->shouldCancelDownloadForSafety();
  155. _requestGroup->getPieceStorage()->getDiskAdaptor()->initAndOpenFile();
  156. e->commands.push_back(createHttpDownloadCommand(httpResponse));
  157. return true;
  158. }
  159. HttpDownloadCommand* HttpResponseCommand::createHttpDownloadCommand(const HttpResponseHandle& httpResponse)
  160. {
  161. TransferEncodingHandle enc = 0;
  162. if(httpResponse->isTransferEncodingSpecified()) {
  163. enc = httpResponse->getTransferDecoder();
  164. if(enc.isNull()) {
  165. throw new DlAbortEx(EX_TRANSFER_ENCODING_NOT_SUPPORTED,
  166. httpResponse->getTransferEncoding().c_str());
  167. }
  168. enc->init();
  169. }
  170. HttpDownloadCommand* command =
  171. new HttpDownloadCommand(cuid, req, _requestGroup, httpConnection, e, socket);
  172. command->setMaxDownloadSpeedLimit(e->option->getAsInt(PREF_MAX_DOWNLOAD_LIMIT));
  173. command->setStartupIdleTime(e->option->getAsInt(PREF_STARTUP_IDLE_TIME));
  174. command->setLowestDownloadSpeedLimit(e->option->getAsInt(PREF_LOWEST_SPEED_LIMIT));
  175. command->setTransferDecoder(enc);
  176. return command;
  177. }