| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
#pragma once |
| 29 |
#include <boost/utility/string_ref.hpp> |
| 30 |
#include <chrono> |
| 31 |
#include <string> |
| 32 |
#include "byte_slice.h" |
| 33 |
#include "portable_storage_template_helper.h" |
| 34 |
#include "net/http_base.h" |
| 35 |
#include "net/http_server_handlers_map2.h" |
| 36 |
|
| 37 |
namespace epee |
| 38 |
{ |
| 39 |
namespace net_utils |
| 40 |
{ |
| 41 |
template<class t_request, class t_response, class t_transport> |
| 42 |
bool invoke_http_json(const boost::string_ref uri, const t_request& out_struct, t_response& result_struct, t_transport& transport, std::chrono::milliseconds timeout = std::chrono::seconds(15), const boost::string_ref method = "POST") |
| 43 |
{ |
| 44 |
std::string req_param; |
| 45 |
if(!serialization::store_t_to_json(out_struct, req_param)) |
| 46 |
return false; |
| 47 |
|
| 48 |
http::fields_list additional_params; |
| 49 |
additional_params.push_back(std::make_pair("Content-Type","application/json; charset=utf-8")); |
| 50 |
|
| 51 |
const http::http_response_info* pri = NULL; |
| 52 |
if(!transport.invoke(uri, method, req_param, timeout, std::addressof(pri), std::move(additional_params))) |
| 53 |
{ |
| 54 |
LOG_PRINT_L1("Failed to invoke http request to " << uri); |
| 55 |
return false; |
| 56 |
} |
| 57 |
|
| 58 |
if(!pri) |
| 59 |
{ |
| 60 |
LOG_PRINT_L1("Failed to invoke http request to " << uri << ", internal error (null response ptr)"); |
| 61 |
return false; |
| 62 |
} |
| 63 |
|
| 64 |
if(pri->m_response_code != 200) |
| 65 |
{ |
| 66 |
LOG_PRINT_L1("Failed to invoke http request to " << uri << ", wrong response code: " << pri->m_response_code); |
| 67 |
return false; |
| 68 |
} |
| 69 |
|
| 70 |
return serialization::load_t_from_json(result_struct, pri->m_body); |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
template<class t_request, class t_response, class t_transport> |
| 76 |
bool invoke_http_bin(const boost::string_ref uri, const t_request& out_struct, t_response& result_struct, t_transport& transport, std::chrono::milliseconds timeout = std::chrono::seconds(15), const boost::string_ref method = "POST") |
| 77 |
{ |
| 78 |
byte_slice req_param; |
| 79 |
if(!serialization::store_t_to_binary(out_struct, req_param, 16 * 1024)) |
| 80 |
return false; |
| 81 |
|
| 82 |
const http::http_response_info* pri = NULL; |
| 83 |
if(!transport.invoke(uri, method, boost::string_ref{reinterpret_cast<const char*>(req_param.data()), req_param.size()}, timeout, std::addressof(pri))) |
| 84 |
{ |
| 85 |
LOG_PRINT_L1("Failed to invoke http request to " << uri); |
| 86 |
return false; |
| 87 |
} |
| 88 |
|
| 89 |
if(!pri) |
| 90 |
{ |
| 91 |
LOG_PRINT_L1("Failed to invoke http request to " << uri << ", internal error (null response ptr)"); |
| 92 |
return false; |
| 93 |
} |
| 94 |
|
| 95 |
if(pri->m_response_code != 200) |
| 96 |
{ |
| 97 |
LOG_PRINT_L1("Failed to invoke http request to " << uri << ", wrong response code: " << pri->m_response_code); |
| 98 |
return false; |
| 99 |
} |
| 100 |
|
| 101 |
static const constexpr epee::serialization::portable_storage::limits_t default_http_bin_limits = { |
| 102 |
65536 * 3, |
| 103 |
65536 * 3, |
| 104 |
65536 * 3, |
| 105 |
}; |
| 106 |
return serialization::load_t_from_binary(result_struct, epee::strspan<uint8_t>(pri->m_body), &default_http_bin_limits); |
| 107 |
} |
| 108 |
|
| 109 |
template<class t_request, class t_response, class t_transport> |
| 110 |
bool invoke_http_json_rpc(const boost::string_ref uri, std::string method_name, const t_request& out_struct, t_response& result_struct, epee::json_rpc::error &error_struct, t_transport& transport, std::chrono::milliseconds timeout = std::chrono::seconds(15), const boost::string_ref http_method = "POST", const std::string& req_id = "0") |
| 111 |
{ |
| 112 |
epee::json_rpc::request<t_request> req_t = AUTO_VAL_INIT(req_t); |
| 113 |
req_t.jsonrpc = "2.0"; |
| 114 |
req_t.id = req_id; |
| 115 |
req_t.method = std::move(method_name); |
| 116 |
req_t.params = out_struct; |
| 117 |
epee::json_rpc::response<t_response, epee::json_rpc::error> resp_t = AUTO_VAL_INIT(resp_t); |
| 118 |
if(!epee::net_utils::invoke_http_json(uri, req_t, resp_t, transport, timeout, http_method)) |
| 119 |
{ |
| 120 |
error_struct = {}; |
| 121 |
return false; |
| 122 |
} |
| 123 |
if(resp_t.error.code || resp_t.error.message.size()) |
| 124 |
{ |
| 125 |
error_struct = resp_t.error; |
| 126 |
LOG_ERROR("RPC call of \"" << req_t.method << "\" returned error: " << resp_t.error.code << ", message: " << resp_t.error.message); |
| 127 |
return false; |
| 128 |
} |
| 129 |
result_struct = resp_t.result; |
| 130 |
return true; |
| 131 |
} |
| 132 |
|
| 133 |
template<class t_request, class t_response, class t_transport> |
| 134 |
bool invoke_http_json_rpc(const boost::string_ref uri, std::string method_name, const t_request& out_struct, t_response& result_struct, t_transport& transport, std::chrono::milliseconds timeout = std::chrono::seconds(15), const boost::string_ref http_method = "POST", const std::string& req_id = "0") |
| 135 |
{ |
| 136 |
epee::json_rpc::error error_struct; |
| 137 |
return invoke_http_json_rpc(uri, method_name, out_struct, result_struct, error_struct, transport, timeout, http_method, req_id); |
| 138 |
} |
| 139 |
|
| 140 |
template<class t_command, class t_transport> |
| 141 |
bool invoke_http_json_rpc(const boost::string_ref uri, typename t_command::request& out_struct, typename t_command::response& result_struct, t_transport& transport, std::chrono::milliseconds timeout = std::chrono::seconds(15), const boost::string_ref http_method = "POST", const std::string& req_id = "0") |
| 142 |
{ |
| 143 |
return invoke_http_json_rpc(uri, t_command::methodname(), out_struct, result_struct, transport, timeout, http_method, req_id); |
| 144 |
} |
| 145 |
|
| 146 |
} |
| 147 |
} |