diff options
| author | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2011-03-09 23:07:27 +0900 |
|---|---|---|
| committer | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2011-03-09 23:07:27 +0900 |
| commit | 7338a2503547ae1310d018d0fc2903fb85b309ea (patch) | |
| tree | c7111eb641f6bf1b661c03270a8f4407d2fada21 /src/HttpServerBodyCommand.cc | |
| parent | d541b350d21cd7747479f21b7c4b6d363248a450 (diff) | |
Added initial JSON-RPC support.
JSON-RPC is enabled using --enable-xml-rpc. We are implementing
JSON-RPC based on JSON-RPC 2.0 draft spec.
Diffstat (limited to 'src/HttpServerBodyCommand.cc')
| -rw-r--r-- | src/HttpServerBodyCommand.cc | 56 |
1 files changed, 55 insertions, 1 deletions
diff --git a/src/HttpServerBodyCommand.cc b/src/HttpServerBodyCommand.cc index e49c253c..3d39e111 100644 --- a/src/HttpServerBodyCommand.cc +++ b/src/HttpServerBodyCommand.cc @@ -54,6 +54,8 @@ #include "util.h" #include "fmt.h" #include "SocketRecvBuffer.h" +#include "json.h" +#include "DlAbortEx.h" namespace aria2 { @@ -92,8 +94,16 @@ bool HttpServerBodyCommand::execute() timeoutTimer_ = global::wallclock; if(httpServer_->receiveBody()) { + A2_LOG_DEBUG(fmt("%s", httpServer_->getBody().c_str())); + + std::string reqPath = httpServer_->getRequestPath(); + reqPath.erase(std::find(reqPath.begin(), reqPath.end(), '#'), + reqPath.end()); + std::string query(std::find(reqPath.begin(), reqPath.end(), '?'), + reqPath.end()); + reqPath.erase(reqPath.size()-query.size(), query.size()); // Do something for requestpath and body - if(httpServer_->getRequestPath() == "/rpc") { + if(reqPath == "/rpc") { xmlrpc::XmlRpcRequest req = xmlrpc::XmlRpcRequestProcessor().parseMemory(httpServer_->getBody()); @@ -108,6 +118,50 @@ bool HttpServerBodyCommand::execute() e_->addCommand(command); e_->setNoWait(true); return true; + } else if(reqPath == "/jsonrpc") { + // TODO handle query parameter + std::string callback;// = "callback"; + + SharedHandle<ValueBase> json = json::decode(httpServer_->getBody()); + const Dict* jsondict = asDict(json); + if(!jsondict) { + // TODO code: -32600, Invalid Request + throw DL_ABORT_EX("JSON-RPC Invalid Request"); + } + const String* methodName = asString(jsondict->get("method")); + if(!methodName) { + // TODO Batch request does not have method + throw DL_ABORT_EX("JSON-RPC No Method Found"); + } + SharedHandle<List> params; + const SharedHandle<ValueBase>& tempParams = jsondict->get("params"); + if(asList(tempParams)) { + params = static_pointer_cast<List>(tempParams); + } else if(!tempParams) { + params = List::g(); + } else { + // TODO No support for Named params + throw DL_ABORT_EX("JSON-RPC Named params are not supported"); + } + SharedHandle<ValueBase> id = jsondict->get("id"); + if(!id) { + // TODO Batch request does not have id + throw DL_ABORT_EX("JSON-RPC NO id found"); + } + xmlrpc::XmlRpcRequest req(methodName->s(), params, id); + + SharedHandle<xmlrpc::XmlRpcMethod> method = + xmlrpc::XmlRpcMethodFactory::create(req.methodName); + method->setJsonRpc(true); + xmlrpc::XmlRpcResponse res = method->execute(req, e_); + bool gzip = httpServer_->supportsGZip(); + std::string responseData = res.toJson(callback, gzip); + httpServer_->feedResponse(responseData, "application/json-rpc"); + Command* command = + new HttpServerResponseCommand(getCuid(), httpServer_, e_, socket_); + e_->addCommand(command); + e_->setNoWait(true); + return true; } else { return true; } |
