2006-02-17 13:35:04 +00:00
|
|
|
/* <!-- copyright */
|
|
|
|
/*
|
2006-09-21 15:31:24 +00:00
|
|
|
* aria2 - The high speed download utility
|
2006-02-17 13:35:04 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2006 Tatsuhiro Tsujikawa
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2006-09-21 15:31:24 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
|
|
|
* In addition, as a special exception, the copyright holders give
|
|
|
|
* permission to link the code of portions of this program with the
|
|
|
|
* OpenSSL library under certain conditions as described in each
|
|
|
|
* individual source file, and distribute linked combinations
|
|
|
|
* including the two.
|
|
|
|
* You must obey the GNU General Public License in all respects
|
|
|
|
* for all of the code used other than OpenSSL. If you modify
|
|
|
|
* file(s) with this exception, you may extend this exception to your
|
|
|
|
* version of the file(s), but you are not obligated to do so. If you
|
|
|
|
* do not wish to do so, delete this exception statement from your
|
|
|
|
* version. If you delete this exception statement from all source
|
|
|
|
* files in the program, then also delete it here.
|
2006-02-17 13:35:04 +00:00
|
|
|
*/
|
|
|
|
/* copyright --> */
|
|
|
|
#include "DownloadCommand.h"
|
2008-11-03 07:49:13 +00:00
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
#include "Request.h"
|
|
|
|
#include "RequestGroup.h"
|
|
|
|
#include "DownloadEngine.h"
|
2007-10-11 16:58:24 +00:00
|
|
|
#include "PeerStat.h"
|
2006-08-07 16:05:00 +00:00
|
|
|
#include "DlAbortEx.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
#include "DlRetryEx.h"
|
|
|
|
#include "SegmentMan.h"
|
2007-10-11 16:58:24 +00:00
|
|
|
#include "Segment.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
#include "Logger.h"
|
2007-11-12 11:28:16 +00:00
|
|
|
#include "ChecksumCheckIntegrityEntry.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
#include "PieceStorage.h"
|
2007-11-12 11:28:16 +00:00
|
|
|
#include "CheckIntegrityCommand.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
#include "DiskAdaptor.h"
|
|
|
|
#include "DownloadContext.h"
|
|
|
|
#include "Option.h"
|
|
|
|
#include "Util.h"
|
|
|
|
#include "Socket.h"
|
|
|
|
#include "message.h"
|
|
|
|
#include "prefs.h"
|
2008-04-27 02:22:14 +00:00
|
|
|
#include "StringFormat.h"
|
2008-06-29 14:29:36 +00:00
|
|
|
#include "Decoder.h"
|
2009-02-28 11:48:26 +00:00
|
|
|
#include "RequestGroupMan.h"
|
2007-10-12 15:11:37 +00:00
|
|
|
#ifdef ENABLE_MESSAGE_DIGEST
|
2008-02-28 17:40:47 +00:00
|
|
|
# include "MessageDigestHelper.h"
|
2007-10-12 15:11:37 +00:00
|
|
|
#endif // ENABLE_MESSAGE_DIGEST
|
2008-02-08 15:53:45 +00:00
|
|
|
|
|
|
|
namespace aria2 {
|
2006-02-17 13:35:04 +00:00
|
|
|
|
2006-10-18 14:57:00 +00:00
|
|
|
DownloadCommand::DownloadCommand(int cuid,
|
2008-02-08 15:53:45 +00:00
|
|
|
const RequestHandle& req,
|
2009-06-23 15:35:45 +00:00
|
|
|
const SharedHandle<FileEntry>& fileEntry,
|
2007-05-20 13:51:52 +00:00
|
|
|
RequestGroup* requestGroup,
|
2006-10-18 14:57:00 +00:00
|
|
|
DownloadEngine* e,
|
2006-07-19 17:07:45 +00:00
|
|
|
const SocketHandle& s):
|
2009-06-23 15:35:45 +00:00
|
|
|
AbstractCommand(cuid, req, fileEntry, requestGroup, e, s)
|
2008-06-04 16:28:16 +00:00
|
|
|
#ifdef ENABLE_MESSAGE_DIGEST
|
|
|
|
, _pieceHashValidationEnabled(false)
|
|
|
|
#endif // ENABLE_MESSAGE_DIGEST
|
2007-03-15 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
To handle Segment as SegmentHandle:
* src/AbstractCommand.cc (execute): Rewritten.
* src/SegmentMan.h: Segment -> SegmentHandle
Introducded HttpResponse class, HttpRequest class to improve
code
extensiveness and make it clear:
* src/HttpDownloadCommand.cc: transfer encoders are now managed
by
HttpResponse class.
* src/HttpRequest.h, src/HttpRequest.cc: New class.
* src/HttpResponse.h, src/HttpResponse.cc: New class.
* src/HttpConnection.cc: Contruction of http request were moved
to
HttpRequest class.
* src/HttpResponseCommand.h, src/HttpResponseCommand.cc:
Refactored.
* src/HttpRequestCommand.cc (executeInternal): Rewritten.
* src/HttpAuthConfig.h: New class.
* src/Range.h: New class.
To make FtpTunnel{Request, Response}Command and
HttpProxy{Request, Response}Command derived from
AbstractProxy{Request, Response}Command:
* src/FtpTunnelResponseCommand.h,
src/FtpTunnelResponseCommand.cc:
Derived from AbstractProxyRequestCommand class.
* src/FtpTunnelRequestCommand.h, src/FtpTunnelRequestCommand.cc:
Derived from AbstractProxyResponseCommand class.
* src/HttpProxyRequestCommand.h, src/HttpProxyRequestCommand.cc:
Derived from AbstractProxyRequestCommand class.
* src/HttpProxyResponseCommand.h,
src/HttpProxyResponseCommand.cc:
Derived from AbstractProxyResponseCommand class.
* src/AbstractProxyRequestCommand.h,
src/AbstractProxyRequestCommand.cc
: New class.
* src/AbstractProxyResponseCommand.h,
src/AbstractProxyResponseCommand.cc: New class.
To add netrc support:
* src/Netrc.h, src/Netrc.cc: New class.
* src/Util.h, src/Util.cc (split): New function.
* src/HttpHeader.cc (getRange): Fixed so that it inspects
"Content-Range" header instead of "Range" header.
* src/HttpHeader.h
(getStatus): Removed.
(setStatus): Removed.
* src/Segment.h
(getPositionToWrite): New function.
2007-03-15 15:07:18 +00:00
|
|
|
{
|
2008-02-28 17:40:47 +00:00
|
|
|
#ifdef ENABLE_MESSAGE_DIGEST
|
|
|
|
{
|
2009-05-08 16:41:21 +00:00
|
|
|
if(getOption()->getAsBool(PREF_REALTIME_CHUNK_CHECKSUM)) {
|
2008-06-04 16:28:16 +00:00
|
|
|
std::string algo = _requestGroup->getDownloadContext()->getPieceHashAlgo();
|
|
|
|
if(MessageDigestContext::supports(algo)) {
|
|
|
|
_messageDigestContext.reset(new MessageDigestContext());
|
|
|
|
_messageDigestContext->trySetAlgo(algo);
|
|
|
|
_messageDigestContext->digestInit();
|
|
|
|
|
|
|
|
_pieceHashValidationEnabled = true;
|
|
|
|
}
|
2008-02-28 17:40:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif // ENABLE_MESSAGE_DIGEST
|
2009-06-29 15:18:21 +00:00
|
|
|
peerStat = req->getPeerStat();
|
2007-07-01 14:19:15 +00:00
|
|
|
if(peerStat.isNull()) {
|
2009-06-29 15:18:21 +00:00
|
|
|
peerStat = req->initPeerStat();
|
2007-05-20 13:51:52 +00:00
|
|
|
_requestGroup->getSegmentMan()->registerPeerStat(peerStat);
|
2006-11-08 16:25:38 +00:00
|
|
|
}
|
2006-09-19 14:52:59 +00:00
|
|
|
peerStat->downloadStart();
|
2006-07-19 17:07:45 +00:00
|
|
|
}
|
2006-02-17 13:35:04 +00:00
|
|
|
|
2006-09-19 14:52:59 +00:00
|
|
|
DownloadCommand::~DownloadCommand() {
|
|
|
|
assert(peerStat.get());
|
|
|
|
peerStat->downloadStop();
|
2009-06-29 15:18:21 +00:00
|
|
|
req->purgePeerStat();
|
2006-09-19 14:52:59 +00:00
|
|
|
}
|
2006-02-17 13:35:04 +00:00
|
|
|
|
2007-03-15 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
To handle Segment as SegmentHandle:
* src/AbstractCommand.cc (execute): Rewritten.
* src/SegmentMan.h: Segment -> SegmentHandle
Introducded HttpResponse class, HttpRequest class to improve
code
extensiveness and make it clear:
* src/HttpDownloadCommand.cc: transfer encoders are now managed
by
HttpResponse class.
* src/HttpRequest.h, src/HttpRequest.cc: New class.
* src/HttpResponse.h, src/HttpResponse.cc: New class.
* src/HttpConnection.cc: Contruction of http request were moved
to
HttpRequest class.
* src/HttpResponseCommand.h, src/HttpResponseCommand.cc:
Refactored.
* src/HttpRequestCommand.cc (executeInternal): Rewritten.
* src/HttpAuthConfig.h: New class.
* src/Range.h: New class.
To make FtpTunnel{Request, Response}Command and
HttpProxy{Request, Response}Command derived from
AbstractProxy{Request, Response}Command:
* src/FtpTunnelResponseCommand.h,
src/FtpTunnelResponseCommand.cc:
Derived from AbstractProxyRequestCommand class.
* src/FtpTunnelRequestCommand.h, src/FtpTunnelRequestCommand.cc:
Derived from AbstractProxyResponseCommand class.
* src/HttpProxyRequestCommand.h, src/HttpProxyRequestCommand.cc:
Derived from AbstractProxyRequestCommand class.
* src/HttpProxyResponseCommand.h,
src/HttpProxyResponseCommand.cc:
Derived from AbstractProxyResponseCommand class.
* src/AbstractProxyRequestCommand.h,
src/AbstractProxyRequestCommand.cc
: New class.
* src/AbstractProxyResponseCommand.h,
src/AbstractProxyResponseCommand.cc: New class.
To add netrc support:
* src/Netrc.h, src/Netrc.cc: New class.
* src/Util.h, src/Util.cc (split): New function.
* src/HttpHeader.cc (getRange): Fixed so that it inspects
"Content-Range" header instead of "Range" header.
* src/HttpHeader.h
(getStatus): Removed.
(setStatus): Removed.
* src/Segment.h
(getPositionToWrite): New function.
2007-03-15 15:07:18 +00:00
|
|
|
bool DownloadCommand::executeInternal() {
|
2009-02-28 11:48:26 +00:00
|
|
|
if(e->_requestGroupMan->doesOverallDownloadSpeedExceed() ||
|
|
|
|
_requestGroup->doesDownloadSpeedExceed()) {
|
2006-09-19 14:52:59 +00:00
|
|
|
e->commands.push_back(this);
|
2007-11-07 12:36:33 +00:00
|
|
|
disableReadCheckSocket();
|
2006-09-19 14:52:59 +00:00
|
|
|
return false;
|
|
|
|
}
|
2007-11-07 12:36:33 +00:00
|
|
|
setReadCheckSocket(socket);
|
2007-10-17 16:26:51 +00:00
|
|
|
SegmentHandle segment = _segments.front();
|
|
|
|
|
2008-03-08 08:04:28 +00:00
|
|
|
size_t BUFSIZE = 16*1024;
|
2008-03-07 12:05:50 +00:00
|
|
|
unsigned char buf[BUFSIZE];
|
2008-03-09 12:24:01 +00:00
|
|
|
size_t bufSize;
|
2009-06-23 15:35:45 +00:00
|
|
|
if(segment->getLength() > 0) {
|
|
|
|
if(segment->getPosition()+segment->getLength() <= static_cast<uint64_t>(_fileEntry->getLastOffset())) {
|
|
|
|
bufSize = std::min(segment->getLength()-segment->getWrittenLength(),
|
|
|
|
BUFSIZE);
|
|
|
|
} else {
|
|
|
|
bufSize = std::min(static_cast<size_t>(_fileEntry->getLastOffset()-_fileEntry->gtoloff(segment->getPositionToWrite())), BUFSIZE);
|
|
|
|
}
|
2007-10-17 16:26:51 +00:00
|
|
|
} else {
|
|
|
|
bufSize = BUFSIZE;
|
|
|
|
}
|
2006-02-17 13:35:04 +00:00
|
|
|
socket->readData(buf, bufSize);
|
2007-03-15 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
To handle Segment as SegmentHandle:
* src/AbstractCommand.cc (execute): Rewritten.
* src/SegmentMan.h: Segment -> SegmentHandle
Introducded HttpResponse class, HttpRequest class to improve
code
extensiveness and make it clear:
* src/HttpDownloadCommand.cc: transfer encoders are now managed
by
HttpResponse class.
* src/HttpRequest.h, src/HttpRequest.cc: New class.
* src/HttpResponse.h, src/HttpResponse.cc: New class.
* src/HttpConnection.cc: Contruction of http request were moved
to
HttpRequest class.
* src/HttpResponseCommand.h, src/HttpResponseCommand.cc:
Refactored.
* src/HttpRequestCommand.cc (executeInternal): Rewritten.
* src/HttpAuthConfig.h: New class.
* src/Range.h: New class.
To make FtpTunnel{Request, Response}Command and
HttpProxy{Request, Response}Command derived from
AbstractProxy{Request, Response}Command:
* src/FtpTunnelResponseCommand.h,
src/FtpTunnelResponseCommand.cc:
Derived from AbstractProxyRequestCommand class.
* src/FtpTunnelRequestCommand.h, src/FtpTunnelRequestCommand.cc:
Derived from AbstractProxyResponseCommand class.
* src/HttpProxyRequestCommand.h, src/HttpProxyRequestCommand.cc:
Derived from AbstractProxyRequestCommand class.
* src/HttpProxyResponseCommand.h,
src/HttpProxyResponseCommand.cc:
Derived from AbstractProxyResponseCommand class.
* src/AbstractProxyRequestCommand.h,
src/AbstractProxyRequestCommand.cc
: New class.
* src/AbstractProxyResponseCommand.h,
src/AbstractProxyResponseCommand.cc: New class.
To add netrc support:
* src/Netrc.h, src/Netrc.cc: New class.
* src/Util.h, src/Util.cc (split): New function.
* src/HttpHeader.cc (getRange): Fixed so that it inspects
"Content-Range" header instead of "Range" header.
* src/HttpHeader.h
(getStatus): Removed.
(setStatus): Removed.
* src/Segment.h
(getPositionToWrite): New function.
2007-03-15 15:07:18 +00:00
|
|
|
|
2008-06-29 14:29:36 +00:00
|
|
|
const SharedHandle<DiskAdaptor>& diskAdaptor =
|
|
|
|
_requestGroup->getPieceStorage()->getDiskAdaptor();
|
2008-06-04 16:28:16 +00:00
|
|
|
|
2008-07-01 11:38:25 +00:00
|
|
|
const unsigned char* bufFinal;
|
2008-06-29 14:29:36 +00:00
|
|
|
size_t bufSizeFinal;
|
2008-06-04 16:28:16 +00:00
|
|
|
|
2008-07-01 11:38:25 +00:00
|
|
|
std::string decoded;
|
|
|
|
if(_transferEncodingDecoder.isNull()) {
|
2008-06-29 14:29:36 +00:00
|
|
|
bufFinal = buf;
|
|
|
|
bufSizeFinal = bufSize;
|
2007-03-15 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
To handle Segment as SegmentHandle:
* src/AbstractCommand.cc (execute): Rewritten.
* src/SegmentMan.h: Segment -> SegmentHandle
Introducded HttpResponse class, HttpRequest class to improve
code
extensiveness and make it clear:
* src/HttpDownloadCommand.cc: transfer encoders are now managed
by
HttpResponse class.
* src/HttpRequest.h, src/HttpRequest.cc: New class.
* src/HttpResponse.h, src/HttpResponse.cc: New class.
* src/HttpConnection.cc: Contruction of http request were moved
to
HttpRequest class.
* src/HttpResponseCommand.h, src/HttpResponseCommand.cc:
Refactored.
* src/HttpRequestCommand.cc (executeInternal): Rewritten.
* src/HttpAuthConfig.h: New class.
* src/Range.h: New class.
To make FtpTunnel{Request, Response}Command and
HttpProxy{Request, Response}Command derived from
AbstractProxy{Request, Response}Command:
* src/FtpTunnelResponseCommand.h,
src/FtpTunnelResponseCommand.cc:
Derived from AbstractProxyRequestCommand class.
* src/FtpTunnelRequestCommand.h, src/FtpTunnelRequestCommand.cc:
Derived from AbstractProxyResponseCommand class.
* src/HttpProxyRequestCommand.h, src/HttpProxyRequestCommand.cc:
Derived from AbstractProxyRequestCommand class.
* src/HttpProxyResponseCommand.h,
src/HttpProxyResponseCommand.cc:
Derived from AbstractProxyResponseCommand class.
* src/AbstractProxyRequestCommand.h,
src/AbstractProxyRequestCommand.cc
: New class.
* src/AbstractProxyResponseCommand.h,
src/AbstractProxyResponseCommand.cc: New class.
To add netrc support:
* src/Netrc.h, src/Netrc.cc: New class.
* src/Util.h, src/Util.cc (split): New function.
* src/HttpHeader.cc (getRange): Fixed so that it inspects
"Content-Range" header instead of "Range" header.
* src/HttpHeader.h
(getStatus): Removed.
(setStatus): Removed.
* src/Segment.h
(getPositionToWrite): New function.
2007-03-15 15:07:18 +00:00
|
|
|
} else {
|
2008-07-01 11:38:25 +00:00
|
|
|
decoded = _transferEncodingDecoder->decode(buf, bufSize);
|
2008-06-29 14:29:36 +00:00
|
|
|
|
2008-07-01 11:38:25 +00:00
|
|
|
bufFinal = reinterpret_cast<const unsigned char*>(decoded.c_str());
|
|
|
|
bufSizeFinal = decoded.size();
|
2008-06-29 14:29:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(_contentEncodingDecoder.isNull()) {
|
|
|
|
diskAdaptor->writeData(bufFinal, bufSizeFinal,
|
|
|
|
segment->getPositionToWrite());
|
|
|
|
} else {
|
|
|
|
std::string out = _contentEncodingDecoder->decode(bufFinal, bufSizeFinal);
|
|
|
|
diskAdaptor->writeData(reinterpret_cast<const unsigned char*>(out.data()),
|
|
|
|
out.size(),
|
|
|
|
segment->getPositionToWrite());
|
|
|
|
bufSizeFinal = out.size();
|
|
|
|
}
|
2008-06-04 16:28:16 +00:00
|
|
|
|
|
|
|
#ifdef ENABLE_MESSAGE_DIGEST
|
|
|
|
|
2008-06-29 14:29:36 +00:00
|
|
|
if(_pieceHashValidationEnabled) {
|
|
|
|
segment->updateHash(segment->getWrittenLength(), bufFinal, bufSizeFinal);
|
|
|
|
}
|
2008-06-04 16:28:16 +00:00
|
|
|
|
|
|
|
#endif // ENABLE_MESSAGE_DIGEST
|
2009-04-17 13:19:17 +00:00
|
|
|
if(bufSizeFinal > 0) {
|
|
|
|
segment->updateWrittenLength(bufSizeFinal);
|
|
|
|
}
|
2008-06-04 16:28:16 +00:00
|
|
|
|
2008-06-29 14:29:36 +00:00
|
|
|
peerStat->updateDownloadLength(bufSize);
|
2008-06-04 16:28:16 +00:00
|
|
|
|
2009-03-19 13:42:10 +00:00
|
|
|
_requestGroup->getSegmentMan()->updateDownloadSpeedFor(peerStat);
|
|
|
|
|
2008-09-27 16:06:34 +00:00
|
|
|
bool segmentComplete = false;
|
2009-05-05 15:19:02 +00:00
|
|
|
// Note that GrowSegment::complete() always returns false.
|
2008-09-27 16:06:34 +00:00
|
|
|
if(_transferEncodingDecoder.isNull() && _contentEncodingDecoder.isNull()) {
|
2009-06-23 15:35:45 +00:00
|
|
|
if(segment->complete() ||
|
|
|
|
segment->getPositionToWrite() == _fileEntry->getLastOffset()) {
|
2008-09-27 16:06:34 +00:00
|
|
|
segmentComplete = true;
|
|
|
|
} else if(segment->getLength() == 0 && bufSize == 0 &&
|
|
|
|
!socket->wantRead() && !socket->wantWrite()) {
|
|
|
|
segmentComplete = true;
|
|
|
|
}
|
2009-06-23 15:35:45 +00:00
|
|
|
} else if(!_transferEncodingDecoder.isNull() &&
|
|
|
|
(segment->complete() || segment->getPositionToWrite() == _fileEntry->getLastOffset())) {
|
2009-05-05 15:19:02 +00:00
|
|
|
segmentComplete = true;
|
2009-04-17 14:55:18 +00:00
|
|
|
} else if((_transferEncodingDecoder.isNull() ||
|
|
|
|
_transferEncodingDecoder->finished()) &&
|
|
|
|
(_contentEncodingDecoder.isNull() ||
|
|
|
|
_contentEncodingDecoder->finished())) {
|
|
|
|
segmentComplete = true;
|
2008-09-27 16:06:34 +00:00
|
|
|
}
|
2008-06-29 14:29:36 +00:00
|
|
|
|
2009-04-17 13:19:17 +00:00
|
|
|
if(!segmentComplete && bufSize == 0 &&
|
|
|
|
!socket->wantRead() && !socket->wantWrite()) {
|
2009-05-18 15:07:15 +00:00
|
|
|
throw DL_RETRY_EX(EX_GOT_EOF);
|
2009-04-17 13:19:17 +00:00
|
|
|
}
|
|
|
|
|
2008-09-27 16:06:34 +00:00
|
|
|
if(segmentComplete) {
|
2009-06-23 15:35:45 +00:00
|
|
|
if(segment->complete() || segment->getLength() == 0) {
|
|
|
|
// If segment->getLength() == 0, the server doesn't provide
|
|
|
|
// content length, but the client detected that download
|
|
|
|
// completed.
|
|
|
|
logger->info(MSG_SEGMENT_DOWNLOAD_COMPLETED, cuid);
|
2008-06-04 16:28:16 +00:00
|
|
|
#ifdef ENABLE_MESSAGE_DIGEST
|
|
|
|
|
2009-06-23 15:35:45 +00:00
|
|
|
{
|
|
|
|
std::string expectedPieceHash =
|
|
|
|
_requestGroup->getDownloadContext()->getPieceHash(segment->getIndex());
|
|
|
|
if(_pieceHashValidationEnabled && !expectedPieceHash.empty()) {
|
|
|
|
if(segment->isHashCalculated()) {
|
|
|
|
logger->debug("Hash is available! index=%lu",
|
|
|
|
static_cast<unsigned long>(segment->getIndex()));
|
|
|
|
validatePieceHash(segment, expectedPieceHash, segment->getHashString());
|
|
|
|
} else {
|
|
|
|
_messageDigestContext->digestReset();
|
|
|
|
validatePieceHash(segment, expectedPieceHash,
|
|
|
|
MessageDigestHelper::digest
|
|
|
|
(_messageDigestContext.get(),
|
|
|
|
_requestGroup->getPieceStorage()->getDiskAdaptor(),
|
|
|
|
segment->getPosition(),
|
|
|
|
segment->getLength()));
|
|
|
|
}
|
2008-06-04 16:28:16 +00:00
|
|
|
} else {
|
2009-06-23 15:35:45 +00:00
|
|
|
_requestGroup->getSegmentMan()->completeSegment(cuid, segment);
|
2008-06-04 16:28:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#else // !ENABLE_MESSAGE_DIGEST
|
2009-06-23 15:35:45 +00:00
|
|
|
_requestGroup->getSegmentMan()->completeSegment(cuid, segment);
|
2008-06-04 16:28:16 +00:00
|
|
|
#endif // !ENABLE_MESSAGE_DIGEST
|
2009-06-23 15:35:45 +00:00
|
|
|
}
|
|
|
|
|
2008-06-04 16:28:16 +00:00
|
|
|
|
2008-01-06 16:32:52 +00:00
|
|
|
checkLowestDownloadSpeed();
|
2006-02-17 13:35:04 +00:00
|
|
|
// this unit is going to download another segment.
|
2007-03-15 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
To handle Segment as SegmentHandle:
* src/AbstractCommand.cc (execute): Rewritten.
* src/SegmentMan.h: Segment -> SegmentHandle
Introducded HttpResponse class, HttpRequest class to improve
code
extensiveness and make it clear:
* src/HttpDownloadCommand.cc: transfer encoders are now managed
by
HttpResponse class.
* src/HttpRequest.h, src/HttpRequest.cc: New class.
* src/HttpResponse.h, src/HttpResponse.cc: New class.
* src/HttpConnection.cc: Contruction of http request were moved
to
HttpRequest class.
* src/HttpResponseCommand.h, src/HttpResponseCommand.cc:
Refactored.
* src/HttpRequestCommand.cc (executeInternal): Rewritten.
* src/HttpAuthConfig.h: New class.
* src/Range.h: New class.
To make FtpTunnel{Request, Response}Command and
HttpProxy{Request, Response}Command derived from
AbstractProxy{Request, Response}Command:
* src/FtpTunnelResponseCommand.h,
src/FtpTunnelResponseCommand.cc:
Derived from AbstractProxyRequestCommand class.
* src/FtpTunnelRequestCommand.h, src/FtpTunnelRequestCommand.cc:
Derived from AbstractProxyResponseCommand class.
* src/HttpProxyRequestCommand.h, src/HttpProxyRequestCommand.cc:
Derived from AbstractProxyRequestCommand class.
* src/HttpProxyResponseCommand.h,
src/HttpProxyResponseCommand.cc:
Derived from AbstractProxyResponseCommand class.
* src/AbstractProxyRequestCommand.h,
src/AbstractProxyRequestCommand.cc
: New class.
* src/AbstractProxyResponseCommand.h,
src/AbstractProxyResponseCommand.cc: New class.
To add netrc support:
* src/Netrc.h, src/Netrc.cc: New class.
* src/Util.h, src/Util.cc (split): New function.
* src/HttpHeader.cc (getRange): Fixed so that it inspects
"Content-Range" header instead of "Range" header.
* src/HttpHeader.h
(getStatus): Removed.
(setStatus): Removed.
* src/Segment.h
(getPositionToWrite): New function.
2007-03-15 15:07:18 +00:00
|
|
|
return prepareForNextSegment();
|
2006-02-17 13:35:04 +00:00
|
|
|
} else {
|
2008-01-06 16:32:52 +00:00
|
|
|
checkLowestDownloadSpeed();
|
2008-09-27 16:06:34 +00:00
|
|
|
setWriteCheckSocketIf(socket, socket->wantWrite());
|
2006-05-09 15:54:14 +00:00
|
|
|
e->commands.push_back(this);
|
2006-02-17 13:35:04 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-06 16:32:52 +00:00
|
|
|
void DownloadCommand::checkLowestDownloadSpeed() const
|
|
|
|
{
|
|
|
|
// calculate downloading speed
|
|
|
|
if(peerStat->getDownloadStartTime().elapsed(startupIdleTime)) {
|
2008-03-08 10:33:56 +00:00
|
|
|
unsigned int nowSpeed = peerStat->calculateDownloadSpeed();
|
2008-01-06 16:32:52 +00:00
|
|
|
if(lowestDownloadSpeedLimit > 0 && nowSpeed <= lowestDownloadSpeedLimit) {
|
2009-05-18 15:07:15 +00:00
|
|
|
throw DL_ABORT_EX2(StringFormat(EX_TOO_SLOW_DOWNLOAD_SPEED,
|
|
|
|
nowSpeed,
|
|
|
|
lowestDownloadSpeedLimit,
|
|
|
|
req->getHost().c_str()).str(),
|
2009-06-29 08:42:58 +00:00
|
|
|
downloadresultcode::TOO_SLOW_DOWNLOAD_SPEED);
|
2008-01-06 16:32:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-15 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
To handle Segment as SegmentHandle:
* src/AbstractCommand.cc (execute): Rewritten.
* src/SegmentMan.h: Segment -> SegmentHandle
Introducded HttpResponse class, HttpRequest class to improve
code
extensiveness and make it clear:
* src/HttpDownloadCommand.cc: transfer encoders are now managed
by
HttpResponse class.
* src/HttpRequest.h, src/HttpRequest.cc: New class.
* src/HttpResponse.h, src/HttpResponse.cc: New class.
* src/HttpConnection.cc: Contruction of http request were moved
to
HttpRequest class.
* src/HttpResponseCommand.h, src/HttpResponseCommand.cc:
Refactored.
* src/HttpRequestCommand.cc (executeInternal): Rewritten.
* src/HttpAuthConfig.h: New class.
* src/Range.h: New class.
To make FtpTunnel{Request, Response}Command and
HttpProxy{Request, Response}Command derived from
AbstractProxy{Request, Response}Command:
* src/FtpTunnelResponseCommand.h,
src/FtpTunnelResponseCommand.cc:
Derived from AbstractProxyRequestCommand class.
* src/FtpTunnelRequestCommand.h, src/FtpTunnelRequestCommand.cc:
Derived from AbstractProxyResponseCommand class.
* src/HttpProxyRequestCommand.h, src/HttpProxyRequestCommand.cc:
Derived from AbstractProxyRequestCommand class.
* src/HttpProxyResponseCommand.h,
src/HttpProxyResponseCommand.cc:
Derived from AbstractProxyResponseCommand class.
* src/AbstractProxyRequestCommand.h,
src/AbstractProxyRequestCommand.cc
: New class.
* src/AbstractProxyResponseCommand.h,
src/AbstractProxyResponseCommand.cc: New class.
To add netrc support:
* src/Netrc.h, src/Netrc.cc: New class.
* src/Util.h, src/Util.cc (split): New function.
* src/HttpHeader.cc (getRange): Fixed so that it inspects
"Content-Range" header instead of "Range" header.
* src/HttpHeader.h
(getStatus): Removed.
(setStatus): Removed.
* src/Segment.h
(getPositionToWrite): New function.
2007-03-15 15:07:18 +00:00
|
|
|
bool DownloadCommand::prepareForNextSegment() {
|
2007-10-11 16:58:24 +00:00
|
|
|
if(_requestGroup->downloadFinished()) {
|
2007-07-04 16:04:57 +00:00
|
|
|
#ifdef ENABLE_MESSAGE_DIGEST
|
2008-04-20 00:50:22 +00:00
|
|
|
CheckIntegrityEntryHandle entry(new ChecksumCheckIntegrityEntry(_requestGroup));
|
2007-11-12 11:28:16 +00:00
|
|
|
if(entry->isValidationReady()) {
|
|
|
|
entry->initValidator();
|
2009-01-24 10:40:42 +00:00
|
|
|
// TODO do we need cuttrailinggarbage here?
|
|
|
|
e->_checkIntegrityMan->pushEntry(entry);
|
2007-05-31 15:56:20 +00:00
|
|
|
}
|
2008-11-11 16:05:42 +00:00
|
|
|
e->setNoWait(true);
|
|
|
|
e->setRefreshInterval(0);
|
2007-07-04 16:04:57 +00:00
|
|
|
#endif // ENABLE_MESSAGE_DIGEST
|
2006-02-17 13:35:04 +00:00
|
|
|
return true;
|
2006-09-19 14:52:59 +00:00
|
|
|
} else {
|
2008-12-15 15:38:07 +00:00
|
|
|
// The number of segments should be 1 in order to pass through the next
|
|
|
|
// segment.
|
|
|
|
if(_segments.size() == 1) {
|
2008-05-09 14:44:28 +00:00
|
|
|
SegmentHandle tempSegment = _segments.front();
|
2009-06-23 15:35:45 +00:00
|
|
|
if(!tempSegment->complete()) {
|
|
|
|
return prepareForRetry(0);
|
|
|
|
}
|
|
|
|
// TODO1.5 get segment for the same file only
|
2008-05-09 14:44:28 +00:00
|
|
|
SegmentHandle nextSegment =
|
|
|
|
_requestGroup->getSegmentMan()->getSegment(cuid,
|
|
|
|
tempSegment->getIndex()+1);
|
|
|
|
if(!nextSegment.isNull() && nextSegment->getWrittenLength() == 0) {
|
|
|
|
e->commands.push_back(this);
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return prepareForRetry(0);
|
|
|
|
}
|
2007-11-04 12:26:12 +00:00
|
|
|
} else {
|
|
|
|
return prepareForRetry(0);
|
2006-09-19 14:52:59 +00:00
|
|
|
}
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|
|
|
|
}
|
2007-10-11 16:58:24 +00:00
|
|
|
|
2007-10-12 15:11:37 +00:00
|
|
|
#ifdef ENABLE_MESSAGE_DIGEST
|
2008-06-04 16:28:16 +00:00
|
|
|
|
|
|
|
void DownloadCommand::validatePieceHash(const SharedHandle<Segment>& segment,
|
|
|
|
const std::string& expectedPieceHash,
|
|
|
|
const std::string& actualPieceHash)
|
|
|
|
{
|
|
|
|
if(actualPieceHash == expectedPieceHash) {
|
|
|
|
logger->info(MSG_GOOD_CHUNK_CHECKSUM, actualPieceHash.c_str());
|
|
|
|
_requestGroup->getSegmentMan()->completeSegment(cuid, segment);
|
|
|
|
} else {
|
|
|
|
logger->info(EX_INVALID_CHUNK_CHECKSUM,
|
|
|
|
segment->getIndex(),
|
|
|
|
Util::itos(segment->getPosition(), true).c_str(),
|
|
|
|
expectedPieceHash.c_str(),
|
|
|
|
actualPieceHash.c_str());
|
|
|
|
segment->clear();
|
|
|
|
_requestGroup->getSegmentMan()->cancelSegment(cuid);
|
2009-05-18 15:07:15 +00:00
|
|
|
throw DL_RETRY_EX
|
2008-06-04 16:28:16 +00:00
|
|
|
(StringFormat("Invalid checksum index=%d", segment->getIndex()).str());
|
|
|
|
}
|
2007-10-12 15:11:37 +00:00
|
|
|
}
|
|
|
|
|
2008-06-04 16:28:16 +00:00
|
|
|
#endif // ENABLE_MESSAGE_DIGEST
|
|
|
|
|
2008-07-01 11:38:25 +00:00
|
|
|
void DownloadCommand::setTransferEncodingDecoder
|
|
|
|
(const SharedHandle<Decoder>& decoder)
|
2007-10-11 16:58:24 +00:00
|
|
|
{
|
2008-07-01 11:38:25 +00:00
|
|
|
this->_transferEncodingDecoder = decoder;
|
2007-10-11 16:58:24 +00:00
|
|
|
}
|
2008-02-08 15:53:45 +00:00
|
|
|
|
2008-06-29 14:29:36 +00:00
|
|
|
void DownloadCommand::setContentEncodingDecoder
|
|
|
|
(const SharedHandle<Decoder>& decoder)
|
|
|
|
{
|
|
|
|
_contentEncodingDecoder = decoder;
|
|
|
|
}
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
} // namespace aria2
|