DownloadCommand.cc 14 KB

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