From 374955da0df726bc643d6466b97ece8b534ad3eb Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Sun, 25 Sep 2011 23:36:00 +0900 Subject: [PATCH] Throw exception if parameter has wrong type in RPC method. Formally, depending on the method implementation, this kind of error is just ignored and parameter is skipped. Now aria2 responds error for these cases. The required parameter checking is also reworked along with this change. --- src/RpcMethodImpl.cc | 194 +++++++++++++++++++++---------------------- src/RpcMethodImpl.h | 49 +++++++---- src/RpcRequest.cc | 36 -------- src/RpcRequest.h | 8 -- 4 files changed, 128 insertions(+), 159 deletions(-) diff --git a/src/RpcMethodImpl.cc b/src/RpcMethodImpl.cc index 7834da16..c7c82891 100644 --- a/src/RpcMethodImpl.cc +++ b/src/RpcMethodImpl.cc @@ -179,33 +179,24 @@ findRequestGroup(const SharedHandle& rgman, a2_gid_t gid) } // namespace namespace { -void getPosParam(const RpcRequest& req, size_t posParamIndex, - bool& posGiven, size_t& pos) +bool checkPosParam(const Integer* posParam) { - const Integer* p = req.getIntegerParam(posParamIndex); - if(p) { - if(p->i() >= 0) { - pos = p->i(); - posGiven = true; - return; + if(posParam) { + if(posParam->i() >= 0) { + return true; } else { throw DL_ABORT_EX("Position must be greater than or equal to 0."); } } - posGiven = false; -} + return false; +} } // namespace namespace { -a2_gid_t getRequiredGidParam -(const RpcRequest& req, size_t posParamIndex) +a2_gid_t str2Gid(const String* str) { - const String* gidParam = req.getStringParam(posParamIndex); - if(gidParam) { - return util::parseLLInt(gidParam->s()); - } else { - throw DL_ABORT_EX(MSG_GID_NOT_PROVIDED); - } + assert(str); + return util::parseLLInt(str->s()); } } // namespace @@ -228,18 +219,21 @@ void extractUris(OutputIterator out, const List* src) SharedHandle AddUriRpcMethod::process (const RpcRequest& req, DownloadEngine* e) { + const List* urisParam = checkRequiredParam(req, 0); + const Dict* optsParam = checkParam(req, 1); + const Integer* posParam = checkParam(req, 2); + std::vector uris; - extractUris(std::back_inserter(uris), req.getListParam(0)); + extractUris(std::back_inserter(uris), urisParam); if(uris.empty()) { throw DL_ABORT_EX("URI is not provided."); } SharedHandle