DownloadCommand.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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 "DownloadCommand.h"
  36. #include <cassert>
  37. #include "Request.h"
  38. #include "RequestGroup.h"
  39. #include "DownloadEngine.h"
  40. #include "PeerStat.h"
  41. #include "DlAbortEx.h"
  42. #include "DlRetryEx.h"
  43. #include "SegmentMan.h"
  44. #include "Segment.h"
  45. #include "Logger.h"
  46. #include "LogFactory.h"
  47. #include "ChecksumCheckIntegrityEntry.h"
  48. #include "PieceStorage.h"
  49. #include "CheckIntegrityCommand.h"
  50. #include "DiskAdaptor.h"
  51. #include "DownloadContext.h"
  52. #include "Option.h"
  53. #include "util.h"
  54. #include "SocketCore.h"
  55. #include "message.h"
  56. #include "prefs.h"
  57. #include "fmt.h"
  58. #include "RequestGroupMan.h"
  59. #include "wallclock.h"
  60. #include "SinkStreamFilter.h"
  61. #include "FileEntry.h"
  62. #include "SocketRecvBuffer.h"
  63. #include "Piece.h"
  64. #include "WrDiskCacheEntry.h"
  65. #include "DownloadFailureException.h"
  66. #include "MessageDigest.h"
  67. #include "message_digest_helper.h"
  68. #ifdef ENABLE_BITTORRENT
  69. # include "bittorrent_helper.h"
  70. #endif // ENABLE_BITTORRENT
  71. namespace aria2 {
  72. DownloadCommand::DownloadCommand(
  73. cuid_t cuid, const std::shared_ptr<Request>& req,
  74. const std::shared_ptr<FileEntry>& fileEntry, RequestGroup* requestGroup,
  75. DownloadEngine* e, const std::shared_ptr<SocketCore>& s,
  76. const std::shared_ptr<SocketRecvBuffer>& socketRecvBuffer)
  77. : AbstractCommand(cuid, req, fileEntry, requestGroup, e, s,
  78. socketRecvBuffer),
  79. startupIdleTime_(10),
  80. lowestDownloadSpeedLimit_(0),
  81. pieceHashValidationEnabled_(false)
  82. {
  83. {
  84. if (getOption()->getAsBool(PREF_REALTIME_CHUNK_CHECKSUM)) {
  85. const std::string& algo = getDownloadContext()->getPieceHashType();
  86. if (MessageDigest::supports(algo)) {
  87. messageDigest_ = MessageDigest::create(algo);
  88. pieceHashValidationEnabled_ = true;
  89. }
  90. }
  91. }
  92. peerStat_ = req->initPeerStat();
  93. peerStat_->downloadStart();
  94. getSegmentMan()->registerPeerStat(peerStat_);
  95. streamFilter_ = make_unique<SinkStreamFilter>(
  96. getPieceStorage()->getWrDiskCache(), pieceHashValidationEnabled_);
  97. streamFilter_->init();
  98. sinkFilterOnly_ = true;
  99. checkSocketRecvBuffer();
  100. }
  101. DownloadCommand::~DownloadCommand()
  102. {
  103. peerStat_->downloadStop();
  104. getSegmentMan()->updateFastestPeerStat(peerStat_);
  105. }
  106. namespace {
  107. void flushWrDiskCacheEntry(WrDiskCache* wrDiskCache,
  108. const std::shared_ptr<Segment>& segment)
  109. {
  110. const std::shared_ptr<Piece>& piece = segment->getPiece();
  111. if (piece->getWrDiskCacheEntry()) {
  112. piece->flushWrCache(wrDiskCache);
  113. if (piece->getWrDiskCacheEntry()->getError() !=
  114. WrDiskCacheEntry::CACHE_ERR_SUCCESS) {
  115. segment->clear(wrDiskCache);
  116. throw DOWNLOAD_FAILURE_EXCEPTION2(
  117. fmt("Write disk cache flush failure index=%lu",
  118. static_cast<unsigned long>(piece->getIndex())),
  119. piece->getWrDiskCacheEntry()->getErrorCode());
  120. }
  121. }
  122. }
  123. } // namespace
  124. bool DownloadCommand::executeInternal()
  125. {
  126. if (getDownloadEngine()
  127. ->getRequestGroupMan()
  128. ->doesOverallDownloadSpeedExceed() ||
  129. getRequestGroup()->doesDownloadSpeedExceed()) {
  130. addCommandSelf();
  131. disableReadCheckSocket();
  132. disableWriteCheckSocket();
  133. return false;
  134. }
  135. setReadCheckSocket(getSocket());
  136. const std::shared_ptr<DiskAdaptor>& diskAdaptor =
  137. getPieceStorage()->getDiskAdaptor();
  138. std::shared_ptr<Segment> segment = getSegments().front();
  139. bool eof = false;
  140. if (getSocketRecvBuffer()->bufferEmpty()) {
  141. // Only read from socket when buffer is empty. Imagine that When
  142. // segment length is *short* and we are using HTTP pilelining. We
  143. // issued 2 requests in pipeline. When reading first response
  144. // header, we may read its response body and 2nd response header
  145. // and 2nd response body in buffer if they are small enough to fit
  146. // in buffer. And then server may sends EOF. In this case, we
  147. // read data from socket here, we will get EOF and leaves 2nd
  148. // response unprocessed. To prevent this, we don't read from
  149. // socket when buffer is not empty.
  150. eof = getSocketRecvBuffer()->recv() == 0 && !getSocket()->wantRead() &&
  151. !getSocket()->wantWrite();
  152. }
  153. if (!eof) {
  154. size_t bufSize;
  155. if (sinkFilterOnly_) {
  156. if (segment->getLength() > 0) {
  157. if (segment->getPosition() + segment->getLength() <=
  158. getFileEntry()->getLastOffset()) {
  159. bufSize = std::min(static_cast<size_t>(segment->getLength() -
  160. segment->getWrittenLength()),
  161. getSocketRecvBuffer()->getBufferLength());
  162. }
  163. else {
  164. bufSize =
  165. std::min(static_cast<size_t>(getFileEntry()->getLastOffset() -
  166. segment->getPositionToWrite()),
  167. getSocketRecvBuffer()->getBufferLength());
  168. }
  169. }
  170. else {
  171. bufSize = getSocketRecvBuffer()->getBufferLength();
  172. }
  173. streamFilter_->transform(diskAdaptor, segment,
  174. getSocketRecvBuffer()->getBuffer(), bufSize);
  175. }
  176. else {
  177. // It is possible that segment is completed but we have some bytes
  178. // of stream to read. For example, chunked encoding has "0"+CRLF
  179. // after data. After we read data(at this moment segment is
  180. // completed), we need another 3bytes(or more if it has trailers).
  181. streamFilter_->transform(diskAdaptor, segment,
  182. getSocketRecvBuffer()->getBuffer(),
  183. getSocketRecvBuffer()->getBufferLength());
  184. bufSize = streamFilter_->getBytesProcessed();
  185. }
  186. getSocketRecvBuffer()->drain(bufSize);
  187. peerStat_->updateDownload(bufSize);
  188. getDownloadContext()->updateDownload(bufSize);
  189. }
  190. bool segmentPartComplete = false;
  191. // Note that GrowSegment::complete() always returns false.
  192. if (sinkFilterOnly_) {
  193. if (segment->complete() ||
  194. (getFileEntry()->getLength() != 0 &&
  195. segment->getPositionToWrite() == getFileEntry()->getLastOffset())) {
  196. segmentPartComplete = true;
  197. }
  198. else if (segment->getLength() == 0 && eof) {
  199. segmentPartComplete = true;
  200. }
  201. }
  202. else {
  203. int64_t loff = getFileEntry()->gtoloff(segment->getPositionToWrite());
  204. if (getFileEntry()->getLength() > 0 && !sinkFilterOnly_ &&
  205. ((loff == getRequestEndOffset() && streamFilter_->finished()) ||
  206. loff < getRequestEndOffset()) &&
  207. (segment->complete() ||
  208. segment->getPositionToWrite() == getFileEntry()->getLastOffset())) {
  209. // In this case, StreamFilter other than *SinkStreamFilter is
  210. // used and Content-Length is known. We check
  211. // streamFilter_->finished() only if the requested end offset
  212. // equals to written position in file local offset; in other
  213. // words, data in the requested range is all received. If
  214. // requested end offset is greater than this segment, then
  215. // streamFilter_ is not finished in this segment.
  216. segmentPartComplete = true;
  217. }
  218. else if (streamFilter_->finished()) {
  219. segmentPartComplete = true;
  220. }
  221. }
  222. if (!segmentPartComplete && eof) {
  223. throw DL_RETRY_EX(EX_GOT_EOF);
  224. }
  225. if (segmentPartComplete) {
  226. if (segment->complete() || segment->getLength() == 0) {
  227. // If segment->getLength() == 0, the server doesn't provide
  228. // content length, but the client detected that download
  229. // completed.
  230. A2_LOG_INFO(fmt(MSG_SEGMENT_DOWNLOAD_COMPLETED, getCuid()));
  231. {
  232. const std::string& expectedPieceHash =
  233. getDownloadContext()->getPieceHash(segment->getIndex());
  234. if (pieceHashValidationEnabled_ && !expectedPieceHash.empty()) {
  235. if (
  236. #ifdef ENABLE_BITTORRENT
  237. // TODO Is this necessary?
  238. (!getPieceStorage()->isEndGame() ||
  239. !getDownloadContext()->hasAttribute(CTX_ATTR_BT)) &&
  240. #endif // ENABLE_BITTORRENT
  241. segment->isHashCalculated()) {
  242. A2_LOG_DEBUG(fmt("Hash is available! index=%lu",
  243. static_cast<unsigned long>(segment->getIndex())));
  244. validatePieceHash(segment, expectedPieceHash, segment->getDigest());
  245. }
  246. else {
  247. try {
  248. std::string actualHash =
  249. segment->getPiece()->getDigestWithWrCache(
  250. segment->getSegmentLength(), diskAdaptor);
  251. validatePieceHash(segment, expectedPieceHash, actualHash);
  252. }
  253. catch (RecoverableException& e) {
  254. segment->clear(getPieceStorage()->getWrDiskCache());
  255. getSegmentMan()->cancelSegment(getCuid());
  256. throw;
  257. }
  258. }
  259. }
  260. else {
  261. completeSegment(getCuid(), segment);
  262. }
  263. }
  264. }
  265. else {
  266. // If segment is not canceled here, in the next pipelining
  267. // request, aria2 requests bad range
  268. // [FileEntry->getLastOffset(), FileEntry->getLastOffset())
  269. getSegmentMan()->cancelSegment(getCuid(), segment);
  270. }
  271. checkLowestDownloadSpeed();
  272. // this unit is going to download another segment.
  273. return prepareForNextSegment();
  274. }
  275. else {
  276. checkLowestDownloadSpeed();
  277. setWriteCheckSocketIf(getSocket(), shouldEnableWriteCheck());
  278. checkSocketRecvBuffer();
  279. addCommandSelf();
  280. return false;
  281. }
  282. }
  283. bool DownloadCommand::shouldEnableWriteCheck()
  284. {
  285. return getSocket()->wantWrite();
  286. }
  287. void DownloadCommand::checkLowestDownloadSpeed() const
  288. {
  289. if (lowestDownloadSpeedLimit_ > 0 &&
  290. peerStat_->getDownloadStartTime().difference(global::wallclock()) >=
  291. startupIdleTime_) {
  292. int nowSpeed = peerStat_->calculateDownloadSpeed();
  293. if (nowSpeed <= lowestDownloadSpeedLimit_) {
  294. throw DL_ABORT_EX2(fmt(EX_TOO_SLOW_DOWNLOAD_SPEED, nowSpeed,
  295. lowestDownloadSpeedLimit_,
  296. getRequest()->getHost().c_str()),
  297. error_code::TOO_SLOW_DOWNLOAD_SPEED);
  298. }
  299. }
  300. }
  301. bool DownloadCommand::prepareForNextSegment()
  302. {
  303. if (getRequestGroup()->downloadFinished()) {
  304. // Remove in-flight request here.
  305. getFileEntry()->poolRequest(getRequest());
  306. // If this is a single file download, and file size becomes known
  307. // just after downloading, set total length to FileEntry object
  308. // here.
  309. if (getDownloadContext()->getFileEntries().size() == 1) {
  310. if (getFileEntry()->getLength() == 0) {
  311. getFileEntry()->setLength(getPieceStorage()->getCompletedLength());
  312. }
  313. }
  314. if (getDownloadContext()->getPieceHashType().empty()) {
  315. auto entry = make_unique<ChecksumCheckIntegrityEntry>(getRequestGroup());
  316. if (entry->isValidationReady()) {
  317. entry->initValidator();
  318. entry->cutTrailingGarbage();
  319. getDownloadEngine()->getCheckIntegrityMan()->pushEntry(
  320. std::move(entry));
  321. }
  322. }
  323. // Following 2lines are needed for DownloadEngine to detect
  324. // completed RequestGroups without 1sec delay.
  325. getDownloadEngine()->setNoWait(true);
  326. getDownloadEngine()->setRefreshInterval(std::chrono::milliseconds(0));
  327. return true;
  328. }
  329. else {
  330. // The number of segments should be 1 in order to pass through the next
  331. // segment.
  332. if (getSegments().size() == 1) {
  333. std::shared_ptr<Segment> tempSegment = getSegments().front();
  334. if (!tempSegment->complete()) {
  335. return prepareForRetry(0);
  336. }
  337. if (getRequestEndOffset() ==
  338. getFileEntry()->gtoloff(tempSegment->getPosition() +
  339. tempSegment->getLength())) {
  340. return prepareForRetry(0);
  341. }
  342. std::shared_ptr<Segment> nextSegment =
  343. getSegmentMan()->getSegmentWithIndex(getCuid(),
  344. tempSegment->getIndex() + 1);
  345. if (!nextSegment) {
  346. nextSegment = getSegmentMan()->getCleanSegmentIfOwnerIsIdle(
  347. getCuid(), tempSegment->getIndex() + 1);
  348. }
  349. if (!nextSegment || nextSegment->getWrittenLength() > 0) {
  350. // If nextSegment->getWrittenLength() > 0, current socket must
  351. // be closed because writing incoming data at
  352. // nextSegment->getWrittenLength() corrupts file.
  353. return prepareForRetry(0);
  354. }
  355. else {
  356. checkSocketRecvBuffer();
  357. addCommandSelf();
  358. return false;
  359. }
  360. }
  361. else {
  362. return prepareForRetry(0);
  363. }
  364. }
  365. }
  366. void DownloadCommand::validatePieceHash(const std::shared_ptr<Segment>& segment,
  367. const std::string& expectedHash,
  368. const std::string& actualHash)
  369. {
  370. if (actualHash == expectedHash) {
  371. A2_LOG_INFO(fmt(MSG_GOOD_CHUNK_CHECKSUM, util::toHex(actualHash).c_str()));
  372. completeSegment(getCuid(), segment);
  373. }
  374. else {
  375. A2_LOG_INFO(fmt(EX_INVALID_CHUNK_CHECKSUM,
  376. static_cast<unsigned long>(segment->getIndex()),
  377. segment->getPosition(), util::toHex(expectedHash).c_str(),
  378. util::toHex(actualHash).c_str()));
  379. segment->clear(getPieceStorage()->getWrDiskCache());
  380. getSegmentMan()->cancelSegment(getCuid());
  381. throw DL_RETRY_EX(fmt("Invalid checksum index=%lu",
  382. static_cast<unsigned long>(segment->getIndex())));
  383. }
  384. }
  385. void DownloadCommand::completeSegment(cuid_t cuid,
  386. const std::shared_ptr<Segment>& segment)
  387. {
  388. flushWrDiskCacheEntry(getPieceStorage()->getWrDiskCache(), segment);
  389. getSegmentMan()->completeSegment(cuid, segment);
  390. }
  391. void DownloadCommand::installStreamFilter(
  392. std::unique_ptr<StreamFilter> streamFilter)
  393. {
  394. if (!streamFilter) {
  395. return;
  396. }
  397. streamFilter->installDelegate(std::move(streamFilter_));
  398. streamFilter_ = std::move(streamFilter);
  399. const std::string& name = streamFilter_->getName();
  400. sinkFilterOnly_ = util::endsWith(name, SinkStreamFilter::NAME);
  401. }
  402. // We need to override noCheck() to return true in order to measure
  403. // download speed to check lowest speed.
  404. bool DownloadCommand::noCheck() const { return lowestDownloadSpeedLimit_ > 0; }
  405. } // namespace aria2