HttpResponseCommand.cc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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 "DownloadContext.h"
  38. #include "FileEntry.h"
  39. #include "RequestGroup.h"
  40. #include "RequestGroupMan.h"
  41. #include "Request.h"
  42. #include "HttpRequest.h"
  43. #include "HttpResponse.h"
  44. #include "HttpConnection.h"
  45. #include "SegmentMan.h"
  46. #include "Segment.h"
  47. #include "HttpDownloadCommand.h"
  48. #include "DiskAdaptor.h"
  49. #include "PieceStorage.h"
  50. #include "DefaultBtProgressInfoFile.h"
  51. #include "DownloadFailureException.h"
  52. #include "DlAbortEx.h"
  53. #include "util.h"
  54. #include "File.h"
  55. #include "Option.h"
  56. #include "Logger.h"
  57. #include "Socket.h"
  58. #include "message.h"
  59. #include "prefs.h"
  60. #include "StringFormat.h"
  61. #include "HttpSkipResponseCommand.h"
  62. #include "HttpHeader.h"
  63. #include "LogFactory.h"
  64. #include "CookieStorage.h"
  65. #include "AuthConfigFactory.h"
  66. #include "AuthConfig.h"
  67. #include "a2functional.h"
  68. #include "URISelector.h"
  69. namespace aria2 {
  70. static SharedHandle<Decoder> getTransferEncodingDecoder
  71. (const SharedHandle<HttpResponse>& httpResponse);
  72. static SharedHandle<Decoder> getContentEncodingDecoder
  73. (const SharedHandle<HttpResponse>& httpResponse);
  74. HttpResponseCommand::HttpResponseCommand
  75. (cuid_t cuid,
  76. const SharedHandle<Request>& req,
  77. const SharedHandle<FileEntry>& fileEntry,
  78. RequestGroup* requestGroup,
  79. const HttpConnectionHandle& httpConnection,
  80. DownloadEngine* e,
  81. const SocketHandle& s)
  82. :AbstractCommand(cuid, req, fileEntry, requestGroup, e, s),
  83. httpConnection(httpConnection)
  84. {}
  85. HttpResponseCommand::~HttpResponseCommand() {}
  86. bool HttpResponseCommand::executeInternal()
  87. {
  88. SharedHandle<HttpRequest> httpRequest = httpConnection->getFirstHttpRequest();
  89. SharedHandle<HttpResponse> httpResponse = httpConnection->receiveResponse();
  90. if(httpResponse.isNull()) {
  91. // The server has not responded to our request yet.
  92. // For socket->wantRead() == true, setReadCheckSocket(socket) is already
  93. // done in the constructor.
  94. setWriteCheckSocketIf(socket, socket->wantWrite());
  95. e->commands.push_back(this);
  96. return false;
  97. }
  98. // check HTTP status number
  99. httpResponse->validateResponse();
  100. httpResponse->retrieveCookie();
  101. SharedHandle<HttpHeader> httpHeader = httpResponse->getHttpHeader();
  102. // Disable persistent connection if:
  103. // Connection: close is received or the remote server is not HTTP/1.1.
  104. // We don't care whether non-HTTP/1.1 server returns Connection: keep-alive.
  105. req->supportsPersistentConnection
  106. (httpResponse->supportsPersistentConnection());
  107. if(req->isPipeliningEnabled()) {
  108. req->setMaxPipelinedRequest(getOption()->getAsInt(PREF_MAX_HTTP_PIPELINING));
  109. }
  110. if(httpResponse->getResponseStatus() >= HttpHeader::S300) {
  111. if(httpResponse->getResponseStatus() == HttpHeader::S404) {
  112. _requestGroup->increaseAndValidateFileNotFoundCount();
  113. }
  114. return skipResponseBody(httpResponse);
  115. }
  116. if(!_fileEntry->isSingleHostMultiConnectionEnabled()) {
  117. // TODO redirection should be considered here. We need to parse
  118. // original URI to get hostname.
  119. _fileEntry->removeURIWhoseHostnameIs(req->getHost());
  120. }
  121. if(_requestGroup->getPieceStorage().isNull()) {
  122. uint64_t totalLength = httpResponse->getEntityLength();
  123. _fileEntry->setLength(totalLength);
  124. if(_fileEntry->getPath().empty()) {
  125. _fileEntry->setPath
  126. (util::applyDir
  127. (getDownloadContext()->getDir(),
  128. util::fixTaintedBasename(httpResponse->determinFilename())));
  129. }
  130. _fileEntry->setContentType(httpResponse->getContentType());
  131. _requestGroup->preDownloadProcessing();
  132. if(e->_requestGroupMan->isSameFileBeingDownloaded(_requestGroup)) {
  133. throw DOWNLOAD_FAILURE_EXCEPTION
  134. (StringFormat(EX_DUPLICATE_FILE_DOWNLOAD,
  135. _requestGroup->getFirstFilePath().c_str()).str());
  136. }
  137. // update last modified time
  138. updateLastModifiedTime(httpResponse->getLastModifiedTime());
  139. // If both transfer-encoding and total length is specified, we
  140. // assume we can do segmented downloading
  141. if(totalLength == 0 || shouldInflateContentEncoding(httpResponse)) {
  142. // we ignore content-length when inflate is required
  143. _fileEntry->setLength(0);
  144. if(req->getMethod() == Request::METHOD_GET &&
  145. (totalLength != 0 ||
  146. !httpResponse->getHttpHeader()->defined(HttpHeader::CONTENT_LENGTH))){
  147. // DownloadContext::knowsTotalLength() == true only when
  148. // server says the size of file is 0 explicitly.
  149. getDownloadContext()->markTotalLengthIsUnknown();
  150. }
  151. return handleOtherEncoding(httpResponse);
  152. } else {
  153. return handleDefaultEncoding(httpResponse);
  154. }
  155. } else {
  156. // validate totalsize
  157. _requestGroup->validateTotalLength(_fileEntry->getLength(),
  158. httpResponse->getEntityLength());
  159. // update last modified time
  160. updateLastModifiedTime(httpResponse->getLastModifiedTime());
  161. if(_requestGroup->getTotalLength() == 0) {
  162. // Since total length is unknown, the file size in previously
  163. // failed download could be larger than the size this time.
  164. // Also we can't resume in this case too. So truncate the file
  165. // anyway.
  166. _requestGroup->getPieceStorage()->getDiskAdaptor()->truncate(0);
  167. e->commands.push_back
  168. (createHttpDownloadCommand(httpResponse,
  169. getTransferEncodingDecoder(httpResponse),
  170. getContentEncodingDecoder(httpResponse)));
  171. } else {
  172. e->commands.push_back(createHttpDownloadCommand(httpResponse,
  173. getTransferEncodingDecoder(httpResponse)));
  174. }
  175. return true;
  176. }
  177. }
  178. void HttpResponseCommand::updateLastModifiedTime(const Time& lastModified)
  179. {
  180. if(getOption()->getAsBool(PREF_REMOTE_TIME)) {
  181. _requestGroup->updateLastModifiedTime(lastModified);
  182. }
  183. }
  184. bool HttpResponseCommand::shouldInflateContentEncoding
  185. (const SharedHandle<HttpResponse>& httpResponse)
  186. {
  187. // Basically, on the fly inflation cannot be made with segment
  188. // download, because in each segment we don't know where the date
  189. // should be written. So turn off segmented downloading.
  190. // Meanwhile, Some server returns content-encoding: gzip for .tgz
  191. // files. I think those files should not be inflated by clients,
  192. // because it is the original format of those files. Current
  193. // implementation just inflates these files nonetheless.
  194. const std::string& ce = httpResponse->getContentEncoding();
  195. return httpResponse->getHttpRequest()->acceptGZip() &&
  196. (ce == "gzip" || ce == "deflate");
  197. }
  198. bool HttpResponseCommand::handleDefaultEncoding
  199. (const SharedHandle<HttpResponse>& httpResponse)
  200. {
  201. SharedHandle<HttpRequest> httpRequest = httpResponse->getHttpRequest();
  202. _requestGroup->adjustFilename
  203. (SharedHandle<BtProgressInfoFile>(new DefaultBtProgressInfoFile
  204. (_requestGroup->getDownloadContext(),
  205. SharedHandle<PieceStorage>(),
  206. getOption().get())));
  207. _requestGroup->initPieceStorage();
  208. if(getOption()->getAsBool(PREF_DRY_RUN)) {
  209. onDryRunFileFound();
  210. return true;
  211. }
  212. BtProgressInfoFileHandle infoFile(new DefaultBtProgressInfoFile(_requestGroup->getDownloadContext(), _requestGroup->getPieceStorage(), getOption().get()));
  213. if(!infoFile->exists() && _requestGroup->downloadFinishedByFileLength()) {
  214. _requestGroup->getPieceStorage()->markAllPiecesDone();
  215. logger->notice(MSG_DOWNLOAD_ALREADY_COMPLETED,
  216. util::itos(_requestGroup->getGID()).c_str(),
  217. _requestGroup->getFirstFilePath().c_str());
  218. return true;
  219. }
  220. DownloadCommand* command = 0;
  221. try {
  222. _requestGroup->loadAndOpenFile(infoFile);
  223. File file(_requestGroup->getFirstFilePath());
  224. // We have to make sure that command that has Request object must
  225. // have segment after PieceStorage is initialized. See
  226. // AbstractCommand::execute()
  227. SharedHandle<Segment> segment =
  228. _requestGroup->getSegmentMan()->getSegment(cuid, 0);
  229. // pipelining requires implicit range specified. But the request for
  230. // this response most likely dones't contains range header. This means
  231. // we can't continue to use this socket because server sends all entity
  232. // body instead of a segment.
  233. // Therefore, we shutdown the socket here if pipelining is enabled.
  234. if(req->getMethod() == Request::METHOD_GET &&
  235. !segment.isNull() && segment->getPositionToWrite() == 0 &&
  236. !req->isPipeliningEnabled()) {
  237. command = createHttpDownloadCommand
  238. (httpResponse, getTransferEncodingDecoder(httpResponse));
  239. } else {
  240. _requestGroup->getSegmentMan()->cancelSegment(cuid);
  241. _fileEntry->poolRequest(req);
  242. }
  243. prepareForNextAction(command);
  244. if(req->getMethod() == Request::METHOD_HEAD) {
  245. poolConnection();
  246. req->setMethod(Request::METHOD_GET);
  247. }
  248. } catch(Exception& e) {
  249. delete command;
  250. throw;
  251. }
  252. return true;
  253. }
  254. static SharedHandle<Decoder> getTransferEncodingDecoder
  255. (const SharedHandle<HttpResponse>& httpResponse)
  256. {
  257. SharedHandle<Decoder> decoder;
  258. if(httpResponse->isTransferEncodingSpecified()) {
  259. decoder = httpResponse->getTransferEncodingDecoder();
  260. if(decoder.isNull()) {
  261. throw DL_ABORT_EX
  262. (StringFormat(EX_TRANSFER_ENCODING_NOT_SUPPORTED,
  263. httpResponse->getTransferEncoding().c_str()).str());
  264. }
  265. decoder->init();
  266. }
  267. return decoder;
  268. }
  269. static SharedHandle<Decoder> getContentEncodingDecoder
  270. (const SharedHandle<HttpResponse>& httpResponse)
  271. {
  272. SharedHandle<Decoder> decoder;
  273. if(httpResponse->isContentEncodingSpecified()) {
  274. decoder = httpResponse->getContentEncodingDecoder();
  275. if(decoder.isNull()) {
  276. LogFactory::getInstance()->info
  277. ("Content-Encoding %s is specified, but the current implementation"
  278. "doesn't support it. The decoding process is skipped and the"
  279. "downloaded content will be still encoded.",
  280. httpResponse->getContentEncoding().c_str());
  281. } else {
  282. decoder->init();
  283. }
  284. }
  285. return decoder;
  286. }
  287. bool HttpResponseCommand::handleOtherEncoding
  288. (const SharedHandle<HttpResponse>& httpResponse) {
  289. // We assume that RequestGroup::getTotalLength() == 0 here
  290. SharedHandle<HttpRequest> httpRequest = httpResponse->getHttpRequest();
  291. if(getOption()->getAsBool(PREF_DRY_RUN)) {
  292. _requestGroup->initPieceStorage();
  293. onDryRunFileFound();
  294. return true;
  295. }
  296. if(req->getMethod() == Request::METHOD_HEAD) {
  297. poolConnection();
  298. req->setMethod(Request::METHOD_GET);
  299. return prepareForRetry(0);
  300. }
  301. // For zero-length file, check existing file comparing its size
  302. if(_requestGroup->downloadFinishedByFileLength()) {
  303. _requestGroup->initPieceStorage();
  304. _requestGroup->getPieceStorage()->markAllPiecesDone();
  305. logger->notice(MSG_DOWNLOAD_ALREADY_COMPLETED,
  306. util::itos(_requestGroup->getGID()).c_str(),
  307. _requestGroup->getFirstFilePath().c_str());
  308. poolConnection();
  309. return true;
  310. }
  311. _requestGroup->shouldCancelDownloadForSafety();
  312. _requestGroup->initPieceStorage();
  313. _requestGroup->getPieceStorage()->getDiskAdaptor()->initAndOpenFile();
  314. // In this context, knowsTotalLength() is true only when the file is
  315. // really zero-length.
  316. if(_requestGroup->getDownloadContext()->knowsTotalLength()) {
  317. poolConnection();
  318. return true;
  319. }
  320. // We have to make sure that command that has Request object must
  321. // have segment after PieceStorage is initialized. See
  322. // AbstractCommand::execute()
  323. _requestGroup->getSegmentMan()->getSegment(cuid, 0);
  324. e->commands.push_back
  325. (createHttpDownloadCommand(httpResponse,
  326. getTransferEncodingDecoder(httpResponse),
  327. getContentEncodingDecoder(httpResponse)));
  328. return true;
  329. }
  330. bool HttpResponseCommand::skipResponseBody
  331. (const SharedHandle<HttpResponse>& httpResponse)
  332. {
  333. SharedHandle<Decoder> decoder = getTransferEncodingDecoder(httpResponse);
  334. // We don't use Content-Encoding here because this response body is just
  335. // thrown away.
  336. HttpSkipResponseCommand* command = new HttpSkipResponseCommand
  337. (cuid, req, _fileEntry, _requestGroup, httpConnection, httpResponse, e, socket);
  338. command->setTransferEncodingDecoder(decoder);
  339. // If request method is HEAD or the response body is zero-length,
  340. // set command's status to real time so that avoid read check blocking
  341. if(req->getMethod() == Request::METHOD_HEAD ||
  342. (httpResponse->getEntityLength() == 0 &&
  343. !httpResponse->isTransferEncodingSpecified())) {
  344. command->setStatusRealtime();
  345. // If entity length == 0, then socket read/write check must be disabled.
  346. command->disableSocketCheck();
  347. e->setNoWait(true);
  348. }
  349. e->commands.push_back(command);
  350. return true;
  351. }
  352. HttpDownloadCommand* HttpResponseCommand::createHttpDownloadCommand
  353. (const SharedHandle<HttpResponse>& httpResponse,
  354. const SharedHandle<Decoder>& transferEncodingDecoder,
  355. const SharedHandle<Decoder>& contentEncodingDecoder)
  356. {
  357. HttpDownloadCommand* command =
  358. new HttpDownloadCommand(cuid, req, _fileEntry, _requestGroup,
  359. httpResponse, httpConnection, e, socket);
  360. command->setStartupIdleTime(getOption()->getAsInt(PREF_STARTUP_IDLE_TIME));
  361. command->setLowestDownloadSpeedLimit
  362. (getOption()->getAsInt(PREF_LOWEST_SPEED_LIMIT));
  363. command->setTransferEncodingDecoder(transferEncodingDecoder);
  364. if(!contentEncodingDecoder.isNull()) {
  365. command->setContentEncodingDecoder(contentEncodingDecoder);
  366. // Since the compressed file's length are returned in the response header
  367. // and the decompressed file size is unknown at this point, disable file
  368. // allocation here.
  369. _requestGroup->setFileAllocationEnabled(false);
  370. }
  371. _requestGroup->getURISelector()->tuneDownloadCommand
  372. (_fileEntry->getRemainingUris(), command);
  373. return command;
  374. }
  375. void HttpResponseCommand::poolConnection()
  376. {
  377. if(req->supportsPersistentConnection()) {
  378. e->poolSocket(req, isProxyDefined(), socket);
  379. }
  380. }
  381. void HttpResponseCommand::onDryRunFileFound()
  382. {
  383. _requestGroup->getPieceStorage()->markAllPiecesDone();
  384. poolConnection();
  385. }
  386. } // namespace aria2