2009-05-12 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>

Throw DlAbortEx instead of FatalException during parsing options
	because we don't want for aria2 to quit when bad option is passed
	via XML-RPC.
	* src/OptionHandlerException.cc
	* src/OptionHandlerException.h
	* src/OptionHandlerImpl.h
	* src/OptionParser.cc
	* src/OptionParser.h
	* src/PStringSegment.cc
	* src/ParameterizedStringParser.cc
	* src/download_helper.cc
	* test/ParameterizedStringParserTest.cc
pull/1/head
Tatsuhiro Tsujikawa 2009-05-12 13:51:12 +00:00
parent 2758562eb0
commit 62165b9ed1
10 changed files with 54 additions and 38 deletions

View File

@ -1,3 +1,18 @@
2009-05-12 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
Throw DlAbortEx instead of FatalException during parsing options
because we don't want for aria2 to quit when bad option is passed
via XML-RPC.
* src/OptionHandlerException.cc
* src/OptionHandlerException.h
* src/OptionHandlerImpl.h
* src/OptionParser.cc
* src/OptionParser.h
* src/PStringSegment.cc
* src/ParameterizedStringParser.cc
* src/download_helper.cc
* test/ParameterizedStringParserTest.cc
2009-05-12 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net> 2009-05-12 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
Selecting files are now done in Selecting files are now done in

View File

@ -41,17 +41,17 @@ const std::string OptionHandlerException::MESSAGE
("Exception occurred while processing option %s:"); ("Exception occurred while processing option %s:");
OptionHandlerException::OptionHandlerException(const std::string& optName): OptionHandlerException::OptionHandlerException(const std::string& optName):
FatalException RecoverableException
(StringFormat(MESSAGE.c_str(), optName.c_str()).str()), _optName(optName) {} (StringFormat(MESSAGE.c_str(), optName.c_str()).str()), _optName(optName) {}
OptionHandlerException::OptionHandlerException(const std::string& optName, OptionHandlerException::OptionHandlerException(const std::string& optName,
const Exception& cause): const Exception& cause):
FatalException RecoverableException
(StringFormat(MESSAGE.c_str(), optName.c_str()).str(), cause), (StringFormat(MESSAGE.c_str(), optName.c_str()).str(), cause),
_optName(optName) {} _optName(optName) {}
OptionHandlerException::OptionHandlerException(const OptionHandlerException& e): OptionHandlerException::OptionHandlerException(const OptionHandlerException& e):
FatalException(e), _optName(e._optName) {} RecoverableException(e), _optName(e._optName) {}
OptionHandlerException::~OptionHandlerException() throw() {} OptionHandlerException::~OptionHandlerException() throw() {}

View File

