diff options
author | Antonio Juarez <antonio.maria.juarez@live.com> | 2014-04-07 16:02:15 +0100 |
---|---|---|
committer | Antonio Juarez <antonio.maria.juarez@live.com> | 2014-04-07 16:02:15 +0100 |
commit | a401a02ddb3fb045d998cf650292cab3b3ebfd58 (patch) | |
tree | 61d021af997193aba90b95b70836c2d031334aff /contrib/epee/include/net | |
parent | json rpc for wallet and bugfix (diff) | |
download | monero-a401a02ddb3fb045d998cf650292cab3b3ebfd58.tar.xz |
Improvements in JSON RPC
Diffstat (limited to 'contrib/epee/include/net')
-rw-r--r-- | contrib/epee/include/net/http_protocol_handler.inl | 2 | ||||
-rw-r--r-- | contrib/epee/include/net/http_server_handlers_map2.h | 86 |
2 files changed, 40 insertions, 48 deletions
diff --git a/contrib/epee/include/net/http_protocol_handler.inl b/contrib/epee/include/net/http_protocol_handler.inl index b8eeb5993..78b46427e 100644 --- a/contrib/epee/include/net/http_protocol_handler.inl +++ b/contrib/epee/include/net/http_protocol_handler.inl @@ -587,7 +587,7 @@ namespace net_utils { std::string buf = "HTTP/1.1 "; buf += boost::lexical_cast<std::string>(response.m_response_code) + " " + response.m_response_comment + "\r\n" + - "Server: Siski v0.1\r\n" + "Server: Epee-based\r\n" "Content-Length: "; buf += boost::lexical_cast<std::string>(response.m_body.size()) + "\r\n"; buf += "Content-Type: "; diff --git a/contrib/epee/include/net/http_server_handlers_map2.h b/contrib/epee/include/net/http_server_handlers_map2.h index 0b6ffa095..af4e035f8 100644 --- a/contrib/epee/include/net/http_server_handlers_map2.h +++ b/contrib/epee/include/net/http_server_handlers_map2.h @@ -120,7 +120,7 @@ namespace epee { std::string jsonrpc; std::string method; - std::string id; + epee::serialization::storage_entry id; t_param params; BEGIN_KV_SERIALIZE_MAP() @@ -158,7 +158,7 @@ namespace epee { std::string jsonrpc; t_param result; - std::string id; + epee::serialization::storage_entry id; t_error error; BEGIN_KV_SERIALIZE_MAP() KV_SERIALIZE(jsonrpc) @@ -173,7 +173,7 @@ namespace epee { std::string jsonrpc; t_param result; - std::string id; + epee::serialization::storage_entry id; BEGIN_KV_SERIALIZE_MAP() KV_SERIALIZE(jsonrpc) KV_SERIALIZE(id) @@ -186,7 +186,7 @@ namespace epee { std::string jsonrpc; t_error error; - std::string id; + epee::serialization::storage_entry id; BEGIN_KV_SERIALIZE_MAP() KV_SERIALIZE(jsonrpc) KV_SERIALIZE(id) @@ -213,6 +213,9 @@ namespace epee epee::serialization::store_t_to_json(static_cast<epee::json_rpc::error_response&>(rsp), response_info.m_body); \ return true; \ } \ + epee::serialization::storage_entry id_; \ + id_ = epee::serialization::storage_entry(std::string()); \ + ps.get_value("id", id_, nullptr); \ std::string callback_name; \ if(!ps.get_value("method", callback_name, nullptr)) \ { \ @@ -225,18 +228,39 @@ namespace epee } \ if(false) return true; //just a stub to have "else if" -#define MAP_JON_RPC_WE(method_name, callback_f, command_type) \ - else if(callback_name == method_name) \ -{ \ + +#define PREPARE_OBJECTS_FROM_JSON(command_type) \ handled = true; \ boost::value_initialized<epee::json_rpc::request<command_type::request> > req_; \ epee::json_rpc::request<command_type::request>& req = static_cast<epee::json_rpc::request<command_type::request>&>(req_);\ - req.load(ps); \ + if(!req.load(ps)) \ + { \ + epee::json_rpc::error_response fail_resp = AUTO_VAL_INIT(fail_resp); \ + fail_resp.jsonrpc = "2.0"; \ + fail_resp.id = req.id; \ + fail_resp.error.code = -32602; \ + fail_resp.error.message = "Invalid params"; \ + epee::serialization::store_t_to_json(static_cast<epee::json_rpc::error_response&>(fail_resp), response_info.m_body); \ + return true; \ + } \ uint64_t ticks1 = epee::misc_utils::get_tick_count(); \ boost::value_initialized<epee::json_rpc::response<command_type::response, epee::json_rpc::dummy_error> > resp_; \ epee::json_rpc::response<command_type::response, epee::json_rpc::dummy_error>& resp = static_cast<epee::json_rpc::response<command_type::response, epee::json_rpc::dummy_error> &>(resp_); \ resp.jsonrpc = "2.0"; \ - resp.id = req.id; \ + resp.id = req.id; + +#define FINALIZE_OBJECTS_TO_JSON(method_name) \ + uint64_t ticks2 = epee::misc_utils::get_tick_count(); \ + epee::serialization::store_t_to_json(resp, response_info.m_body); \ + uint64_t ticks3 = epee::misc_utils::get_tick_count(); \ + response_info.m_mime_tipe = "application/json"; \ + response_info.m_header_info.m_content_type = " application/json"; \ + LOG_PRINT( query_info.m_URI << "[" << method_name << "] processed with " << ticks1-ticks << "/"<< ticks2-ticks1 << "/" << ticks3-ticks2 << "ms", LOG_LEVEL_2); + +#define MAP_JON_RPC_WE(method_name, callback_f, command_type) \ + else if(callback_name == method_name) \ +{ \ + PREPARE_OBJECTS_FROM_JSON(command_type) \ epee::json_rpc::error_response fail_resp = AUTO_VAL_INIT(fail_resp); \ fail_resp.jsonrpc = "2.0"; \ fail_resp.id = req.id; \ @@ -245,27 +269,14 @@ namespace epee epee::serialization::store_t_to_json(static_cast<epee::json_rpc::error_response&>(fail_resp), response_info.m_body); \ return true; \ } \ - uint64_t ticks2 = epee::misc_utils::get_tick_count(); \ - epee::serialization::store_t_to_json(resp, response_info.m_body); \ - uint64_t ticks3 = epee::misc_utils::get_tick_count(); \ - response_info.m_mime_tipe = "application/json"; \ - response_info.m_header_info.m_content_type = " application/json"; \ - LOG_PRINT( query_info.m_URI << "[" << method_name << "] processed with " << ticks1-ticks << "/"<< ticks2-ticks1 << "/" << ticks3-ticks2 << "ms", LOG_LEVEL_2); \ + FINALIZE_OBJECTS_TO_JSON(method_name) \ return true;\ } #define MAP_JON_RPC_WERI(method_name, callback_f, command_type) \ else if(callback_name == method_name) \ { \ - handled = true; \ - boost::value_initialized<epee::json_rpc::request<command_type::request> > req_; \ - epee::json_rpc::request<command_type::request>& req = static_cast<epee::json_rpc::request<command_type::request>&>(req_);\ - req.load(ps); \ - uint64_t ticks1 = epee::misc_utils::get_tick_count(); \ - boost::value_initialized<epee::json_rpc::response<command_type::response, epee::json_rpc::dummy_error> > resp_; \ - epee::json_rpc::response<command_type::response, epee::json_rpc::dummy_error>& resp = static_cast<epee::json_rpc::response<command_type::response, epee::json_rpc::dummy_error> &>(resp_); \ - resp.jsonrpc = "2.0"; \ - resp.id = req.id; \ + PREPARE_OBJECTS_FROM_JSON(command_type) \ epee::json_rpc::error_response fail_resp = AUTO_VAL_INIT(fail_resp); \ fail_resp.jsonrpc = "2.0"; \ fail_resp.id = req.id; \ @@ -274,28 +285,14 @@ namespace epee epee::serialization::store_t_to_json(static_cast<epee::json_rpc::error_response&>(fail_resp), response_info.m_body); \ return true; \ } \ - uint64_t ticks2 = epee::misc_utils::get_tick_count(); \ - epee::serialization::store_t_to_json(resp, response_info.m_body); \ - uint64_t ticks3 = epee::misc_utils::get_tick_count(); \ - response_info.m_mime_tipe = "application/json"; \ - response_info.m_header_info.m_content_type = " application/json"; \ - LOG_PRINT( query_info.m_URI << "[" << method_name << "] processed with " << ticks1-ticks << "/"<< ticks2-ticks1 << "/" << ticks3-ticks2 << "ms", LOG_LEVEL_2); \ + FINALIZE_OBJECTS_TO_JSON(method_name) \ return true;\ } - #define MAP_JON_RPC(method_name, callback_f, command_type) \ else if(callback_name == method_name) \ { \ - handled = true; \ - boost::value_initialized<epee::json_rpc::request<command_type::request> > req_; \ - epee::json_rpc::request<command_type::request>& req = static_cast<epee::json_rpc::request<command_type::request>&>(req_);\ - req.load(ps); \ - uint64_t ticks1 = epee::misc_utils::get_tick_count(); \ - boost::value_initialized<epee::json_rpc::response<command_type::response, epee::json_rpc::dummy_error> > resp_; \ - epee::json_rpc::response<command_type::response, epee::json_rpc::dummy_error>& resp = static_cast<epee::json_rpc::response<command_type::response, epee::json_rpc::dummy_error> &>(resp_); \ - resp.jsonrpc = "2.0"; \ - resp.id = req.id; \ + PREPARE_OBJECTS_FROM_JSON(command_type) \ if(!callback_f(req.params, resp.result, m_conn_context)) \ { \ epee::json_rpc::error_response fail_resp = AUTO_VAL_INIT(fail_resp); \ @@ -306,18 +303,13 @@ namespace epee epee::serialization::store_t_to_json(static_cast<epee::json_rpc::error_response&>(fail_resp), response_info.m_body); \ return true; \ } \ - uint64_t ticks2 = epee::misc_utils::get_tick_count(); \ - epee::serialization::store_t_to_json(resp, response_info.m_body); \ - uint64_t ticks3 = epee::misc_utils::get_tick_count(); \ - response_info.m_mime_tipe = "application/json"; \ - response_info.m_header_info.m_content_type = " application/json"; \ - LOG_PRINT( query_info.m_URI << "[" << method_name << "] processed with " << ticks1-ticks << "/"<< ticks2-ticks1 << "/" << ticks3-ticks2 << "ms", LOG_LEVEL_2); \ + FINALIZE_OBJECTS_TO_JSON(method_name) \ return true;\ } - #define END_JSON_RPC_MAP() \ epee::json_rpc::error_response rsp; \ + rsp.id = id_; \ rsp.jsonrpc = "2.0"; \ rsp.error.code = -32601; \ rsp.error.message = "Method not found"; \ |