2009-05-08 07:58:50 +00:00
|
|
|
/* <!-- copyright */
|
|
|
|
/*
|
|
|
|
* aria2 - The high speed download utility
|
|
|
|
*
|
|
|
|
* Copyright (C) 2009 Tatsuhiro Tsujikawa
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2010-01-05 16:01:46 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2009-05-08 07:58:50 +00:00
|
|
|
*
|
|
|
|
* In addition, as a special exception, the copyright holders give
|
|
|
|
* permission to link the code of portions of this program with the
|
|
|
|
* OpenSSL library under certain conditions as described in each
|
|
|
|
* individual source file, and distribute linked combinations
|
|
|
|
* including the two.
|
|
|
|
* You must obey the GNU General Public License in all respects
|
|
|
|
* for all of the code used other than OpenSSL. If you modify
|
|
|
|
* file(s) with this exception, you may extend this exception to your
|
|
|
|
* version of the file(s), but you are not obligated to do so. If you
|
|
|
|
* do not wish to do so, delete this exception statement from your
|
|
|
|
* version. If you delete this exception statement from all source
|
|
|
|
* files in the program, then also delete it here.
|
|
|
|
*/
|
|
|
|
/* copyright --> */
|
2011-03-14 07:38:54 +00:00
|
|
|
#include "RpcMethod.h"
|
2009-05-08 07:58:50 +00:00
|
|
|
#include "DownloadEngine.h"
|
|
|
|
#include "LogFactory.h"
|
|
|
|
#include "RecoverableException.h"
|
|
|
|
#include "message.h"
|
|
|
|
#include "OptionParser.h"
|
|
|
|
#include "OptionHandler.h"
|
|
|
|
#include "Option.h"
|
|
|
|
#include "array_fun.h"
|
|
|
|
#include "download_helper.h"
|
2011-03-14 07:38:54 +00:00
|
|
|
#include "RpcRequest.h"
|
|
|
|
#include "RpcResponse.h"
|
2009-05-13 15:23:01 +00:00
|
|
|
#include "prefs.h"
|
2010-11-20 08:21:36 +00:00
|
|
|
#include "fmt.h"
|
2010-06-19 17:54:54 +00:00
|
|
|
#include "DlAbortEx.h"
|
2009-05-08 07:58:50 +00:00
|
|
|
|
|
|
|
namespace aria2 {
|
|
|
|
|
2011-03-14 07:38:54 +00:00
|
|
|
namespace rpc {
|
2009-05-08 07:58:50 +00:00
|
|
|
|
2011-03-14 07:38:54 +00:00
|
|
|
RpcMethod::RpcMethod()
|
2011-06-14 15:19:07 +00:00
|
|
|
: optionParser_(OptionParser::getInstance())
|
2010-11-20 08:21:36 +00:00
|
|
|
{}
|
2009-05-08 07:58:50 +00:00
|
|
|
|
2011-03-14 07:38:54 +00:00
|
|
|
RpcMethod::~RpcMethod() {}
|
2010-11-14 07:17:55 +00:00
|
|
|
|
2011-03-14 07:38:54 +00:00
|
|
|
SharedHandle<ValueBase> RpcMethod::createErrorResponse
|
2011-06-14 15:19:07 +00:00
|
|
|
(const Exception& e, const RpcRequest& req)
|
2009-05-08 07:58:50 +00:00
|
|
|
{
|
2010-06-19 17:54:54 +00:00
|
|
|
SharedHandle<Dict> params = Dict::g();
|
2011-06-14 15:19:07 +00:00
|
|
|
params->put((req.jsonRpc ? "code" : "faultCode"), Integer::g(1));
|
|
|
|
params->put((req.jsonRpc ? "message" : "faultString"), std::string(e.what()));
|
2009-05-08 07:58:50 +00:00
|
|
|
return params;
|
|
|
|
}
|
|
|
|
|
2011-03-14 07:38:54 +00:00
|
|
|
RpcResponse RpcMethod::execute
|
|
|
|
(const RpcRequest& req, DownloadEngine* e)
|
2009-05-08 07:58:50 +00:00
|
|
|
{
|
|
|
|
try {
|
2011-03-14 07:38:54 +00:00
|
|
|
return RpcResponse(0, process(req, e), req.id);
|
2011-06-14 15:19:07 +00:00
|
|
|
} catch(RecoverableException& ex) {
|
|
|
|
A2_LOG_DEBUG_EX(EX_EXCEPTION_CAUGHT, ex);
|
|
|
|
return RpcResponse(1, createErrorResponse(ex, req), req.id);
|
2009-05-08 07:58:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-30 16:02:15 +00:00
|
|
|
namespace {
|
2009-05-14 15:23:50 +00:00
|
|
|
template<typename InputIterator>
|
2010-10-30 16:02:15 +00:00
|
|
|
void gatherOption
|
2009-05-14 15:23:50 +00:00
|
|
|
(InputIterator first, InputIterator last,
|
2010-04-03 04:31:37 +00:00
|
|
|
const std::set<std::string>& allowedOptions,
|
2009-10-04 09:01:11 +00:00
|
|
|
const SharedHandle<Option>& option,
|
2009-05-14 15:23:50 +00:00
|
|
|
const SharedHandle<OptionParser>& optionParser)
|
2009-05-08 07:58:50 +00:00
|
|
|
{
|
2009-05-14 15:23:50 +00:00
|
|
|
for(; first != last; ++first) {
|
2009-10-04 09:01:11 +00:00
|
|
|
const std::string& optionName = (*first).first;
|
2010-04-03 04:31:37 +00:00
|
|
|
if(allowedOptions.count(optionName) == 0) {
|
2009-10-04 09:01:11 +00:00
|
|
|
throw DL_ABORT_EX
|
2010-11-20 09:36:14 +00:00
|
|
|
(fmt("%s option cannot be used in this context.",
|
|
|
|
optionName.c_str()));
|
2009-10-04 09:01:11 +00:00
|
|
|
} else {
|
2009-05-14 15:23:50 +00:00
|
|
|
SharedHandle<OptionHandler> optionHandler =
|
2010-01-05 16:01:46 +00:00
|
|
|
optionParser->findByName(optionName);
|
2010-11-12 12:48:48 +00:00
|
|
|
if(!optionHandler) {
|
2010-01-05 16:01:46 +00:00
|
|
|
throw DL_ABORT_EX
|
2010-11-20 09:36:14 +00:00
|
|
|
(fmt("We don't know how to deal with %s option",
|
|
|
|
optionName.c_str()));
|
2009-05-27 17:27:05 +00:00
|
|
|
}
|
2011-09-26 12:45:45 +00:00
|
|
|
const String* opval = downcast<String>((*first).second);
|
2010-06-19 17:54:54 +00:00
|
|
|
if(opval) {
|
|
|
|
optionHandler->parse(*option.get(), opval->s());
|
|
|
|
} else {
|
|
|
|
// header and index-out option can take array as value
|
2011-09-26 12:45:45 +00:00
|
|
|
const List* oplist = downcast<List>((*first).second);
|
2010-06-19 17:54:54 +00:00
|
|
|
if(oplist &&
|
2011-10-21 12:56:42 +00:00
|
|
|
(optionName == PREF_HEADER->k || optionName == PREF_INDEX_OUT->k)) {
|
2010-06-19 17:54:54 +00:00
|
|
|
for(List::ValueType::const_iterator argiter = oplist->begin(),
|
|
|
|
eoi = oplist->end(); argiter != eoi; ++argiter) {
|
2011-09-26 12:45:45 +00:00
|
|
|
const String* opval = downcast<String>(*argiter);
|
2010-06-19 17:54:54 +00:00
|
|
|
if(opval) {
|
|
|
|
optionHandler->parse(*option.get(), opval->s());
|
|
|
|
}
|
2010-01-05 16:01:46 +00:00
|
|
|
}
|
|
|
|
}
|
2009-05-08 07:58:50 +00:00
|
|
|
}
|
|
|
|
}
|
2009-05-14 15:23:50 +00:00
|
|
|
}
|
|
|
|
}
|
2010-10-30 16:02:15 +00:00
|
|
|
} // namespace
|
2009-05-14 15:23:50 +00:00
|
|
|
|
2011-03-14 07:38:54 +00:00
|
|
|
void RpcMethod::gatherRequestOption
|
2010-06-19 17:54:54 +00:00
|
|
|
(const SharedHandle<Option>& option, const Dict* optionsDict)
|
2009-05-14 15:23:50 +00:00
|
|
|
{
|
2010-06-19 17:54:54 +00:00
|
|
|
if(optionsDict) {
|
|
|
|
gatherOption(optionsDict->begin(), optionsDict->end(),
|
|
|
|
listRequestOptions(),
|
2010-06-21 13:51:56 +00:00
|
|
|
option, optionParser_);
|
2010-06-19 17:54:54 +00:00
|
|
|
}
|
2009-10-04 09:01:11 +00:00
|
|
|
}
|
|
|
|
|
2010-10-30 16:02:15 +00:00
|
|
|
namespace {
|
2009-10-04 09:01:11 +00:00
|
|
|
// Copy option in the range [optNameFirst, optNameLast) from src to
|
|
|
|
// dest.
|
|
|
|
template<typename InputIterator>
|
2010-10-30 16:02:15 +00:00
|
|
|
void applyOption(InputIterator optNameFirst,
|
|
|
|
InputIterator optNameLast,
|
|
|
|
Option* dest,
|
|
|
|
Option* src)
|
2009-10-04 09:01:11 +00:00
|
|
|
{
|
|
|
|
for(; optNameFirst != optNameLast; ++optNameFirst) {
|
2011-10-21 12:56:42 +00:00
|
|
|
const Pref* pref = option::k2p(*optNameFirst);
|
|
|
|
if(src->defined(pref)) {
|
|
|
|
dest->put(pref, src->get(pref));
|
2009-10-04 09:01:11 +00:00
|
|
|
}
|
|
|
|
}
|
2009-05-14 15:23:50 +00:00
|
|
|
}
|
2010-10-30 16:02:15 +00:00
|
|
|
} // namespace
|
2009-05-14 15:23:50 +00:00
|
|
|
|
2009-10-04 09:01:11 +00:00
|
|
|
const std::set<std::string>& listChangeableOptions()
|
2009-05-14 15:23:50 +00:00
|
|
|
{
|
|
|
|
static const std::string OPTIONS[] = {
|
2011-10-21 12:56:42 +00:00
|
|
|
PREF_BT_MAX_PEERS->k,
|
|
|
|
PREF_BT_REQUEST_PEER_SPEED_LIMIT->k,
|
|
|
|
PREF_MAX_DOWNLOAD_LIMIT->k,
|
|
|
|
PREF_MAX_UPLOAD_LIMIT->k
|
2009-05-14 15:23:50 +00:00
|
|
|
};
|
2010-03-25 13:51:10 +00:00
|
|
|
static std::set<std::string> options(vbegin(OPTIONS), vend(OPTIONS));
|
2009-05-14 15:23:50 +00:00
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
2011-03-14 07:38:54 +00:00
|
|
|
void RpcMethod::gatherChangeableOption
|
2010-06-19 17:54:54 +00:00
|
|
|
(const SharedHandle<Option>& option, const Dict* optionsDict)
|
2009-05-14 15:23:50 +00:00
|
|
|
{
|
2010-06-19 17:54:54 +00:00
|
|
|
if(optionsDict) {
|
|
|
|
gatherOption(optionsDict->begin(), optionsDict->end(),
|
|
|
|
listChangeableOptions(),
|
2010-06-21 13:51:56 +00:00
|
|
|
option, optionParser_);
|
2010-06-19 17:54:54 +00:00
|
|
|
}
|
2009-05-08 07:58:50 +00:00
|
|
|
}
|
|
|
|
|
2011-03-14 07:38:54 +00:00
|
|
|
void RpcMethod::applyChangeableOption(Option* dest, Option* src) const
|
2009-10-04 09:01:11 +00:00
|
|
|
{
|
|
|
|
applyOption(listChangeableOptions().begin(), listChangeableOptions().end(),
|
2010-01-05 16:01:46 +00:00
|
|
|
dest, src);
|
2009-10-04 09:01:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const std::set<std::string>& listChangeableGlobalOptions()
|
2009-05-14 15:44:59 +00:00
|
|
|
{
|
|
|
|
static const std::string OPTIONS[] = {
|
2011-10-21 12:56:42 +00:00
|
|
|
PREF_MAX_OVERALL_UPLOAD_LIMIT->k,
|
|
|
|
PREF_MAX_OVERALL_DOWNLOAD_LIMIT->k,
|
|
|
|
PREF_MAX_CONCURRENT_DOWNLOADS->k,
|
|
|
|
PREF_LOG->k,
|
|
|
|
PREF_LOG_LEVEL->k
|
2009-05-14 15:44:59 +00:00
|
|
|
};
|
2010-03-25 13:51:10 +00:00
|
|
|
static std::set<std::string> options(vbegin(OPTIONS), vend(OPTIONS));
|
2009-05-14 15:44:59 +00:00
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
2011-03-14 07:38:54 +00:00
|
|
|
void RpcMethod::gatherChangeableGlobalOption
|
2010-06-19 17:54:54 +00:00
|
|
|
(const SharedHandle<Option>& option, const Dict* optionsDict)
|
2009-05-14 15:44:59 +00:00
|
|
|
{
|
2010-06-19 17:54:54 +00:00
|
|
|
if(optionsDict) {
|
|
|
|
gatherOption(optionsDict->begin(), optionsDict->end(),
|
|
|
|
listChangeableGlobalOptions(),
|
2010-06-21 13:51:56 +00:00
|
|
|
option, optionParser_);
|
2010-06-19 17:54:54 +00:00
|
|
|
}
|
2009-10-04 09:01:11 +00:00
|
|
|
}
|
|
|
|
|
2011-03-14 07:38:54 +00:00
|
|
|
void RpcMethod::applyChangeableGlobalOption(Option* dest, Option* src) const
|
2009-10-04 09:01:11 +00:00
|
|
|
{
|
|
|
|
applyOption(listChangeableGlobalOptions().begin(),
|
2010-01-05 16:01:46 +00:00
|
|
|
listChangeableGlobalOptions().end(),
|
|
|
|
dest, src);
|
2009-05-14 15:44:59 +00:00
|
|
|
}
|
|
|
|
|
2011-03-14 07:38:54 +00:00
|
|
|
} // namespace rpc
|
2009-05-08 07:58:50 +00:00
|
|
|
|
|
|
|
} // namespace aria2
|