123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439 |
- /* <!-- copyright */
- /*
- * aria2 - The high speed download utility
- *
- * Copyright (C) 2010 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 "MetalinkParserStateV3Impl.h"
- #include "MetalinkParserStateMachine.h"
- #include "RecoverableException.h"
- #include "MetalinkResource.h"
- #include "util.h"
- namespace aria2 {
- namespace {
- const std::string FILE("file");
- const std::string FILES("files");
- const std::string HASH("hash");
- const std::string LANGUAGE("language");
- const std::string LENGTH("length");
- const std::string LOCATION("location");
- const std::string MAXCONNECTIONS("maxconnections");
- // Can't use name VERSION because it is used as a macro.
- const std::string METALINK_VERSION("version");
- const std::string NAME("name");
- const std::string OS("os");
- const std::string PIECE("piece");
- const std::string PIECES("pieces");
- const std::string PREFERENCE("preference");
- const std::string RESOURCES("resources");
- const std::string SIGNATURE("signature");
- const std::string SIZE("size");
- const std::string TYPE("type");
- const std::string URL("url");
- const std::string VERIFICATION("verification");
- } // namespace
- const std::string METALINK3_NAMESPACE_URI("http://www.metalinker.org/");
- namespace {
- class FindAttr {
- private:
- const std::string& localname_;
- public:
- FindAttr(const std::string& localname):localname_(localname) {}
- bool operator()(const XmlAttr& attr) const
- {
- return attr.localname == localname_ &&
- (attr.nsUri.empty() || attr.nsUri == METALINK3_NAMESPACE_URI);
- }
- };
- } // namespace
- namespace {
- template<typename Container>
- typename Container::const_iterator findAttr
- (const Container& attrs, const std::string& localname)
- {
- return std::find_if(attrs.begin(), attrs.end(), FindAttr(localname));
- }
- } // namespace
- void MetalinkMetalinkParserState::beginElement
- (MetalinkParserStateMachine* stm,
- const std::string& localname,
- const std::string& prefix,
- const std::string& nsUri,
- const std::vector<XmlAttr>& attrs)
- {
- if(nsUri == METALINK3_NAMESPACE_URI && localname == FILES) {
- stm->setFilesState();
- } else {
- stm->setSkipTagState();
- }
- }
- void FilesMetalinkParserState::beginElement
- (MetalinkParserStateMachine* stm,
- const std::string& localname,
- const std::string& prefix,
- const std::string& nsUri,
- const std::vector<XmlAttr>& attrs)
- {
- if(nsUri == METALINK3_NAMESPACE_URI && localname == FILE) {
- stm->setFileState();
- std::vector<XmlAttr>::const_iterator itr = findAttr(attrs, NAME);
- if(itr != attrs.end()) {
- std::string name = util::strip((*itr).value);
- if(name.empty() || util::detectDirTraversal(name)) {
- return;
- }
- stm->newEntryTransaction();
- stm->setFileNameOfEntry(name);
- }
- } else {
- stm->setSkipTagState();
- }
- }
- void FileMetalinkParserState::beginElement
- (MetalinkParserStateMachine* stm,
- const std::string& localname,
- const std::string& prefix,
- const std::string& nsUri,
- const std::vector<XmlAttr>& attrs)
- {
- if(nsUri != METALINK3_NAMESPACE_URI) {
- stm->setSkipTagState();
- } else if(localname == SIZE) {
- stm->setSizeState();
- } else if(localname == METALINK_VERSION) {
- stm->setVersionState();
- } else if(localname == LANGUAGE) {
- stm->setLanguageState();
- } else if(localname == OS) {
- stm->setOSState();
- } else if(localname == VERIFICATION) {
- stm->setVerificationState();
- } else if(localname == RESOURCES) {
- stm->setResourcesState();
- int maxConnections;
- {
- std::vector<XmlAttr>::const_iterator itr = findAttr(attrs,MAXCONNECTIONS);
- if(itr == attrs.end()) {
- maxConnections = -1;
- } else {
- try {
- maxConnections = util::parseInt((*itr).value);
- } catch(RecoverableException& e) {
- maxConnections = -1;
- }
- }
- }
- stm->setMaxConnectionsOfEntry(maxConnections);
- } else {
- stm->setSkipTagState();
- }
- }
- void FileMetalinkParserState::endElement
- (MetalinkParserStateMachine* stm,
- const std::string& localname,
- const std::string& prefix,
- const std::string& nsUri,
- const std::string& characters)
- {
- stm->commitEntryTransaction();
- }
- void SizeMetalinkParserState::endElement
- (MetalinkParserStateMachine* stm,
- const std::string& localname,
- const std::string& prefix,
- const std::string& nsUri,
- const std::string& characters)
- {
- try {
- stm->setFileLengthOfEntry(util::parseULLInt(characters));
- } catch(RecoverableException& e) {
- // current metalink specification doesn't require size element.
- }
- }
- void VersionMetalinkParserState::endElement
- (MetalinkParserStateMachine* stm,
- const std::string& localname,
- const std::string& prefix,
- const std::string& nsUri,
- const std::string& characters)
- {
- stm->setVersionOfEntry(util::strip(characters));
- }
- void LanguageMetalinkParserState::endElement
- (MetalinkParserStateMachine* stm,
- const std::string& localname,
- const std::string& prefix,
- const std::string& nsUri,
- const std::string& characters)
- {
- stm->setLanguageOfEntry(util::strip(characters));
- }
- void OSMetalinkParserState::endElement
- (MetalinkParserStateMachine* stm,
- const std::string& localname,
- const std::string& prefix,
- const std::string& nsUri,
- const std::string& characters)
- {
- stm->setOSOfEntry(util::strip(characters));
- }
- void VerificationMetalinkParserState::beginElement
- (MetalinkParserStateMachine* stm,
- const std::string& localname,
- const std::string& prefix,
- const std::string& nsUri,
- const std::vector<XmlAttr>& attrs)
- {
- if(nsUri != METALINK3_NAMESPACE_URI) {
- stm->setSkipTagState();
- } else
- #ifdef ENABLE_MESSAGE_DIGEST
- if(localname == HASH) {
- stm->setHashState();
- std::vector<XmlAttr>::const_iterator itr = findAttr(attrs, TYPE);
- if(itr == attrs.end()) {
- return;
- } else {
- std::string type = util::strip((*itr).value);
- stm->newChecksumTransaction();
- stm->setTypeOfChecksum(type);
- }
- } else if(localname == PIECES) {
- stm->setPiecesState();
- try {
- size_t length;
- {
- std::vector<XmlAttr>::const_iterator itr = findAttr(attrs, LENGTH);
- if(itr == attrs.end()) {
- return;
- } else {
- length = util::parseInt((*itr).value);
- }
- }
- std::string type;
- {
- std::vector<XmlAttr>::const_iterator itr = findAttr(attrs, TYPE);
- if(itr == attrs.end()) {
- return;
- } else {
- type = util::strip((*itr).value);
- }
- }
- stm->newChunkChecksumTransaction();
- stm->setLengthOfChunkChecksum(length);
- stm->setTypeOfChunkChecksum(type);
- } catch(RecoverableException& e) {
- stm->cancelChunkChecksumTransaction();
- }
- } else
- #endif // ENABLE_MESSAGE_DIGEST
- if(localname == SIGNATURE) {
- stm->setSignatureState();
- std::vector<XmlAttr>::const_iterator itr = findAttr(attrs, TYPE);
- if(itr == attrs.end()) {
- return;
- } else {
- stm->newSignatureTransaction();
- stm->setTypeOfSignature(util::strip((*itr).value));
- std::vector<XmlAttr>::const_iterator itr = findAttr(attrs, FILE);
- if(itr != attrs.end()) {
- std::string file = util::strip((*itr).value);
- if(!util::detectDirTraversal(file)) {
- stm->setFileOfSignature(file);
- }
- }
- }
- } else {
- stm->setSkipTagState();
- }
- }
- void HashMetalinkParserState::endElement
- (MetalinkParserStateMachine* stm,
- const std::string& localname,
- const std::string& prefix,
- const std::string& nsUri,
- const std::string& characters)
- {
- stm->setHashOfChecksum(util::strip(characters));
- stm->commitChecksumTransaction();
- }
- void PiecesMetalinkParserState::beginElement
- (MetalinkParserStateMachine* stm,
- const std::string& localname,
- const std::string& prefix,
- const std::string& nsUri,
- const std::vector<XmlAttr>& attrs)
- {
- if(nsUri == METALINK3_NAMESPACE_URI && localname == HASH) {
- stm->setPieceHashState();
- std::vector<XmlAttr>::const_iterator itr = findAttr(attrs, PIECE);
- if(itr == attrs.end()) {
- stm->cancelChunkChecksumTransaction();
- } else {
- try {
- stm->createNewHashOfChunkChecksum(util::parseInt((*itr).value));
- } catch(RecoverableException& e) {
- stm->cancelChunkChecksumTransaction();
- }
- }
- } else {
- stm->setSkipTagState();
- }
- }
- void PiecesMetalinkParserState::endElement
- (MetalinkParserStateMachine* stm,
- const std::string& localname,
- const std::string& prefix,
- const std::string& nsUri,
- const std::string& characters)
- {
- stm->commitChunkChecksumTransaction();
- }
- void PieceHashMetalinkParserState::endElement
- (MetalinkParserStateMachine* stm,
- const std::string& localname,
- const std::string& prefix,
- const std::string& nsUri,
- const std::string& characters)
- {
- stm->setMessageDigestOfChunkChecksum(util::strip(characters));
- stm->addHashOfChunkChecksum();
- }
- void SignatureMetalinkParserState::endElement
- (MetalinkParserStateMachine* stm,
- const std::string& localname,
- const std::string& prefix,
- const std::string& nsUri,
- const std::string& characters)
- {
- stm->setBodyOfSignature(util::strip(characters));
- stm->commitSignatureTransaction();
- }
- void ResourcesMetalinkParserState::beginElement
- (MetalinkParserStateMachine* stm,
- const std::string& localname,
- const std::string& prefix,
- const std::string& nsUri,
- const std::vector<XmlAttr>& attrs)
- {
- if(nsUri != METALINK3_NAMESPACE_URI) {
- stm->setSkipTagState();
- } else if(localname == URL) {
- stm->setURLState();
- std::string type;
- {
- std::vector<XmlAttr>::const_iterator itr = findAttr(attrs, TYPE);
- if(itr == attrs.end()) {
- return;
- } else {
- type = util::strip((*itr).value);
- }
- }
- std::string location;
- {
- std::vector<XmlAttr>::const_iterator itr = findAttr(attrs, LOCATION);
- if(itr != attrs.end()) {
- location = util::strip((*itr).value);
- }
- }
- int preference;
- {
- std::vector<XmlAttr>::const_iterator itr = findAttr(attrs, PREFERENCE);
- if(itr == attrs.end()) {
- preference = MetalinkResource::getLowestPriority();
- } else {
- try {
- // In Metalink3Spec, highest prefernce value is 100. We
- // uses Metalink4Spec priority unit system in which 1 is
- // higest.
- preference = 101-util::parseInt((*itr).value);
- } catch(RecoverableException& e) {
- preference = MetalinkResource::getLowestPriority();
- }
- }
- }
- int maxConnections;
- {
- std::vector<XmlAttr>::const_iterator itr = findAttr(attrs,MAXCONNECTIONS);
- if(itr == attrs.end()) {
- maxConnections = -1;
- } else {
- try {
- maxConnections = util::parseInt((*itr).value);
- } catch(RecoverableException& e) {
- maxConnections = -1;
- }
- }
- }
- stm->newResourceTransaction();
- stm->setTypeOfResource(type);
- stm->setLocationOfResource(location);
- stm->setPriorityOfResource(preference);
- stm->setMaxConnectionsOfResource(maxConnections);
- } else {
- stm->setSkipTagState();
- }
- }
- void URLMetalinkParserState::endElement
- (MetalinkParserStateMachine* stm,
- const std::string& localname,
- const std::string& prefix,
- const std::string& nsUri,
- const std::string& characters)
- {
- stm->setURLOfResource(util::strip(characters));
- stm->commitResourceTransaction();
- }
- } // namespace aria2
|