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

Return fault structure for error. FailXmlRpcMethod is renamed as
	NoSuchMethodXmlRpcMethod. pieceLength and numPieces are returned
	in non-torrent download. Return single param value.
	* src/XmlRpcMethod.cc
	* src/XmlRpcMethodFactory.cc
	* src/XmlRpcMethodImpl.cc
	* src/XmlRpcMethodImpl.h
	* test/XmlRpcMethodTest.cc
pull/1/head
Tatsuhiro Tsujikawa 2009-05-09 04:55:53 +00:00
parent 7e7f809339
commit 4e18c92371
6 changed files with 78 additions and 20 deletions

View File

@ -1,3 +1,14 @@
2009-05-09 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
Return fault structure for error. FailXmlRpcMethod is renamed as
NoSuchMethodXmlRpcMethod. pieceLength and numPieces are returned
in non-torrent download. Return single param value.
* src/XmlRpcMethod.cc
* src/XmlRpcMethodFactory.cc
* src/XmlRpcMethodImpl.cc
* src/XmlRpcMethodImpl.h
* test/XmlRpcMethodTest.cc
2009-05-09 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net> 2009-05-09 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
Set DownloadResult::IN_PROGRESS for downloads removed by xml-rpc Set DownloadResult::IN_PROGRESS for downloads removed by xml-rpc

View File

@ -59,9 +59,9 @@ XmlRpcMethod::XmlRpcMethod():
static BDE createErrorResponse(const Exception& e) static BDE createErrorResponse(const Exception& e)
{ {
BDE params = BDE::list(); BDE params = BDE::dict();
params << BDE("ERROR"); params["faultCode"] = BDE(1);
params << BDE(e.what()); params["faultString"] = BDE(e.what());
return params; return params;
} }
@ -97,6 +97,8 @@ static void encodeValue(const BDE& value, std::ostream& o)
o << "<value>"; o << "<value>";
if(value.isString()) { if(value.isString()) {
o << "<string>" << value.s() << "</string>"; o << "<string>" << value.s() << "</string>";
} else if(value.isInteger()) {
o << "<int>" << value.i() << "</int>";
} else if(value.isList()) { } else if(value.isList()) {
encodeArray(value.listBegin(), value.listEnd(), o); encodeArray(value.listBegin(), value.listEnd(), o);
} else if(value.isDict()) { } else if(value.isDict()) {
@ -128,6 +130,23 @@ static std::string encodeXml(const BDE& params)
return o.str(); return o.str();
} }
static void encodeFault(const BDE& faultValue, std::ostream& o)
{
o << "<fault>";
encodeValue(faultValue, o);
o << "</fault>";
}
static std::string encodeErrorXml(const BDE& faultValue)
{
assert(faultValue.isDict());
std::stringstream o;
o << "<?xml version=\"1.0\"?>" << "<methodResponse>";
encodeFault(faultValue, o);
o << "</methodResponse>";
return o.str();
}
std::string XmlRpcMethod::execute(const XmlRpcRequest& req, DownloadEngine* e) std::string XmlRpcMethod::execute(const XmlRpcRequest& req, DownloadEngine* e)
{ {
try { try {
@ -135,7 +154,7 @@ std::string XmlRpcMethod::execute(const XmlRpcRequest& req, DownloadEngine* e)
return encodeXml(retparams); return encodeXml(retparams);
} catch(RecoverableException& e) { } catch(RecoverableException& e) {
_logger->debug(EX_EXCEPTION_CAUGHT, e); _logger->debug(EX_EXCEPTION_CAUGHT, e);
return encodeXml(createErrorResponse(e)); return encodeErrorXml(createErrorResponse(e));
} }
} }

View File

@ -53,7 +53,7 @@ XmlRpcMethodFactory::create(const std::string& methodName)
} else if(methodName == "aria2.tellActiveStatus") { } else if(methodName == "aria2.tellActiveStatus") {
return SharedHandle<XmlRpcMethod>(new TellActiveStatusXmlRpcMethod()); return SharedHandle<XmlRpcMethod>(new TellActiveStatusXmlRpcMethod());
} else { } else {
return SharedHandle<XmlRpcMethod>(new FailXmlRpcMethod()); return SharedHandle<XmlRpcMethod>(new NoSuchMethodXmlRpcMethod());
} }
} }

View File

