2007-12-04 11:12: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 "MetalinkParserController.h"
|
|
|
|
#include "Metalinker.h"
|
|
|
|
#include "MetalinkEntry.h"
|
|
|
|
#include "MetalinkResource.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
#include "FileEntry.h"
|
|
|
|
#include "a2functional.h"
|
2008-05-13 14:15:23 +00:00
|
|
|
#include "A2STR.h"
|
2007-12-05 14:54:53 +00:00
|
|
|
#ifdef ENABLE_MESSAGE_DIGEST
|
|
|
|
# include "Checksum.h"
|
|
|
|
# include "ChunkChecksum.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
# include "messageDigest.h"
|
2007-12-05 14:54:53 +00:00
|
|
|
#endif // ENABLE_MESSAGE_DIGEST
|
2008-02-08 15:53:45 +00:00
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
namespace aria2 {
|
2007-12-04 11:12:56 +00:00
|
|
|
|
|
|
|
MetalinkParserController::MetalinkParserController():
|
2008-04-20 00:50:22 +00:00
|
|
|
_metalinker(new Metalinker())
|
2007-12-05 14:54:53 +00:00
|
|
|
{}
|
2007-12-04 11:12:56 +00:00
|
|
|
|
|
|
|
MetalinkParserController::~MetalinkParserController() {}
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
SharedHandle<Metalinker> MetalinkParserController::getResult() const
|
2007-12-04 11:12:56 +00:00
|
|
|
{
|
|
|
|
return _metalinker;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MetalinkParserController::newEntryTransaction()
|
|
|
|
{
|
2008-04-20 00:50:22 +00:00
|
|
|
_tEntry.reset(new MetalinkEntry());
|
|
|
|
_tResource.reset();
|
2007-12-05 14:54:53 +00:00
|
|
|
#ifdef ENABLE_MESSAGE_DIGEST
|
2008-04-20 00:50:22 +00:00
|
|
|
_tChecksum.reset();
|
|
|
|
_tChunkChecksum.reset();
|
2007-12-05 14:54:53 +00:00
|
|
|
#endif // ENABLE_MESSAGE_DIGEST
|
2007-12-04 11:12:56 +00:00
|
|
|
}
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
void MetalinkParserController::setFileNameOfEntry(const std::string& filename)
|
2007-12-04 11:12:56 +00:00
|
|
|
{
|
|
|
|
if(_tEntry.isNull()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(_tEntry->file.isNull()) {
|
2008-04-20 00:50:22 +00:00
|
|
|
_tEntry->file.reset(new FileEntry(filename, 0, 0));
|
2007-12-04 11:12:56 +00:00
|
|
|
} else {
|
|
|
|
_tEntry->file->setPath(filename);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-09 12:24:01 +00:00
|
|
|
void MetalinkParserController::setFileLengthOfEntry(uint64_t length)
|
2007-12-04 11:12:56 +00:00
|
|
|
{
|
|
|
|
if(_tEntry.isNull()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(_tEntry->file.isNull()) {
|
2008-05-13 14:15:23 +00:00
|
|
|
_tEntry->file.reset(new FileEntry(A2STR::NIL, length, 0));
|
2007-12-04 11:12:56 +00:00
|
|
|
} else {
|
|
|
|
_tEntry->file->setLength(length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
void MetalinkParserController::setVersionOfEntry(const std::string& version)
|
2007-12-04 11:12:56 +00:00
|
|
|
{
|
|
|
|
if(_tEntry.isNull()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_tEntry->version = version;
|
|
|
|
}
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
void MetalinkParserController::setLanguageOfEntry(const std::string& language)
|
2007-12-04 11:12:56 +00:00
|
|
|
{
|
|
|
|
if(_tEntry.isNull()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_tEntry->language = language;
|
|
|
|
}
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
void MetalinkParserController::setOSOfEntry(const std::string& os)
|
2007-12-04 11:12:56 +00:00
|
|
|
{
|
|
|
|
if(_tEntry.isNull()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_tEntry->os = os;
|
|
|
|
}
|
|
|
|
|
2008-03-09 12:24:01 +00:00
|
|
|
void MetalinkParserController::setMaxConnectionsOfEntry(int maxConnections)
|
2007-12-04 11:12:56 +00:00
|
|
|
{
|
|
|
|
if(_tEntry.isNull()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_tEntry->maxConnections = maxConnections;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MetalinkParserController::commitEntryTransaction()
|
|
|
|
{
|
|
|
|
if(_tEntry.isNull()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
commitResourceTransaction();
|
|
|
|
commitChecksumTransaction();
|
|
|
|
commitChunkChecksumTransaction();
|
|
|
|
_metalinker->entries.push_back(_tEntry);
|
2008-04-20 00:50:22 +00:00
|
|
|
_tEntry.reset();
|
2007-12-04 11:12:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MetalinkParserController::cancelEntryTransaction()
|
|
|
|
{
|
|
|
|
cancelResourceTransaction();
|
|
|
|
cancelChecksumTransaction();
|
|
|
|
cancelChunkChecksumTransaction();
|
2008-04-20 00:50:22 +00:00
|
|
|
_tEntry.reset();
|
2007-12-04 11:12:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MetalinkParserController::newResourceTransaction()
|
|
|
|
{
|
|
|
|
if(_tEntry.isNull()) {
|
|
|
|
return;
|
|
|
|
}
|
2008-04-20 00:50:22 +00:00
|
|
|
_tResource.reset(new MetalinkResource());
|
2007-12-04 11:12:56 +00:00
|
|
|
}
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
void MetalinkParserController::setURLOfResource(const std::string& url)
|
2007-12-04 11:12:56 +00:00
|
|
|
{
|
|
|
|
if(_tResource.isNull()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_tResource->url = url;
|
|
|
|
}
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
void MetalinkParserController::setTypeOfResource(const std::string& type)
|
2007-12-04 11:12:56 +00:00
|
|
|
{
|
|
|
|
if(_tResource.isNull()) {
|
|
|
|
return;
|
|
|
|
}
|
2008-05-13 14:15:23 +00:00
|
|
|
if(type == MetalinkResource::FTP) {
|
2007-12-04 11:12:56 +00:00
|
|
|
_tResource->type = MetalinkResource::TYPE_FTP;
|
2008-05-13 14:15:23 +00:00
|
|
|
} else if(type == MetalinkResource::HTTP) {
|
2007-12-04 11:12:56 +00:00
|
|
|
_tResource->type = MetalinkResource::TYPE_HTTP;
|
2008-05-13 14:15:23 +00:00
|
|
|
} else if(type == MetalinkResource::HTTPS) {
|
2007-12-04 11:12:56 +00:00
|
|
|
_tResource->type = MetalinkResource::TYPE_HTTPS;
|
2008-05-13 14:15:23 +00:00
|
|
|
} else if(type == MetalinkResource::BITTORRENT) {
|
2007-12-04 11:12:56 +00:00
|
|
|
_tResource->type = MetalinkResource::TYPE_BITTORRENT;
|
|
|
|
} else {
|
|
|
|
_tResource->type = MetalinkResource::TYPE_NOT_SUPPORTED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
void MetalinkParserController::setLocationOfResource(const std::string& location)
|
2007-12-04 11:12:56 +00:00
|
|
|
{
|
|
|
|
if(_tResource.isNull()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_tResource->location = location;
|
|
|
|
}
|
|
|
|
|
2008-03-09 12:24:01 +00:00
|
|
|
void MetalinkParserController::setPreferenceOfResource(int preference)
|
2007-12-04 11:12:56 +00:00
|
|
|
{
|
|
|
|
if(_tResource.isNull()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_tResource->preference = preference;
|
|
|
|
}
|
|
|
|
|
2008-03-09 12:24:01 +00:00
|
|
|
void MetalinkParserController::setMaxConnectionsOfResource(int maxConnections)
|
2007-12-04 11:12:56 +00:00
|
|
|
{
|
|
|
|
if(_tResource.isNull()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_tResource->maxConnections = maxConnections;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MetalinkParserController::commitResourceTransaction()
|
|
|
|
{
|
|
|
|
if(_tResource.isNull()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_tEntry->resources.push_back(_tResource);
|
2008-04-20 00:50:22 +00:00
|
|
|
_tResource.reset();
|
2007-12-04 11:12:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MetalinkParserController::cancelResourceTransaction()
|
|
|
|
{
|
2008-04-20 00:50:22 +00:00
|
|
|
_tResource.reset();
|
2007-12-04 11:12:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MetalinkParserController::newChecksumTransaction()
|
|
|
|
{
|
2007-12-05 14:54:53 +00:00
|
|
|
#ifdef ENABLE_MESSAGE_DIGEST
|
2007-12-04 11:12:56 +00:00
|
|
|
if(_tEntry.isNull()) {
|
|
|
|
return;
|
|
|
|
}
|
2008-04-20 00:50:22 +00:00
|
|
|
_tChecksum.reset(new Checksum());
|
2007-12-05 14:54:53 +00:00
|
|
|
#endif // ENABLE_MESSAGE_DIGEST
|
2007-12-04 11:12:56 +00:00
|
|
|
}
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
void MetalinkParserController::setTypeOfChecksum(const std::string& type)
|
2007-12-04 11:12:56 +00:00
|
|
|
{
|
2007-12-05 14:54:53 +00:00
|
|
|
#ifdef ENABLE_MESSAGE_DIGEST
|
2007-12-04 11:12:56 +00:00
|
|
|
if(_tChecksum.isNull()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(MessageDigestContext::supports(type)) {
|
|
|
|
_tChecksum->setAlgo(type);
|
|
|
|
} else {
|
|
|
|
cancelChecksumTransaction();
|
|
|
|
}
|
2007-12-05 14:54:53 +00:00
|
|
|
#endif // ENABLE_MESSAGE_DIGEST
|
2007-12-04 11:12:56 +00:00
|
|
|
}
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
void MetalinkParserController::setHashOfChecksum(const std::string& md)
|
2007-12-04 11:12:56 +00:00
|
|
|
{
|
2007-12-05 14:54:53 +00:00
|
|
|
#ifdef ENABLE_MESSAGE_DIGEST
|
2007-12-04 11:12:56 +00:00
|
|
|
if(_tChecksum.isNull()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_tChecksum->setMessageDigest(md);
|
2007-12-05 14:54:53 +00:00
|
|
|
#endif // ENABLE_MESSAGE_DIGEST
|
2007-12-04 11:12:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MetalinkParserController::commitChecksumTransaction()
|
|
|
|
{
|
2007-12-05 14:54:53 +00:00
|
|
|
#ifdef ENABLE_MESSAGE_DIGEST
|
2007-12-04 11:12:56 +00:00
|
|
|
if(_tChecksum.isNull()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(_tEntry->checksum.isNull() || _tEntry->checksum->getAlgo() != "sha1") {
|
|
|
|
_tEntry->checksum = _tChecksum;
|
|
|
|
}
|
2008-04-20 00:50:22 +00:00
|
|
|
_tChecksum.reset();
|
2007-12-05 14:54:53 +00:00
|
|
|
#endif // ENABLE_MESSAGE_DIGEST
|
2007-12-04 11:12:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MetalinkParserController::cancelChecksumTransaction()
|
|
|
|
{
|
2007-12-05 14:54:53 +00:00
|
|
|
#ifdef ENABLE_MESSAGE_DIGEST
|
2008-04-20 00:50:22 +00:00
|
|
|
_tChecksum.reset();
|
2007-12-05 14:54:53 +00:00
|
|
|
#endif // ENABLE_MESSAGE_DIGEST
|
2007-12-04 11:12:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MetalinkParserController::newChunkChecksumTransaction()
|
|
|
|
{
|
2007-12-05 14:54:53 +00:00
|
|
|
#ifdef ENABLE_MESSAGE_DIGEST
|
2007-12-04 11:12:56 +00:00
|
|
|
if(_tEntry.isNull()) {
|
|
|
|
return;
|
|
|
|
}
|
2008-04-20 00:50:22 +00:00
|
|
|
_tChunkChecksum.reset(new ChunkChecksum());
|
2007-12-04 11:12:56 +00:00
|
|
|
_tempChunkChecksums.clear();
|
2007-12-05 14:54:53 +00:00
|
|
|
#endif // ENABLE_MESSAGE_DIGEST
|
2007-12-04 11:12:56 +00:00
|
|
|
}
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
void MetalinkParserController::setTypeOfChunkChecksum(const std::string& type)
|
2007-12-04 11:12:56 +00:00
|
|
|
{
|
2007-12-05 14:54:53 +00:00
|
|
|
#ifdef ENABLE_MESSAGE_DIGEST
|
2007-12-04 11:12:56 +00:00
|
|
|
if(_tChunkChecksum.isNull()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(MessageDigestContext::supports(type)) {
|
|
|
|
_tChunkChecksum->setAlgo(type);
|
|
|
|
} else {
|
|
|
|
cancelChunkChecksumTransaction();
|
|
|
|
}
|
2007-12-05 14:54:53 +00:00
|
|
|
#endif // ENABLE_MESSAGE_DIGEST
|
2007-12-04 11:12:56 +00:00
|
|
|
}
|
|
|
|
|
2008-03-09 12:24:01 +00:00
|
|
|
void MetalinkParserController::setLengthOfChunkChecksum(size_t length)
|
2007-12-04 11:12:56 +00:00
|
|
|
{
|
2007-12-05 14:54:53 +00:00
|
|
|
#ifdef ENABLE_MESSAGE_DIGEST
|
2007-12-04 11:12:56 +00:00
|
|
|
if(_tChunkChecksum.isNull()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(length > 0) {
|
|
|
|
_tChunkChecksum->setChecksumLength(length);
|
|
|
|
} else {
|
|
|
|
cancelChunkChecksumTransaction();
|
|
|
|
}
|
2007-12-05 14:54:53 +00:00
|
|
|
#endif // ENABLE_MESSAGE_DIGEST
|
2007-12-04 11:12:56 +00:00
|
|
|
}
|
|
|
|
|
2008-03-09 12:24:01 +00:00
|
|
|
void MetalinkParserController::addHashOfChunkChecksum(size_t order, const std::string& md)
|
2007-12-04 11:12:56 +00:00
|
|
|
{
|
2007-12-05 14:54:53 +00:00
|
|
|
#ifdef ENABLE_MESSAGE_DIGEST
|
2007-12-04 11:12:56 +00:00
|
|
|
if(_tChunkChecksum.isNull()) {
|
|
|
|
return;
|
|
|
|
}
|
2008-03-09 12:24:01 +00:00
|
|
|
_tempChunkChecksums.push_back(std::pair<size_t, std::string>(order, md));
|
2007-12-05 14:54:53 +00:00
|
|
|
#endif // ENABLE_MESSAGE_DIGEST
|
2007-12-04 11:12:56 +00:00
|
|
|
}
|
|
|
|
|
2008-03-09 12:24:01 +00:00
|
|
|
void MetalinkParserController::createNewHashOfChunkChecksum(size_t order)
|
2007-12-04 11:12:56 +00:00
|
|
|
{
|
2007-12-05 14:54:53 +00:00
|
|
|
#ifdef ENABLE_MESSAGE_DIGEST
|
2007-12-04 11:12:56 +00:00
|
|
|
if(_tChunkChecksum.isNull()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_tempHashPair.first = order;
|
2007-12-05 14:54:53 +00:00
|
|
|
#endif // ENABLE_MESSAGE_DIGEST
|
2007-12-04 11:12:56 +00:00
|
|
|
}
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
void MetalinkParserController::setMessageDigestOfChunkChecksum(const std::string& md)
|
2007-12-04 11:12:56 +00:00
|
|
|
{
|
2007-12-05 14:54:53 +00:00
|
|
|
#ifdef ENABLE_MESSAGE_DIGEST
|
2007-12-04 11:12:56 +00:00
|
|
|
if(_tChunkChecksum.isNull()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_tempHashPair.second = md;
|
2007-12-05 14:54:53 +00:00
|
|
|
#endif // ENABLE_MESSAGE_DIGEST
|
2007-12-04 11:12:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MetalinkParserController::addHashOfChunkChecksum()
|
|
|
|
{
|
2007-12-05 14:54:53 +00:00
|
|
|
#ifdef ENABLE_MESSAGE_DIGEST
|
2007-12-04 11:12:56 +00:00
|
|
|
if(_tChunkChecksum.isNull()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_tempChunkChecksums.push_back(_tempHashPair);
|
2007-12-05 14:54:53 +00:00
|
|
|
#endif // ENABLE_MESSAGE_DIGEST
|
2007-12-04 11:12:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MetalinkParserController::commitChunkChecksumTransaction()
|
|
|
|
{
|
2007-12-05 14:54:53 +00:00
|
|
|
#ifdef ENABLE_MESSAGE_DIGEST
|
2007-12-04 11:12:56 +00:00
|
|
|
if(_tChunkChecksum.isNull()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(_tEntry->chunkChecksum.isNull() || _tEntry->chunkChecksum->getAlgo() != "sha1") {
|
2008-03-09 12:24:01 +00:00
|
|
|
std::sort(_tempChunkChecksums.begin(), _tempChunkChecksums.end(), Ascend1st<std::pair<size_t, std::string> >());
|
2008-02-08 15:53:45 +00:00
|
|
|
std::deque<std::string> checksums;
|
|
|
|
std::transform(_tempChunkChecksums.begin(), _tempChunkChecksums.end(),
|
2008-03-09 12:24:01 +00:00
|
|
|
std::back_inserter(checksums), select2nd<std::pair<size_t, std::string> >());
|
2007-12-04 11:12:56 +00:00
|
|
|
_tChunkChecksum->setChecksums(checksums);
|
|
|
|
_tEntry->chunkChecksum = _tChunkChecksum;
|
|
|
|
}
|
2008-04-20 00:50:22 +00:00
|
|
|
_tChunkChecksum.reset();
|
2007-12-05 14:54:53 +00:00
|
|
|
#endif // ENABLE_MESSAGE_DIGEST
|
2007-12-04 11:12:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MetalinkParserController::cancelChunkChecksumTransaction()
|
|
|
|
{
|
2007-12-05 14:54:53 +00:00
|
|
|
#ifdef ENABLE_MESSAGE_DIGEST
|
2008-04-20 00:50:22 +00:00
|
|
|
_tChunkChecksum.reset();
|
2007-12-05 14:54:53 +00:00
|
|
|
#endif // ENABLE_MESSAGE_DIGEST
|
2007-12-04 11:12:56 +00:00
|
|
|
}
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
} // namespace aria2
|