diff options
| author | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2012-03-20 21:42:09 +0900 |
|---|---|---|
| committer | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2012-03-20 21:42:09 +0900 |
| commit | f4e2c7f0603fdb0609bf163ee5ea683a44b98d69 (patch) | |
| tree | 18227134780ec9417ecc7b24969f35725a377e04 /src/rpc_helper.cc | |
| parent | f16511012e28d1303e300e695b310d01b6200c2c (diff) | |
Added JSON-RPC over WebSocket.
Wslay library must be placed under deps/wslay.
Diffstat (limited to 'src/rpc_helper.cc')
| -rw-r--r-- | src/rpc_helper.cc | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/src/rpc_helper.cc b/src/rpc_helper.cc index a9d6e721..054c7fba 100644 --- a/src/rpc_helper.cc +++ b/src/rpc_helper.cc @@ -38,6 +38,12 @@ #include "XmlRpcRequestParserStateMachine.h" #include "message.h" #include "DlAbortEx.h" +#include "DownloadEngine.h" +#include "RpcMethod.h" +#include "RpcResponse.h" +#include "RpcMethodFactory.h" +#include "LogFactory.h" +#include "fmt.h" namespace aria2 { @@ -60,6 +66,51 @@ RpcRequest xmlParseMemory(const char* xml, size_t size) } #endif // ENABLE_XML_RPC +RpcResponse createJsonRpcErrorResponse(int code, + const std::string& msg, + const SharedHandle<ValueBase>& id) +{ + SharedHandle<Dict> params = Dict::g(); + params->put("code", Integer::g(code)); + params->put("message", msg); + rpc::RpcResponse res(code, params, id); + return res; +} + +RpcResponse processJsonRpcRequest(const Dict* jsondict, DownloadEngine* e) +{ + SharedHandle<ValueBase> id = jsondict->get("id"); + if(!id) { + return createJsonRpcErrorResponse(-32600, "Invalid Request.", Null::g()); + } + const String* methodName = downcast<String>(jsondict->get("method")); + if(!methodName) { + return createJsonRpcErrorResponse(-32600, "Invalid Request.", id); + } + SharedHandle<List> params; + const SharedHandle<ValueBase>& tempParams = jsondict->get("params"); + if(downcast<List>(tempParams)) { + params = static_pointer_cast<List>(tempParams); + } else if(!tempParams) { + params = List::g(); + } else { + // TODO No support for Named params + return createJsonRpcErrorResponse(-32602, "Invalid params.", id); + } + rpc::RpcRequest req(methodName->s(), params, id); + req.jsonRpc = true; + SharedHandle<rpc::RpcMethod> method; + try { + method = rpc::RpcMethodFactory::create(req.methodName); + } catch(RecoverableException& e) { + A2_LOG_INFO_EX(EX_EXCEPTION_CAUGHT, e); + return createJsonRpcErrorResponse(-32601, "Method not found.", id); + } + A2_LOG_INFO(fmt("Executing RPC method %s", req.methodName.c_str())); + rpc::RpcResponse res = method->execute(req, e); + return res; +} + } // namespace rpc } // namespace aria2 |