@ -67,7 +67,6 @@ namespace xmlrpc {
static BDE createGIDResponse(int32_t gid) static BDE createGIDResponse(int32_t gid)
{ {
BDE resParams = BDE::list(); BDE resParams = BDE::list();
resParams << BDE("OK");
resParams << BDE(Util::itos(gid)); resParams << BDE(Util::itos(gid));
return resParams; return resParams;
} }
@ -152,10 +151,7 @@ BDE RemoveXmlRpcMethod::process(const XmlRpcRequest& req, DownloadEngine* e)
group->setHaltRequested(true, RequestGroup::USER_REQUEST); group->setHaltRequested(true, RequestGroup::USER_REQUEST);
BDE resParams = BDE::list(); return createGIDResponse(gid);
resParams << BDE("OK");
resParams << BDE(Util::itos(gid));
return resParams;
} }
BDE TellActiveStatusXmlRpcMethod::process BDE TellActiveStatusXmlRpcMethod::process
@ -192,15 +188,16 @@ BDE TellActiveStatusXmlRpcMethod::process
entryDict["files"] = files; entryDict["files"] = files;
} }
} }
entryDict["pieceLength"] =
BDE(Util::uitos((*i)->getDownloadContext()->getPieceLength()));
entryDict["numPieces"] =
BDE(Util::uitos((*i)->getDownloadContext()->getNumPieces()));
SharedHandle<BtContext> btctx = SharedHandle<BtContext> btctx =
dynamic_pointer_cast<BtContext>((*i)->getDownloadContext()); dynamic_pointer_cast<BtContext>((*i)->getDownloadContext());
if(!btctx.isNull()) { if(!btctx.isNull()) {
entryDict["infoHash"] = BDE(btctx->getInfoHashAsString()); entryDict["infoHash"] = BDE(btctx->getInfoHashAsString());
entryDict["pieceLength"] =
BDE(Util::uitos((*i)->getDownloadContext()->getPieceLength()));
entryDict["numPieces"] =
BDE(Util::uitos((*i)->getDownloadContext()->getNumPieces()));
SharedHandle<BtRegistry> btreg = e->getBtRegistry(); SharedHandle<BtRegistry> btreg = e->getBtRegistry();
@ -228,14 +225,17 @@ BDE TellActiveStatusXmlRpcMethod::process
res << entryDict; res << entryDict;
} }
return res;
BDE resParams = BDE::list();
resParams << res;
return resParams;
} }
BDE FailXmlRpcMethod::process(const XmlRpcRequest& req, DownloadEngine* e) BDE NoSuchMethodXmlRpcMethod::process
(const XmlRpcRequest& req, DownloadEngine* e)
{ {
throw DlAbortEx throw DlAbortEx
(StringFormat("Method %s was not recognized.", (StringFormat("No such method: %s", req._methodName.c_str()).str());
req._methodName.c_str()).str());
} }
} // namespace xmlrpc } // namespace xmlrpc

View File

@ -61,7 +61,7 @@ protected:
virtual BDE process(const XmlRpcRequest& req, DownloadEngine* e); virtual BDE process(const XmlRpcRequest& req, DownloadEngine* e);
}; };
class FailXmlRpcMethod:public XmlRpcMethod { class NoSuchMethodXmlRpcMethod:public XmlRpcMethod {
protected: protected:
virtual BDE process(const XmlRpcRequest& req, DownloadEngine* e); virtual BDE process(const XmlRpcRequest& req, DownloadEngine* e);
}; };

View File

@ -22,6 +22,7 @@ class XmlRpcMethodTest:public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(XmlRpcMethodTest); CPPUNIT_TEST_SUITE(XmlRpcMethodTest);
CPPUNIT_TEST(testAddURI); CPPUNIT_TEST(testAddURI);
CPPUNIT_TEST(testNoSuchMethod);
CPPUNIT_TEST_SUITE_END(); CPPUNIT_TEST_SUITE_END();
private: private:
SharedHandle<DownloadEngine> _e; SharedHandle<DownloadEngine> _e;
@ -40,6 +41,7 @@ public:
void tearDown() {} void tearDown() {}
void testAddURI(); void testAddURI();
void testNoSuchMethod();
}; };
@ -57,7 +59,33 @@ void XmlRpcMethodTest::testAddURI()
CPPUNIT_ASSERT_EQUAL((size_t)1, rgs.size()); CPPUNIT_ASSERT_EQUAL((size_t)1, rgs.size());
CPPUNIT_ASSERT_EQUAL(std::string("http://localhost/"), CPPUNIT_ASSERT_EQUAL(std::string("http://localhost/"),
rgs.front()->getRemainingUris().front()); rgs.front()->getRemainingUris().front());
CPPUNIT_ASSERT(res.find("OK") != std::string::npos); }
void XmlRpcMethodTest::testNoSuchMethod()
{
NoSuchMethodXmlRpcMethod m;
XmlRpcRequest req("make.hamburger", BDE::none);
std::string res = m.execute(req, 0);
CPPUNIT_ASSERT_EQUAL
(std::string("<?xml version=\"1.0\"?>"
"<methodResponse>"
"<fault>"
"<value>"
"<struct>"
"<member>"
"<name>faultCode</name><value><int>1</int></value>"
"</member>"
"<member>"
"<name>faultString</name>"
"<value>"
"<string>No such method: make.hamburger</string>"
"</value>"
"</member>"
"</struct>"
"</value>"
"</fault>"
"</methodResponse>"),
res);
} }
} // namespace xmlrpc } // namespace xmlrpc