From f461dee4325b73dc05687e5a8a2471bd057fab82 Mon Sep 17 00:00:00 2001 From: theoreticalbts Date: Tue, 30 Jun 2015 03:00:43 -0400 Subject: [PATCH] http_api: Implement http_api_connection class to serve RPC over HTTP --- include/fc/rpc/http_api.hpp | 119 ++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 include/fc/rpc/http_api.hpp diff --git a/include/fc/rpc/http_api.hpp b/include/fc/rpc/http_api.hpp new file mode 100644 index 0000000..38acf98 --- /dev/null +++ b/include/fc/rpc/http_api.hpp @@ -0,0 +1,119 @@ +#pragma once +#include +#include +#include +#include +#include +#include + +namespace fc { namespace rpc { + + class http_api_connection : public api_connection + { + public: + ~http_api_connection() + { + } + + http_api_connection() + { + _rpc_state.add_method( "call", [this]( const variants& args ) -> variant { + FC_ASSERT( args.size() == 3 && args[2].is_array() ); + return this->receive_call( args[0].as_uint64(), + args[1].as_string(), + args[2].get_array() ); + }); + + _rpc_state.add_method( "notice", [this]( const variants& args ) -> variant { + FC_ASSERT( args.size() == 2 && args[1].is_array() ); + this->receive_notice( args[0].as_uint64(), args[1].get_array() ); + return variant(); + }); + + _rpc_state.add_method( "callback", [this]( const variants& args ) -> variant { + FC_ASSERT( args.size() == 2 && args[1].is_array() ); + this->receive_callback( args[0].as_uint64(), args[1].get_array() ); + return variant(); + }); + + _rpc_state.on_unhandled( [&]( const std::string& method_name, const variants& args ){ + return this->receive_call( 0, method_name, args ); + }); + } + + virtual variant send_call( api_id_type api_id, + string method_name, + variants args = variants() ) override + { + // HTTP has no way to do this, so do nothing + return variant(); + } + + virtual variant send_callback( uint64_t callback_id, variants args = variants() ) override + { + // HTTP has no way to do this, so do nothing + return variant(); + } + + virtual void send_notice( uint64_t callback_id, variants args = variants() ) override + { + // HTTP has no way to do this, so do nothing + return; + } + + void on_request( const fc::http::request& req, const fc::http::server::response& resp ) + { + // this must be called by outside HTTP server's on_request method + std::string resp_body; + http::reply::status_code resp_status; + + try + { + resp.add_header( "Content-Type", "application/json" ); + std::string req_body( req.body.begin(), req.body.end() ); + auto var = fc::json::from_string( req_body ); + const auto& var_obj = var.get_object(); + + if( var_obj.contains( "method" ) ) + { + auto call = var.as(); + try + { + auto result = _rpc_state.local_call( call.method, call.params ); + resp_body = fc::json::to_string( fc::rpc::response( *call.id, result ) ); + resp_status = http::reply::OK; + } + catch ( const fc::exception& e ) + { + resp_body = fc::json::to_string( fc::rpc::response( *call.id, error_object{ 1, e.to_detail_string(), fc::variant(e)} ) ); + resp_status = http::reply::InternalServerError; + } + } + else + { + resp_status = http::reply::BadRequest; + resp_body = ""; + } + } + catch ( const fc::exception& e ) + { + resp_status = http::reply::InternalServerError; + resp_body = ""; + wdump((e.to_detail_string())); + } + try + { + resp.set_status( resp_status ); + resp.set_length( resp_body.length() ); + resp.write( resp_body.c_str(), resp_body.length() ); + } + catch( const fc::exception& e ) + { + wdump((e.to_detail_string())); + } + return; + } + fc::rpc::state _rpc_state; + }; + +} } // namespace fc::rpc