HttpResponseCommand.cc 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. #include "StringFormat.h"
  63. #include "HttpSkipResponseCommand.h"
  64. #include "HttpHeader.h"
  65. namespace aria2 {
  66. HttpResponseCommand::HttpResponseCommand(int32_t cuid,
  67. const RequestHandle& req,
  68. RequestGroup* requestGroup,
  69. const HttpConnectionHandle& httpConnection,
  70. DownloadEngine* e,
  71. const SocketHandle& s)
  72. :AbstractCommand(cuid, req, requestGroup, e, s),
  73. httpConnection(httpConnection) {}
  74. HttpResponseCommand::~HttpResponseCommand() {}
  75. bool HttpResponseCommand::executeInternal()
  76. {
  77. HttpRequestHandle httpRequest = httpConnection->getFirstHttpRequest();
  78. HttpResponseHandle httpResponse = httpConnection->receiveResponse();
  79. if(httpResponse.isNull()) {
  80. // The server has not responded to our request yet.
  81. e->commands.push_back(this);
  82. return false;
  83. }
  84. // check HTTP status number
  85. httpResponse->validateResponse();
  86. httpResponse->retrieveCookie();
  87. if(httpResponse->getResponseStatus() >= HttpHeader::S300) {
  88. return skipResponseBody(httpResponse);
  89. }
  90. if(!_requestGroup->isSingleHostMultiConnectionEnabled()) {
  91. _requestGroup->removeURIWhoseHostnameIs(_requestGroup->searchServerHost(cuid)->getHostname());
  92. }
  93. if(_requestGroup->getPieceStorage().isNull()) {
  94. uint64_t totalLength = httpResponse->getEntityLength();
  95. SingleFileDownloadContextHandle dctx =
  96. dynamic_pointer_cast<SingleFileDownloadContext>(_requestGroup->getDownloadContext());
  97. dctx->setTotalLength(totalLength);
  98. dctx->setFilename(httpResponse->determinFilename());
  99. dctx->setContentType(httpResponse->getContentType());
  100. _requestGroup->preDownloadProcessing();
  101. if(e->_requestGroupMan->isSameFileBeingDownloaded(_requestGroup)) {
  102. throw DownloadFailureException
  103. (StringFormat(EX_DUPLICATE_FILE_DOWNLOAD,
  104. _requestGroup->getFilePath().c_str()).str());
  105. }
  106. if(totalLength == 0 || httpResponse->isTransferEncodingSpecified()) {
  107. // we ignore content-length when transfer-encoding is set
  108. dctx->setTotalLength(0);
  109. return handleOtherEncoding(httpResponse);
  110. } else {
  111. return handleDefaultEncoding(httpResponse);
  112. }
  113. } else {
  114. // validate totalsize
  115. _requestGroup->validateTotalLength(httpResponse->getEntityLength());
  116. e->commands.push_back(createHttpDownloadCommand(httpResponse));
  117. return true;
  118. }
  119. }
  120. bool HttpResponseCommand::handleDefaultEncoding(const HttpResponseHandle& httpResponse)
  121. {
  122. HttpRequestHandle httpRequest = httpResponse->getHttpRequest();
  123. _requestGroup->initPieceStorage();
  124. // quick hack for method 'head',, is it necessary?
  125. if(httpRequest->getMethod() == Request::METHOD_HEAD) {
  126. // TODO because we don't want segment file to be saved.
  127. return true;
  128. }
  129. BtProgressInfoFileHandle infoFile(new DefaultBtProgressInfoFile(_requestGroup->getDownloadContext(), _requestGroup->getPieceStorage(), e->option));
  130. if(!infoFile->exists() && _requestGroup->downloadFinishedByFileLength()) {
  131. return true;
  132. }
  133. DownloadCommand* command = 0;
  134. try {
  135. _requestGroup->loadAndOpenFile(infoFile);
  136. File file(_requestGroup->getFilePath());
  137. SegmentHandle segment = _requestGroup->getSegmentMan()->getSegment(cuid, 0);
  138. // pipelining requires implicit range specified. But the request for
  139. // this response most likely dones't contains range header. This means
  140. // we can't continue to use this socket because server sends all entity
  141. // body instead of a segment.
  142. // Therefore, we shutdown the socket here if pipelining is enabled.
  143. if(!segment.isNull() && segment->getPositionToWrite() == 0 &&
  144. !req->isPipeliningEnabled()) {
  145. command = createHttpDownloadCommand(httpResponse);
  146. } else {
  147. _requestGroup->getSegmentMan()->cancelSegment(cuid);
  148. }
  149. prepareForNextAction(command);
  150. } catch(Exception& e) {
  151. delete command;
  152. throw;
  153. }
  154. return true;
  155. }
  156. bool HttpResponseCommand::handleOtherEncoding(const HttpResponseHandle& httpResponse) {
  157. HttpRequestHandle httpRequest = httpResponse->getHttpRequest();
  158. // quick hack for method 'head',, is it necessary?
  159. if(httpRequest->getMethod() == Request::METHOD_HEAD) {
  160. return true;
  161. }
  162. _requestGroup->initPieceStorage();
  163. _requestGroup->shouldCancelDownloadForSafety();
  164. _requestGroup->getPieceStorage()->getDiskAdaptor()->initAndOpenFile();
  165. e->commands.push_back(createHttpDownloadCommand(httpResponse));
  166. return true;
  167. }
  168. static SharedHandle<TransferEncoding> getTransferEncoding
  169. (const SharedHandle<HttpResponse>& httpResponse)
  170. {
  171. TransferEncodingHandle enc;
  172. if(httpResponse->isTransferEncodingSpecified()) {
  173. enc = httpResponse->getTransferDecoder();
  174. if(enc.isNull()) {
  175. throw DlAbortEx
  176. (StringFormat(EX_TRANSFER_ENCODING_NOT_SUPPORTED,
  177. httpResponse->getTransferEncoding().c_str()).str());
  178. }
  179. enc->init();
  180. }
  181. return enc;
  182. }
  183. bool HttpResponseCommand::skipResponseBody
  184. (const SharedHandle<HttpResponse>& httpResponse)
  185. {
  186. SharedHandle<TransferEncoding> enc(getTransferEncoding(httpResponse));
  187. HttpSkipResponseCommand* command = new HttpSkipResponseCommand
  188. (cuid, req, _requestGroup, httpConnection, httpResponse, e, socket);
  189. command->setTransferDecoder(enc);
  190. // If the response body is zero-length, set command's status to real time
  191. // so that avoid read check blocking
  192. if(httpResponse->getEntityLength() == 0 &&
  193. !httpResponse->isTransferEncodingSpecified()) {
  194. command->setStatusRealtime();
  195. e->setNoWait(true);
  196. }
  197. e->commands.push_back(command);
  198. return true;
  199. }
  200. HttpDownloadCommand* HttpResponseCommand::createHttpDownloadCommand(const HttpResponseHandle& httpResponse)
  201. {
  202. TransferEncodingHandle enc(getTransferEncoding(httpResponse));
  203. HttpDownloadCommand* command =
  204. new HttpDownloadCommand(cuid, req, _requestGroup, httpConnection, e, socket);
  205. command->setMaxDownloadSpeedLimit(e->option->getAsInt(PREF_MAX_DOWNLOAD_LIMIT));
  206. command->setStartupIdleTime(e->option->getAsInt(PREF_STARTUP_IDLE_TIME));
  207. command->setLowestDownloadSpeedLimit(e->option->getAsInt(PREF_LOWEST_SPEED_LIMIT));
  208. command->setTransferDecoder(enc);
  209. return command;
  210. }
  211. } // namespace aria2