From 4f8db487e77675ebd691f309ac4143c05fefd158 Mon Sep 17 00:00:00 2001 From: hagen Date: Thu, 26 May 2016 00:00:00 +0000 Subject: [PATCH] * I2PControl.{cpp,h} : add BuildErrorResponse() --- I2PControl.cpp | 19 ++++++++++++------- I2PControl.h | 1 + 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/I2PControl.cpp b/I2PControl.cpp index 7a5014ec..047f211e 100644 --- a/I2PControl.cpp +++ b/I2PControl.cpp @@ -221,9 +221,7 @@ namespace client std::ostringstream response; #if GCC47_BOOST149 LogPrint (eLogError, "I2PControl: json_read is not supported due bug in boost 1.49 with gcc 4.7"); - response << "{\"id\":null,\"error\":"; - response << "{\"code\":-32603,\"message\":\"JSON requests is not supported with this version of boost\"},"; - response << "\"jsonrpc\":\"2.0\"}"; + BuildErrorResponse(content, 32603, "JSON requests is not supported with this version of boost"); #else boost::property_tree::ptree pt; boost::property_tree::read_json (ss, pt); @@ -236,19 +234,18 @@ namespace client response << "{\"id\":" << id << ",\"result\":{"; (this->*(it->second))(pt.get_child ("params"), response); response << "},\"jsonrpc\":\"2.0\"}"; + content = response.str(); } else { LogPrint (eLogWarning, "I2PControl: unknown method ", method); - response << "{\"id\":null,\"error\":"; - response << "{\"code\":-32601,\"message\":\"Method not found\"},"; - response << "\"jsonrpc\":\"2.0\"}"; + BuildErrorResponse(content, 32601, "Method not found"); } #endif - content = response.str(); SendResponse (socket, buf, content, isHtml); } catch (std::exception& ex) { LogPrint (eLogError, "I2PControl: exception when handle request: ", ex.what ()); + /* TODO: also send error, code 32603 */ } catch (...) { @@ -276,6 +273,14 @@ namespace client ss << "\"" << name << "\":" << std::fixed << std::setprecision(2) << value; } + void I2PControlService::BuildErrorResponse (std::string & content, int code, const char *message) { + std::stringstream ss; + ss << "{\"id\":null,\"error\":"; + ss << "{\"code\":" << -code << ",\"message\":\"" << message << "\"},"; + ss << "\"jsonrpc\":\"2.0\"}"; + content = ss.str(); + } + void I2PControlService::SendResponse (std::shared_ptr socket, std::shared_ptr buf, std::string& content, bool isHtml) { diff --git a/I2PControl.h b/I2PControl.h index ed572c42..bd5b9bad 100644 --- a/I2PControl.h +++ b/I2PControl.h @@ -45,6 +45,7 @@ namespace client void ReadRequest (std::shared_ptr socket); void HandleRequestReceived (const boost::system::error_code& ecode, size_t bytes_transferred, std::shared_ptr socket, std::shared_ptr buf); + void BuildErrorResponse (std::string & content, int code, const char *message); void SendResponse (std::shared_ptr socket, std::shared_ptr buf, std::string& response, bool isHtml); void HandleResponseSent (const boost::system::error_code& ecode, std::size_t bytes_transferred,