mirror of https://github.com/aria2/aria2
Rewritten Expat Metalink XML parser.
parent
440f29aca3
commit
3b4a368554
|
@ -2,7 +2,7 @@
|
||||||
/*
|
/*
|
||||||
* aria2 - The high speed download utility
|
* aria2 - The high speed download utility
|
||||||
*
|
*
|
||||||
* Copyright (C) 2006 Tatsuhiro Tsujikawa
|
* Copyright (C) 2011 Tatsuhiro Tsujikawa
|
||||||
*
|
*
|
||||||
* This program is free software; you can redistribute it and/or modify
|
* 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
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
@ -32,50 +32,52 @@
|
||||||
* files in the program, then also delete it here.
|
* files in the program, then also delete it here.
|
||||||
*/
|
*/
|
||||||
/* copyright --> */
|
/* copyright --> */
|
||||||
#include "ExpatMetalinkProcessor.h"
|
#include "ExpatXmlParser.h"
|
||||||
|
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
#include <deque>
|
||||||
|
|
||||||
#include "DefaultDiskWriter.h"
|
#include <expat.h>
|
||||||
#include "MetalinkParserStateMachine.h"
|
|
||||||
#include "Metalinker.h"
|
#include "a2io.h"
|
||||||
#include "MetalinkEntry.h"
|
#include "BinaryStream.h"
|
||||||
#include "util.h"
|
|
||||||
#include "message.h"
|
|
||||||
#include "DlAbortEx.h"
|
|
||||||
#include "MetalinkParserState.h"
|
|
||||||
#include "A2STR.h"
|
|
||||||
#include "error_code.h"
|
|
||||||
#include "BufferedFile.h"
|
#include "BufferedFile.h"
|
||||||
|
#include "ParserStateMachine.h"
|
||||||
|
#include "A2STR.h"
|
||||||
#include "a2functional.h"
|
#include "a2functional.h"
|
||||||
|
#include "XmlAttr.h"
|
||||||
|
|
||||||
namespace aria2 {
|
namespace aria2 {
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
class SessionData {
|
struct SessionData {
|
||||||
public:
|
|
||||||
SharedHandle<MetalinkParserStateMachine> stm_;
|
|
||||||
|
|
||||||
std::deque<std::string> charactersStack_;
|
std::deque<std::string> charactersStack_;
|
||||||
|
ParserStateMachine* psm_;
|
||||||
SessionData(const SharedHandle<MetalinkParserStateMachine>& stm):stm_(stm) {}
|
SessionData(ParserStateMachine* psm)
|
||||||
|
: psm_(psm)
|
||||||
|
{}
|
||||||
};
|
};
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
template<typename InputIterator>
|
template<typename InputIterator>
|
||||||
void splitNsName
|
void splitNsName
|
||||||
(std::string& localname, std::string& prefix, std::string& nsUri,
|
(const char** localname,
|
||||||
InputIterator first, InputIterator last)
|
const char** nsUri,
|
||||||
|
InputIterator first,
|
||||||
|
InputIterator last)
|
||||||
{
|
{
|
||||||
std::pair<Scip, Scip> nsNamePair;
|
InputIterator sep = std::find(first, last, '\t');
|
||||||
util::divide(nsNamePair, first, last, '\t');
|
if(sep == last) {
|
||||||
if(nsNamePair.second.first == nsNamePair.second.second) {
|
*localname = first;
|
||||||
localname.assign(nsNamePair.first.first, nsNamePair.first.second);
|
|
||||||
} else {
|
} else {
|
||||||
nsUri.assign(nsNamePair.first.first, nsNamePair.first.second);
|
*localname = sep+1;
|
||||||
localname.assign(nsNamePair.second.first, nsNamePair.second.second);
|
size_t nsUriLen = sep-first;
|
||||||
|
char* temp = new char[nsUriLen+1];
|
||||||
|
memcpy(temp, first, nsUriLen);
|
||||||
|
temp[nsUriLen] = '\0';
|
||||||
|
*nsUri = temp;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} // namespace
|
} // namespace
|
||||||
|
@ -84,29 +86,34 @@ namespace {
|
||||||
void mlStartElement(void* userData, const char* nsName, const char** attrs)
|
void mlStartElement(void* userData, const char* nsName, const char** attrs)
|
||||||
{
|
{
|
||||||
SessionData* sd = reinterpret_cast<SessionData*>(userData);
|
SessionData* sd = reinterpret_cast<SessionData*>(userData);
|
||||||
|
|
||||||
std::vector<XmlAttr> xmlAttrs;
|
std::vector<XmlAttr> xmlAttrs;
|
||||||
if(attrs) {
|
if(attrs) {
|
||||||
const char** p = attrs;
|
const char** p = attrs;
|
||||||
while(*p != 0) {
|
while(*p != 0) {
|
||||||
|
XmlAttr xa;
|
||||||
const char* attrNsName = *p++;
|
const char* attrNsName = *p++;
|
||||||
if(*p == 0) {
|
if(*p == 0) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
XmlAttr xa;
|
splitNsName(&xa.localname, &xa.nsUri,
|
||||||
splitNsName(xa.localname, xa.prefix, xa.nsUri,
|
|
||||||
attrNsName, attrNsName+strlen(attrNsName));
|
attrNsName, attrNsName+strlen(attrNsName));
|
||||||
const char* value = *p++;
|
const char* value = *p++;
|
||||||
xa.value.assign(value, value+strlen(value));
|
xa.value = value;
|
||||||
|
xa.valueLength = strlen(value);
|
||||||
xmlAttrs.push_back(xa);
|
xmlAttrs.push_back(xa);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
std::string localname;
|
const char* localname = 0;
|
||||||
std::string prefix;
|
const char* prefix = 0;
|
||||||
std::string nsUri;
|
const char* nsUri = 0;
|
||||||
splitNsName(localname, prefix, nsUri, nsName, nsName+strlen(nsName));
|
splitNsName(&localname, &nsUri, nsName, nsName+strlen(nsName));
|
||||||
sd->stm_->beginElement(localname, prefix, nsUri, xmlAttrs);
|
sd->psm_->beginElement(localname, prefix, nsUri, xmlAttrs);
|
||||||
if(sd->stm_->needsCharactersBuffering()) {
|
delete [] nsUri;
|
||||||
|
for(std::vector<XmlAttr>::iterator i = xmlAttrs.begin(),
|
||||||
|
eoi = xmlAttrs.end(); i != eoi; ++i) {
|
||||||
|
delete [] (*i).nsUri;
|
||||||
|
}
|
||||||
|
if(sd->psm_->needsCharactersBuffering()) {
|
||||||
sd->charactersStack_.push_front(A2STR::NIL);
|
sd->charactersStack_.push_front(A2STR::NIL);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -115,18 +122,18 @@ void mlStartElement(void* userData, const char* nsName, const char** attrs)
|
||||||
namespace {
|
namespace {
|
||||||
void mlEndElement(void* userData, const char* nsName)
|
void mlEndElement(void* userData, const char* nsName)
|
||||||
{
|
{
|
||||||
std::string localname;
|
const char* localname = 0;
|
||||||
std::string prefix;
|
const char* prefix = 0;
|
||||||
std::string nsUri;
|
const char* nsUri = 0;
|
||||||
splitNsName(localname, prefix, nsUri, nsName, nsName+strlen(nsName));
|
splitNsName(&localname, &nsUri, nsName, nsName+strlen(nsName));
|
||||||
|
|
||||||
SessionData* sd = reinterpret_cast<SessionData*>(userData);
|
SessionData* sd = reinterpret_cast<SessionData*>(userData);
|
||||||
std::string characters;
|
std::string characters;
|
||||||
if(sd->stm_->needsCharactersBuffering()) {
|
if(sd->psm_->needsCharactersBuffering()) {
|
||||||
characters = sd->charactersStack_.front();
|
characters = sd->charactersStack_.front();
|
||||||
sd->charactersStack_.pop_front();
|
sd->charactersStack_.pop_front();
|
||||||
}
|
}
|
||||||
sd->stm_->endElement(localname, prefix, nsUri, characters);
|
sd->psm_->endElement(localname, prefix, nsUri, characters);
|
||||||
|
delete [] nsUri;
|
||||||
}
|
}
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
|
@ -134,113 +141,81 @@ namespace {
|
||||||
void mlCharacters(void* userData, const char* ch, int len)
|
void mlCharacters(void* userData, const char* ch, int len)
|
||||||
{
|
{
|
||||||
SessionData* sd = reinterpret_cast<SessionData*>(userData);
|
SessionData* sd = reinterpret_cast<SessionData*>(userData);
|
||||||
if(sd->stm_->needsCharactersBuffering()) {
|
if(sd->psm_->needsCharactersBuffering()) {
|
||||||
sd->charactersStack_.front().append(&ch[0], &ch[len]);
|
sd->charactersStack_.front().append(&ch[0], &ch[len]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
|
XmlParser::XmlParser(ParserStateMachine* psm)
|
||||||
|
: psm_(psm)
|
||||||
|
{}
|
||||||
|
|
||||||
|
XmlParser::~XmlParser() {}
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
XML_Parser createParser(const SharedHandle<SessionData>& sessionData)
|
XML_Parser createParser(SessionData* sd)
|
||||||
{
|
{
|
||||||
XML_Parser parser = XML_ParserCreateNS(0, static_cast<const XML_Char>('\t'));
|
XML_Parser parser = XML_ParserCreateNS(0, static_cast<const XML_Char>('\t'));
|
||||||
XML_SetUserData(parser, sessionData.get());
|
XML_SetUserData(parser, sd);
|
||||||
XML_SetElementHandler(parser, &mlStartElement, &mlEndElement);
|
XML_SetElementHandler(parser, &mlStartElement, &mlEndElement);
|
||||||
XML_SetCharacterDataHandler(parser, &mlCharacters);
|
XML_SetCharacterDataHandler(parser, &mlCharacters);
|
||||||
return parser;
|
return parser;
|
||||||
}
|
}
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
namespace {
|
bool XmlParser::parseFile(const char* filename)
|
||||||
void checkError(XML_Parser parser)
|
|
||||||
{
|
|
||||||
if(XML_Parse(parser, 0, 0, 1) == XML_STATUS_ERROR) {
|
|
||||||
throw DL_ABORT_EX2(MSG_CANNOT_PARSE_METALINK,
|
|
||||||
error_code::METALINK_PARSE_ERROR);
|
|
||||||
}
|
|
||||||
SessionData* sessionData =
|
|
||||||
reinterpret_cast<SessionData*>(XML_GetUserData(parser));
|
|
||||||
const SharedHandle<MetalinkParserStateMachine>& stm = sessionData->stm_;
|
|
||||||
if(!stm->finished()) {
|
|
||||||
throw DL_ABORT_EX2(MSG_CANNOT_PARSE_METALINK,
|
|
||||||
error_code::METALINK_PARSE_ERROR);
|
|
||||||
}
|
|
||||||
if(!stm->getErrors().empty()) {
|
|
||||||
throw DL_ABORT_EX2(stm->getErrorString(),
|
|
||||||
error_code::METALINK_PARSE_ERROR);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} // namespace
|
|
||||||
|
|
||||||
SharedHandle<Metalinker>
|
|
||||||
MetalinkProcessor::parseFile
|
|
||||||
(const std::string& filename,
|
|
||||||
const std::string& baseUri)
|
|
||||||
{
|
{
|
||||||
BufferedFile* fp = 0;
|
BufferedFile* fp = 0;
|
||||||
auto_delete_d<BufferedFile*> deleter(fp);
|
if(strcmp(filename, DEV_STDIN) == 0) {
|
||||||
if(filename == DEV_STDIN) {
|
|
||||||
fp = new BufferedFile(stdin);
|
fp = new BufferedFile(stdin);
|
||||||
return parseFile(*fp);
|
auto_delete_d<BufferedFile*> deleter(fp);
|
||||||
|
return parseFile(fp);
|
||||||
} else {
|
} else {
|
||||||
fp = new BufferedFile(filename, BufferedFile::READ);
|
fp = new BufferedFile(filename, BufferedFile::READ);
|
||||||
return parseFile(*fp, baseUri);
|
auto_delete_d<BufferedFile*> deleter(fp);
|
||||||
|
return parseFile(fp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
SharedHandle<Metalinker>
|
bool XmlParser::parseFile(BufferedFile* fp)
|
||||||
MetalinkProcessor::parseFile
|
|
||||||
(BufferedFile& fp, const std::string& baseUri)
|
|
||||||
{
|
{
|
||||||
stm_.reset(new MetalinkParserStateMachine());
|
|
||||||
stm_->setBaseUri(baseUri);
|
|
||||||
char buf[4096];
|
char buf[4096];
|
||||||
|
SessionData sessionData(psm_);
|
||||||
SharedHandle<SessionData> sessionData(new SessionData(stm_));
|
XML_Parser parser = createParser(&sessionData);
|
||||||
XML_Parser parser = createParser(sessionData);
|
|
||||||
auto_delete<XML_Parser> deleter(parser, XML_ParserFree);
|
auto_delete<XML_Parser> deleter(parser, XML_ParserFree);
|
||||||
while(1) {
|
while(1) {
|
||||||
size_t res = fp.read(buf, sizeof(buf));
|
size_t res = fp->read(buf, sizeof(buf));
|
||||||
if(XML_Parse(parser, buf, res, 0) == XML_STATUS_ERROR) {
|
if(XML_Parse(parser, buf, res, 0) == XML_STATUS_ERROR) {
|
||||||
throw DL_ABORT_EX2(MSG_CANNOT_PARSE_METALINK,
|
return false;
|
||||||
error_code::METALINK_PARSE_ERROR);
|
|
||||||
}
|
}
|
||||||
if(res < sizeof(buf)) {
|
if(res < sizeof(buf)) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
checkError(parser);
|
return XML_Parse(parser, 0, 0, 1) != XML_STATUS_ERROR && psm_->finished();
|
||||||
return stm_->getResult();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SharedHandle<Metalinker>
|
bool XmlParser::parseBinaryStream(BinaryStream* bs)
|
||||||
MetalinkProcessor::parseFromBinaryStream
|
|
||||||
(const SharedHandle<BinaryStream>& binaryStream,
|
|
||||||
const std::string& baseUri)
|
|
||||||
{
|
{
|
||||||
stm_.reset(new MetalinkParserStateMachine());
|
const ssize_t bufSize = 4096;
|
||||||
stm_->setBaseUri(baseUri);
|
|
||||||
ssize_t bufSize = 4096;
|
|
||||||
unsigned char buf[bufSize];
|
unsigned char buf[bufSize];
|
||||||
|
SessionData sessionData(psm_);
|
||||||
SharedHandle<SessionData> sessionData(new SessionData(stm_));
|
XML_Parser parser = createParser(&sessionData);
|
||||||
XML_Parser parser = createParser(sessionData);
|
|
||||||
auto_delete<XML_Parser> deleter(parser, XML_ParserFree);
|
auto_delete<XML_Parser> deleter(parser, XML_ParserFree);
|
||||||
off_t readOffset = 0;
|
off_t readOffset = 0;
|
||||||
while(1) {
|
while(1) {
|
||||||
ssize_t res = binaryStream->readData(buf, bufSize, readOffset);
|
ssize_t res = bs->readData(buf, bufSize, readOffset);
|
||||||
if(res == 0) {
|
if(res == 0) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if(XML_Parse(parser, reinterpret_cast<const char*>(buf), res, 0) ==
|
if(XML_Parse(parser, reinterpret_cast<const char*>(buf), res, 0) ==
|
||||||
XML_STATUS_ERROR) {
|
XML_STATUS_ERROR) {
|
||||||
throw DL_ABORT_EX2(MSG_CANNOT_PARSE_METALINK,
|
return false;
|
||||||
error_code::METALINK_PARSE_ERROR);
|
|
||||||
}
|
}
|
||||||
readOffset += res;
|
readOffset += res;
|
||||||
}
|
}
|
||||||
checkError(parser);
|
return XML_Parse(parser, 0, 0, 1) != XML_STATUS_ERROR && psm_->finished();
|
||||||
return stm_->getResult();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace aria2
|
} // namespace aria2
|
|
@ -2,7 +2,7 @@
|
||||||
/*
|
/*
|
||||||
* aria2 - The high speed download utility
|
* aria2 - The high speed download utility
|
||||||
*
|
*
|
||||||
* Copyright (C) 2006 Tatsuhiro Tsujikawa
|
* Copyright (C) 2011 Tatsuhiro Tsujikawa
|
||||||
*
|
*
|
||||||
* This program is free software; you can redistribute it and/or modify
|
* 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
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
@ -32,42 +32,33 @@
|
||||||
* files in the program, then also delete it here.
|
* files in the program, then also delete it here.
|
||||||
*/
|
*/
|
||||||
/* copyright --> */
|
/* copyright --> */
|
||||||
#ifndef D_EXPAT_METALINK_PROCESSOR_H
|
#ifndef D_EXPAT_XML_PARSER_H
|
||||||
#define D_EXPAT_METALINK_PROCESSOR_H
|
#define D_EXPAT_XML_PARSER_H
|
||||||
|
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
|
|
||||||
#include <string>
|
#include <cstdlib>
|
||||||
|
|
||||||
#include <expat.h>
|
|
||||||
|
|
||||||
#include "SharedHandle.h"
|
|
||||||
#include "A2STR.h"
|
|
||||||
|
|
||||||
namespace aria2 {
|
namespace aria2 {
|
||||||
|
|
||||||
class Metalinker;
|
|
||||||
class BinaryStream;
|
class BinaryStream;
|
||||||
class MetalinkParserStateMachine;
|
class ParserStateMachine;
|
||||||
class BufferedFile;
|
class BufferedFile;
|
||||||
|
|
||||||
class MetalinkProcessor {
|
class XmlParser {
|
||||||
private:
|
|
||||||
SharedHandle<MetalinkParserStateMachine> stm_;
|
|
||||||
|
|
||||||
SharedHandle<Metalinker> parseFile
|
|
||||||
(BufferedFile& fp,
|
|
||||||
const std::string& baseUri = A2STR::NIL);
|
|
||||||
public:
|
public:
|
||||||
SharedHandle<Metalinker> parseFile
|
// This object does not delete psm.
|
||||||
(const std::string& filename,
|
XmlParser(ParserStateMachine* psm);
|
||||||
const std::string& baseUri = A2STR::NIL);
|
~XmlParser();
|
||||||
|
bool parseFile(const char* filename);
|
||||||
|
bool parseBinaryStream(BinaryStream* binaryStream);
|
||||||
|
bool parseMemory(const char* xml, size_t size);
|
||||||
|
private:
|
||||||
|
bool parseFile(BufferedFile* fp);
|
||||||
|
|
||||||
SharedHandle<Metalinker> parseFromBinaryStream
|
ParserStateMachine* psm_;
|
||||||
(const SharedHandle<BinaryStream>& binaryStream,
|
|
||||||
const std::string& baseUri = A2STR::NIL);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace aria2
|
} // namespace aria2
|
||||||
|
|
||||||
#endif // D_EXPAT_METALINK_PROCESSOR_H
|
#endif // D_EXPAT_XML_PARSER_H
|
|
@ -43,8 +43,6 @@
|
||||||
#include "a2io.h"
|
#include "a2io.h"
|
||||||
#include "BinaryStream.h"
|
#include "BinaryStream.h"
|
||||||
#include "ParserStateMachine.h"
|
#include "ParserStateMachine.h"
|
||||||
#include "message.h"
|
|
||||||
#include "DlAbortEx.h"
|
|
||||||
#include "A2STR.h"
|
#include "A2STR.h"
|
||||||
#include "a2functional.h"
|
#include "a2functional.h"
|
||||||
#include "XmlAttr.h"
|
#include "XmlAttr.h"
|
||||||
|
|
|
@ -40,7 +40,7 @@
|
||||||
#ifdef HAVE_LIBXML2
|
#ifdef HAVE_LIBXML2
|
||||||
# include "Xml2XmlParser.h"
|
# include "Xml2XmlParser.h"
|
||||||
#elif HAVE_LIBEXPAT
|
#elif HAVE_LIBEXPAT
|
||||||
# include "Expat2XmlParser.h"
|
# include "ExpatXmlParser.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#endif // D_XML_PARSER_H
|
#endif // D_XML_PARSER_H
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
#include "MetalinkProcessor.h"
|
#include "metalink_helper.h"
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
@ -21,7 +21,6 @@
|
||||||
#include "fmt.h"
|
#include "fmt.h"
|
||||||
#include "RecoverableException.h"
|
#include "RecoverableException.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
#include "metalink_helper.h"
|
|
||||||
|
|
||||||
namespace aria2 {
|
namespace aria2 {
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue