HttpResponseCommand.cc 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  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 = std::unique_ptr<StreamFilter>{})
  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 = std::unique_ptr<StreamFilter>{})
  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(std::vector<MetalinkHttpEntry>::iterator i = entries.begin(),
  189. eoi = entries.end(); i != eoi; ++i) {
  190. getFileEntry()->addUri((*i).uri);
  191. A2_LOG_DEBUG(fmt("Adding URI=%s", (*i).uri.c_str()));
  192. }
  193. }
  194. }
  195. #ifdef ENABLE_MESSAGE_DIGEST
  196. if(httpHeader->defined(HttpHeader::DIGEST)) {
  197. std::vector<Checksum> checksums;
  198. httpResponse->getDigest(checksums);
  199. for(std::vector<Checksum>::iterator i = checksums.begin(),
  200. eoi = checksums.end(); i != eoi; ++i) {
  201. if(getDownloadContext()->getHashType().empty()) {
  202. A2_LOG_DEBUG(fmt("Setting digest: type=%s, digest=%s",
  203. (*i).getHashType().c_str(),
  204. (*i).getDigest().c_str()));
  205. getDownloadContext()->setDigest((*i).getHashType(), (*i).getDigest());
  206. break;
  207. } else {
  208. if(checkChecksum(getDownloadContext(), *i)) {
  209. break;
  210. }
  211. }
  212. }
  213. }
  214. #endif // ENABLE_MESSAGE_DIGEST
  215. }
  216. if(statusCode >= 300) {
  217. if(statusCode == 404) {
  218. getRequestGroup()->increaseAndValidateFileNotFoundCount();
  219. }
  220. return skipResponseBody(std::move(httpResponse));
  221. }
  222. if(getFileEntry()->isUniqueProtocol()) {
  223. // Redirection should be considered here. We need to parse
  224. // original URI to get hostname.
  225. const std::string& uri = getRequest()->getUri();
  226. uri_split_result us;
  227. if(uri_split(&us, uri.c_str()) == 0) {
  228. std::string host = uri::getFieldString(us, USR_HOST, uri.c_str());
  229. getFileEntry()->removeURIWhoseHostnameIs(host);
  230. }
  231. }
  232. if(!getPieceStorage()) {
  233. getDownloadContext()->setAcceptMetalink(false);
  234. int64_t totalLength = httpResponse->getEntityLength();
  235. getFileEntry()->setLength(totalLength);
  236. if(getFileEntry()->getPath().empty()) {
  237. getFileEntry()->setPath
  238. (util::createSafePath
  239. (getOption()->get(PREF_DIR), httpResponse->determinFilename()));
  240. }
  241. getFileEntry()->setContentType(httpResponse->getContentType());
  242. getRequestGroup()->preDownloadProcessing();
  243. if(getDownloadEngine()->getRequestGroupMan()->
  244. isSameFileBeingDownloaded(getRequestGroup())) {
  245. throw DOWNLOAD_FAILURE_EXCEPTION2
  246. (fmt(EX_DUPLICATE_FILE_DOWNLOAD,
  247. getRequestGroup()->getFirstFilePath().c_str()),
  248. error_code::DUPLICATE_DOWNLOAD);
  249. }
  250. // update last modified time
  251. updateLastModifiedTime(httpResponse->getLastModifiedTime());
  252. // If both transfer-encoding and total length is specified, we
  253. // assume we can do segmented downloading
  254. if(totalLength == 0 || shouldInflateContentEncoding(httpResponse.get())) {
  255. // we ignore content-length when inflate is required
  256. getFileEntry()->setLength(0);
  257. if(getRequest()->getMethod() == Request::METHOD_GET &&
  258. (totalLength != 0 ||
  259. !httpResponse->getHttpHeader()->defined(HttpHeader::CONTENT_LENGTH))){
  260. // DownloadContext::knowsTotalLength() == true only when
  261. // server says the size of file is 0 explicitly.
  262. getDownloadContext()->markTotalLengthIsUnknown();
  263. }
  264. return handleOtherEncoding(std::move(httpResponse));
  265. } else {
  266. return handleDefaultEncoding(std::move(httpResponse));
  267. }
  268. } else {
  269. #ifdef ENABLE_MESSAGE_DIGEST
  270. if(!getDownloadContext()->getHashType().empty() &&
  271. httpHeader->defined(HttpHeader::DIGEST)) {
  272. std::vector<Checksum> checksums;
  273. httpResponse->getDigest(checksums);
  274. for(std::vector<Checksum>::iterator i = checksums.begin(),
  275. eoi = checksums.end(); i != eoi; ++i) {
  276. if(checkChecksum(getDownloadContext(), *i)) {
  277. break;
  278. }
  279. }
  280. }
  281. #endif // ENABLE_MESSAGE_DIGEST
  282. // validate totalsize
  283. getRequestGroup()->validateTotalLength(getFileEntry()->getLength(),
  284. httpResponse->getEntityLength());
  285. // update last modified time
  286. updateLastModifiedTime(httpResponse->getLastModifiedTime());
  287. if(getRequestGroup()->getTotalLength() == 0) {
  288. // Since total length is unknown, the file size in previously
  289. // failed download could be larger than the size this time.
  290. // Also we can't resume in this case too. So truncate the file
  291. // anyway.
  292. getPieceStorage()->getDiskAdaptor()->truncate(0);
  293. auto teFilter = getTransferEncodingStreamFilter
  294. (httpResponse.get(),
  295. getContentEncodingStreamFilter(httpResponse.get()));
  296. getDownloadEngine()->addCommand
  297. (createHttpDownloadCommand(std::move(httpResponse),
  298. std::move(teFilter)));
  299. } else {
  300. auto teFilter = getTransferEncodingStreamFilter(httpResponse.get());
  301. getDownloadEngine()->addCommand
  302. (createHttpDownloadCommand(std::move(httpResponse),
  303. std::move(teFilter)));
  304. }
  305. return true;
  306. }
  307. }
  308. void HttpResponseCommand::updateLastModifiedTime(const Time& lastModified)
  309. {
  310. if(getOption()->getAsBool(PREF_REMOTE_TIME)) {
  311. getRequestGroup()->updateLastModifiedTime(lastModified);
  312. }
  313. }
  314. bool HttpResponseCommand::shouldInflateContentEncoding
  315. (HttpResponse* httpResponse)
  316. {
  317. // Basically, on the fly inflation cannot be made with segment
  318. // download, because in each segment we don't know where the date
  319. // should be written. So turn off segmented downloading.
  320. // Meanwhile, Some server returns content-encoding: gzip for .tgz
  321. // files. I think those files should not be inflated by clients,
  322. // because it is the original format of those files. Current
  323. // implementation just inflates these files nonetheless.
  324. const std::string& ce = httpResponse->getContentEncoding();
  325. return httpResponse->getHttpRequest()->acceptGZip() &&
  326. (ce == "gzip" || ce == "deflate");
  327. }
  328. bool HttpResponseCommand::handleDefaultEncoding
  329. (std::unique_ptr<HttpResponse> httpResponse)
  330. {
  331. auto progressInfoFile = std::make_shared<DefaultBtProgressInfoFile>
  332. (getDownloadContext(), std::shared_ptr<PieceStorage>{}, getOption().get());
  333. getRequestGroup()->adjustFilename(progressInfoFile);
  334. getRequestGroup()->initPieceStorage();
  335. if(getOption()->getAsBool(PREF_DRY_RUN)) {
  336. onDryRunFileFound();
  337. return true;
  338. }
  339. auto checkEntry = getRequestGroup()->createCheckIntegrityEntry();
  340. if(!checkEntry) {
  341. return true;
  342. }
  343. File file(getRequestGroup()->getFirstFilePath());
  344. // We have to make sure that command that has Request object must
  345. // have segment after PieceStorage is initialized. See
  346. // AbstractCommand::execute()
  347. auto segment = getSegmentMan()->getSegmentWithIndex(getCuid(), 0);
  348. // pipelining requires implicit range specified. But the request for
  349. // this response most likely dones't contains range header. This means
  350. // we can't continue to use this socket because server sends all entity
  351. // body instead of a segment.
  352. // Therefore, we shutdown the socket here if pipelining is enabled.
  353. if(getRequest()->getMethod() == Request::METHOD_GET &&
  354. segment && segment->getPositionToWrite() == 0 &&
  355. !getRequest()->isPipeliningEnabled()) {
  356. auto teFilter = getTransferEncodingStreamFilter(httpResponse.get());
  357. checkEntry->pushNextCommand
  358. (createHttpDownloadCommand(std::move(httpResponse),
  359. std::move(teFilter)));
  360. } else {
  361. getSegmentMan()->cancelSegment(getCuid());
  362. getFileEntry()->poolRequest(getRequest());
  363. }
  364. prepareForNextAction(std::move(checkEntry));
  365. if(getRequest()->getMethod() == Request::METHOD_HEAD) {
  366. poolConnection();
  367. getRequest()->setMethod(Request::METHOD_GET);
  368. }
  369. return true;
  370. }
  371. bool HttpResponseCommand::handleOtherEncoding
  372. (std::unique_ptr<HttpResponse> httpResponse) {
  373. // We assume that RequestGroup::getTotalLength() == 0 here
  374. if(getOption()->getAsBool(PREF_DRY_RUN)) {
  375. getRequestGroup()->initPieceStorage();
  376. onDryRunFileFound();
  377. return true;
  378. }
  379. if(getRequest()->getMethod() == Request::METHOD_HEAD) {
  380. poolConnection();
  381. getRequest()->setMethod(Request::METHOD_GET);
  382. return prepareForRetry(0);
  383. }
  384. // In this context, knowsTotalLength() is true only when the file is
  385. // really zero-length.
  386. auto streamFilter = getTransferEncodingStreamFilter
  387. (httpResponse.get(), getContentEncodingStreamFilter(httpResponse.get()));
  388. // If chunked transfer-encoding is specified, we have to read end of
  389. // chunk markers(0\r\n\r\n, for example).
  390. bool chunkedUsed = streamFilter &&
  391. streamFilter->getName() == ChunkedDecodingStreamFilter::NAME;
  392. // For zero-length file, check existing file comparing its size
  393. if(!chunkedUsed && getDownloadContext()->knowsTotalLength() &&
  394. getRequestGroup()->downloadFinishedByFileLength()) {
  395. getRequestGroup()->initPieceStorage();
  396. #ifdef ENABLE_MESSAGE_DIGEST
  397. // TODO Known issue: if .aria2 file exists, it will not be deleted
  398. // on successful verification, because .aria2 file is not loaded.
  399. // See also FtpNegotiationCommand::onFileSizeDetermined()
  400. if(getDownloadContext()->isChecksumVerificationNeeded()) {
  401. A2_LOG_DEBUG("Zero length file exists. Verify checksum.");
  402. auto entry = make_unique<ChecksumCheckIntegrityEntry>
  403. (getRequestGroup());
  404. entry->initValidator();
  405. getPieceStorage()->getDiskAdaptor()->openExistingFile();
  406. getDownloadEngine()->getCheckIntegrityMan()->pushEntry(std::move(entry));
  407. } else
  408. #endif // ENABLE_MESSAGE_DIGEST
  409. {
  410. getPieceStorage()->markAllPiecesDone();
  411. getDownloadContext()->setChecksumVerified(true);
  412. A2_LOG_NOTICE
  413. (fmt(MSG_DOWNLOAD_ALREADY_COMPLETED,
  414. GroupId::toHex(getRequestGroup()->getGID()).c_str(),
  415. getRequestGroup()->getFirstFilePath().c_str()));
  416. }
  417. poolConnection();
  418. return true;
  419. }
  420. getRequestGroup()->shouldCancelDownloadForSafety();
  421. getRequestGroup()->initPieceStorage();
  422. getPieceStorage()->getDiskAdaptor()->initAndOpenFile();
  423. // Local file size becomes zero when DiskAdaptor::initAndOpenFile()
  424. // is called. So zero-length file is complete if chunked encoding is
  425. // not used.
  426. if(!chunkedUsed && getDownloadContext()->knowsTotalLength()) {
  427. A2_LOG_DEBUG("File length becomes zero and it means download completed.");
  428. // TODO Known issue: if .aria2 file exists, it will not be deleted
  429. // on successful verification, because .aria2 file is not loaded.
  430. // See also FtpNegotiationCommand::onFileSizeDetermined()
  431. #ifdef ENABLE_MESSAGE_DIGEST
  432. if(getDownloadContext()->isChecksumVerificationNeeded()) {
  433. A2_LOG_DEBUG("Verify checksum for zero-length file");
  434. auto entry = make_unique<ChecksumCheckIntegrityEntry>
  435. (getRequestGroup());
  436. entry->initValidator();
  437. getDownloadEngine()->getCheckIntegrityMan()->pushEntry(std::move(entry));
  438. } else
  439. #endif // ENABLE_MESSAGE_DIGEST
  440. {
  441. getRequestGroup()->getPieceStorage()->markAllPiecesDone();
  442. }
  443. poolConnection();
  444. return true;
  445. }
  446. // We have to make sure that command that has Request object must
  447. // have segment after PieceStorage is initialized. See
  448. // AbstractCommand::execute()
  449. getSegmentMan()->getSegmentWithIndex(getCuid(), 0);
  450. getDownloadEngine()->addCommand
  451. (createHttpDownloadCommand(std::move(httpResponse),
  452. std::move(streamFilter)));
  453. return true;
  454. }
  455. bool HttpResponseCommand::skipResponseBody
  456. (std::unique_ptr<HttpResponse> httpResponse)
  457. {
  458. auto filter = getTransferEncodingStreamFilter(httpResponse.get());
  459. // We don't use Content-Encoding here because this response body is just
  460. // thrown away.
  461. auto httpResponsePtr = httpResponse.get();
  462. auto command = make_unique<HttpSkipResponseCommand>
  463. (getCuid(), getRequest(), getFileEntry(), getRequestGroup(),
  464. httpConnection_, std::move(httpResponse),
  465. getDownloadEngine(), getSocket());
  466. command->installStreamFilter(std::move(filter));
  467. // If request method is HEAD or the response body is zero-length,
  468. // set command's status to real time so that avoid read check blocking
  469. if(getRequest()->getMethod() == Request::METHOD_HEAD ||
  470. (httpResponsePtr->getEntityLength() == 0 &&
  471. !httpResponsePtr->isTransferEncodingSpecified())) {
  472. command->setStatusRealtime();
  473. // If entity length == 0, then socket read/write check must be disabled.
  474. command->disableSocketCheck();
  475. getDownloadEngine()->setNoWait(true);
  476. }
  477. getDownloadEngine()->addCommand(std::move(command));
  478. return true;
  479. }
  480. namespace {
  481. bool decideFileAllocation(StreamFilter* filter)
  482. {
  483. #ifdef HAVE_ZLIB
  484. for(StreamFilter* f = filter; f; f = f->getDelegate().get()){
  485. // Since the compressed file's length are returned in the response header
  486. // and the decompressed file size is unknown at this point, disable file
  487. // allocation here.
  488. if(f->getName() == GZipDecodingStreamFilter::NAME) {
  489. return false;
  490. }
  491. }
  492. #endif // HAVE_ZLIB
  493. return true;
  494. }
  495. } // namespace
  496. std::unique_ptr<HttpDownloadCommand>
  497. HttpResponseCommand::createHttpDownloadCommand
  498. (std::unique_ptr<HttpResponse> httpResponse,
  499. std::unique_ptr<StreamFilter> filter)
  500. {
  501. auto command = make_unique<HttpDownloadCommand>
  502. (getCuid(), getRequest(), getFileEntry(),
  503. getRequestGroup(),
  504. std::move(httpResponse), httpConnection_,
  505. getDownloadEngine(), getSocket());
  506. command->setStartupIdleTime(getOption()->getAsInt(PREF_STARTUP_IDLE_TIME));
  507. command->setLowestDownloadSpeedLimit
  508. (getOption()->getAsInt(PREF_LOWEST_SPEED_LIMIT));
  509. if(getRequestGroup()->isFileAllocationEnabled() &&
  510. !decideFileAllocation(filter.get())) {
  511. getRequestGroup()->setFileAllocationEnabled(false);
  512. }
  513. command->installStreamFilter(std::move(filter));
  514. getRequestGroup()->getURISelector()->tuneDownloadCommand
  515. (getFileEntry()->getRemainingUris(), command.get());
  516. return std::move(command);
  517. }
  518. void HttpResponseCommand::poolConnection()
  519. {
  520. if(getRequest()->supportsPersistentConnection()) {
  521. getDownloadEngine()->poolSocket(getRequest(), createProxyRequest(),
  522. getSocket());
  523. }
  524. }
  525. void HttpResponseCommand::onDryRunFileFound()
  526. {
  527. getPieceStorage()->markAllPiecesDone();
  528. getDownloadContext()->setChecksumVerified(true);
  529. poolConnection();
  530. }
  531. #ifdef ENABLE_MESSAGE_DIGEST
  532. bool HttpResponseCommand::checkChecksum
  533. (const std::shared_ptr<DownloadContext>& dctx,
  534. const Checksum& checksum)
  535. {
  536. if(dctx->getHashType() == checksum.getHashType()) {
  537. if(dctx->getDigest() == checksum.getDigest()) {
  538. A2_LOG_INFO("Valid hash found in Digest header field.");
  539. return true;
  540. } else {
  541. throw DL_ABORT_EX("Invalid hash found in Digest header field.");
  542. }
  543. }
  544. return false;
  545. }
  546. #endif // ENABLE_MESSAGE_DIGEST
  547. } // namespace aria2