Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
http_api.cpp
Go to the documentation of this file.
1
2#include <fc/rpc/http_api.hpp>
3
4namespace fc { namespace rpc {
5
9
11{
12 _rpc_state.add_method( "call", [this]( const variants& args ) -> variant
13 {
14 // TODO: This logic is duplicated between http_api_connection and websocket_api_connection
15 // it should be consolidated into one place instead of copy-pasted
16 FC_ASSERT( args.size() == 3 && args[2].is_array() );
17 api_id_type api_id;
18 if( args[0].is_string() )
19 {
20 variants subargs;
21 subargs.push_back( args[0] );
22 variant subresult = this->receive_call( 1, "get_api_by_name", subargs );
23 api_id = subresult.as_uint64();
24 }
25 else
26 api_id = args[0].as_uint64();
27
28 return this->receive_call(
29 api_id,
30 args[1].as_string(),
31 args[2].get_array() );
32 } );
33
34 _rpc_state.add_method( "notice", [this]( const variants& args ) -> variant
35 {
36 FC_ASSERT( args.size() == 2 && args[1].is_array() );
37 this->receive_notice(
38 args[0].as_uint64(),
39 args[1].get_array() );
40 return variant();
41 } );
42
43 _rpc_state.add_method( "callback", [this]( const variants& args ) -> variant
44 {
45 FC_ASSERT( args.size() == 2 && args[1].is_array() );
46 this->receive_callback(
47 args[0].as_uint64(),
48 args[1].get_array() );
49 return variant();
50 } );
51
52 _rpc_state.on_unhandled( [&]( const std::string& method_name, const variants& args )
53 {
54 return this->receive_call( 0, method_name, args );
55 } );
56}
57
59 api_id_type api_id,
60 string method_name,
61 variants args /* = variants() */ )
62{
63 // HTTP has no way to do this, so do nothing
64 return variant();
65}
66
68 uint64_t callback_id,
69 variants args /* = variants() */ )
70{
71 // HTTP has no way to do this, so do nothing
72 return variant();
73}
74
76 uint64_t callback_id,
77 variants args /* = variants() */ )
78{
79 // HTTP has no way to do this, so do nothing
80 return;
81}
82
83void http_api_connection::on_request( const fc::http::request& req, const fc::http::server::response& resp )
84{
85 // this must be called by outside HTTP server's on_request method
86 std::string resp_body;
87 http::reply::status_code resp_status;
88
89 auto handle_error = [&](const auto& e)
90 {
91 resp_status = http::reply::InternalServerError;
92 resp_body = "";
93 wdump((e.to_detail_string()));
94 };
95
96 try
97 {
98 resp.add_header( "Content-Type", "application/json" );
99 std::string req_body( req.body.begin(), req.body.end() );
100 auto var = fc::json::from_string( req_body );
101 const auto& var_obj = var.get_object();
102
103 if( var_obj.contains( "method" ) )
104 {
105 auto call = var.as<fc::rpc::request>();
106 auto handle_error_inner = [&](const auto& e)
107 {
108 resp_body = fc::json::to_string( fc::rpc::response( *call.id, error_object{ 1, e.to_detail_string(), fc::variant(e)} ) );
109 resp_status = http::reply::InternalServerError;}
110 };
111
112 try
113 {
114 auto result = _rpc_state.local_call( call.method, call.params );
115 resp_body = fc::json::to_string( fc::rpc::response( *call.id, result ) );
116 resp_status = http::reply::OK;
117 }
118 catch ( const std::bad_alloc& )
119 {
120 throw;
121 }
122 catch ( const boost::interprocess::bad_alloc& )
123 {
124 throw;
125 }
126 catch ( const fc::exception& e )
127 {
128 handle_error_inner(e);
129 }
130 catch ( const std::exception& e )
131 {
133 }
134 }
135 else
136 {
137 resp_status = http::reply::BadRequest;
138 resp_body = "";
139 }
140 }
141 catch ( const std::bad_alloc& )
142 {
143 throw;
144 }
145 catch ( const boost::interprocess::bad_alloc& )
146 {
147 throw;
148 }
149 catch ( const fc::exception& e )
150 {
151 handle_error(e);
152 }
153 catch ( const std::exception& e )
154 {
156 }
157
158 try
159 {
160 resp.set_status( resp_status );
161 resp.set_length( resp_body.length() );
162 resp.write( resp_body.c_str(), resp_body.length() );
163 }
164 catch ( const std::bad_alloc& )
165 {
166 throw;
167 }
168 catch ( const boost::interprocess::bad_alloc& )
169 {
170 throw;
171 }
172 catch( const fc::exception& e )
173 {
174 wdump((e.to_detail_string()));
175 }
176 catch ( const std::exception& e )
177 {
179 }
180 return;
181}
182
183} } // namespace fc::rpc
variant receive_callback(uint64_t callback_id, const variants &args=variants()) const
void receive_notice(uint64_t callback_id, const variants &args=variants()) const
variant receive_call(api_id_type api_id, const string &method_name, const variants &args=variants()) const
Used to generate a useful error report when an exception is thrown.
Definition exception.hpp:58
std::string to_detail_string(log_level ll=log_level::all) const
static string to_string(const variant &v, const yield_function_t &yield, const output_formatting format=output_formatting::stringify_large_ints_and_doubles)
Definition json.cpp:674
static variant from_string(const string &utf8_str, const parse_type ptype=parse_type::legacy_parser, uint32_t max_depth=DEFAULT_MAX_RECURSION_DEPTH)
Definition json.cpp:442
virtual variant send_callback(uint64_t callback_id, variants args=variants()) override
Definition http_api.cpp:67
void on_request(const fc::http::request &req, const fc::http::server::response &resp)
Definition http_api.cpp:83
virtual variant send_call(api_id_type api_id, string method_name, variants args=variants()) override
Definition http_api.cpp:58
virtual void send_notice(uint64_t callback_id, variants args=variants()) override
Definition http_api.cpp:75
static std_exception_wrapper from_current_exception(const std::exception &e)
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object's.
Definition variant.hpp:191
uint64_t as_uint64() const
Definition variant.cpp:398
T as() const
Definition variant.hpp:327
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
#define wdump(SEQ)
Definition logger.hpp:160
namespace sysio::chain
Definition authority.cpp:3
uint32_t api_id_type
Definition api.hpp:46
std::vector< fc::variant > variants
Definition variant.hpp:173
fc::variant call(const std::string &url, const std::string &path, const T &v)
Definition main.cpp:258
unsigned __int64 uint64_t
Definition stdint.h:136