HttpResponseCommand.cc 21 KB

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