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
|
2010-01-05 16:01:46 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2006-09-21 15:31:24 +00:00
|
|
|
*
|
|
|
|
* 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 "HttpResponseCommand.h"
|
2007-10-11 16:58:24 +00:00
|
|
|
#include "DownloadEngine.h"
|
2009-06-28 10:37:15 +00:00
|
|
|
#include "DownloadContext.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
#include "FileEntry.h"
|
|
|
|
#include "RequestGroup.h"
|
|
|
|
#include "RequestGroupMan.h"
|
|
|
|
#include "Request.h"
|
|
|
|
#include "HttpRequest.h"
|
2007-10-11 16:58:24 +00:00
|
|
|
#include "HttpResponse.h"
|
|
|
|
#include "HttpConnection.h"
|
|
|
|
#include "SegmentMan.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
#include "Segment.h"
|
2006-02-17 13:35:04 +00:00
|
|
|
#include "HttpDownloadCommand.h"
|
2007-10-11 16:58:24 +00:00
|
|
|
#include "DiskAdaptor.h"
|
|
|
|
#include "PieceStorage.h"
|
2007-11-05 15:50:49 +00:00
|
|
|
#include "DefaultBtProgressInfoFile.h"
|
2007-11-13 11:46:58 +00:00
|
|
|
#include "DownloadFailureException.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
#include "DlAbortEx.h"
|
2009-10-22 15:35:33 +00:00
|
|
|
#include "util.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
#include "File.h"
|
|
|
|
#include "Option.h"
|
|
|
|
#include "Logger.h"
|
|
|
|
#include "Socket.h"
|
|
|
|
#include "message.h"
|
|
|
|
#include "prefs.h"
|
2010-11-20 08:21:36 +00:00
|
|
|
#include "fmt.h"
|
2008-05-10 04:57:33 +00:00
|
|
|
#include "HttpSkipResponseCommand.h"
|
2008-05-14 14:02:18 +00:00
|
|
|
#include "HttpHeader.h"
|
2008-06-29 14:29:36 +00:00
|
|
|
#include "LogFactory.h"
|
2008-09-01 13:46:03 +00:00
|
|
|
#include "CookieStorage.h"
|
2008-11-03 10:06:25 +00:00
|
|
|
#include "AuthConfigFactory.h"
|
2009-01-30 16:12:41 +00:00
|
|
|
#include "AuthConfig.h"
|
2009-06-06 12:33:07 +00:00
|
|
|
#include "a2functional.h"
|
2009-06-29 08:42:58 +00:00
|
|
|
#include "URISelector.h"
|
2010-06-08 14:11:36 +00:00
|
|
|
#include "CheckIntegrityEntry.h"
|
2010-09-06 14:29:36 +00:00
|
|
|
#include "StreamFilter.h"
|
|
|
|
#include "SinkStreamFilter.h"
|
|
|
|
#include "ChunkedDecodingStreamFilter.h"
|
2010-09-11 12:09:19 +00:00
|
|
|
#include "uri.h"
|
2011-01-16 07:55:41 +00:00
|
|
|
#include "SocketRecvBuffer.h"
|
2011-07-27 11:54:21 +00:00
|
|
|
#include "MetalinkHttpEntry.h"
|
|
|
|
#ifdef ENABLE_MESSAGE_DIGEST
|
|
|
|
#include "Checksum.h"
|
|
|
|
#endif // ENABLE_MESSAGE_DIGEST
|
2011-02-18 13:34:38 +00:00
|
|
|
#ifdef HAVE_ZLIB
|
2010-09-13 13:31:09 +00:00
|
|
|
# include "GZipDecodingStreamFilter.h"
|
2011-02-18 13:34:38 +00:00
|
|
|
#endif // HAVE_ZLIB
|
2008-02-08 15:53:45 +00:00
|
|
|
|
|
|
|
namespace aria2 {
|
2006-02-17 13:35:04 +00:00
|
|
|
|
2010-10-30 16:02:15 +00:00
|
|
|
namespace {
|
|
|
|
SharedHandle<StreamFilter> getTransferEncodingStreamFilter
|
2010-09-06 14:29:36 +00:00
|
|
|
(const SharedHandle<HttpResponse>& httpResponse,
|
2010-10-30 16:02:15 +00:00
|
|
|
const SharedHandle<StreamFilter>& delegate = SharedHandle<StreamFilter>())
|
|
|
|
{
|
|
|
|
SharedHandle<StreamFilter> filter;
|
|
|
|
if(httpResponse->isTransferEncodingSpecified()) {
|
|
|
|
filter = httpResponse->getTransferEncodingStreamFilter();
|
2010-11-12 12:48:48 +00:00
|
|
|
if(!filter) {
|
2010-10-30 16:02:15 +00:00
|
|
|
throw DL_ABORT_EX
|
2010-11-20 09:36:14 +00:00
|
|
|
(fmt(EX_TRANSFER_ENCODING_NOT_SUPPORTED,
|
|
|
|
httpResponse->getTransferEncoding().c_str()));
|
2010-10-30 16:02:15 +00:00
|
|
|
}
|
|
|
|
filter->init();
|
|
|
|
filter->installDelegate(delegate);
|
|
|
|
}
|
2010-11-12 12:48:48 +00:00
|
|
|
if(!filter) {
|
2010-10-30 16:02:15 +00:00
|
|
|
filter = delegate;
|
|
|
|
}
|
|
|
|
return filter;
|
|
|
|
}
|
|
|
|
} // namespace
|
2009-04-17 13:19:17 +00:00
|
|
|
|
2010-10-30 16:02:15 +00:00
|
|
|
namespace {
|
|
|
|
SharedHandle<StreamFilter> getContentEncodingStreamFilter
|
2010-09-06 14:29:36 +00:00
|
|
|
(const SharedHandle<HttpResponse>& httpResponse,
|
2010-10-30 16:02:15 +00:00
|
|
|
const SharedHandle<StreamFilter>& delegate = SharedHandle<StreamFilter>())
|
|
|
|
{
|
|
|
|
SharedHandle<StreamFilter> filter;
|
|
|
|
if(httpResponse->isContentEncodingSpecified()) {
|
|
|
|
filter = httpResponse->getContentEncodingStreamFilter();
|
2010-11-12 12:48:48 +00:00
|
|
|
if(!filter) {
|
2010-11-20 08:21:36 +00:00
|
|
|
A2_LOG_INFO
|
|
|
|
(fmt("Content-Encoding %s is specified, but the current implementation"
|
|
|
|
"doesn't support it. The decoding process is skipped and the"
|
|
|
|
"downloaded content will be still encoded.",
|
|
|
|
httpResponse->getContentEncoding().c_str()));
|
2010-10-30 16:02:15 +00:00
|
|
|
} else {
|
|
|
|
filter->init();
|
|
|
|
filter->installDelegate(delegate);
|
|
|
|
}
|
|
|
|
}
|
2010-11-12 12:48:48 +00:00
|
|
|
if(!filter) {
|
2010-10-30 16:02:15 +00:00
|
|
|
filter = delegate;
|
|
|
|
}
|
|
|
|
return filter;
|
|
|
|
}
|
|
|
|
} // namespace
|
2009-04-17 13:19:17 +00:00
|
|
|
|
2009-06-23 15:35:45 +00:00
|
|
|
HttpResponseCommand::HttpResponseCommand
|
2010-03-20 14:30:36 +00:00
|
|
|
(cuid_t cuid,
|
2010-02-28 12:30:11 +00:00
|
|
|
const SharedHandle<Request>& req,
|
2009-06-23 15:35:45 +00:00
|
|
|
const SharedHandle<FileEntry>& fileEntry,
|
|
|
|
RequestGroup* requestGroup,
|
|
|
|
const HttpConnectionHandle& httpConnection,
|
|
|
|
DownloadEngine* e,
|
|
|
|
const SocketHandle& s)
|
2011-01-16 07:55:41 +00:00
|
|
|
: AbstractCommand(cuid, req, fileEntry, requestGroup, e, s,
|
|
|
|
httpConnection->getSocketRecvBuffer()),
|
2010-11-20 08:21:36 +00:00
|
|
|
httpConnection_(httpConnection)
|
2011-01-16 07:55:41 +00:00
|
|
|
{
|
|
|
|
checkSocketRecvBuffer();
|
|
|
|
}
|
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
|
|
|
HttpResponseCommand::~HttpResponseCommand() {}
|
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 HttpResponseCommand::executeInternal()
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
SharedHandle<HttpRequest> httpRequest =httpConnection_->getFirstHttpRequest();
|
|
|
|
SharedHandle<HttpResponse> httpResponse = httpConnection_->receiveResponse();
|
2010-11-12 12:48:48 +00:00
|
|
|
if(!httpResponse) {
|
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
|
|
|
// The server has not responded to our request yet.
|
2008-09-27 16:06:34 +00:00
|
|
|
// For socket->wantRead() == true, setReadCheckSocket(socket) is already
|
|
|
|
// done in the constructor.
|
2010-06-09 12:43:44 +00:00
|
|
|
setWriteCheckSocketIf(getSocket(), getSocket()->wantWrite());
|
|
|
|
getDownloadEngine()->addCommand(this);
|
2006-02-21 12:27:17 +00:00
|
|
|
return false;
|
|
|
|
}
|
2006-02-17 13:35:04 +00:00
|
|
|
// check HTTP status number
|
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
|
|
|
httpResponse->validateResponse();
|
|
|
|
httpResponse->retrieveCookie();
|
2008-05-10 04:50:49 +00:00
|
|
|
|
2008-09-30 15:54:50 +00:00
|
|
|
SharedHandle<HttpHeader> httpHeader = httpResponse->getHttpHeader();
|
|
|
|
// Disable persistent connection if:
|
|
|
|
// Connection: close is received or the remote server is not HTTP/1.1.
|
|
|
|
// We don't care whether non-HTTP/1.1 server returns Connection: keep-alive.
|
2010-06-09 12:43:44 +00:00
|
|
|
getRequest()->supportsPersistentConnection
|
2008-09-30 15:54:50 +00:00
|
|
|
(httpResponse->supportsPersistentConnection());
|
2010-06-09 12:43:44 +00:00
|
|
|
if(getRequest()->isPipeliningEnabled()) {
|
|
|
|
getRequest()->setMaxPipelinedRequest
|
2010-06-08 15:02:20 +00:00
|
|
|
(getOption()->getAsInt(PREF_MAX_HTTP_PIPELINING));
|
2011-01-16 09:12:14 +00:00
|
|
|
} else {
|
|
|
|
getRequest()->setMaxPipelinedRequest(1);
|
2008-12-15 15:38:07 +00:00
|
|
|
}
|
2008-09-30 15:54:50 +00:00
|
|
|
|
2010-11-15 12:52:03 +00:00
|
|
|
int statusCode = httpResponse->getStatusCode();
|
2011-01-29 14:19:10 +00:00
|
|
|
|
|
|
|
if(statusCode == 304) {
|
|
|
|
uint64_t totalLength = httpResponse->getEntityLength();
|
|
|
|
getFileEntry()->setLength(totalLength);
|
|
|
|
getRequestGroup()->initPieceStorage();
|
|
|
|
getPieceStorage()->markAllPiecesDone();
|
|
|
|
// Just set checksum verification done.
|
|
|
|
getDownloadContext()->setChecksumVerified(true);
|
|
|
|
A2_LOG_NOTICE(fmt(MSG_DOWNLOAD_ALREADY_COMPLETED,
|
|
|
|
util::itos(getRequestGroup()->getGID()).c_str(),
|
|
|
|
getRequestGroup()->getFirstFilePath().c_str()));
|
|
|
|
poolConnection();
|
|
|
|
getFileEntry()->poolRequest(getRequest());
|
|
|
|
return true;
|
2010-07-16 14:13:04 +00:00
|
|
|
}
|
2011-07-27 11:54:21 +00:00
|
|
|
if(!getPieceStorage()) {
|
|
|
|
// Metalink/HTTP
|
|
|
|
if(!getDownloadContext()->getMetalinkServerContacted()) {
|
|
|
|
if(httpHeader->defined(HttpHeader::LINK)) {
|
|
|
|
getDownloadContext()->setMetalinkServerContacted(true);
|
|
|
|
std::vector<MetalinkHttpEntry> entries;
|
|
|
|
httpResponse->getMetalinKHttpEntries(entries, getOption());
|
|
|
|
for(std::vector<MetalinkHttpEntry>::iterator i = entries.begin(),
|
|
|
|
eoi = entries.end(); i != eoi; ++i) {
|
|
|
|
getFileEntry()->addUri((*i).uri);
|
|
|
|
A2_LOG_DEBUG(fmt("Adding URI=%s", (*i).uri.c_str()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#ifdef ENABLE_MESSAGE_DIGEST
|
|
|
|
if(httpHeader->defined(HttpHeader::DIGEST)) {
|
|
|
|
std::vector<Checksum> checksums;
|
|
|
|
httpResponse->getDigest(checksums);
|
|
|
|
for(std::vector<Checksum>::iterator i = checksums.begin(),
|
|
|
|
eoi = checksums.end(); i != eoi; ++i) {
|
2011-07-27 13:44:30 +00:00
|
|
|
if(getDownloadContext()->getHashType().empty()) {
|
2011-07-27 11:54:21 +00:00
|
|
|
A2_LOG_DEBUG(fmt("Setting digest: type=%s, digest=%s",
|
2011-07-27 12:44:54 +00:00
|
|
|
(*i).getHashType().c_str(),
|
|
|
|
(*i).getDigest().c_str()));
|
2011-07-27 13:44:30 +00:00
|
|
|
getDownloadContext()->setDigest((*i).getHashType(), (*i).getDigest());
|
2011-07-27 11:54:21 +00:00
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
if(checkChecksum(getDownloadContext(), *i)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif // ENABLE_MESSAGE_DIGEST
|
|
|
|
}
|
2011-01-29 14:19:10 +00:00
|
|
|
if(statusCode >= 300) {
|
2010-11-15 12:52:03 +00:00
|
|
|
if(statusCode == 404) {
|
2010-06-09 12:43:44 +00:00
|
|
|
getRequestGroup()->increaseAndValidateFileNotFoundCount();
|
2008-09-10 14:56:44 +00:00
|
|
|
}
|
2008-05-10 04:50:49 +00:00
|
|
|
return skipResponseBody(httpResponse);
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|
2010-07-14 11:39:05 +00:00
|
|
|
if(getFileEntry()->isUniqueProtocol()) {
|
2010-09-06 14:52:19 +00:00
|
|
|
// Redirection should be considered here. We need to parse
|
2009-06-29 16:43:41 +00:00
|
|
|
// original URI to get hostname.
|
2010-09-11 12:09:19 +00:00
|
|
|
uri::UriStruct us;
|
|
|
|
if(uri::parse(us, getRequest()->getUri())) {
|
|
|
|
getFileEntry()->removeURIWhoseHostnameIs(us.host);
|
|
|
|
}
|
2007-12-12 13:53:33 +00:00
|
|
|
}
|
2010-11-12 12:48:48 +00:00
|
|
|
if(!getPieceStorage()) {
|
2011-07-24 15:03:13 +00:00
|
|
|
util::removeMetalinkContentTypes(getRequestGroup());
|
2008-03-09 12:24:01 +00:00
|
|
|
uint64_t totalLength = httpResponse->getEntityLength();
|
2010-06-09 12:43:44 +00:00
|
|
|
getFileEntry()->setLength(totalLength);
|
|
|
|
if(getFileEntry()->getPath().empty()) {
|
|
|
|
getFileEntry()->setPath
|
2010-10-02 07:54:43 +00:00
|
|
|
(util::createSafePath
|
2010-12-03 04:08:22 +00:00
|
|
|
(getOption()->get(PREF_DIR), httpResponse->determinFilename()));
|
2009-06-28 10:37:15 +00:00
|
|
|
}
|
2010-06-09 12:43:44 +00:00
|
|
|
getFileEntry()->setContentType(httpResponse->getContentType());
|
|
|
|
getRequestGroup()->preDownloadProcessing();
|
|
|
|
if(getDownloadEngine()->getRequestGroupMan()->
|
|
|
|
isSameFileBeingDownloaded(getRequestGroup())) {
|
2010-11-28 08:29:32 +00:00
|
|
|
throw DOWNLOAD_FAILURE_EXCEPTION2
|
2010-11-20 09:36:14 +00:00
|
|
|
(fmt(EX_DUPLICATE_FILE_DOWNLOAD,
|
2010-11-28 08:29:32 +00:00
|
|
|
getRequestGroup()->getFirstFilePath().c_str()),
|
|
|
|
error_code::DUPLICATE_DOWNLOAD);
|
2007-11-13 11:46:58 +00:00
|
|
|
}
|
2008-09-07 14:38:26 +00:00
|
|
|
// update last modified time
|
|
|
|
updateLastModifiedTime(httpResponse->getLastModifiedTime());
|
|
|
|
|
2009-05-05 15:19:02 +00:00
|
|
|
// If both transfer-encoding and total length is specified, we
|
|
|
|
// assume we can do segmented downloading
|
|
|
|
if(totalLength == 0 || shouldInflateContentEncoding(httpResponse)) {
|
|
|
|
// we ignore content-length when inflate is required
|
2010-06-09 12:43:44 +00:00
|
|
|
getFileEntry()->setLength(0);
|
|
|
|
if(getRequest()->getMethod() == Request::METHOD_GET &&
|
2010-01-05 16:01:46 +00:00
|
|
|
(totalLength != 0 ||
|
|
|
|
!httpResponse->getHttpHeader()->defined(HttpHeader::CONTENT_LENGTH))){
|
|
|
|
// DownloadContext::knowsTotalLength() == true only when
|
|
|
|
// server says the size of file is 0 explicitly.
|
|
|
|
getDownloadContext()->markTotalLengthIsUnknown();
|
2008-11-26 15:22:54 +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
|
|
|
return handleOtherEncoding(httpResponse);
|
|
|
|
} else {
|
|
|
|
return handleDefaultEncoding(httpResponse);
|
|
|
|
}
|
2007-11-10 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Don't connect server before checking file integrity at startup,
if
filesize and output file path are known.
* src/AbstractCommand.cc
* src/StreamFileAllocationEntry.cc
* src/Metalink2RequestGroup.cc
* src/RequestGroup.{h, cc}
* src/HttpResponseCommand.cc
* src/FtpNegotiationCommand.cc
Added DownloadFailureException. If it is thrown, RequestGroup
should
halt.
* src/AbstractCommand.cc
* src/DownloadFailureException.h
* src/RequestGroup.cc
Catch RecoverableException, instead of DlAbortEx.
* src/RequestGroupMan.cc
* src/FillRequestGroupCommand.cc
* src/MetaFileUtil.cc
* src/IteratableChunkChecksumValidator.cc
Now first parameter of MSG_DOWNLOAD_ABORTED is
gid(RequestGroup::
getGID())
* src/CheckIntegrityCommand.cc
* src/message.h
Print gid instead of idx.
* src/RequestGroupMan.cc
Removed exception throwers declaration.
* src/DirectDiskAdaptor.{h, cc}
* src/SocketCore.{h, cc}
* src/MultiDiskAdaptor.{h, cc}
* src/HttpConnection.{h, cc}
* src/HttpResponse.{h, cc}
* src/DiskAdaptor.{h, cc}
* src/CopyDiskAdaptor.{h, cc}
* src/MultiDiskAdaptor.{h, cc}
* src/HttpHeaderProcessor.{h, cc}
* src/AbstractSingleDiskAdaptor.{h, cc}
* src/Util.{h, cc}
* test/UtilTest.cc
* src/DefaultDiskWriter.{h, cc}
* src/FtpConnection.{h, cc}
* src/AbstractDiskWriter.{h, cc}
Removed duplicate code.
* src/StreamCheckIntegrityEntry.cc
Removed unnecessary include.
* src/DiskWriter.h
Included Exception.h
* src/option_processing.cc
Included 2 files and added doc
* src/TrackerWatcherCommand.cc
* src/SocketCore.cc (writeData): Fixed send error with GnuTLS.
2007-11-09 18:01:12 +00:00
|
|
|
} else {
|
2011-07-27 11:54:21 +00:00
|
|
|
#ifdef ENABLE_MESSAGE_DIGEST
|
2011-07-27 13:44:30 +00:00
|
|
|
if(!getDownloadContext()->getHashType().empty() &&
|
2011-07-27 11:54:21 +00:00
|
|
|
httpHeader->defined(HttpHeader::DIGEST)) {
|
|
|
|
std::vector<Checksum> checksums;
|
|
|
|
httpResponse->getDigest(checksums);
|
|
|
|
for(std::vector<Checksum>::iterator i = checksums.begin(),
|
|
|
|
eoi = checksums.end(); i != eoi; ++i) {
|
|
|
|
if(checkChecksum(getDownloadContext(), *i)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif // ENABLE_MESSAGE_DIGEST
|
2007-11-10 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Don't connect server before checking file integrity at startup,
if
filesize and output file path are known.
* src/AbstractCommand.cc
* src/StreamFileAllocationEntry.cc
* src/Metalink2RequestGroup.cc
* src/RequestGroup.{h, cc}
* src/HttpResponseCommand.cc
* src/FtpNegotiationCommand.cc
Added DownloadFailureException. If it is thrown, RequestGroup
should
halt.
* src/AbstractCommand.cc
* src/DownloadFailureException.h
* src/RequestGroup.cc
Catch RecoverableException, instead of DlAbortEx.
* src/RequestGroupMan.cc
* src/FillRequestGroupCommand.cc
* src/MetaFileUtil.cc
* src/IteratableChunkChecksumValidator.cc
Now first parameter of MSG_DOWNLOAD_ABORTED is
gid(RequestGroup::
getGID())
* src/CheckIntegrityCommand.cc
* src/message.h
Print gid instead of idx.
* src/RequestGroupMan.cc
Removed exception throwers declaration.
* src/DirectDiskAdaptor.{h, cc}
* src/SocketCore.{h, cc}
* src/MultiDiskAdaptor.{h, cc}
* src/HttpConnection.{h, cc}
* src/HttpResponse.{h, cc}
* src/DiskAdaptor.{h, cc}
* src/CopyDiskAdaptor.{h, cc}
* src/MultiDiskAdaptor.{h, cc}
* src/HttpHeaderProcessor.{h, cc}
* src/AbstractSingleDiskAdaptor.{h, cc}
* src/Util.{h, cc}
* test/UtilTest.cc
* src/DefaultDiskWriter.{h, cc}
* src/FtpConnection.{h, cc}
* src/AbstractDiskWriter.{h, cc}
Removed duplicate code.
* src/StreamCheckIntegrityEntry.cc
Removed unnecessary include.
* src/DiskWriter.h
Included Exception.h
* src/option_processing.cc
Included 2 files and added doc
* src/TrackerWatcherCommand.cc
* src/SocketCore.cc (writeData): Fixed send error with GnuTLS.
2007-11-09 18:01:12 +00:00
|
|
|
// validate totalsize
|
2010-06-09 12:43:44 +00:00
|
|
|
getRequestGroup()->validateTotalLength(getFileEntry()->getLength(),
|
2010-01-05 16:01:46 +00:00
|
|
|
httpResponse->getEntityLength());
|
2008-09-07 14:38:26 +00:00
|
|
|
// update last modified time
|
|
|
|
updateLastModifiedTime(httpResponse->getLastModifiedTime());
|
2010-06-09 12:43:44 +00:00
|
|
|
if(getRequestGroup()->getTotalLength() == 0) {
|
2009-04-17 13:19:17 +00:00
|
|
|
// Since total length is unknown, the file size in previously
|
|
|
|
// failed download could be larger than the size this time.
|
|
|
|
// Also we can't resume in this case too. So truncate the file
|
|
|
|
// anyway.
|
2010-06-09 12:43:44 +00:00
|
|
|
getPieceStorage()->getDiskAdaptor()->truncate(0);
|
|
|
|
getDownloadEngine()->addCommand
|
2010-09-06 14:29:36 +00:00
|
|
|
(createHttpDownloadCommand
|
|
|
|
(httpResponse,
|
|
|
|
getTransferEncodingStreamFilter
|
|
|
|
(httpResponse,
|
|
|
|
getContentEncodingStreamFilter(httpResponse))));
|
2009-04-17 13:19:17 +00:00
|
|
|
} else {
|
2010-09-06 14:29:36 +00:00
|
|
|
getDownloadEngine()->addCommand
|
|
|
|
(createHttpDownloadCommand
|
|
|
|
(httpResponse,
|
|
|
|
getTransferEncodingStreamFilter(httpResponse)));
|
2009-04-17 13:19:17 +00:00
|
|
|
}
|
2007-11-10 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Don't connect server before checking file integrity at startup,
if
filesize and output file path are known.
* src/AbstractCommand.cc
* src/StreamFileAllocationEntry.cc
* src/Metalink2RequestGroup.cc
* src/RequestGroup.{h, cc}
* src/HttpResponseCommand.cc
* src/FtpNegotiationCommand.cc
Added DownloadFailureException. If it is thrown, RequestGroup
should
halt.
* src/AbstractCommand.cc
* src/DownloadFailureException.h
* src/RequestGroup.cc
Catch RecoverableException, instead of DlAbortEx.
* src/RequestGroupMan.cc
* src/FillRequestGroupCommand.cc
* src/MetaFileUtil.cc
* src/IteratableChunkChecksumValidator.cc
Now first parameter of MSG_DOWNLOAD_ABORTED is
gid(RequestGroup::
getGID())
* src/CheckIntegrityCommand.cc
* src/message.h
Print gid instead of idx.
* src/RequestGroupMan.cc
Removed exception throwers declaration.
* src/DirectDiskAdaptor.{h, cc}
* src/SocketCore.{h, cc}
* src/MultiDiskAdaptor.{h, cc}
* src/HttpConnection.{h, cc}
* src/HttpResponse.{h, cc}
* src/DiskAdaptor.{h, cc}
* src/CopyDiskAdaptor.{h, cc}
* src/MultiDiskAdaptor.{h, cc}
* src/HttpHeaderProcessor.{h, cc}
* src/AbstractSingleDiskAdaptor.{h, cc}
* src/Util.{h, cc}
* test/UtilTest.cc
* src/DefaultDiskWriter.{h, cc}
* src/FtpConnection.{h, cc}
* src/AbstractDiskWriter.{h, cc}
Removed duplicate code.
* src/StreamCheckIntegrityEntry.cc
Removed unnecessary include.
* src/DiskWriter.h
Included Exception.h
* src/option_processing.cc
Included 2 files and added doc
* src/TrackerWatcherCommand.cc
* src/SocketCore.cc (writeData): Fixed send error with GnuTLS.
2007-11-09 18:01:12 +00:00
|
|
|
return true;
|
2006-05-09 15:54:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-07 14:38:26 +00:00
|
|
|
void HttpResponseCommand::updateLastModifiedTime(const Time& lastModified)
|
|
|
|
{
|
2009-05-08 16:41:21 +00:00
|
|
|
if(getOption()->getAsBool(PREF_REMOTE_TIME)) {
|
2010-06-09 12:43:44 +00:00
|
|
|
getRequestGroup()->updateLastModifiedTime(lastModified);
|
2008-09-07 14:38:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-31 12:42:28 +00:00
|
|
|
bool HttpResponseCommand::shouldInflateContentEncoding
|
|
|
|
(const SharedHandle<HttpResponse>& httpResponse)
|
|
|
|
{
|
2010-04-02 14:16:10 +00:00
|
|
|
// Basically, on the fly inflation cannot be made with segment
|
|
|
|
// download, because in each segment we don't know where the date
|
|
|
|
// should be written. So turn off segmented downloading.
|
|
|
|
// Meanwhile, Some server returns content-encoding: gzip for .tgz
|
|
|
|
// files. I think those files should not be inflated by clients,
|
|
|
|
// because it is the original format of those files. Current
|
|
|
|
// implementation just inflates these files nonetheless.
|
|
|
|
const std::string& ce = httpResponse->getContentEncoding();
|
|
|
|
return httpResponse->getHttpRequest()->acceptGZip() &&
|
|
|
|
(ce == "gzip" || ce == "deflate");
|
2008-07-31 12:42:28 +00:00
|
|
|
}
|
|
|
|
|
2010-02-28 12:30:11 +00:00
|
|
|
bool HttpResponseCommand::handleDefaultEncoding
|
|
|
|
(const SharedHandle<HttpResponse>& httpResponse)
|
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
|
|
|
{
|
2010-02-28 12:30:11 +00:00
|
|
|
SharedHandle<HttpRequest> httpRequest = httpResponse->getHttpRequest();
|
2010-11-11 05:12:37 +00:00
|
|
|
SharedHandle<BtProgressInfoFile> progressInfoFile
|
|
|
|
(new DefaultBtProgressInfoFile
|
|
|
|
(getDownloadContext(), SharedHandle<PieceStorage>(), getOption().get()));
|
|
|
|
getRequestGroup()->adjustFilename(progressInfoFile);
|
2010-06-09 12:43:44 +00:00
|
|
|
getRequestGroup()->initPieceStorage();
|
2007-10-11 16:58:24 +00:00
|
|
|
|
2009-05-08 16:41:21 +00:00
|
|
|
if(getOption()->getAsBool(PREF_DRY_RUN)) {
|
2009-03-08 10:20:42 +00:00
|
|
|
onDryRunFileFound();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-09-15 11:46:25 +00:00
|
|
|
SharedHandle<CheckIntegrityEntry> checkEntry =
|
|
|
|
getRequestGroup()->createCheckIntegrityEntry();
|
2010-11-12 12:48:48 +00:00
|
|
|
if(!checkEntry) {
|
2010-09-15 11:46:25 +00:00
|
|
|
return true;
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|
2010-06-09 12:43:44 +00:00
|
|
|
File file(getRequestGroup()->getFirstFilePath());
|
2010-06-01 12:40:57 +00:00
|
|
|
// We have to make sure that command that has Request object must
|
|
|
|
// have segment after PieceStorage is initialized. See
|
|
|
|
// AbstractCommand::execute()
|
2010-07-17 14:36:18 +00:00
|
|
|
SharedHandle<Segment> segment =
|
|
|
|
getSegmentMan()->getSegmentWithIndex(getCuid(), 0);
|
2010-06-01 12:40:57 +00:00
|
|
|
// pipelining requires implicit range specified. But the request for
|
|
|
|
// this response most likely dones't contains range header. This means
|
|
|
|
// we can't continue to use this socket because server sends all entity
|
|
|
|
// body instead of a segment.
|
|
|
|
// Therefore, we shutdown the socket here if pipelining is enabled.
|
2007-05-31 15:56:20 +00:00
|
|
|
DownloadCommand* command = 0;
|
2010-06-09 12:43:44 +00:00
|
|
|
if(getRequest()->getMethod() == Request::METHOD_GET &&
|
2010-11-12 12:48:48 +00:00
|
|
|
segment && segment->getPositionToWrite() == 0 &&
|
2010-06-09 12:43:44 +00:00
|
|
|
!getRequest()->isPipeliningEnabled()) {
|
2010-06-01 12:40:57 +00:00
|
|
|
command = createHttpDownloadCommand
|
2010-09-06 14:29:36 +00:00
|
|
|
(httpResponse,
|
|
|
|
getTransferEncodingStreamFilter(httpResponse));
|
2010-06-01 12:40:57 +00:00
|
|
|
} else {
|
2010-06-09 12:43:44 +00:00
|
|
|
getSegmentMan()->cancelSegment(getCuid());
|
|
|
|
getFileEntry()->poolRequest(getRequest());
|
2010-06-01 12:40:57 +00:00
|
|
|
}
|
|
|
|
// After command is passed to prepareForNextAction(), it is managed
|
|
|
|
// by CheckIntegrityEntry.
|
2010-09-15 11:46:25 +00:00
|
|
|
checkEntry->pushNextCommand(command);
|
2010-06-01 12:40:57 +00:00
|
|
|
command = 0;
|
2010-09-15 11:46:25 +00:00
|
|
|
|
|
|
|
prepareForNextAction(checkEntry);
|
|
|
|
|
2010-06-09 12:43:44 +00:00
|
|
|
if(getRequest()->getMethod() == Request::METHOD_HEAD) {
|
2010-06-01 12:40:57 +00:00
|
|
|
poolConnection();
|
2010-06-09 12:43:44 +00:00
|
|
|
getRequest()->setMethod(Request::METHOD_GET);
|
2007-05-31 15:56:20 +00:00
|
|
|
}
|
2007-05-20 13:51:52 +00:00
|
|
|
return true;
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|
|
|
|
|
2010-02-28 12:30:11 +00:00
|
|
|
bool HttpResponseCommand::handleOtherEncoding
|
|
|
|
(const SharedHandle<HttpResponse>& httpResponse) {
|
2008-11-26 15:22:54 +00:00
|
|
|
// We assume that RequestGroup::getTotalLength() == 0 here
|
2010-02-28 12:30:11 +00:00
|
|
|
SharedHandle<HttpRequest> httpRequest = httpResponse->getHttpRequest();
|
2009-03-08 10:20:42 +00:00
|
|
|
|
2009-05-08 16:41:21 +00:00
|
|
|
if(getOption()->getAsBool(PREF_DRY_RUN)) {
|
2010-06-09 12:43:44 +00:00
|
|
|
getRequestGroup()->initPieceStorage();
|
2009-03-08 10:20:42 +00:00
|
|
|
onDryRunFileFound();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-06-09 12:43:44 +00:00
|
|
|
if(getRequest()->getMethod() == Request::METHOD_HEAD) {
|
2008-11-26 15:22:54 +00:00
|
|
|
poolConnection();
|
2010-06-09 12:43:44 +00:00
|
|
|
getRequest()->setMethod(Request::METHOD_GET);
|
2008-11-23 15:00:44 +00:00
|
|
|
return prepareForRetry(0);
|
2008-09-01 15:06:29 +00:00
|
|
|
}
|
2008-11-26 15:22:54 +00:00
|
|
|
|
2010-09-13 11:47:04 +00:00
|
|
|
// In this context, knowsTotalLength() is true only when the file is
|
|
|
|
// really zero-length.
|
|
|
|
|
|
|
|
SharedHandle<StreamFilter> streamFilter =
|
|
|
|
getTransferEncodingStreamFilter
|
|
|
|
(httpResponse,
|
|
|
|
getContentEncodingStreamFilter(httpResponse));
|
|
|
|
// If chunked transfer-encoding is specified, we have to read end of
|
|
|
|
// chunk markers(0\r\n\r\n, for example).
|
2010-11-12 12:48:48 +00:00
|
|
|
bool chunkedUsed = streamFilter &&
|
2010-09-13 11:47:04 +00:00
|
|
|
streamFilter->getName() == ChunkedDecodingStreamFilter::NAME;
|
|
|
|
|
2008-11-26 15:22:54 +00:00
|
|
|
// For zero-length file, check existing file comparing its size
|
2010-09-13 11:47:04 +00:00
|
|
|
if(!chunkedUsed && getDownloadContext()->knowsTotalLength() &&
|
|
|
|
getRequestGroup()->downloadFinishedByFileLength()) {
|
2010-09-13 11:53:38 +00:00
|
|
|
// TODO If metalink file does not contain size and it contains
|
|
|
|
// hash and file is not zero length, but remote server says the
|
|
|
|
// file size is 0, no hash check is performed in the current
|
|
|
|
// implementation. See also
|
|
|
|
// FtpNegotiationCommand::onFileSizeDetermined()
|
2010-06-09 12:43:44 +00:00
|
|
|
getRequestGroup()->initPieceStorage();
|
|
|
|
getPieceStorage()->markAllPiecesDone();
|
2010-09-08 14:38:32 +00:00
|
|
|
getDownloadContext()->setChecksumVerified(true);
|
2010-11-20 08:21:36 +00:00
|
|
|
A2_LOG_NOTICE(fmt(MSG_DOWNLOAD_ALREADY_COMPLETED,
|
|
|
|
util::itos(getRequestGroup()->getGID()).c_str(),
|
|
|
|
getRequestGroup()->getFirstFilePath().c_str()));
|
2008-11-26 15:22:54 +00:00
|
|
|
poolConnection();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-06-09 12:43:44 +00:00
|
|
|
getRequestGroup()->shouldCancelDownloadForSafety();
|
|
|
|
getRequestGroup()->initPieceStorage();
|
|
|
|
getPieceStorage()->getDiskAdaptor()->initAndOpenFile();
|
2010-09-06 14:29:36 +00:00
|
|
|
|
2010-09-13 11:47:04 +00:00
|
|
|
// Local file size becomes zero when DiskAdaptor::initAndOpenFile()
|
|
|
|
// is called. So zero-length file is complete if chunked encoding is
|
|
|
|
// not used.
|
|
|
|
if(!chunkedUsed && getDownloadContext()->knowsTotalLength()) {
|
|
|
|
getRequestGroup()->getPieceStorage()->markAllPiecesDone();
|
2008-11-26 15:22:54 +00:00
|
|
|
poolConnection();
|
|
|
|
return true;
|
|
|
|
}
|
2010-03-28 07:23:33 +00:00
|
|
|
// We have to make sure that command that has Request object must
|
|
|
|
// have segment after PieceStorage is initialized. See
|
|
|
|
// AbstractCommand::execute()
|
2010-07-17 14:36:18 +00:00
|
|
|
getSegmentMan()->getSegmentWithIndex(getCuid(), 0);
|
2010-03-28 07:23:33 +00:00
|
|
|
|
2010-06-09 12:43:44 +00:00
|
|
|
getDownloadEngine()->addCommand
|
2010-09-06 14:29:36 +00:00
|
|
|
(createHttpDownloadCommand(httpResponse, streamFilter));
|
2008-09-01 15:06:29 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-05-10 04:50:49 +00:00
|
|
|
bool HttpResponseCommand::skipResponseBody
|
2008-05-06 15:20:59 +00:00
|
|
|
(const SharedHandle<HttpResponse>& httpResponse)
|
|
|
|
{
|
2010-09-06 14:29:36 +00:00
|
|
|
SharedHandle<StreamFilter> filter =
|
|
|
|
getTransferEncodingStreamFilter(httpResponse);
|
2008-06-29 14:29:36 +00:00
|
|
|
// We don't use Content-Encoding here because this response body is just
|
|
|
|
// thrown away.
|
|
|
|
|
2008-05-10 04:57:33 +00:00
|
|
|
HttpSkipResponseCommand* command = new HttpSkipResponseCommand
|
2010-06-09 12:43:44 +00:00
|
|
|
(getCuid(), getRequest(), getFileEntry(), getRequestGroup(),
|
2010-06-21 13:51:56 +00:00
|
|
|
httpConnection_, httpResponse,
|
2010-06-09 12:43:44 +00:00
|
|
|
getDownloadEngine(), getSocket());
|
2010-09-06 14:29:36 +00:00
|
|
|
command->installStreamFilter(filter);
|
2008-05-10 04:50:49 +00:00
|
|
|
|
2008-11-23 15:00:44 +00:00
|
|
|
// If request method is HEAD or the response body is zero-length,
|
|
|
|
// set command's status to real time so that avoid read check blocking
|
2010-06-09 12:43:44 +00:00
|
|
|
if(getRequest()->getMethod() == Request::METHOD_HEAD ||
|
2008-11-23 15:00:44 +00:00
|
|
|
(httpResponse->getEntityLength() == 0 &&
|
|
|
|
!httpResponse->isTransferEncodingSpecified())) {
|
2008-05-10 04:50:49 +00:00
|
|
|
command->setStatusRealtime();
|
2008-10-22 12:20:48 +00:00
|
|
|
// If entity length == 0, then socket read/write check must be disabled.
|
|
|
|
command->disableSocketCheck();
|
2010-06-09 12:43:44 +00:00
|
|
|
getDownloadEngine()->setNoWait(true);
|
2008-05-10 04:50:49 +00:00
|
|
|
}
|
|
|
|
|
2010-06-09 12:43:44 +00:00
|
|
|
getDownloadEngine()->addCommand(command);
|
2008-05-06 15:20:59 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-10-30 16:02:15 +00:00
|
|
|
namespace {
|
|
|
|
bool decideFileAllocation
|
2010-09-06 14:29:36 +00:00
|
|
|
(const SharedHandle<StreamFilter>& filter)
|
|
|
|
{
|
2011-02-18 13:34:38 +00:00
|
|
|
#ifdef HAVE_ZLIB
|
2010-11-12 12:48:48 +00:00
|
|
|
for(SharedHandle<StreamFilter> f = filter; f; f = f->getDelegate()){
|
2010-09-06 14:29:36 +00:00
|
|
|
// Since the compressed file's length are returned in the response header
|
|
|
|
// and the decompressed file size is unknown at this point, disable file
|
|
|
|
// allocation here.
|
|
|
|
if(f->getName() == GZipDecodingStreamFilter::NAME) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2011-02-18 13:34:38 +00:00
|
|
|
#endif // HAVE_ZLIB
|
2010-09-06 14:29:36 +00:00
|
|
|
return true;
|
|
|
|
}
|
2010-10-30 16:02:15 +00:00
|
|
|
} // namespace
|
2010-09-06 14:29:36 +00:00
|
|
|
|
2008-07-01 11:38:25 +00:00
|
|
|
HttpDownloadCommand* HttpResponseCommand::createHttpDownloadCommand
|
2010-02-28 12:30:11 +00:00
|
|
|
(const SharedHandle<HttpResponse>& httpResponse,
|
2010-09-06 14:29:36 +00:00
|
|
|
const SharedHandle<StreamFilter>& filter)
|
2008-05-06 15:20:59 +00:00
|
|
|
{
|
2008-06-29 14:29:36 +00:00
|
|
|
|
2007-05-20 13:51:52 +00:00
|
|
|
HttpDownloadCommand* command =
|
2010-06-09 12:43:44 +00:00
|
|
|
new HttpDownloadCommand(getCuid(), getRequest(), getFileEntry(),
|
|
|
|
getRequestGroup(),
|
2010-06-21 13:51:56 +00:00
|
|
|
httpResponse, httpConnection_,
|
2010-06-09 12:43:44 +00:00
|
|
|
getDownloadEngine(), getSocket());
|
2009-05-08 16:41:21 +00:00
|
|
|
command->setStartupIdleTime(getOption()->getAsInt(PREF_STARTUP_IDLE_TIME));
|
2008-07-01 11:38:25 +00:00
|
|
|
command->setLowestDownloadSpeedLimit
|
2009-05-08 16:41:21 +00:00
|
|
|
(getOption()->getAsInt(PREF_LOWEST_SPEED_LIMIT));
|
2010-09-06 14:29:36 +00:00
|
|
|
command->installStreamFilter(filter);
|
2010-10-19 13:46:51 +00:00
|
|
|
if(getRequestGroup()->isFileAllocationEnabled() &&
|
|
|
|
!decideFileAllocation(filter)) {
|
|
|
|
getRequestGroup()->setFileAllocationEnabled(false);
|
|
|
|
}
|
2010-06-09 12:43:44 +00:00
|
|
|
getRequestGroup()->getURISelector()->tuneDownloadCommand
|
|
|
|
(getFileEntry()->getRemainingUris(), command);
|
2009-01-06 13:13:42 +00:00
|
|
|
|
2007-05-31 15:56:20 +00:00
|
|
|
return command;
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|
2008-02-08 15:53:45 +00:00
|
|
|
|
2008-11-26 15:22:54 +00:00
|
|
|
void HttpResponseCommand::poolConnection()
|
|
|
|
{
|
2010-06-09 12:43:44 +00:00
|
|
|
if(getRequest()->supportsPersistentConnection()) {
|
|
|
|
getDownloadEngine()->poolSocket(getRequest(), createProxyRequest(),
|
|
|
|
getSocket());
|
2008-11-26 15:22:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-08 10:20:42 +00:00
|
|
|
void HttpResponseCommand::onDryRunFileFound()
|
|
|
|
{
|
2010-06-09 12:43:44 +00:00
|
|
|
getPieceStorage()->markAllPiecesDone();
|
2010-09-15 11:46:25 +00:00
|
|
|
getDownloadContext()->setChecksumVerified(true);
|
2009-03-08 10:20:42 +00:00
|
|
|
poolConnection();
|
|
|
|
}
|
|
|
|
|
2011-07-27 11:54:21 +00:00
|
|
|
#ifdef ENABLE_MESSAGE_DIGEST
|
|
|
|
bool HttpResponseCommand::checkChecksum
|
|
|
|
(const SharedHandle<DownloadContext>& dctx,
|
|
|
|
const Checksum& checksum)
|
|
|
|
{
|
2011-07-27 13:44:30 +00:00
|
|
|
if(dctx->getHashType() == checksum.getHashType()) {
|
|
|
|
if(dctx->getDigest() == checksum.getDigest()) {
|
2011-07-27 11:54:21 +00:00
|
|
|
A2_LOG_INFO("Valid hash found in Digest header field.");
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
throw DL_ABORT_EX("Invalid hash found in Digest header field.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif // ENABLE_MESSAGE_DIGEST
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
} // namespace aria2
|