@ -34,11 +34,11 @@
/* copyright --> */ /* copyright --> */
#ifndef _D_OPTION_HANDLER_EXCEPTION_H_ #ifndef _D_OPTION_HANDLER_EXCEPTION_H_
#define _D_OPTION_HANDLER_EXCEPTION_H_ #define _D_OPTION_HANDLER_EXCEPTION_H_
#include "FatalException.h" #include "RecoverableException.h"
namespace aria2 { namespace aria2 {
class OptionHandlerException:public FatalException { class OptionHandlerException:public RecoverableException {
private: private:
std::string _optName; std::string _optName;

View File

@ -46,7 +46,7 @@
#include "NameMatchOptionHandler.h" #include "NameMatchOptionHandler.h"
#include "Util.h" #include "Util.h"
#include "FatalException.h" #include "DlAbortEx.h"
#include "prefs.h" #include "prefs.h"
#include "Option.h" #include "Option.h"
#include "StringFormat.h" #include "StringFormat.h"
@ -127,7 +127,7 @@ public:
option.put(_optName, V_FALSE); option.put(_optName, V_FALSE);
} else { } else {
std::string msg = _optName+" "+_("must be either 'true' or 'false'."); std::string msg = _optName+" "+_("must be either 'true' or 'false'.");
throw FatalException(msg); throw DlAbortEx(msg);
} }
} }
@ -160,7 +160,7 @@ public:
int32_t v = seq.next(); int32_t v = seq.next();
if(v < _min || _max < v) { if(v < _min || _max < v) {
std::string msg = _optName+" "+_("must be between %s and %s."); std::string msg = _optName+" "+_("must be between %s and %s.");
throw FatalException throw DlAbortEx
(StringFormat(msg.c_str(), Util::itos(_min).c_str(), (StringFormat(msg.c_str(), Util::itos(_min).c_str(),
Util::itos(_max).c_str()).str()); Util::itos(_max).c_str()).str());
} }
@ -215,7 +215,7 @@ public:
} else { } else {
msg += _("must be a number."); msg += _("must be a number.");
} }
throw FatalException(msg); throw DlAbortEx(msg);
} }
} }
@ -281,7 +281,7 @@ public:
} else { } else {
msg += _("must be a number."); msg += _("must be a number.");
} }
throw FatalException(msg); throw DlAbortEx(msg);
} }
} }
@ -462,7 +462,7 @@ public:
msg += "'"+*itr+"' "; msg += "'"+*itr+"' ";
} }
} }
throw FatalException(msg); throw DlAbortEx(msg);
} else { } else {
option.put(_optName, optarg); option.put(_optName, optarg);
} }
@ -502,7 +502,7 @@ public:
int32_t port = Util::parseInt(proxy.second); int32_t port = Util::parseInt(proxy.second);
if(proxy.first.empty() || proxy.second.empty() || if(proxy.first.empty() || proxy.second.empty() ||
port <= 0 || 65535 < port) { port <= 0 || 65535 < port) {
throw FatalException(_("unrecognized proxy format")); throw DlAbortEx(_("unrecognized proxy format"));
} }
option.put(_optName, optarg); option.put(_optName, optarg);
setHostAndPort(option, proxy.first, port); setHostAndPort(option, proxy.first, port);
@ -545,7 +545,7 @@ public:
if(req.setUrl(url)) { if(req.setUrl(url)) {
option.put(_optName, url); option.put(_optName, url);
} else { } else {
throw FatalException(_("unrecognized proxy format")); throw DlAbortEx(_("unrecognized proxy format"));
} }
} }

View File

