diff options
| author | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2012-07-14 18:59:56 +0900 |
|---|---|---|
| committer | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2012-07-14 18:59:56 +0900 |
| commit | 7100b1b9ca10f8bd940ff436b2a16f91d34d5429 (patch) | |
| tree | b7a31fb6e5a0cf01ad110bdb11dde0dffa7da697 /src/HttpServerBodyCommand.cc | |
| parent | 5352d76d4f24a8ad7096979f8f826a1dd0d8e5d7 (diff) | |
Parse HTTP RPC request on the fly without buffering
Diffstat (limited to 'src/HttpServerBodyCommand.cc')
| -rw-r--r-- | src/HttpServerBodyCommand.cc | 67 |
1 files changed, 44 insertions, 23 deletions
diff --git a/src/HttpServerBodyCommand.cc b/src/HttpServerBodyCommand.cc index 44cd6b95..ff9e3eb0 100644 --- a/src/HttpServerBodyCommand.cc +++ b/src/HttpServerBodyCommand.cc @@ -57,8 +57,11 @@ #include "RpcRequest.h" #include "RpcResponse.h" #include "rpc_helper.h" +#include "JsonDiskWriter.h" +#include "ValueBaseJsonParser.h" #ifdef ENABLE_XML_RPC -# include "XmlRpcRequestParserStateMachine.h" +# include "XmlRpcRequestParserStateMachine.h" +# include "XmlRpcDiskWriter.h" #endif // ENABLE_XML_RPC namespace aria2 { @@ -187,17 +190,22 @@ bool HttpServerBodyCommand::execute() } // Do something for requestpath and body - if(reqPath == "/rpc") { + switch(httpServer_->getRequestType()) { + case RPC_TYPE_XML: { #ifdef ENABLE_XML_RPC - std::string body = httpServer_->getBody(); + SharedHandle<rpc::XmlRpcDiskWriter> dw = + static_pointer_cast<rpc::XmlRpcDiskWriter>(httpServer_->getBody()); + int error; + error = dw->finalize(); rpc::RpcRequest req; - try { - req = rpc::xmlParseMemory(body.c_str(), body.size()); - } catch(RecoverableException& e) { - A2_LOG_INFO_EX + if(error == 0) { + req = dw->getResult(); + } + dw->reset(); + if(error < 0) { + A2_LOG_INFO (fmt("CUID#%" PRId64 " - Failed to parse XML-RPC request", - getCuid()), - e); + getCuid())); httpServer_->feedResponse(400); addHttpServerResponseCommand(); return true; @@ -215,22 +223,33 @@ bool HttpServerBodyCommand::execute() addHttpServerResponseCommand(); #endif // !ENABLE_XML_RPC return true; - } else if(reqPath == "/jsonrpc") { + } + case RPC_TYPE_JSON: + case RPC_TYPE_JSONP: { std::string callback; SharedHandle<ValueBase> json; - try { - if(httpServer_->getMethod() == "GET") { - json::JsonGetParam param = json::decodeGetParams(query); - callback = param.callback; - json = json::decode(param.request); - } else { - json = json::decode(httpServer_->getBody()); + ssize_t error = 0; + if(httpServer_->getRequestType() == RPC_TYPE_JSONP) { + json::JsonGetParam param = json::decodeGetParams(query); + callback = param.callback; + ssize_t error = 0; + json = json::ValueBaseJsonParser().parseFinal + (param.request.c_str(), + param.request.size(), + error); + } else { + SharedHandle<json::JsonDiskWriter> dw = + static_pointer_cast<json::JsonDiskWriter>(httpServer_->getBody()); + error = dw->finalize(); + if(error == 0) { + json = dw->getResult(); } - } catch(RecoverableException& e) { - A2_LOG_INFO_EX + dw->reset(); + } + if(error < 0) { + A2_LOG_INFO (fmt("CUID#%" PRId64 " - Failed to parse JSON-RPC request", - getCuid()), - e); + getCuid())); rpc::RpcResponse res (rpc::createJsonRpcErrorResponse(-32700, "Parse error.", Null::g())); @@ -250,7 +269,8 @@ bool HttpServerBodyCommand::execute() eoi = jsonlist->end(); i != eoi; ++i) { const Dict* jsondict = downcast<Dict>(*i); if(jsondict) { - rpc::RpcResponse r = rpc::processJsonRpcRequest(jsondict, e_); + rpc::RpcResponse r = + rpc::processJsonRpcRequest(jsondict, e_); results.push_back(r); } } @@ -263,7 +283,8 @@ bool HttpServerBodyCommand::execute() } } return true; - } else { + } + default: httpServer_->feedResponse(404); addHttpServerResponseCommand(); return true; |
