mirror of https://github.com/aria2/aria2
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.pull/2/head
parent
fee9b95db3
commit
374955da0d
|
@ -179,33 +179,24 @@ findRequestGroup(const SharedHandle<RequestGroupMan>& rgman, a2_gid_t gid)
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
void getPosParam(const RpcRequest& req, size_t posParamIndex,
|
bool checkPosParam(const Integer* posParam)
|
||||||
bool& posGiven, size_t& pos)
|
|
||||||
{
|
{
|
||||||
const Integer* p = req.getIntegerParam(posParamIndex);
|
if(posParam) {
|
||||||
if(p) {
|
if(posParam->i() >= 0) {
|
||||||
if(p->i() >= 0) {
|
return true;
|
||||||
pos = p->i();
|
|
||||||
posGiven = true;
|
|
||||||
return;
|
|
||||||
} else {
|
} else {
|
||||||
throw DL_ABORT_EX("Position must be greater than or equal to 0.");
|
throw DL_ABORT_EX("Position must be greater than or equal to 0.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
posGiven = false;
|
return false;
|
||||||
}
|
}
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
a2_gid_t getRequiredGidParam
|
a2_gid_t str2Gid(const String* str)
|
||||||
(const RpcRequest& req, size_t posParamIndex)
|
|
||||||
{
|
{
|
||||||
const String* gidParam = req.getStringParam(posParamIndex);
|
assert(str);
|
||||||
if(gidParam) {
|
return util::parseLLInt(str->s());
|
||||||
return util::parseLLInt(gidParam->s());
|
|
||||||
} else {
|
|
||||||
throw DL_ABORT_EX(MSG_GID_NOT_PROVIDED);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
|
@ -228,18 +219,21 @@ void extractUris(OutputIterator out, const List* src)
|
||||||
SharedHandle<ValueBase> AddUriRpcMethod::process
|
SharedHandle<ValueBase> AddUriRpcMethod::process
|
||||||
(const RpcRequest& req, DownloadEngine* e)
|
(const RpcRequest& req, DownloadEngine* e)
|
||||||
{
|
{
|
||||||
|
const List* urisParam = checkRequiredParam<List>(req, 0);
|
||||||
|
const Dict* optsParam = checkParam<Dict>(req, 1);
|
||||||
|
const Integer* posParam = checkParam<Integer>(req, 2);
|
||||||
|
|
||||||
std::vector<std::string> uris;
|
std::vector<std::string> uris;
|
||||||
extractUris(std::back_inserter(uris), req.getListParam(0));
|
extractUris(std::back_inserter(uris), urisParam);
|
||||||
if(uris.empty()) {
|
if(uris.empty()) {
|
||||||
throw DL_ABORT_EX("URI is not provided.");
|
throw DL_ABORT_EX("URI is not provided.");
|
||||||
}
|
}
|
||||||
|
|
||||||
SharedHandle<Option> requestOption(new Option(*e->getOption()));
|
SharedHandle<Option> requestOption(new Option(*e->getOption()));
|
||||||
gatherRequestOption(requestOption, req.getDictParam(1));
|
gatherRequestOption(requestOption, optsParam);
|
||||||
|
|
||||||
size_t pos = 0;
|
bool posGiven = checkPosParam(posParam);
|
||||||
bool posGiven = false;
|
size_t pos = posGiven ? posParam->i() : 0;
|
||||||
getPosParam(req, 2, posGiven, pos);
|
|
||||||
|
|
||||||
std::vector<SharedHandle<RequestGroup> > result;
|
std::vector<SharedHandle<RequestGroup> > result;
|
||||||
createRequestGroupForUri(result, requestOption, uris,
|
createRequestGroupForUri(result, requestOption, uris,
|
||||||
|
@ -269,24 +263,24 @@ std::string getHexSha1(const std::string& s)
|
||||||
SharedHandle<ValueBase> AddTorrentRpcMethod::process
|
SharedHandle<ValueBase> AddTorrentRpcMethod::process
|
||||||
(const RpcRequest& req, DownloadEngine* e)
|
(const RpcRequest& req, DownloadEngine* e)
|
||||||
{
|
{
|
||||||
const String* torrentParam = req.getStringParam(0);
|
const String* torrentParam = checkRequiredParam<String>(req, 0);
|
||||||
if(!torrentParam) {
|
const List* urisParam = checkParam<List>(req, 1);
|
||||||
throw DL_ABORT_EX("Torrent data is not provided.");
|
const Dict* optsParam = checkParam<Dict>(req, 2);
|
||||||
}
|
const Integer* posParam = checkParam<Integer>(req, 3);
|
||||||
|
|
||||||
SharedHandle<String> tempTorrentParam;
|
SharedHandle<String> tempTorrentParam;
|
||||||
if(req.jsonRpc) {
|
if(req.jsonRpc) {
|
||||||
tempTorrentParam = String::g(Base64::decode(torrentParam->s()));
|
tempTorrentParam = String::g(Base64::decode(torrentParam->s()));
|
||||||
torrentParam = tempTorrentParam.get();
|
torrentParam = tempTorrentParam.get();
|
||||||
}
|
}
|
||||||
std::vector<std::string> uris;
|
std::vector<std::string> uris;
|
||||||
extractUris(std::back_inserter(uris), req.getListParam(1));
|
extractUris(std::back_inserter(uris), urisParam);
|
||||||
|
|
||||||
SharedHandle<Option> requestOption(new Option(*e->getOption()));
|
SharedHandle<Option> requestOption(new Option(*e->getOption()));
|
||||||
gatherRequestOption(requestOption, req.getDictParam(2));
|
gatherRequestOption(requestOption, optsParam);
|
||||||
|
|
||||||
size_t pos = 0;
|
bool posGiven = checkPosParam(posParam);
|
||||||
bool posGiven = false;
|
size_t pos = posGiven ? posParam->i() : 0;
|
||||||
getPosParam(req, 3, posGiven, pos);
|
|
||||||
|
|
||||||
std::string filename = util::applyDir
|
std::string filename = util::applyDir
|
||||||
(requestOption->get(PREF_DIR), getHexSha1(torrentParam->s())+".torrent");
|
(requestOption->get(PREF_DIR), getHexSha1(torrentParam->s())+".torrent");
|
||||||
|
@ -316,21 +310,20 @@ SharedHandle<ValueBase> AddTorrentRpcMethod::process
|
||||||
SharedHandle<ValueBase> AddMetalinkRpcMethod::process
|
SharedHandle<ValueBase> AddMetalinkRpcMethod::process
|
||||||
(const RpcRequest& req, DownloadEngine* e)
|
(const RpcRequest& req, DownloadEngine* e)
|
||||||
{
|
{
|
||||||
const String* metalinkParam = req.getStringParam(0);
|
const String* metalinkParam = checkRequiredParam<String>(req, 0);
|
||||||
if(!metalinkParam) {
|
const Dict* optsParam = checkParam<Dict>(req, 1);
|
||||||
throw DL_ABORT_EX("Metalink data is not provided.");
|
const Integer* posParam = checkParam<Integer>(req, 2);
|
||||||
}
|
|
||||||
SharedHandle<String> tempMetalinkParam;
|
SharedHandle<String> tempMetalinkParam;
|
||||||
if(req.jsonRpc) {
|
if(req.jsonRpc) {
|
||||||
tempMetalinkParam = String::g(Base64::decode(metalinkParam->s()));
|
tempMetalinkParam = String::g(Base64::decode(metalinkParam->s()));
|
||||||
metalinkParam = tempMetalinkParam.get();
|
metalinkParam = tempMetalinkParam.get();
|
||||||
}
|
}
|
||||||
SharedHandle<Option> requestOption(new Option(*e->getOption()));
|
SharedHandle<Option> requestOption(new Option(*e->getOption()));
|
||||||
gatherRequestOption(requestOption, req.getDictParam(1));
|
gatherRequestOption(requestOption, optsParam);
|
||||||
|
|
||||||
size_t pos = 0;
|
bool posGiven = checkPosParam(posParam);
|
||||||
bool posGiven = false;
|
size_t pos = posGiven ? posParam->i() : 0;
|
||||||
getPosParam(req, 2, posGiven, pos);
|
|
||||||
|
|
||||||
std::vector<SharedHandle<RequestGroup> > result;
|
std::vector<SharedHandle<RequestGroup> > result;
|
||||||
#ifdef ENABLE_MESSAGE_DIGEST
|
#ifdef ENABLE_MESSAGE_DIGEST
|
||||||
|
@ -375,8 +368,9 @@ namespace {
|
||||||
SharedHandle<ValueBase> removeDownload
|
SharedHandle<ValueBase> removeDownload
|
||||||
(const RpcRequest& req, DownloadEngine* e, bool forceRemove)
|
(const RpcRequest& req, DownloadEngine* e, bool forceRemove)
|
||||||
{
|
{
|
||||||
a2_gid_t gid = getRequiredGidParam(req, 0);
|
const String* gidParam = checkRequiredParam<String>(req, 0);
|
||||||
|
|
||||||
|
a2_gid_t gid = str2Gid(gidParam);
|
||||||
SharedHandle<RequestGroup> group =
|
SharedHandle<RequestGroup> group =
|
||||||
e->getRequestGroupMan()->findRequestGroup(gid);
|
e->getRequestGroupMan()->findRequestGroup(gid);
|
||||||
if(!group) {
|
if(!group) {
|
||||||
|
@ -447,8 +441,9 @@ namespace {
|
||||||
SharedHandle<ValueBase> pauseDownload
|
SharedHandle<ValueBase> pauseDownload
|
||||||
(const RpcRequest& req, DownloadEngine* e, bool forcePause)
|
(const RpcRequest& req, DownloadEngine* e, bool forcePause)
|
||||||
{
|
{
|
||||||
a2_gid_t gid = getRequiredGidParam(req, 0);
|
const String* gidParam = checkRequiredParam<String>(req, 0);
|
||||||
|
|
||||||
|
a2_gid_t gid = str2Gid(gidParam);
|
||||||
bool reserved = false;
|
bool reserved = false;
|
||||||
SharedHandle<RequestGroup> group =
|
SharedHandle<RequestGroup> group =
|
||||||
e->getRequestGroupMan()->findRequestGroup(gid);
|
e->getRequestGroupMan()->findRequestGroup(gid);
|
||||||
|
@ -520,7 +515,9 @@ SharedHandle<ValueBase> ForcePauseAllRpcMethod::process
|
||||||
SharedHandle<ValueBase> UnpauseRpcMethod::process
|
SharedHandle<ValueBase> UnpauseRpcMethod::process
|
||||||
(const RpcRequest& req, DownloadEngine* e)
|
(const RpcRequest& req, DownloadEngine* e)
|
||||||
{
|
{
|
||||||
a2_gid_t gid = getRequiredGidParam(req, 0);
|
const String* gidParam = checkRequiredParam<String>(req, 0);
|
||||||
|
|
||||||
|
a2_gid_t gid = str2Gid(gidParam);
|
||||||
SharedHandle<RequestGroup> group =
|
SharedHandle<RequestGroup> group =
|
||||||
e->getRequestGroupMan()->findReservedGroup(gid);
|
e->getRequestGroupMan()->findReservedGroup(gid);
|
||||||
if(!group || !group->isPauseRequested()) {
|
if(!group || !group->isPauseRequested()) {
|
||||||
|
@ -919,7 +916,9 @@ void gatherStoppedDownload
|
||||||
SharedHandle<ValueBase> GetFilesRpcMethod::process
|
SharedHandle<ValueBase> GetFilesRpcMethod::process
|
||||||
(const RpcRequest& req, DownloadEngine* e)
|
(const RpcRequest& req, DownloadEngine* e)
|
||||||
{
|
{
|
||||||
a2_gid_t gid = getRequiredGidParam(req, 0);
|
const String* gidParam = checkRequiredParam<String>(req, 0);
|
||||||
|
|
||||||
|
a2_gid_t gid = str2Gid(gidParam);
|
||||||
SharedHandle<List> files = List::g();
|
SharedHandle<List> files = List::g();
|
||||||
SharedHandle<RequestGroup> group =
|
SharedHandle<RequestGroup> group =
|
||||||
findRequestGroup(e->getRequestGroupMan(), gid);
|
findRequestGroup(e->getRequestGroupMan(), gid);
|
||||||
|
@ -950,7 +949,9 @@ SharedHandle<ValueBase> GetFilesRpcMethod::process
|
||||||
SharedHandle<ValueBase> GetUrisRpcMethod::process
|
SharedHandle<ValueBase> GetUrisRpcMethod::process
|
||||||
(const RpcRequest& req, DownloadEngine* e)
|
(const RpcRequest& req, DownloadEngine* e)
|
||||||
{
|
{
|
||||||
a2_gid_t gid = getRequiredGidParam(req, 0);
|
const String* gidParam = checkRequiredParam<String>(req, 0);
|
||||||
|
|
||||||
|
a2_gid_t gid = str2Gid(gidParam);
|
||||||
SharedHandle<RequestGroup> group =
|
SharedHandle<RequestGroup> group =
|
||||||
findRequestGroup(e->getRequestGroupMan(), gid);
|
findRequestGroup(e->getRequestGroupMan(), gid);
|
||||||
if(!group) {
|
if(!group) {
|
||||||
|
@ -970,8 +971,9 @@ SharedHandle<ValueBase> GetUrisRpcMethod::process
|
||||||
SharedHandle<ValueBase> GetPeersRpcMethod::process
|
SharedHandle<ValueBase> GetPeersRpcMethod::process
|
||||||
(const RpcRequest& req, DownloadEngine* e)
|
(const RpcRequest& req, DownloadEngine* e)
|
||||||
{
|
{
|
||||||
a2_gid_t gid = getRequiredGidParam(req, 0);
|
const String* gidParam = checkRequiredParam<String>(req, 0);
|
||||||
|
|
||||||
|
a2_gid_t gid = str2Gid(gidParam);
|
||||||
SharedHandle<RequestGroup> group =
|
SharedHandle<RequestGroup> group =
|
||||||
findRequestGroup(e->getRequestGroupMan(), gid);
|
findRequestGroup(e->getRequestGroupMan(), gid);
|
||||||
if(!group) {
|
if(!group) {
|
||||||
|
@ -992,9 +994,10 @@ SharedHandle<ValueBase> GetPeersRpcMethod::process
|
||||||
SharedHandle<ValueBase> TellStatusRpcMethod::process
|
SharedHandle<ValueBase> TellStatusRpcMethod::process
|
||||||
(const RpcRequest& req, DownloadEngine* e)
|
(const RpcRequest& req, DownloadEngine* e)
|
||||||
{
|
{
|
||||||
a2_gid_t gid = getRequiredGidParam(req, 0);
|
const String* gidParam = checkRequiredParam<String>(req, 0);
|
||||||
|
const List* keysParam = checkParam<List>(req, 1);
|
||||||
const List* keysParam = req.getListParam(1);
|
|
||||||
|
a2_gid_t gid = str2Gid(gidParam);
|
||||||
std::vector<std::string> keys;
|
std::vector<std::string> keys;
|
||||||
toStringList(std::back_inserter(keys), keysParam);
|
toStringList(std::back_inserter(keys), keysParam);
|
||||||
|
|
||||||
|
@ -1035,7 +1038,7 @@ SharedHandle<ValueBase> TellStatusRpcMethod::process
|
||||||
SharedHandle<ValueBase> TellActiveRpcMethod::process
|
SharedHandle<ValueBase> TellActiveRpcMethod::process
|
||||||
(const RpcRequest& req, DownloadEngine* e)
|
(const RpcRequest& req, DownloadEngine* e)
|
||||||
{
|
{
|
||||||
const List* keysParam = req.getListParam(0);
|
const List* keysParam = checkParam<List>(req, 0);
|
||||||
std::vector<std::string> keys;
|
std::vector<std::string> keys;
|
||||||
toStringList(std::back_inserter(keys), keysParam);
|
toStringList(std::back_inserter(keys), keysParam);
|
||||||
SharedHandle<List> list = List::g();
|
SharedHandle<List> list = List::g();
|
||||||
|
@ -1100,7 +1103,9 @@ SharedHandle<ValueBase> PurgeDownloadResultRpcMethod::process
|
||||||
SharedHandle<ValueBase> RemoveDownloadResultRpcMethod::process
|
SharedHandle<ValueBase> RemoveDownloadResultRpcMethod::process
|
||||||
(const RpcRequest& req, DownloadEngine* e)
|
(const RpcRequest& req, DownloadEngine* e)
|
||||||
{
|
{
|
||||||
a2_gid_t gid = getRequiredGidParam(req, 0);
|
const String* gidParam = checkRequiredParam<String>(req, 0);
|
||||||
|
|
||||||
|
a2_gid_t gid = str2Gid(gidParam);
|
||||||
if(!e->getRequestGroupMan()->removeDownloadResult(gid)) {
|
if(!e->getRequestGroupMan()->removeDownloadResult(gid)) {
|
||||||
throw DL_ABORT_EX
|
throw DL_ABORT_EX
|
||||||
(fmt("Could not remove download result of GID#%s",
|
(fmt("Could not remove download result of GID#%s",
|
||||||
|
@ -1112,8 +1117,10 @@ SharedHandle<ValueBase> RemoveDownloadResultRpcMethod::process
|
||||||
SharedHandle<ValueBase> ChangeOptionRpcMethod::process
|
SharedHandle<ValueBase> ChangeOptionRpcMethod::process
|
||||||
(const RpcRequest& req, DownloadEngine* e)
|
(const RpcRequest& req, DownloadEngine* e)
|
||||||
{
|
{
|
||||||
a2_gid_t gid = getRequiredGidParam(req, 0);
|
const String* gidParam = checkRequiredParam<String>(req, 0);
|
||||||
|
const Dict* optsParam = checkRequiredParam<Dict>(req, 1);
|
||||||
|
|
||||||
|
a2_gid_t gid = str2Gid(gidParam);
|
||||||
SharedHandle<RequestGroup> group =
|
SharedHandle<RequestGroup> group =
|
||||||
findRequestGroup(e->getRequestGroupMan(), gid);
|
findRequestGroup(e->getRequestGroupMan(), gid);
|
||||||
if(!group) {
|
if(!group) {
|
||||||
|
@ -1122,38 +1129,33 @@ SharedHandle<ValueBase> ChangeOptionRpcMethod::process
|
||||||
util::itos(gid).c_str()));
|
util::itos(gid).c_str()));
|
||||||
}
|
}
|
||||||
SharedHandle<Option> option(new Option());
|
SharedHandle<Option> option(new Option());
|
||||||
const Dict* optionsParam = req.getDictParam(1);
|
gatherChangeableOption(option, optsParam);
|
||||||
if(optionsParam) {
|
applyChangeableOption(group->getOption().get(), option.get());
|
||||||
gatherChangeableOption(option, optionsParam);
|
if(option->defined(PREF_MAX_DOWNLOAD_LIMIT)) {
|
||||||
applyChangeableOption(group->getOption().get(), option.get());
|
group->setMaxDownloadSpeedLimit
|
||||||
if(option->defined(PREF_MAX_DOWNLOAD_LIMIT)) {
|
(option->getAsInt(PREF_MAX_DOWNLOAD_LIMIT));
|
||||||
group->setMaxDownloadSpeedLimit
|
|
||||||
(option->getAsInt(PREF_MAX_DOWNLOAD_LIMIT));
|
|
||||||
}
|
|
||||||
if(option->defined(PREF_MAX_UPLOAD_LIMIT)) {
|
|
||||||
group->setMaxUploadSpeedLimit(option->getAsInt(PREF_MAX_UPLOAD_LIMIT));
|
|
||||||
}
|
|
||||||
#ifdef ENABLE_BITTORRENT
|
|
||||||
BtObject btObject = e->getBtRegistry()->get(group->getGID());
|
|
||||||
if(!btObject.isNull()) {
|
|
||||||
if(option->defined(PREF_BT_MAX_PEERS)) {
|
|
||||||
btObject.btRuntime_->setMaxPeers(option->getAsInt(PREF_BT_MAX_PEERS));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif // ENABLE_BITTORRENT
|
|
||||||
}
|
}
|
||||||
|
if(option->defined(PREF_MAX_UPLOAD_LIMIT)) {
|
||||||
|
group->setMaxUploadSpeedLimit(option->getAsInt(PREF_MAX_UPLOAD_LIMIT));
|
||||||
|
}
|
||||||
|
#ifdef ENABLE_BITTORRENT
|
||||||
|
BtObject btObject = e->getBtRegistry()->get(group->getGID());
|
||||||
|
if(!btObject.isNull()) {
|
||||||
|
if(option->defined(PREF_BT_MAX_PEERS)) {
|
||||||
|
btObject.btRuntime_->setMaxPeers(option->getAsInt(PREF_BT_MAX_PEERS));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif // ENABLE_BITTORRENT
|
||||||
return VLB_OK;
|
return VLB_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
SharedHandle<ValueBase> ChangeGlobalOptionRpcMethod::process
|
SharedHandle<ValueBase> ChangeGlobalOptionRpcMethod::process
|
||||||
(const RpcRequest& req, DownloadEngine* e)
|
(const RpcRequest& req, DownloadEngine* e)
|
||||||
{
|
{
|
||||||
const Dict* optionsParam = req.getDictParam(0);
|
const Dict* optsParam = checkRequiredParam<Dict>(req, 0);
|
||||||
if(!optionsParam) {
|
|
||||||
return VLB_OK;
|
|
||||||
}
|
|
||||||
SharedHandle<Option> option(new Option());
|
SharedHandle<Option> option(new Option());
|
||||||
gatherChangeableGlobalOption(option, optionsParam);
|
gatherChangeableGlobalOption(option, optsParam);
|
||||||
applyChangeableGlobalOption(e->getOption(), option.get());
|
applyChangeableGlobalOption(e->getOption(), option.get());
|
||||||
|
|
||||||
if(option->defined(PREF_MAX_OVERALL_DOWNLOAD_LIMIT)) {
|
if(option->defined(PREF_MAX_OVERALL_DOWNLOAD_LIMIT)) {
|
||||||
|
@ -1219,8 +1221,9 @@ void pushRequestOption
|
||||||
SharedHandle<ValueBase> GetOptionRpcMethod::process
|
SharedHandle<ValueBase> GetOptionRpcMethod::process
|
||||||
(const RpcRequest& req, DownloadEngine* e)
|
(const RpcRequest& req, DownloadEngine* e)
|
||||||
{
|
{
|
||||||
a2_gid_t gid = getRequiredGidParam(req, 0);
|
const String* gidParam = checkRequiredParam<String>(req, 0);
|
||||||
|
|
||||||
|
a2_gid_t gid = str2Gid(gidParam);
|
||||||
SharedHandle<RequestGroup> group =
|
SharedHandle<RequestGroup> group =
|
||||||
findRequestGroup(e->getRequestGroupMan(), gid);
|
findRequestGroup(e->getRequestGroupMan(), gid);
|
||||||
if(!group) {
|
if(!group) {
|
||||||
|
@ -1251,13 +1254,11 @@ SharedHandle<ValueBase> GetGlobalOptionRpcMethod::process
|
||||||
SharedHandle<ValueBase> ChangePositionRpcMethod::process
|
SharedHandle<ValueBase> ChangePositionRpcMethod::process
|
||||||
(const RpcRequest& req, DownloadEngine* e)
|
(const RpcRequest& req, DownloadEngine* e)
|
||||||
{
|
{
|
||||||
a2_gid_t gid = getRequiredGidParam(req, 0);
|
const String* gidParam = checkRequiredParam<String>(req, 0);
|
||||||
const Integer* posParam = req.getIntegerParam(1);
|
const Integer* posParam = checkRequiredParam<Integer>(req, 1);
|
||||||
const String* howParam = req.getStringParam(2);
|
const String* howParam = checkRequiredParam<String>(req, 2);
|
||||||
|
|
||||||
if(!posParam || !howParam) {
|
a2_gid_t gid = str2Gid(gidParam);
|
||||||
throw DL_ABORT_EX("Illegal argument.");
|
|
||||||
}
|
|
||||||
int pos = posParam->i();
|
int pos = posParam->i();
|
||||||
const std::string& howStr = howParam->s();
|
const std::string& howStr = howParam->s();
|
||||||
RequestGroupMan::HOW how;
|
RequestGroupMan::HOW how;
|
||||||
|
@ -1287,7 +1288,9 @@ SharedHandle<ValueBase> GetSessionInfoRpcMethod::process
|
||||||
SharedHandle<ValueBase> GetServersRpcMethod::process
|
SharedHandle<ValueBase> GetServersRpcMethod::process
|
||||||
(const RpcRequest& req, DownloadEngine* e)
|
(const RpcRequest& req, DownloadEngine* e)
|
||||||
{
|
{
|
||||||
a2_gid_t gid = getRequiredGidParam(req, 0);
|
const String* gidParam = checkRequiredParam<String>(req, 0);
|
||||||
|
|
||||||
|
a2_gid_t gid = str2Gid(gidParam);
|
||||||
SharedHandle<RequestGroup> group =
|
SharedHandle<RequestGroup> group =
|
||||||
e->getRequestGroupMan()->findRequestGroup(gid);
|
e->getRequestGroupMan()->findRequestGroup(gid);
|
||||||
if(!group) {
|
if(!group) {
|
||||||
|
@ -1326,17 +1329,15 @@ SharedHandle<ValueBase> GetServersRpcMethod::process
|
||||||
SharedHandle<ValueBase> ChangeUriRpcMethod::process
|
SharedHandle<ValueBase> ChangeUriRpcMethod::process
|
||||||
(const RpcRequest& req, DownloadEngine* e)
|
(const RpcRequest& req, DownloadEngine* e)
|
||||||
{
|
{
|
||||||
a2_gid_t gid = getRequiredGidParam(req, 0);
|
const String* gidParam = checkRequiredParam<String>(req, 0);
|
||||||
const Integer* indexParam = req.getIntegerParam(1);
|
const Integer* indexParam = checkRequiredParam<Integer>(req, 1);
|
||||||
const List* delUrisParam = req.getListParam(2);
|
const List* delUrisParam = checkRequiredParam<List>(req, 2);
|
||||||
const List* addUrisParam = req.getListParam(3);
|
const List* addUrisParam = checkRequiredParam<List>(req, 3);
|
||||||
if(!indexParam || !delUrisParam || ! addUrisParam) {
|
const Integer* posParam = checkParam<Integer>(req, 4);
|
||||||
throw DL_ABORT_EX("Bad request");
|
|
||||||
}
|
|
||||||
size_t pos = 0;
|
|
||||||
bool posGiven = false;
|
|
||||||
getPosParam(req, 4, posGiven, pos);
|
|
||||||
|
|
||||||
|
a2_gid_t gid = str2Gid(gidParam);
|
||||||
|
bool posGiven = checkPosParam(posParam);
|
||||||
|
size_t pos = posGiven ? posParam->i() : 0;
|
||||||
size_t index = indexParam->i()-1;
|
size_t index = indexParam->i()-1;
|
||||||
SharedHandle<RequestGroup> group =
|
SharedHandle<RequestGroup> group =
|
||||||
findRequestGroup(e->getRequestGroupMan(), gid);
|
findRequestGroup(e->getRequestGroupMan(), gid);
|
||||||
|
@ -1431,10 +1432,7 @@ SharedHandle<ValueBase> GetGlobalStatRpcMethod::process
|
||||||
SharedHandle<ValueBase> SystemMulticallRpcMethod::process
|
SharedHandle<ValueBase> SystemMulticallRpcMethod::process
|
||||||
(const RpcRequest& req, DownloadEngine* e)
|
(const RpcRequest& req, DownloadEngine* e)
|
||||||
{
|
{
|
||||||
const List* methodSpecs = req.getListParam(0);
|
const List* methodSpecs = checkRequiredParam<List>(req, 0);
|
||||||
if(!methodSpecs) {
|
|
||||||
throw DL_ABORT_EX("Illegal argument. One item list is expected.");
|
|
||||||
}
|
|
||||||
SharedHandle<List> list = List::g();
|
SharedHandle<List> list = List::g();
|
||||||
for(List::ValueType::const_iterator i = methodSpecs->begin(),
|
for(List::ValueType::const_iterator i = methodSpecs->begin(),
|
||||||
eoi = methodSpecs->end(); i != eoi; ++i) {
|
eoi = methodSpecs->end(); i != eoi; ++i) {
|
||||||
|
|
|
@ -45,6 +45,7 @@
|
||||||
#include "ValueBase.h"
|
#include "ValueBase.h"
|
||||||
#include "TorrentAttribute.h"
|
#include "TorrentAttribute.h"
|
||||||
#include "DlAbortEx.h"
|
#include "DlAbortEx.h"
|
||||||
|
#include "fmt.h"
|
||||||
|
|
||||||
namespace aria2 {
|
namespace aria2 {
|
||||||
|
|
||||||
|
@ -53,6 +54,28 @@ class RequestGroup;
|
||||||
|
|
||||||
namespace rpc {
|
namespace rpc {
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
const T* checkParam(const RpcRequest& req, size_t index, bool required = false)
|
||||||
|
{
|
||||||
|
const T* p = 0;
|
||||||
|
if(req.params->size() > index) {
|
||||||
|
if((p = downcast<T>(req.params->get(index))) == 0) {
|
||||||
|
throw DL_ABORT_EX(fmt("The parameter at %lu has wrong type.",
|
||||||
|
static_cast<unsigned long>(index)));
|
||||||
|
}
|
||||||
|
} else if(required) {
|
||||||
|
throw DL_ABORT_EX(fmt("The parameter at %lu is required but missing.",
|
||||||
|
static_cast<unsigned long>(index)));
|
||||||
|
}
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
const T* checkRequiredParam(const RpcRequest& req, size_t index)
|
||||||
|
{
|
||||||
|
return checkParam<T>(req, index, true);
|
||||||
|
}
|
||||||
|
|
||||||
template<typename OutputIterator>
|
template<typename OutputIterator>
|
||||||
void toStringList(OutputIterator out, const List* src)
|
void toStringList(OutputIterator out, const List* src)
|
||||||
{
|
{
|
||||||
|
@ -335,27 +358,19 @@ private:
|
||||||
std::advance(last, lastDistance);
|
std::advance(last, lastDistance);
|
||||||
return std::make_pair(first, last);
|
return std::make_pair(first, last);
|
||||||
}
|
}
|
||||||
|
|
||||||
void checkPaginationParams(const SharedHandle<List>& params) const
|
|
||||||
{
|
|
||||||
if(params->size() < 2) {
|
|
||||||
throw DL_ABORT_EX("Invalid argument. Specify offset and num in integer.");
|
|
||||||
}
|
|
||||||
const Integer* p1 = asInteger(params->get(0));
|
|
||||||
const Integer* p2 = asInteger(params->get(1));
|
|
||||||
if(!p1 || !p2 || p2->i() < 0) {
|
|
||||||
throw DL_ABORT_EX("Invalid argument. Specify offset and num in integer.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
protected:
|
protected:
|
||||||
virtual SharedHandle<ValueBase> process
|
virtual SharedHandle<ValueBase> process
|
||||||
(const RpcRequest& req, DownloadEngine* e)
|
(const RpcRequest& req, DownloadEngine* e)
|
||||||
{
|
{
|
||||||
const SharedHandle<List>& params = req.params;
|
const Integer* offsetParam = checkRequiredParam<Integer>(req, 0);
|
||||||
checkPaginationParams(params);
|
const Integer* numParam = checkRequiredParam<Integer>(req, 1);
|
||||||
ssize_t offset = req.getIntegerParam(0)->i();
|
const List* keysParam = checkParam<List>(req, 2);
|
||||||
size_t num = req.getIntegerParam(1)->i();
|
|
||||||
const List* keysParam = req.getListParam(2);
|
if(numParam->i() < 0) {
|
||||||
|
throw DL_ABORT_EX("The parameter num must be zero or positive integer.");
|
||||||
|
}
|
||||||
|
ssize_t offset = offsetParam->i();
|
||||||
|
size_t num = numParam->i();
|
||||||
std::vector<std::string> keys;
|
std::vector<std::string> keys;
|
||||||
toStringList(std::back_inserter(keys), keysParam);
|
toStringList(std::back_inserter(keys), keysParam);
|
||||||
const std::deque<SharedHandle<T> >& items = getItems(e);
|
const std::deque<SharedHandle<T> >& items = getItems(e);
|
||||||
|
|
|
@ -64,42 +64,6 @@ RpcRequest& RpcRequest::operator=(const RpcRequest& c)
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
const String* RpcRequest::getStringParam(size_t index) const
|
|
||||||
{
|
|
||||||
const String* stringParam = 0;
|
|
||||||
if(params->size() > index) {
|
|
||||||
stringParam = asString(params->get(index));
|
|
||||||
}
|
|
||||||
return stringParam;
|
|
||||||
}
|
|
||||||
|
|
||||||
const Integer* RpcRequest::getIntegerParam(size_t index) const
|
|
||||||
{
|
|
||||||
const Integer* integerParam = 0;
|
|
||||||
if(params->size() > index) {
|
|
||||||
integerParam = asInteger(params->get(index));
|
|
||||||
}
|
|
||||||
return integerParam;
|
|
||||||
}
|
|
||||||
|
|
||||||
const List* RpcRequest::getListParam(size_t index) const
|
|
||||||
{
|
|
||||||
const List* listParam = 0;
|
|
||||||
if(params->size() > index) {
|
|
||||||
listParam = asList(params->get(index));
|
|
||||||
}
|
|
||||||
return listParam;
|
|
||||||
}
|
|
||||||
|
|
||||||
const Dict* RpcRequest::getDictParam(size_t index) const
|
|
||||||
{
|
|
||||||
const Dict* dictParam = 0;
|
|
||||||
if(params->size() > index) {
|
|
||||||
dictParam = asDict(params->get(index));
|
|
||||||
}
|
|
||||||
return dictParam;
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace rpc
|
} // namespace rpc
|
||||||
|
|
||||||
} // namespace aria2
|
} // namespace aria2
|
||||||
|
|
|
@ -63,14 +63,6 @@ struct RpcRequest {
|
||||||
RpcRequest(const RpcRequest& c);
|
RpcRequest(const RpcRequest& c);
|
||||||
|
|
||||||
RpcRequest& operator=(const RpcRequest& c);
|
RpcRequest& operator=(const RpcRequest& c);
|
||||||
|
|
||||||
const String* getStringParam(size_t index) const;
|
|
||||||
|
|
||||||
const Integer* getIntegerParam(size_t index) const;
|
|
||||||
|
|
||||||
const List* getListParam(size_t index) const;
|
|
||||||
|
|
||||||
const Dict* getDictParam(size_t index) const;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace rpc
|
} // namespace rpc
|
||||||
|
|
Loading…
Reference in New Issue