HttpResponseCommand.cc 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  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 "SocketCore.h"
  58. #include "message.h"
  59. #include "prefs.h"
  60. #include "fmt.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. #include "CheckIntegrityEntry.h"
  70. #include "StreamFilter.h"
  71. #include "SinkStreamFilter.h"
  72. #include "ChunkedDecodingStreamFilter.h"
  73. #include "uri.h"
  74. #include "SocketRecvBuffer.h"
  75. #include "MetalinkHttpEntry.h"
  76. #ifdef ENABLE_MESSAGE_DIGEST
  77. # include "Checksum.h"
  78. # include "ChecksumCheckIntegrityEntry.h"
  79. #endif // ENABLE_MESSAGE_DIGEST
  80. #ifdef HAVE_ZLIB
  81. # include "GZipDecodingStreamFilter.h"
  82. #endif // HAVE_ZLIB
  83. namespace aria2 {
  84. namespace {
  85. std::unique_ptr<StreamFilter> getTransferEncodingStreamFilter
  86. (HttpResponse* httpResponse,
  87. std::unique_ptr<StreamFilter> delegate = nullptr)
  88. {
  89. if(httpResponse->isTransferEncodingSpecified()) {
  90. auto filter = httpResponse->getTransferEncodingStreamFilter();
  91. if(!filter) {
  92. throw DL_ABORT_EX
  93. (fmt(EX_TRANSFER_ENCODING_NOT_SUPPORTED,
  94. httpResponse->getTransferEncoding().c_str()));
  95. }
  96. filter->init();
  97. filter->installDelegate(std::move(delegate));
  98. return filter;
  99. }
  100. return delegate;
  101. }
  102. } // namespace
  103. namespace {
  104. std::unique_ptr<StreamFilter> getContentEncodingStreamFilter
  105. (HttpResponse* httpResponse,
  106. std::unique_ptr<StreamFilter> delegate = nullptr)
  107. {
  108. if(httpResponse->isContentEncodingSpecified()) {
  109. auto filter = httpResponse->getContentEncodingStreamFilter();
  110. if(!filter) {
  111. A2_LOG_INFO
  112. (fmt("Content-Encoding %s is specified, but the current implementation"
  113. "doesn't support it. The decoding process is skipped and the"
  114. "downloaded content will be still encoded.",
  115. httpResponse->getContentEncoding().c_str()));
  116. }
  117. filter->init();
  118. filter->installDelegate(std::move(delegate));
  119. return filter;
  120. }
  121. return delegate;
  122. }
  123. } // namespace
  124. HttpResponseCommand::HttpResponseCommand
  125. (cuid_t cuid,
  126. const std::shared_ptr<Request>& req,
  127. const std::shared_ptr<FileEntry>& fileEntry,
  128. RequestGroup* requestGroup,
  129. const std::shared_ptr<HttpConnection>& httpConnection,
  130. DownloadEngine* e,
  131. const std::shared_ptr<SocketCore>& s)
  132. : AbstractCommand(cuid, req, fileEntry, requestGroup, e, s,
  133. httpConnection->getSocketRecvBuffer()),
  134. httpConnection_(httpConnection)
  135. {
  136. checkSocketRecvBuffer();
  137. }
  138. HttpResponseCommand::~HttpResponseCommand() {}
  139. bool HttpResponseCommand::executeInternal()
  140. {
  141. auto httpResponse = httpConnection_->receiveResponse();
  142. if(!httpResponse) {
  143. // The server has not responded to our request yet.
  144. // For socket->wantRead() == true, setReadCheckSocket(socket) is already
  145. // done in the constructor.
  146. setWriteCheckSocketIf(getSocket(), getSocket()->wantWrite());
  147. addCommandSelf();
  148. return false;
  149. }
  150. // check HTTP status number
  151. httpResponse->validateResponse();
  152. httpResponse->retrieveCookie();
  153. const auto& httpHeader = httpResponse->getHttpHeader();
  154. // Disable persistent connection if:
  155. // Connection: close is received or the remote server is not HTTP/1.1.
  156. // We don't care whether non-HTTP/1.1 server returns Connection: keep-alive.
  157. getRequest()->supportsPersistentConnection
  158. (httpResponse->supportsPersistentConnection());
  159. if(getRequest()->isPipeliningEnabled()) {
  160. getRequest()->setMaxPipelinedRequest
  161. (getOption()->getAsInt(PREF_MAX_HTTP_PIPELINING));
  162. } else {
  163. getRequest()->setMaxPipelinedRequest(1);
  164. }
  165. int statusCode = httpResponse->getStatusCode();
  166. if(statusCode == 304) {
  167. int64_t totalLength = httpResponse->getEntityLength();
  168. getFileEntry()->setLength(totalLength);
  169. getRequestGroup()->initPieceStorage();
  170. getPieceStorage()->markAllPiecesDone();
  171. // Just set checksum verification done.
  172. getDownloadContext()->setChecksumVerified(true);
  173. A2_LOG_NOTICE
  174. (fmt(MSG_DOWNLOAD_ALREADY_COMPLETED,
  175. GroupId::toHex(getRequestGroup()->getGID()).c_str(),
  176. getRequestGroup()->getFirstFilePath().c_str()));
  177. poolConnection();
  178. getFileEntry()->poolRequest(getRequest());
  179. return true;
  180. }
  181. if(!getPieceStorage()) {
  182. // Metalink/HTTP
  183. if(getDownloadContext()->getAcceptMetalink()) {
  184. if(httpHeader->defined(HttpHeader::LINK)) {
  185. getDownloadContext()->setAcceptMetalink(false);
  186. std::vector<MetalinkHttpEntry> entries;
  187. httpResponse->getMetalinKHttpEntries(entries, getOption());
  188. for(const auto& e : entries) {
  189. getFileEntry()->addUri(e.uri);
  190. A2_LOG_DEBUG(fmt("Adding URI=%s", e.uri.c_str()));
  191. }
  192. }
  193. }
  194. #ifdef ENABLE_MESSAGE_DIGEST
  195. if(httpHeader->defined(HttpHeader::DIGEST)) {
  196. std::vector<Checksum> checksums;
  197. httpResponse->getDigest(checksums);
  198. for(const auto &checksum : checksums) {
  199. if(getDownloadContext()->getHashType().empty()) {
  200. A2_LOG_DEBUG(fmt("Setting digest: type=%s, digest=%s",
  201. checksum.getHashType().c_str(),
  202. checksum.getDigest().c_str()));
  203. getDownloadContext()->setDigest(checksum.getHashType(),
  204. checksum.getDigest());
  205. break;
  206. } else {
  207. if(checkChecksum(getDownloadContext(), checksum)) {
  208. break;
  209. }
  210. }
  211. }
  212. }
  213. #endif // ENABLE_MESSAGE_DIGEST
  214. }
  215. if(statusCode >= 300) {
  216. if(statusCode == 404) {
  217. getRequestGroup()->increaseAndValidateFileNotFoundCount();
  218. }
  219. return skipResponseBody(std::move(httpResponse));
  220. }
  221. if(getFileEntry()->isUniqueProtocol()) {
  222. // Redirection should be considered here. We need to parse
  223. // original URI to get hostname.
  224. const std::string& uri = getRequest()->getUri();
  225. uri_split_result us;
  226. if(uri_split(&us, uri.c_str()) == 0) {
  227. std::string host = uri::getFieldString(us, USR_HOST, uri.c_str());
  228. getFileEntry()->removeURIWhoseHostnameIs(host);
  229. }
  230. }
  231. if(!getPieceStorage()) {
  232. getDownloadContext()->setAcceptMetalink(false);
  233. int64_t totalLength = httpResponse->getEntityLength();
  234. getFileEntry()->setLength(totalLength);
  235. if(getFileEntry()->getPath().empty()) {
  236. getFileEntry()->setPath
  237. (util::createSafePath
  238. (getOption()->get(PREF_DIR), httpResponse->determinFilename()));
  239. }
  240. getFileEntry()->setContentType(httpResponse->getContentType());
  241. getRequestGroup()->preDownloadProcessing();
  242. if(getDownloadEngine()->getRequestGroupMan()->
  243. isSameFileBeingDownloaded(getRequestGroup())) {
  244. throw DOWNLOAD_FAILURE_EXCEPTION2
  245. (fmt(EX_DUPLICATE_FILE_DOWNLOAD,
  246. getRequestGroup()->getFirstFilePath().c_str()),
  247. error_code::DUPLICATE_DOWNLOAD);
  248. }
  249. // update last modified time
  250. updateLastModifiedTime(httpResponse->getLastModifiedTime());
  251. // If both transfer-encoding and total length is specified, we
  252. // assume we can do segmented downloading
  253. if(totalLength == 0 || shouldInflateContentEncoding(httpResponse.get())) {
  254. // we ignore content-length when inflate is required
  255. getFileEntry()->setLength(0);
  256. if(getRequest()->getMethod() == Request::METHOD_GET &&
  257. (totalLength != 0 ||
  258. !httpResponse->getHttpHeader()->defined(HttpHeader::CONTENT_LENGTH))){
  259. // DownloadContext::knowsTotalLength() == true only when
  260. // server says the size of file is 0 explicitly.
  261. getDownloadContext()->markTotalLengthIsUnknown();
  262. }
  263. return handleOtherEncoding(std::move(httpResponse));
  264. } else {
  265. return handleDefaultEncoding(std::move(httpResponse));
  266. }
  267. } else {
  268. #ifdef ENABLE_MESSAGE_DIGEST
  269. if(!getDownloadContext()->getHashType().empty() &&
  270. httpHeader->defined(HttpHeader::DIGEST)) {
  271. std::vector<Checksum> checksums;
  272. httpResponse->getDigest(checksums);
  273. for(const auto &checksum : checksums) {
  274. if(checkChecksum(getDownloadContext(), checksum)) {
  275. break;
  276. }
  277. }
  278. }
  279. #endif // ENABLE_MESSAGE_DIGEST
  280. // validate totalsize
  281. getRequestGroup()->validateTotalLength(getFileEntry()->getLength(),
  282. httpResponse->getEntityLength());
  283. // update last modified time
  284. updateLastModifiedTime(httpResponse->getLastModifiedTime());
  285. if(getRequestGroup()->getTotalLength() == 0) {
  286. // Since total length is unknown, the file size in previously
  287. // failed download could be larger than the size this time.
  288. // Also we can't resume in this case too. So truncate the file
  289. // anyway.
  290. getPieceStorage()->getDiskAdaptor()->truncate(0);
  291. auto teFilter = getTransferEncodingStreamFilter
  292. (httpResponse.get(),
  293. getContentEncodingStreamFilter(httpResponse.get()));
  294. getDownloadEngine()->addCommand
  295. (createHttpDownloadCommand(std::move(httpResponse),
  296. std::move(teFilter)));
  297. } else {
  298. auto teFilter = getTransferEncodingStreamFilter(httpResponse.get());
  299. getDownloadEngine()->addCommand
  300. (createHttpDownloadCommand(std::move(httpResponse),
  301. std::move(teFilter)));
  302. }
  303. return true;
  304. }
  305. }
  306. void HttpResponseCommand::updateLastModifiedTime(const Time& lastModified)
  307. {
  308. if(getOption()->getAsBool(PREF_REMOTE_TIME)) {
  309. getRequestGroup()->updateLastModifiedTime(lastModified);
  310. }
  311. }
  312. bool HttpResponseCommand::shouldInflateContentEncoding
  313. (HttpResponse* httpResponse)
  314. {
  315. // Basically, on the fly inflation cannot be made with segment
  316. // download, because in each segment we don't know where the date
  317. // should be written. So turn off segmented downloading.
  318. // Meanwhile, Some server returns content-encoding: gzip for .tgz
  319. // files. I think those files should not be inflated by clients,
  320. // because it is the original format of those files. Current
  321. // implementation just inflates these files nonetheless.
  322. const std::string& ce = httpResponse->getContentEncoding();
  323. return httpResponse->getHttpRequest()->acceptGZip() &&
  324. (ce == "gzip" || ce == "deflate");
  325. }
  326. bool HttpResponseCommand::handleDefaultEncoding
  327. (std::unique_ptr<HttpResponse> httpResponse)
  328. {
  329. auto progressInfoFile = std::make_shared<DefaultBtProgressInfoFile>
  330. (getDownloadContext(), std::shared_ptr<PieceStorage>{}, getOption().get());
  331. getRequestGroup()->adjustFilename(progressInfoFile);
  332. getRequestGroup()->initPieceStorage();
  333. if(getOption()->getAsBool(PREF_DRY_RUN)) {
  334. onDryRunFileFound();
  335. return true;
  336. }
  337. auto checkEntry = getRequestGroup()->createCheckIntegrityEntry();
  338. if(!checkEntry) {
  339. return true;
  340. }
  341. File file(getRequestGroup()->getFirstFilePath());
  342. // We have to make sure that command that has Request object must
  343. // have segment after PieceStorage is initialized. See
  344. // AbstractCommand::execute()
  345. auto segment = getSegmentMan()->getSegmentWithIndex(getCuid(), 0);
  346. // pipelining requires implicit range specified. But the request for
  347. // this response most likely dones't contains range header. This means
  348. // we can't continue to use this socket because server sends all entity
  349. // body instead of a segment.
  350. // Therefore, we shutdown the socket here if pipelining is enabled.
  351. if(getRequest()->getMethod() == Request::METHOD_GET &&
  352. segment && segment->getPositionToWrite() == 0 &&
  353. !getRequest()->isPipeliningEnabled()) {
  354. auto teFilter = getTransferEncodingStreamFilter(httpResponse.get());
  355. checkEntry->pushNextCommand
  356. (createHttpDownloadCommand(std::move(httpResponse),
  357. std::move(teFilter)));
  358. } else {
  359. getSegmentMan()->cancelSegment(getCuid());
  360. getFileEntry()->poolRequest(getRequest());
  361. }
  362. prepareForNextAction(std::move(checkEntry));
  363. if(getRequest()->getMethod() == Request::METHOD_HEAD) {
  364. poolConnection();
  365. getRequest()->setMethod(Request::METHOD_GET);
  366. }
  367. return true;
  368. }
  369. bool HttpResponseCommand::handleOtherEncoding
  370. (std::unique_ptr<HttpResponse> httpResponse) {
  371. // We assume that RequestGroup::getTotalLength() == 0 here
  372. if(getOption()->getAsBool(PREF_DRY_RUN)) {
  373. getRequestGroup()->initPieceStorage();
  374. onDryRunFileFound();
  375. return true;
  376. }
  377. if(getRequest()->getMethod() == Request::METHOD_HEAD) {
  378. poolConnection();
  379. getRequest()->setMethod(Request::METHOD_GET);
  380. return prepareForRetry(0);
  381. }
  382. // In this context, knowsTotalLength() is true only when the file is
  383. // really zero-length.
  384. auto streamFilter = getTransferEncodingStreamFilter
  385. (httpResponse.get(), getContentEncodingStreamFilter(httpResponse.get()));
  386. // If chunked transfer-encoding is specified, we have to read end of
  387. // chunk markers(0\r\n\r\n, for example).
  388. bool chunkedUsed = streamFilter &&
  389. streamFilter->getName() == ChunkedDecodingStreamFilter::NAME;
  390. // For zero-length file, check existing file comparing its size
  391. if(!chunkedUsed && getDownloadContext()->knowsTotalLength() &&
  392. getRequestGroup()->downloadFinishedByFileLength()) {
  393. getRequestGroup()->initPieceStorage();
  394. #ifdef ENABLE_MESSAGE_DIGEST
  395. // TODO Known issue: if .aria2 file exists, it will not be deleted
  396. // on successful verification, because .aria2 file is not loaded.
  397. // See also FtpNegotiationCommand::onFileSizeDetermined()
  398. if(getDownloadContext()->isChecksumVerificationNeeded()) {
  399. A2_LOG_DEBUG("Zero length file exists. Verify checksum.");
  400. auto entry = make_unique<ChecksumCheckIntegrityEntry>
  401. (getRequestGroup());
  402. entry->initValidator();
  403. getPieceStorage()->getDiskAdaptor()->openExistingFile();
  404. getDownloadEngine()->getCheckIntegrityMan()->pushEntry(std::move(entry));
  405. } else
  406. #endif // ENABLE_MESSAGE_DIGEST
  407. {
  408. getPieceStorage()->markAllPiecesDone();
  409. getDownloadContext()->setChecksumVerified(true);
  410. A2_LOG_NOTICE
  411. (fmt(MSG_DOWNLOAD_ALREADY_COMPLETED,
  412. GroupId::toHex(getRequestGroup()->getGID()).c_str(),
  413. getRequestGroup()->getFirstFilePath().c_str()));
  414. }
  415. poolConnection();
  416. return true;
  417. }
  418. getRequestGroup()->shouldCancelDownloadForSafety();
  419. getRequestGroup()->initPieceStorage();
  420. getPieceStorage()->getDiskAdaptor()->initAndOpenFile();
  421. // Local file size becomes zero when DiskAdaptor::initAndOpenFile()
  422. // is called. So zero-length file is complete if chunked encoding is
  423. // not used.
  424. if(!chunkedUsed && getDownloadContext()->knowsTotalLength()) {
  425. A2_LOG_DEBUG("File length becomes zero and it means download completed.");
  426. // TODO Known issue: if .aria2 file exists, it will not be deleted
  427. // on successful verification, because .aria2 file is not loaded.
  428. // See also FtpNegotiationCommand::onFileSizeDetermined()
  429. #ifdef ENABLE_MESSAGE_DIGEST
  430. if(getDownloadContext()->isChecksumVerificationNeeded()) {
  431. A2_LOG_DEBUG("Verify checksum for zero-length file");
  432. auto entry = make_unique<ChecksumCheckIntegrityEntry>
  433. (getRequestGroup());
  434. entry->initValidator();
  435. getDownloadEngine()->getCheckIntegrityMan()->pushEntry(std::move(entry));
  436. } else
  437. #endif // ENABLE_MESSAGE_DIGEST
  438. {
  439. getRequestGroup()->getPieceStorage()->markAllPiecesDone();
  440. }
  441. poolConnection();
  442. return true;
  443. }
  444. // We have to make sure that command that has Request object must
  445. // have segment after PieceStorage is initialized. See
  446. // AbstractCommand::execute()
  447. getSegmentMan()->getSegmentWithIndex(getCuid(), 0);
  448. getDownloadEngine()->addCommand
  449. (createHttpDownloadCommand(std::move(httpResponse),
  450. std::move(streamFilter)));
  451. return true;
  452. }
  453. bool HttpResponseCommand::skipResponseBody
  454. (std::unique_ptr<HttpResponse> httpResponse)
  455. {
  456. auto filter = getTransferEncodingStreamFilter(httpResponse.get());
  457. // We don't use Content-Encoding here because this response body is just
  458. // thrown away.
  459. auto httpResponsePtr = httpResponse.get();
  460. auto command = make_unique<HttpSkipResponseCommand>
  461. (getCuid(), getRequest(), getFileEntry(), getRequestGroup(),
  462. httpConnection_, std::move(httpResponse),
  463. getDownloadEngine(), getSocket());
  464. command->installStreamFilter(std::move(filter));
  465. // If request method is HEAD or the response body is zero-length,
  466. // set command's status to real time so that avoid read check blocking
  467. if(getRequest()->getMethod() == Request::METHOD_HEAD ||
  468. (httpResponsePtr->getEntityLength() == 0 &&
  469. !httpResponsePtr->isTransferEncodingSpecified())) {
  470. command->setStatusRealtime();
  471. // If entity length == 0, then socket read/write check must be disabled.
  472. command->disableSocketCheck();
  473. getDownloadEngine()->setNoWait(true);
  474. }
  475. getDownloadEngine()->addCommand(std::move(command));
  476. return true;
  477. }
  478. namespace {
  479. bool decideFileAllocation(StreamFilter* filter)
  480. {
  481. #ifdef HAVE_ZLIB
  482. for(StreamFilter* f = filter; f; f = f->getDelegate().get()){
  483. // Since the compressed file's length are returned in the response header
  484. // and the decompressed file size is unknown at this point, disable file
  485. // allocation here.
  486. if(f->getName() == GZipDecodingStreamFilter::NAME) {
  487. return false;
  488. }
  489. }
  490. #endif // HAVE_ZLIB
  491. return true;
  492. }
  493. } // namespace
  494. std::unique_ptr<HttpDownloadCommand>
  495. HttpResponseCommand::createHttpDownloadCommand
  496. (std::unique_ptr<HttpResponse> httpResponse,
  497. std::unique_ptr<StreamFilter> filter)
  498. {
  499. auto command = make_unique<HttpDownloadCommand>
  500. (getCuid(), getRequest(), getFileEntry(),
  501. getRequestGroup(),
  502. std::move(httpResponse), httpConnection_,
  503. getDownloadEngine(), getSocket());
  504. command->setStartupIdleTime(getOption()->getAsInt(PREF_STARTUP_IDLE_TIME));
  505. command->setLowestDownloadSpeedLimit
  506. (getOption()->getAsInt(PREF_LOWEST_SPEED_LIMIT));
  507. if(getRequestGroup()->isFileAllocationEnabled() &&
  508. !decideFileAllocation(filter.get())) {
  509. getRequestGroup()->setFileAllocationEnabled(false);
  510. }
  511. command->installStreamFilter(std::move(filter));
  512. getRequestGroup()->getURISelector()->tuneDownloadCommand
  513. (getFileEntry()->getRemainingUris(), command.get());
  514. return std::move(command);
  515. }
  516. void HttpResponseCommand::poolConnection()
  517. {
  518. if(getRequest()->supportsPersistentConnection()) {
  519. getDownloadEngine()->poolSocket(getRequest(), createProxyRequest(),
  520. getSocket());
  521. }
  522. }
  523. void HttpResponseCommand::onDryRunFileFound()
  524. {
  525. getPieceStorage()->markAllPiecesDone();
  526. getDownloadContext()->setChecksumVerified(true);
  527. poolConnection();
  528. }
  529. #ifdef ENABLE_MESSAGE_DIGEST
  530. bool HttpResponseCommand::checkChecksum
  531. (const std::shared_ptr<DownloadContext>& dctx,
  532. const Checksum& checksum)
  533. {
  534. if(dctx->getHashType() == checksum.getHashType()) {
  535. if(dctx->getDigest() == checksum.getDigest()) {
  536. A2_LOG_INFO("Valid hash found in Digest header field.");
  537. return true;
  538. } else {
  539. throw DL_ABORT_EX("Invalid hash found in Digest header field.");
  540. }
  541. }
  542. return false;
  543. }
  544. #endif // ENABLE_MESSAGE_DIGEST
  545. } // namespace aria2