* I2PControl.cpp :

* unwrap big else {} block
  * smaller try {} block, only for json parsing & request handling
  * respond with valid error message on exception
This commit is contained in:
hagen 2016-05-26 00:00:00 +00:00
parent 0ab5f993c7
commit 9291f5c9c6

View file

@ -188,9 +188,8 @@ namespace client
if (ecode) { if (ecode) {
LogPrint (eLogError, "I2PControl: read error: ", ecode.message ()); LogPrint (eLogError, "I2PControl: read error: ", ecode.message ());
return; return;
} else { }
try /* try to parse received data */
{
std::stringstream json; std::stringstream json;
std::string response; std::string response;
bool isHTTP = false; bool isHTTP = false;
@ -221,14 +220,15 @@ namespace client
LogPrint (eLogError, "I2PControl: json_read is not supported due bug in boost 1.49 with gcc 4.7"); LogPrint (eLogError, "I2PControl: json_read is not supported due bug in boost 1.49 with gcc 4.7");
BuildErrorResponse(response, 32603, "JSON requests is not supported with this version of boost"); BuildErrorResponse(response, 32603, "JSON requests is not supported with this version of boost");
#else #else
/* now try to parse json itself */
try {
boost::property_tree::ptree pt; boost::property_tree::ptree pt;
boost::property_tree::read_json (json, pt); boost::property_tree::read_json (json, pt);
std::string id = pt.get<std::string>("id"); std::string id = pt.get<std::string>("id");
std::string method = pt.get<std::string>("method"); std::string method = pt.get<std::string>("method");
auto it = m_MethodHandlers.find (method); auto it = m_MethodHandlers.find (method);
if (it != m_MethodHandlers.end ()) if (it != m_MethodHandlers.end ()) {
{
std::ostringstream ss; std::ostringstream ss;
ss << "{\"id\":" << id << ",\"result\":{"; ss << "{\"id\":" << id << ",\"result\":{";
(this->*(it->second))(pt.get_child ("params"), ss); (this->*(it->second))(pt.get_child ("params"), ss);
@ -238,19 +238,14 @@ namespace client
LogPrint (eLogWarning, "I2PControl: unknown method ", method); LogPrint (eLogWarning, "I2PControl: unknown method ", method);
BuildErrorResponse(response, 32601, "Method not found"); BuildErrorResponse(response, 32601, "Method not found");
} }
#endif } catch (std::exception& ex) {
SendResponse (socket, buf, response, isHTTP);
}
catch (std::exception& ex)
{
LogPrint (eLogError, "I2PControl: exception when handle request: ", ex.what ()); LogPrint (eLogError, "I2PControl: exception when handle request: ", ex.what ());
/* TODO: also send error, code 32603 */ BuildErrorResponse(response, 32603, ex.what());
} } catch (...) {
catch (...)
{
LogPrint (eLogError, "I2PControl: handle request unknown exception"); LogPrint (eLogError, "I2PControl: handle request unknown exception");
} }
} #endif
SendResponse (socket, buf, response, isHTTP);
} }
void I2PControlService::InsertParam (std::ostringstream& ss, const std::string& name, int value) const void I2PControlService::InsertParam (std::ostringstream& ss, const std::string& name, int value) const