@ -142,7 +142,7 @@ void OptionParser::parseArg
op = findByShortName(c); op = findByShortName(c);
} }
if(op.isNull()) { if(op.isNull()) {
throw FatalException("Failed to parse command-line options."); throw DlAbortEx("Failed to parse command-line options.");
} }
out << op->getName() << "="; out << op->getName() << "=";
if(optarg) { if(optarg) {

View File

@ -65,7 +65,7 @@ public:
// Parses options in argv and writes option name and value to out in // Parses options in argv and writes option name and value to out in
// NAME=VALUE format. Non-option strings are stored in nonopts. // NAME=VALUE format. Non-option strings are stored in nonopts.
// Throws FatalException when an unrecognized option is found. // Throws Exception when an unrecognized option is found.
void parseArg(std::ostream& out, std::deque<std::string>& nonopts, void parseArg(std::ostream& out, std::deque<std::string>& nonopts,
int argc, char* const argv[]); int argc, char* const argv[]);

View File

@ -33,7 +33,6 @@
*/ */
/* copyright --> */ /* copyright --> */
#include "PStringSegment.h" #include "PStringSegment.h"
#include "FatalException.h"
#include "PStringVisitor.h" #include "PStringVisitor.h"
namespace aria2 { namespace aria2 {

View File

@ -33,7 +33,10 @@
*/ */
/* copyright --> */ /* copyright --> */
#include "ParameterizedStringParser.h" #include "ParameterizedStringParser.h"
#include "FatalException.h"
#include <utility>
#include "DlAbortEx.h"
#include "Util.h" #include "Util.h"
#include "PStringSegment.h" #include "PStringSegment.h"
#include "PStringSelect.h" #include "PStringSelect.h"
@ -41,7 +44,6 @@
#include "NumberDecorator.h" #include "NumberDecorator.h"
#include "FixedWidthNumberDecorator.h" #include "FixedWidthNumberDecorator.h"
#include "AlphaNumberDecorator.h" #include "AlphaNumberDecorator.h"
#include <utility>
namespace aria2 { namespace aria2 {
@ -86,12 +88,12 @@ PStringDatumHandle ParameterizedStringParser::createSelect(const std::string& sr
++offset; ++offset;
std::string::size_type rightParenIndex = src.find("}", offset); std::string::size_type rightParenIndex = src.find("}", offset);
if(rightParenIndex == std::string::npos) { if(rightParenIndex == std::string::npos) {
throw FatalException("Missing '}' in the parameterized string."); throw DlAbortEx("Missing '}' in the parameterized string.");
} }
std::deque<std::string> values; std::deque<std::string> values;
Util::slice(values, src.substr(offset, rightParenIndex-offset), ',', true); Util::slice(values, src.substr(offset, rightParenIndex-offset), ',', true);
if(values.empty()) { if(values.empty()) {
throw FatalException("Empty {} is not allowed."); throw DlAbortEx("Empty {} is not allowed.");
} }
offset = rightParenIndex+1; offset = rightParenIndex+1;
PStringDatumHandle next = diggPString(src, offset); PStringDatumHandle next = diggPString(src, offset);
@ -104,7 +106,7 @@ PStringDatumHandle ParameterizedStringParser::createLoop(const std::string& src,
++offset; ++offset;
std::string::size_type rightParenIndex = src.find("]", offset); std::string::size_type rightParenIndex = src.find("]", offset);
if(rightParenIndex == std::string::npos) { if(rightParenIndex == std::string::npos) {
throw FatalException("Missing ']' in the parameterized string."); throw DlAbortEx("Missing ']' in the parameterized string.");
} }
std::string loopStr = src.substr(offset, rightParenIndex-offset); std::string loopStr = src.substr(offset, rightParenIndex-offset);
offset = rightParenIndex+1; offset = rightParenIndex+1;
@ -116,13 +118,13 @@ PStringDatumHandle ParameterizedStringParser::createLoop(const std::string& src,
if(Util::isNumber(stepStr)) { if(Util::isNumber(stepStr)) {
step = Util::parseUInt(stepStr); step = Util::parseUInt(stepStr);
} else { } else {
throw FatalException("A step count must be a positive number."); throw DlAbortEx("A step count must be a positive number.");
} }
loopStr.erase(colonIndex); loopStr.erase(colonIndex);
} }
std::pair<std::string, std::string> range = Util::split(loopStr, "-"); std::pair<std::string, std::string> range = Util::split(loopStr, "-");
if(range.first.empty() || range.second.empty()) { if(range.first.empty() || range.second.empty()) {
throw FatalException("Loop range missing."); throw DlAbortEx("Loop range missing.");
} }
NumberDecoratorHandle nd; NumberDecoratorHandle nd;
unsigned int start; unsigned int start;
@ -140,7 +142,7 @@ PStringDatumHandle ParameterizedStringParser::createLoop(const std::string& src,
start = Util::alphaToNum(range.first); start = Util::alphaToNum(range.first);
end = Util::alphaToNum(range.second); end = Util::alphaToNum(range.second);
} else { } else {
throw FatalException("Invalid loop range."); throw DlAbortEx("Invalid loop range.");
} }
PStringDatumHandle next(diggPString(src, offset)); PStringDatumHandle next(diggPString(src, offset));

View File

@ -50,7 +50,7 @@
#include "UriListParser.h" #include "UriListParser.h"
#include "SingleFileDownloadContext.h" #include "SingleFileDownloadContext.h"
#include "RecoverableException.h" #include "RecoverableException.h"
#include "FatalException.h" #include "DlAbortEx.h"
#include "message.h" #include "message.h"
#include "StringFormat.h" #include "StringFormat.h"
#include "DefaultBtContext.h" #include "DefaultBtContext.h"
@ -258,7 +258,7 @@ void createRequestGroupForMetalink
option->get(PREF_METALINK_FILE), option->get(PREF_METALINK_FILE),
option); option);
if(result.empty()) { if(result.empty()) {
throw FatalException(MSG_NO_FILES_TO_DOWNLOAD); throw DlAbortEx(MSG_NO_FILES_TO_DOWNLOAD);
} }
} }
#endif // ENABLE_METALINK #endif // ENABLE_METALINK
@ -397,7 +397,7 @@ void createRequestGroupForUriList
createRequestGroupForUriList(result, option, std::cin); createRequestGroupForUriList(result, option, std::cin);
} else { } else {
if(!File(option->get(PREF_INPUT_FILE)).isFile()) { if(!File(option->get(PREF_INPUT_FILE)).isFile()) {
throw FatalException throw DlAbortEx
(StringFormat(EX_FILE_OPEN, option->get(PREF_INPUT_FILE).c_str(), (StringFormat(EX_FILE_OPEN, option->get(PREF_INPUT_FILE).c_str(),
"No such file").str()); "No such file").str());
} }

View File

@ -8,7 +8,7 @@
#include "PStringSelect.h" #include "PStringSelect.h"
#include "PStringSegment.h" #include "PStringSegment.h"
#include "PStringNumLoop.h" #include "PStringNumLoop.h"
#include "FatalException.h" #include "DlAbortEx.h"
namespace aria2 { namespace aria2 {
@ -74,7 +74,7 @@ void ParameterizedStringParserTest::testParse_select_empty()
try { try {
SharedHandle<PStringDatum> ls = ParameterizedStringParser().parse("{}"); SharedHandle<PStringDatum> ls = ParameterizedStringParser().parse("{}");
CPPUNIT_FAIL("exception must be thrown."); CPPUNIT_FAIL("exception must be thrown.");
} catch(FatalException& e) { } catch(DlAbortEx& e) {
std::cerr << e.stackTrace() << std::endl; std::cerr << e.stackTrace() << std::endl;
} catch(...) { } catch(...) {
CPPUNIT_FAIL("unexpected exception thrown."); CPPUNIT_FAIL("unexpected exception thrown.");
@ -86,7 +86,7 @@ void ParameterizedStringParserTest::testParse_select_missingParen()
try { try {
SharedHandle<PStringDatum> ls = ParameterizedStringParser().parse("{alpha"); SharedHandle<PStringDatum> ls = ParameterizedStringParser().parse("{alpha");
CPPUNIT_FAIL("exception must be thrown."); CPPUNIT_FAIL("exception must be thrown.");
} catch(FatalException& e) { } catch(DlAbortEx& e) {
std::cerr << e.stackTrace() << std::endl; std::cerr << e.stackTrace() << std::endl;
} catch(...) { } catch(...) {
CPPUNIT_FAIL("unexpected exception was thrown."); CPPUNIT_FAIL("unexpected exception was thrown.");
@ -141,7 +141,7 @@ void ParameterizedStringParserTest::testParse_loop_empty()
try { try {
SharedHandle<PStringDatum> ls = ParameterizedStringParser().parse("[]"); SharedHandle<PStringDatum> ls = ParameterizedStringParser().parse("[]");
CPPUNIT_FAIL("exception must be thrown."); CPPUNIT_FAIL("exception must be thrown.");
} catch(FatalException& e) { } catch(DlAbortEx& e) {
std::cerr << e.stackTrace() << std::endl; std::cerr << e.stackTrace() << std::endl;
} catch(...) { } catch(...) {
CPPUNIT_FAIL("unexpected exception was thrown."); CPPUNIT_FAIL("unexpected exception was thrown.");
@ -153,7 +153,7 @@ void ParameterizedStringParserTest::testParse_loop_missingParen()
try { try {
SharedHandle<PStringDatum> ls = ParameterizedStringParser().parse("["); SharedHandle<PStringDatum> ls = ParameterizedStringParser().parse("[");
CPPUNIT_FAIL("exception must be thrown."); CPPUNIT_FAIL("exception must be thrown.");
} catch(FatalException& e) { } catch(DlAbortEx& e) {
std::cerr << e.stackTrace() << std::endl; std::cerr << e.stackTrace() << std::endl;
} catch(...) { } catch(...) {
CPPUNIT_FAIL("unexpected exception was thrown."); CPPUNIT_FAIL("unexpected exception was thrown.");
@ -165,7 +165,7 @@ void ParameterizedStringParserTest::testParse_loop_missingStep()
try { try {
SharedHandle<PStringDatum> ls = ParameterizedStringParser().parse("[1-10:]"); SharedHandle<PStringDatum> ls = ParameterizedStringParser().parse("[1-10:]");
CPPUNIT_FAIL("exception must be thrown."); CPPUNIT_FAIL("exception must be thrown.");
} catch(FatalException& e) { } catch(DlAbortEx& e) {
std::cerr << e.stackTrace() << std::endl; std::cerr << e.stackTrace() << std::endl;
} catch(...) { } catch(...) {
CPPUNIT_FAIL("unexpected exception was thrown."); CPPUNIT_FAIL("unexpected exception was thrown.");
@ -177,7 +177,7 @@ void ParameterizedStringParserTest::testParse_loop_missingRange()
try { try {
SharedHandle<PStringDatum> ls = ParameterizedStringParser().parse("[1-]"); SharedHandle<PStringDatum> ls = ParameterizedStringParser().parse("[1-]");
CPPUNIT_FAIL("exception must be thrown."); CPPUNIT_FAIL("exception must be thrown.");
} catch(FatalException& e) { } catch(DlAbortEx& e) {
std::cerr << e.stackTrace() << std::endl; std::cerr << e.stackTrace() << std::endl;
} catch(...) { } catch(...) {
CPPUNIT_FAIL("unexpected exception was thrown."); CPPUNIT_FAIL("unexpected exception was thrown.");
@ -200,10 +200,10 @@ void ParameterizedStringParserTest::testParse_loop_mixedChar()
try { try {
ParameterizedStringParser().parse("[1-z:2]"); ParameterizedStringParser().parse("[1-z:2]");
CPPUNIT_FAIL("exception must be thrown."); CPPUNIT_FAIL("exception must be thrown.");
} catch(FatalException& e) { } catch(DlAbortEx& e) {
std::cerr << e.stackTrace() << std::endl; std::cerr << e.stackTrace() << std::endl;
} catch(...) { } catch(...) {
CPPUNIT_FAIL("FatalException must be thrown."); CPPUNIT_FAIL("DlAbortEx must be thrown.");
} }
} }
@ -212,10 +212,10 @@ void ParameterizedStringParserTest::testParse_loop_mixedCase()
try { try {
ParameterizedStringParser().parse("[a-Z:2]"); ParameterizedStringParser().parse("[a-Z:2]");
CPPUNIT_FAIL("exception must be thrown."); CPPUNIT_FAIL("exception must be thrown.");
} catch(FatalException& e) { } catch(DlAbortEx& e) {
std::cerr << e.stackTrace() << std::endl; std::cerr << e.stackTrace() << std::endl;
} catch(...) { } catch(...) {
CPPUNIT_FAIL("FatalException must be thrown."); CPPUNIT_FAIL("DlAbortEx must be thrown.");
} }
} }