aria2/src/RequestGroup.cc

306 lines
8.9 KiB
C++
Raw Normal View History

2007-05-20 13:57:56 +00:00
/* <!-- copyright */
/*
* aria2 - The high speed download utility
*
* 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
* 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.
*/
/* copyright --> */
#include "RequestGroup.h"
#include "DownloadEngine.h"
#include "prefs.h"
#include "DefaultDiskWriter.h"
#include "RequestFactory.h"
#include "InitiateConnectionCommandFactory.h"
#include "CUIDCounter.h"
#include "DlAbortEx.h"
#include "File.h"
#include "message.h"
#include "DlAbortEx.h"
#include "Util.h"
#include "FatalException.h"
#include "DownloadCommand.h"
#ifdef ENABLE_MESSAGE_DIGEST
#include "CheckIntegrityCommand.h"
#include "CheckIntegrityEntry.h"
#endif // ENABLE_MESSAGE_DIGEST
#include <cerrno>
2007-05-20 13:57:56 +00:00
SegmentManHandle RequestGroup::initSegmentMan()
{
_segmentMan = _segmentManFactory->createNewInstance();
_segmentMan->ufilename = _ufilename;
2007-06-30 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com> Made -S option work with metalink file and provided selective download to metalink. * src/MetalinkEntry.h, src/MetalinkEntry.cc (filename): Removed. (file): New variable. (size): Removed. (operator=): Updated. (getPath): New function. (getLength): New function. (toFileEntry): New function. * src/TorrentRequestInfo.h, src/TorrentRequestInfo.cc (execute): Use toStream. (showFileEntry): Removed. * src/MetalinkRequestInfo.h (targetFiles): New variable. (setTargetFiles): New variable. * src/MetalinkRequestInfo.cc (execute): Added the ability to print file information included in a metalink file. Added selective download mode to metalink. * src/main.cc (showUsage): Updated to denote that -S and --select-file options are applicable to metalink. * src/FileEntry.h (operator=): New function. (getBasename): New function. (getDirname): New function. * src/Util.h, src/Util.cc (toStream): New function. * src/Xml2MetalinkProcessor.cc: Updated. Made aria2 work with metalink with directory structure. * src/File.h, src/File.cc (getBasename): New function. (getDirname): New function. * src/RequestGroup.h, src/RequestGroup.cc (_topDir): New variable. (setTopDir): New function. (initSegmentMan): A directory structure is added to _segmentMan->dir. Rewrote HTTP header parsing with stringstream. * src/HttpConnection.h, src/HttpConnection.cc (HttpRequestEntry): New class. (headerBuf): Removed. (headerBufLength): Removed. (outstandingHttpRequests): Now its element type is HttpRequestEntryHandle. (findEndOfHeader): Removed. (receiveResponse): Rewritten. Updated doc for -j option to notice that it should be used with -i option. * src/main.cc (showUsage) Removed unused classes. * src/RequestInfo.h (FileInfo): Removed. (checksum): Removed. (fileInfo): Removed. (setChecksum): Removed. (getChecksum): Removed. (getFileInfo): Removed. Use ISO units. * src/ConsoleDownloadEngine.cc * src/TorrentConsoleDownloadEngine.cc * src/Util.cc (abbrevSize)
2007-06-30 09:52:39 +00:00
if(!_topDir.empty() && _topDir != ".") {
_segmentMan->dir += "/"+_topDir;
}
2007-05-20 13:57:56 +00:00
return _segmentMan;
}
2007-06-10 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com> * src/AbstractCommand.cc (execute): Changed log level of MSG_RESTARTING_DOWNLOAD and MSG_MAX_TRY from error to info. Added MSG_DOWNLOAD_ABORTED after MSG_MAX_TRY. * src/message.h (MSG_TORRENT_DOWNLOAD_ABORTED): New definition. (MSG_DOWNLOAD_ABORTED): Added %s. (MSG_RESTARTING_DOWNLOAD): Added %s. (MSG_DOWNLOAD_ALREADY_COMPLETED): Updated. * src/PeerAbstractCommand.cc (execute): MSG_DOWNLOAD_ABORTED -> MSG_TORRENT_DOWNLOAD_ABORTED * src/Request.h (cookieBox): Made ShardHandle. * src/RequestGroup.h, src/RequestGroup.cc (createNextCommandWithAdj): New function. * src/FileAllocationCommand.cc (executeInternal): Use createNextCommandWithAdj(). * src/CheckIntegrityCommand.cc (executeInternal): Use createNextCommandWithAdj(). Added --load-cookies command-option. * src/OptionHandlerFactory.cc (createOptionHandlers): Added PREF_LOAD_COOKIES. * src/CookieBox.h, src/CookieBox.cc: Rwritten using CookieParser. Now aria2 can handle cookie's expiration date. * src/Cookie.h (expires): Changed its type to time_t. * src/main.cc: Added --load-cookies command-line option. * src/prefs.h (PREF_LOAD_COOKIES): New definition. * src/Util.h, src/Util.cc (httpGMT): New function. * src/Request.cc (Request): Initialize cookieBox using CookieBoxFactory. * src/CookieBoxFactory.h, src/CookieBoxFactory.cc: New class. * src/CookieParser.h, src/CookieParser.cc: New class. * src/main.cc: Chagned the default value of --metalink-servers to 5. * src/HttpResponseCommand.cc (handleOtherEncoding): Call RequestGroup::shouldCancelDownloadForSafety
2007-06-10 07:55:43 +00:00
Commands RequestGroup::createNextCommandWithAdj(DownloadEngine* e, int32_t numAdj)
{
2007-06-10 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com> * src/AbstractCommand.cc (execute): Changed log level of MSG_RESTARTING_DOWNLOAD and MSG_MAX_TRY from error to info. Added MSG_DOWNLOAD_ABORTED after MSG_MAX_TRY. * src/message.h (MSG_TORRENT_DOWNLOAD_ABORTED): New definition. (MSG_DOWNLOAD_ABORTED): Added %s. (MSG_RESTARTING_DOWNLOAD): Added %s. (MSG_DOWNLOAD_ALREADY_COMPLETED): Updated. * src/PeerAbstractCommand.cc (execute): MSG_DOWNLOAD_ABORTED -> MSG_TORRENT_DOWNLOAD_ABORTED * src/Request.h (cookieBox): Made ShardHandle. * src/RequestGroup.h, src/RequestGroup.cc (createNextCommandWithAdj): New function. * src/FileAllocationCommand.cc (executeInternal): Use createNextCommandWithAdj(). * src/CheckIntegrityCommand.cc (executeInternal): Use createNextCommandWithAdj(). Added --load-cookies command-option. * src/OptionHandlerFactory.cc (createOptionHandlers): Added PREF_LOAD_COOKIES. * src/CookieBox.h, src/CookieBox.cc: Rwritten using CookieParser. Now aria2 can handle cookie's expiration date. * src/Cookie.h (expires): Changed its type to time_t. * src/main.cc: Added --load-cookies command-line option. * src/prefs.h (PREF_LOAD_COOKIES): New definition. * src/Util.h, src/Util.cc (httpGMT): New function. * src/Request.cc (Request): Initialize cookieBox using CookieBoxFactory. * src/CookieBoxFactory.h, src/CookieBoxFactory.cc: New class. * src/CookieParser.h, src/CookieParser.cc: New class. * src/main.cc: Chagned the default value of --metalink-servers to 5. * src/HttpResponseCommand.cc (handleOtherEncoding): Call RequestGroup::shouldCancelDownloadForSafety
2007-06-10 07:55:43 +00:00
int32_t numCommand = _numConcurrentCommand == 0 ? _uris.size() : _numConcurrentCommand+numAdj;
return createNextCommand(e, numCommand, "GET");
}
Commands RequestGroup::createNextCommand(DownloadEngine* e, int32_t numCommand, const string& method)
2007-05-20 13:57:56 +00:00
{
Commands commands;
for(;!_uris.empty() && commands.size() < (size_t)numCommand; _uris.pop_front()) {
2007-05-20 13:57:56 +00:00
string uri = _uris.front();
_spentUris.push_back(uri);
RequestHandle req = RequestFactorySingletonHolder::instance()->createRequest();
req->setReferer(_option->get(PREF_REFERER));
req->setMethod(method);
if(req->setUrl(uri)) {
commands.push_back(InitiateConnectionCommandFactory::createInitiateConnectionCommand(CUIDCounterSingletonHolder::instance()->newID(), req, this, e));
} else {
logger->error(MSG_UNRECOGNIZED_URI,
req->getUrl().c_str());
2007-05-20 13:57:56 +00:00
}
}
return commands;
}
void RequestGroup::initBitfield()
{
_segmentMan->initBitfield(_option->getAsInt(PREF_SEGMENT_SIZE),
_segmentMan->totalSize);
}
void RequestGroup::openExistingFile()
{
_segmentMan->diskWriter->openExistingFile(_segmentMan->getFilePath());
}
void RequestGroup::markAllPiecesDone()
{
_segmentMan->markAllPiecesDone();
}
void RequestGroup::markExistingPiecesDone()
{
_segmentMan->markPieceDone(File(_segmentMan->getFilePath()).size());
}
void RequestGroup::markPieceDone(int64_t length)
{
_segmentMan->markPieceDone(length);
}
void RequestGroup::shouldCancelDownloadForSafety()
{
if(_segmentMan->shouldCancelDownloadForSafety()) {
logger->notice(MSG_FILE_ALREADY_EXISTS,
_segmentMan->getFilePath().c_str(),
_segmentMan->getSegmentFilePath().c_str());
throw new FatalException(EX_DOWNLOAD_ABORTED);
2007-05-20 13:57:56 +00:00
}
}
void RequestGroup::initAndOpenFile()
{
File d(getDir());
if(d.isDir()) {
} else if(d.exists()) {
throw new FatalException(EX_MAKE_DIR, getDir().c_str(), MSG_NOT_DIRECTORY);
} else if(!d.mkdirs()) {
throw new FatalException(EX_MAKE_DIR, getDir().c_str(), strerror(errno));
}
2007-05-20 13:57:56 +00:00
_segmentMan->diskWriter->initAndOpenFile(_segmentMan->getFilePath());
}
bool RequestGroup::needsFileAllocation() const
{
return _option->get(PREF_FILE_ALLOCATION) == V_PREALLOC
&& File(_segmentMan->getFilePath()).size() < _segmentMan->totalSize;
}
bool RequestGroup::fileExists() const
{
return _segmentMan->fileExists();
}
bool RequestGroup::segmentFileExists() const
{
return _segmentMan->segmentFileExists();
}
string RequestGroup::getFilePath() const
{
if(_segmentMan.isNull()) {
return "";
} else {
return _segmentMan->getFilePath();
}
}
string RequestGroup::getDir() const
{
if(_segmentMan.isNull()) {
return "";
} else {
return _segmentMan->dir;
}
}
2007-05-20 13:57:56 +00:00
void RequestGroup::loadAndOpenFile()
{
bool segFileExists = segmentFileExists();
if(segFileExists) {
load();
openExistingFile();
} else if(fileExists() && _option->get(PREF_CONTINUE) == V_TRUE) {
File existingFile(getFilePath());
if(getTotalLength() < existingFile.size()) {
throw new DlAbortEx(EX_FILE_LENGTH_MISMATCH_BETWEEN_LOCAL_AND_REMOTE,
2007-05-20 13:57:56 +00:00
getFilePath().c_str(),
Util::llitos(existingFile.size()).c_str(),
Util::llitos(getTotalLength()).c_str());
}
initBitfield();
openExistingFile();
_segmentMan->markPieceDone(existingFile.size());
2007-05-20 13:57:56 +00:00
} else {
shouldCancelDownloadForSafety();
initBitfield();
#ifdef ENABLE_MESSAGE_DIGEST
2007-05-20 13:57:56 +00:00
if(fileExists() && _option->get(PREF_CHECK_INTEGRITY) == V_TRUE) {
openExistingFile();
} else {
initAndOpenFile();
}
#else // ENABLE_MESSAGE_DIGEST
initAndOpenFile();
#endif // ENABLE_MESSAGE_DIGEST
2007-05-20 13:57:56 +00:00
}
}
bool RequestGroup::downloadFinishedByFileLength()
{
if(_segmentMan->segmentFileExists()) {
return false;
}
File existingFile(getFilePath());
if(existingFile.exists() &&
getTotalLength() == existingFile.size()) {
_segmentMan->downloadStarted = true;
initBitfield();
_segmentMan->markAllPiecesDone();
return true;
} else {
return false;
}
}
void RequestGroup::prepareForNextAction(int cuid, const RequestHandle& req, DownloadEngine* e, DownloadCommand* downloadCommand)
2007-05-20 13:57:56 +00:00
{
File existingFile(getFilePath());
#ifdef ENABLE_MESSAGE_DIGEST
2007-05-20 13:57:56 +00:00
if(existingFile.size() > 0 && _option->get(PREF_CHECK_INTEGRITY) == V_TRUE) {
// purge SegmentEntries
_segmentMan->purgeSegmentEntry();
2007-07-05 15:14:00 +00:00
CheckIntegrityEntryHandle entry = new CheckIntegrityEntry(cuid, req, this, downloadCommand);
entry->initValidator();
CheckIntegrityCommand* command = new CheckIntegrityCommand(cuid, this, e, entry);
2007-05-20 13:57:56 +00:00
e->commands.push_back(command);
} else
#endif // ENABLE_MESSAGE_DIGEST
if(needsFileAllocation()) {
2007-07-05 15:14:00 +00:00
FileAllocationEntryHandle entry = new FileAllocationEntry(cuid, req, this, downloadCommand, existingFile.size());
e->_fileAllocationMan->pushFileAllocationEntry(entry);
} else {
if(downloadCommand) {
e->commands.push_back(downloadCommand);
} else {
Commands commands = createNextCommandWithAdj(e, -1);
Command* command = InitiateConnectionCommandFactory::createInitiateConnectionCommand(cuid, req, this, e);
commands.push_front(command);
e->addCommand(commands);
}
}
2007-05-20 13:57:56 +00:00
}
void RequestGroup::validateFilename(const string& expectedFilename,
const string& actualFilename) const
{
if(expectedFilename.empty()) {
return;
}
if(expectedFilename != actualFilename) {
throw new DlAbortEx(EX_FILENAME_MISMATCH,
expectedFilename.c_str(),
actualFilename.c_str());
}
}
void RequestGroup::validateTotalLength(int64_t expectedTotalLength,
int64_t actualTotalLength) const
{
if(expectedTotalLength <= 0) {
return;
}
if(expectedTotalLength != actualTotalLength) {
throw new DlAbortEx(EX_SIZE_MISMATCH,
Util::llitos(expectedTotalLength, true).c_str(),
Util::llitos(actualTotalLength, true).c_str());
2007-05-20 13:57:56 +00:00
}
}
void RequestGroup::validateFilename(const string& actualFilename) const
{
validateFilename(_segmentMan->filename, actualFilename);
}
void RequestGroup::validateTotalLength(int64_t actualTotalLength) const
{
validateTotalLength(_segmentMan->totalSize, actualTotalLength);
}
void RequestGroup::validateFilenameByHint(const string& actualFilename) const
{
validateFilename(_hintFilename, actualFilename);
}
void RequestGroup::validateTotalLengthByHint(int64_t actualTotalLength) const
{
validateTotalLength(_hintTotalLength, actualTotalLength);
}
void RequestGroup::setUserDefinedFilename(const string& filename)
{
_ufilename = filename;
}
int64_t RequestGroup::getExistingFileLength() const
{
return File(getFilePath()).size();
}