DownloadCommand.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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 "ChecksumCheckIntegrityEntry.h"
  47. #include "PieceStorage.h"
  48. #include "CheckIntegrityCommand.h"
  49. #include "DiskAdaptor.h"
  50. #include "DownloadContext.h"
  51. #include "Option.h"
  52. #include "util.h"
  53. #include "Socket.h"
  54. #include "message.h"
  55. #include "prefs.h"
  56. #include "StringFormat.h"
  57. #include "Decoder.h"
  58. #include "RequestGroupMan.h"
  59. #include "wallclock.h"
  60. #include "ServerStatMan.h"
  61. #include "FileAllocationEntry.h"
  62. #ifdef ENABLE_MESSAGE_DIGEST
  63. # include "MessageDigestHelper.h"
  64. #endif // ENABLE_MESSAGE_DIGEST
  65. #ifdef ENABLE_BITTORRENT
  66. # include "bittorrent_helper.h"
  67. #endif // ENABLE_BITTORRENT
  68. namespace aria2 {
  69. static const size_t BUFSIZE = 16*1024;
  70. DownloadCommand::DownloadCommand(cuid_t cuid,
  71. const SharedHandle<Request>& req,
  72. const SharedHandle<FileEntry>& fileEntry,
  73. RequestGroup* requestGroup,
  74. DownloadEngine* e,
  75. const SocketHandle& s):
  76. AbstractCommand(cuid, req, fileEntry, requestGroup, e, s),
  77. buf_(new unsigned char[BUFSIZE]),
  78. startupIdleTime_(10),
  79. lowestDownloadSpeedLimit_(0)
  80. #ifdef ENABLE_MESSAGE_DIGEST
  81. , pieceHashValidationEnabled_(false)
  82. #endif // ENABLE_MESSAGE_DIGEST
  83. {
  84. #ifdef ENABLE_MESSAGE_DIGEST
  85. {
  86. if(getOption()->getAsBool(PREF_REALTIME_CHUNK_CHECKSUM)) {
  87. const std::string& algo = getDownloadContext()->getPieceHashAlgo();
  88. if(MessageDigestContext::supports(algo)) {
  89. messageDigestContext_.reset(new MessageDigestContext());
  90. messageDigestContext_->trySetAlgo(algo);
  91. messageDigestContext_->digestInit();
  92. pieceHashValidationEnabled_ = true;
  93. }
  94. }
  95. }
  96. #endif // ENABLE_MESSAGE_DIGEST
  97. peerStat_ = req->initPeerStat();
  98. peerStat_->downloadStart();
  99. getSegmentMan()->registerPeerStat(peerStat_);
  100. }
  101. DownloadCommand::~DownloadCommand() {
  102. peerStat_->downloadStop();
  103. getSegmentMan()->updateFastestPeerStat(peerStat_);
  104. delete [] buf_;
  105. }
  106. bool DownloadCommand::executeInternal() {
  107. if(getDownloadEngine()->getRequestGroupMan()->doesOverallDownloadSpeedExceed()
  108. || getRequestGroup()->doesDownloadSpeedExceed()) {
  109. getDownloadEngine()->addCommand(this);
  110. disableReadCheckSocket();
  111. return false;
  112. }
  113. setReadCheckSocket(getSocket());
  114. SharedHandle<Segment> segment = getSegments().front();
  115. size_t bufSize;
  116. if(segment->getLength() > 0) {
  117. if(static_cast<uint64_t>(segment->getPosition()+segment->getLength()) <=
  118. static_cast<uint64_t>(getFileEntry()->getLastOffset())) {
  119. bufSize = std::min(segment->getLength()-segment->getWrittenLength(),
  120. BUFSIZE);
  121. } else {
  122. bufSize =
  123. std::min
  124. (static_cast<size_t>
  125. (getFileEntry()->getLastOffset()-segment->getPositionToWrite()),
  126. BUFSIZE);
  127. }
  128. } else {
  129. bufSize = BUFSIZE;
  130. }
  131. // It is possible that segment is completed but we have some bytes
  132. // of stream to read. For example, chunked encoding has "0"+CRLF
  133. // after data. After we read data(at this moment segment is
  134. // completed), we need another 3bytes(or more if it has extension).
  135. if(bufSize == 0 &&
  136. ((!transferEncodingDecoder_.isNull() &&
  137. !transferEncodingDecoder_->finished()) ||
  138. (!contentEncodingDecoder_.isNull() &&
  139. !contentEncodingDecoder_->finished()))) {
  140. bufSize = 1;
  141. }
  142. getSocket()->readData(buf_, bufSize);
  143. const SharedHandle<DiskAdaptor>& diskAdaptor =
  144. getPieceStorage()->getDiskAdaptor();
  145. const unsigned char* bufFinal;
  146. size_t bufSizeFinal;
  147. std::string decoded;
  148. if(transferEncodingDecoder_.isNull()) {
  149. bufFinal = buf_;
  150. bufSizeFinal = bufSize;
  151. } else {
  152. decoded = transferEncodingDecoder_->decode(buf_, bufSize);
  153. bufFinal = reinterpret_cast<const unsigned char*>(decoded.c_str());
  154. bufSizeFinal = decoded.size();
  155. }
  156. if(contentEncodingDecoder_.isNull()) {
  157. diskAdaptor->writeData(bufFinal, bufSizeFinal,
  158. segment->getPositionToWrite());
  159. } else {
  160. std::string out = contentEncodingDecoder_->decode(bufFinal, bufSizeFinal);
  161. diskAdaptor->writeData(reinterpret_cast<const unsigned char*>(out.data()),
  162. out.size(),
  163. segment->getPositionToWrite());
  164. bufSizeFinal = out.size();
  165. }
  166. #ifdef ENABLE_MESSAGE_DIGEST
  167. if(pieceHashValidationEnabled_) {
  168. segment->updateHash(segment->getWrittenLength(), bufFinal, bufSizeFinal);
  169. }
  170. #endif // ENABLE_MESSAGE_DIGEST
  171. if(bufSizeFinal > 0) {
  172. segment->updateWrittenLength(bufSizeFinal);
  173. }
  174. peerStat_->updateDownloadLength(bufSize);
  175. getSegmentMan()->updateDownloadSpeedFor(peerStat_);
  176. bool segmentPartComplete = false;
  177. // Note that GrowSegment::complete() always returns false.
  178. if(transferEncodingDecoder_.isNull() && contentEncodingDecoder_.isNull()) {
  179. if(segment->complete() ||
  180. segment->getPositionToWrite() == getFileEntry()->getLastOffset()) {
  181. segmentPartComplete = true;
  182. } else if(segment->getLength() == 0 && bufSize == 0 &&
  183. !getSocket()->wantRead() && !getSocket()->wantWrite()) {
  184. segmentPartComplete = true;
  185. }
  186. } else {
  187. off_t loff = getFileEntry()->gtoloff(segment->getPositionToWrite());
  188. if(!transferEncodingDecoder_.isNull() &&
  189. ((loff == getRequestEndOffset() && transferEncodingDecoder_->finished())
  190. || loff < getRequestEndOffset()) &&
  191. (segment->complete() ||
  192. segment->getPositionToWrite() == getFileEntry()->getLastOffset())) {
  193. // In this case, transferEncodingDecoder is used and
  194. // Content-Length is known. We check
  195. // transferEncodingDecoder_->finished() only if the requested
  196. // end offset equals to written position in file local offset;
  197. // in other words, data in the requested ranage is all received.
  198. // If requested end offset is greater than this segment, then
  199. // transferEncodingDecoder_ is not finished in this segment.
  200. segmentPartComplete = true;
  201. } else if((transferEncodingDecoder_.isNull() ||
  202. transferEncodingDecoder_->finished()) &&
  203. (contentEncodingDecoder_.isNull() ||
  204. contentEncodingDecoder_->finished())) {
  205. segmentPartComplete = true;
  206. }
  207. }
  208. if(!segmentPartComplete && bufSize == 0 &&
  209. !getSocket()->wantRead() && !getSocket()->wantWrite()) {
  210. throw DL_RETRY_EX(EX_GOT_EOF);
  211. }
  212. if(segmentPartComplete) {
  213. if(segment->complete() || segment->getLength() == 0) {
  214. // If segment->getLength() == 0, the server doesn't provide
  215. // content length, but the client detected that download
  216. // completed.
  217. if(getLogger()->info()) {
  218. getLogger()->info(MSG_SEGMENT_DOWNLOAD_COMPLETED,
  219. util::itos(getCuid()).c_str());
  220. }
  221. #ifdef ENABLE_MESSAGE_DIGEST
  222. {
  223. const std::string& expectedPieceHash =
  224. getDownloadContext()->getPieceHash(segment->getIndex());
  225. if(pieceHashValidationEnabled_ && !expectedPieceHash.empty()) {
  226. if(
  227. #ifdef ENABLE_BITTORRENT
  228. (!getPieceStorage()->isEndGame() ||
  229. !getDownloadContext()->hasAttribute(bittorrent::BITTORRENT)) &&
  230. #endif // ENABLE_BITTORRENT
  231. segment->isHashCalculated()) {
  232. if(getLogger()->debug()) {
  233. getLogger()->debug
  234. ("Hash is available! index=%lu",
  235. static_cast<unsigned long>(segment->getIndex()));
  236. }
  237. validatePieceHash
  238. (segment, expectedPieceHash, segment->getHashString());
  239. } else {
  240. messageDigestContext_->digestReset();
  241. validatePieceHash
  242. (segment, expectedPieceHash,
  243. MessageDigestHelper::digest
  244. (messageDigestContext_.get(),
  245. getPieceStorage()->getDiskAdaptor(),
  246. segment->getPosition(),
  247. segment->getLength()));
  248. }
  249. } else {
  250. getSegmentMan()->completeSegment(getCuid(), segment);
  251. }
  252. }
  253. #else // !ENABLE_MESSAGE_DIGEST
  254. getSegmentMan()->completeSegment(getCuid(), segment);
  255. #endif // !ENABLE_MESSAGE_DIGEST
  256. } else {
  257. // If segment is not canceled here, in the next pipelining
  258. // request, aria2 requests bad range
  259. // [FileEntry->getLastOffset(), FileEntry->getLastOffset())
  260. getSegmentMan()->cancelSegment(getCuid(), segment);
  261. }
  262. checkLowestDownloadSpeed();
  263. // this unit is going to download another segment.
  264. return prepareForNextSegment();
  265. } else {
  266. checkLowestDownloadSpeed();
  267. setWriteCheckSocketIf(getSocket(), getSocket()->wantWrite());
  268. getDownloadEngine()->addCommand(this);
  269. return false;
  270. }
  271. }
  272. void DownloadCommand::checkLowestDownloadSpeed() const
  273. {
  274. // calculate downloading speed
  275. if(peerStat_->getDownloadStartTime().difference(global::wallclock) >=
  276. startupIdleTime_) {
  277. unsigned int nowSpeed = peerStat_->calculateDownloadSpeed();
  278. if(lowestDownloadSpeedLimit_ > 0 && nowSpeed <= lowestDownloadSpeedLimit_) {
  279. throw DL_ABORT_EX2(StringFormat(EX_TOO_SLOW_DOWNLOAD_SPEED,
  280. nowSpeed,
  281. lowestDownloadSpeedLimit_,
  282. getRequest()->getHost().c_str()).str(),
  283. downloadresultcode::TOO_SLOW_DOWNLOAD_SPEED);
  284. }
  285. }
  286. }
  287. bool DownloadCommand::prepareForNextSegment() {
  288. if(getRequestGroup()->downloadFinished()) {
  289. // Remove in-flight request here.
  290. getFileEntry()->poolRequest(getRequest());
  291. // If this is a single file download, and file size becomes known
  292. // just after downloading, set total length to FileEntry object
  293. // here.
  294. if(getDownloadContext()->getFileEntries().size() == 1) {
  295. if(getFileEntry()->getLength() == 0) {
  296. getFileEntry()->setLength(getPieceStorage()->getCompletedLength());
  297. }
  298. }
  299. #ifdef ENABLE_MESSAGE_DIGEST
  300. if(getDownloadContext()->getPieceHashAlgo().empty()) {
  301. SharedHandle<CheckIntegrityEntry> entry
  302. (new ChecksumCheckIntegrityEntry(getRequestGroup()));
  303. if(entry->isValidationReady()) {
  304. entry->initValidator();
  305. entry->cutTrailingGarbage();
  306. getDownloadEngine()->getCheckIntegrityMan()->pushEntry(entry);
  307. }
  308. }
  309. // Following 2lines are needed for DownloadEngine to detect
  310. // completed RequestGroups without 1sec delay.
  311. getDownloadEngine()->setNoWait(true);
  312. getDownloadEngine()->setRefreshInterval(0);
  313. #endif // ENABLE_MESSAGE_DIGEST
  314. return true;
  315. } else {
  316. // The number of segments should be 1 in order to pass through the next
  317. // segment.
  318. if(getSegments().size() == 1) {
  319. SharedHandle<Segment> tempSegment = getSegments().front();
  320. if(!tempSegment->complete()) {
  321. return prepareForRetry(0);
  322. }
  323. if(getRequestEndOffset() ==
  324. getFileEntry()->gtoloff
  325. (tempSegment->getPosition()+tempSegment->getLength())) {
  326. return prepareForRetry(0);
  327. }
  328. SharedHandle<Segment> nextSegment = getSegmentMan()->getSegmentWithIndex
  329. (getCuid(), tempSegment->getIndex()+1);
  330. if(nextSegment.isNull()) {
  331. nextSegment = getSegmentMan()->getCleanSegmentIfOwnerIsIdle
  332. (getCuid(), tempSegment->getIndex()+1);
  333. }
  334. if(nextSegment.isNull() || nextSegment->getWrittenLength() > 0) {
  335. // If nextSegment->getWrittenLength() > 0, current socket must
  336. // be closed because writing incoming data at
  337. // nextSegment->getWrittenLength() corrupts file.
  338. return prepareForRetry(0);
  339. } else {
  340. getDownloadEngine()->addCommand(this);
  341. return false;
  342. }
  343. } else {
  344. return prepareForRetry(0);
  345. }
  346. }
  347. }
  348. #ifdef ENABLE_MESSAGE_DIGEST
  349. void DownloadCommand::validatePieceHash(const SharedHandle<Segment>& segment,
  350. const std::string& expectedPieceHash,
  351. const std::string& actualPieceHash)
  352. {
  353. if(actualPieceHash == expectedPieceHash) {
  354. getLogger()->info(MSG_GOOD_CHUNK_CHECKSUM, actualPieceHash.c_str());
  355. getSegmentMan()->completeSegment(getCuid(), segment);
  356. } else {
  357. getLogger()->info(EX_INVALID_CHUNK_CHECKSUM,
  358. segment->getIndex(),
  359. util::itos(segment->getPosition(), true).c_str(),
  360. expectedPieceHash.c_str(),
  361. actualPieceHash.c_str());
  362. segment->clear();
  363. getSegmentMan()->cancelSegment(getCuid());
  364. throw DL_RETRY_EX
  365. (StringFormat("Invalid checksum index=%d", segment->getIndex()).str());
  366. }
  367. }
  368. #endif // ENABLE_MESSAGE_DIGEST
  369. void DownloadCommand::setTransferEncodingDecoder
  370. (const SharedHandle<Decoder>& decoder)
  371. {
  372. this->transferEncodingDecoder_ = decoder;
  373. }
  374. void DownloadCommand::setContentEncodingDecoder
  375. (const SharedHandle<Decoder>& decoder)
  376. {
  377. contentEncodingDecoder_ = decoder;
  378. }
  379. } // namespace aria2