HttpResponseCommand.cc 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 "SingleFileDownloadContext.h"
  38. #include "FileEntry.h"
  39. #include "RequestGroup.h"
  40. #include "ServerHost.h"
  41. #include "RequestGroupMan.h"
  42. #include "Request.h"
  43. #include "HttpRequest.h"
  44. #include "HttpResponse.h"
  45. #include "HttpConnection.h"
  46. #include "TransferEncoding.h"
  47. #include "SegmentMan.h"
  48. #include "Segment.h"
  49. #include "HttpDownloadCommand.h"
  50. #include "DiskAdaptor.h"
  51. #include "PieceStorage.h"
  52. #include "DefaultBtProgressInfoFile.h"
  53. #include "DownloadFailureException.h"
  54. #include "DlAbortEx.h"
  55. #include "Util.h"
  56. #include "File.h"
  57. #include "Option.h"
  58. #include "Logger.h"
  59. #include "Socket.h"
  60. #include "message.h"
  61. #include "prefs.h"
  62. namespace aria2 {
  63. HttpResponseCommand::HttpResponseCommand(int32_t cuid,
  64. const RequestHandle& req,
  65. RequestGroup* requestGroup,
  66. const HttpConnectionHandle& httpConnection,
  67. DownloadEngine* e,
  68. const SocketHandle& s)
  69. :AbstractCommand(cuid, req, requestGroup, e, s),
  70. httpConnection(httpConnection) {}
  71. HttpResponseCommand::~HttpResponseCommand() {}
  72. bool HttpResponseCommand::executeInternal()
  73. {
  74. HttpRequestHandle httpRequest = httpConnection->getFirstHttpRequest();
  75. HttpResponseHandle httpResponse = httpConnection->receiveResponse();
  76. if(httpResponse.isNull()) {
  77. // The server has not responded to our request yet.
  78. e->commands.push_back(this);
  79. return false;
  80. }
  81. // check HTTP status number
  82. httpResponse->validateResponse();
  83. httpResponse->retrieveCookie();
  84. // check whether Location header exists. If it does, update request object
  85. // with redirected URL.
  86. // then establish a connection to the new host and port
  87. if(httpResponse->isRedirect()) {
  88. httpResponse->processRedirect();
  89. logger->info(MSG_REDIRECT, cuid, httpResponse->getRedirectURI().c_str());
  90. e->noWait = true;
  91. return prepareForRetry(0);
  92. }
  93. if(!_requestGroup->isSingleHostMultiConnectionEnabled()) {
  94. _requestGroup->removeURIWhoseHostnameIs(_requestGroup->searchServerHost(cuid)->getHostname());
  95. }
  96. if(_requestGroup->getPieceStorage().isNull()) {
  97. int64_t totalLength = httpResponse->getEntityLength();
  98. SingleFileDownloadContextHandle dctx = _requestGroup->getDownloadContext();
  99. dctx->setTotalLength(totalLength);
  100. dctx->setFilename(httpResponse->determinFilename());
  101. dctx->setContentType(httpResponse->getContentType());
  102. _requestGroup->preDownloadProcessing();
  103. if(e->_requestGroupMan->isSameFileBeingDownloaded(_requestGroup)) {
  104. throw new DownloadFailureException(EX_DUPLICATE_FILE_DOWNLOAD,
  105. _requestGroup->getFilePath().c_str());
  106. }
  107. if(totalLength == 0 || httpResponse->isTransferEncodingSpecified()) {
  108. // we ignore content-length when transfer-encoding is set
  109. dctx->setTotalLength(0);
  110. return handleOtherEncoding(httpResponse);
  111. } else {
  112. return handleDefaultEncoding(httpResponse);
  113. }
  114. } else {
  115. // validate totalsize
  116. _requestGroup->validateTotalLength(httpResponse->getEntityLength());
  117. e->commands.push_back(createHttpDownloadCommand(httpResponse));
  118. return true;
  119. }
  120. }
  121. bool HttpResponseCommand::handleDefaultEncoding(const HttpResponseHandle& httpResponse)
  122. {
  123. HttpRequestHandle httpRequest = httpResponse->getHttpRequest();
  124. _requestGroup->initPieceStorage();
  125. // quick hack for method 'head',, is it necessary?
  126. if(httpRequest->getMethod() == Request::METHOD_HEAD) {
  127. // TODO because we don't want segment file to be saved.
  128. return true;
  129. }
  130. BtProgressInfoFileHandle infoFile = new DefaultBtProgressInfoFile(_requestGroup->getDownloadContext(), _requestGroup->getPieceStorage(), e->option);
  131. if(!infoFile->exists() && _requestGroup->downloadFinishedByFileLength()) {
  132. return true;
  133. }
  134. DownloadCommand* command = 0;
  135. try {
  136. _requestGroup->loadAndOpenFile(infoFile);
  137. File file(_requestGroup->getFilePath());
  138. SegmentHandle segment = _requestGroup->getSegmentMan()->getSegment(cuid, 0);
  139. if(!segment.isNull() && segment->getPositionToWrite() == 0) {
  140. command = createHttpDownloadCommand(httpResponse);
  141. } else {
  142. _requestGroup->getSegmentMan()->cancelSegment(cuid);
  143. }
  144. prepareForNextAction(command);
  145. e->noWait = true;
  146. } catch(Exception* e) {
  147. delete command;
  148. throw;
  149. }
  150. return true;
  151. }
  152. bool HttpResponseCommand::handleOtherEncoding(const HttpResponseHandle& httpResponse) {
  153. HttpRequestHandle httpRequest = httpResponse->getHttpRequest();
  154. // quick hack for method 'head',, is it necessary?
  155. if(httpRequest->getMethod() == Request::METHOD_HEAD) {
  156. return true;
  157. }
  158. // disable keep-alive
  159. req->setKeepAlive(false);
  160. _requestGroup->initPieceStorage();
  161. _requestGroup->shouldCancelDownloadForSafety();
  162. _requestGroup->getPieceStorage()->getDiskAdaptor()->initAndOpenFile();
  163. e->commands.push_back(createHttpDownloadCommand(httpResponse));
  164. return true;
  165. }
  166. HttpDownloadCommand* HttpResponseCommand::createHttpDownloadCommand(const HttpResponseHandle& httpResponse)
  167. {
  168. TransferEncodingHandle enc = 0;
  169. if(httpResponse->isTransferEncodingSpecified()) {
  170. enc = httpResponse->getTransferDecoder();
  171. if(enc.isNull()) {
  172. throw new DlAbortEx(EX_TRANSFER_ENCODING_NOT_SUPPORTED,
  173. httpResponse->getTransferEncoding().c_str());
  174. }
  175. enc->init();
  176. }
  177. HttpDownloadCommand* command =
  178. new HttpDownloadCommand(cuid, req, _requestGroup, httpConnection, e, socket);
  179. command->setMaxDownloadSpeedLimit(e->option->getAsInt(PREF_MAX_DOWNLOAD_LIMIT));
  180. command->setStartupIdleTime(e->option->getAsInt(PREF_STARTUP_IDLE_TIME));
  181. command->setLowestDownloadSpeedLimit(e->option->getAsInt(PREF_LOWEST_SPEED_LIMIT));
  182. command->setTransferDecoder(enc);
  183. return command;
  184. }
  185. } // namespace aria2