From 7649f1f47d19657da32c48208aa22c3c02fc5d8f Mon Sep 17 00:00:00 2001 From: Peter Conrad Date: Thu, 6 Jun 2019 14:54:48 +0200 Subject: [PATCH] Removed broken http_server + unused http_api --- CMakeLists.txt | 2 - include/fc/network/http/server.hpp | 61 --------- include/fc/rpc/http_api.hpp | 35 ----- src/network/http/http_server.cpp | 207 ----------------------------- src/rpc/http_api.cpp | 141 -------------------- 5 files changed, 446 deletions(-) delete mode 100644 include/fc/network/http/server.hpp delete mode 100644 include/fc/rpc/http_api.hpp delete mode 100644 src/network/http/http_server.cpp delete mode 100644 src/rpc/http_api.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 50c70f2..9769e66 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -224,7 +224,6 @@ set( fc_sources src/interprocess/signals.cpp src/interprocess/file_mapping.cpp src/rpc/cli.cpp - src/rpc/http_api.cpp src/rpc/state.cpp src/rpc/websocket_api.cpp src/log/log_message.cpp @@ -256,7 +255,6 @@ set( fc_sources src/network/tcp_socket.cpp src/network/udp_socket.cpp src/network/http/http_connection.cpp - src/network/http/http_server.cpp src/network/http/websocket.cpp src/network/ip.cpp src/network/rate_limiting.cpp diff --git a/include/fc/network/http/server.hpp b/include/fc/network/http/server.hpp deleted file mode 100644 index a7dc3a3..0000000 --- a/include/fc/network/http/server.hpp +++ /dev/null @@ -1,61 +0,0 @@ -#pragma once -#include -#include -#include - -namespace fc { namespace http { - - /** - * Listens on a given port for incomming http - * connections and then calls a user provided callback - * function for every http request. - * - */ - class server - { - public: - server(); - server( uint16_t port ); - server( server&& s ); - ~server(); - - server& operator=(server&& s); - - class response - { - public: - class impl; - - response(); - response( const std::shared_ptr& my); - response( const response& r); - response( response&& r ); - ~response(); - response& operator=(const response& ); - response& operator=( response&& ); - - void add_header( const std::string& key, const std::string& val )const; - void set_status( const http::reply::status_code& s )const; - void set_length( uint64_t s )const; - - void write( const char* data, uint64_t len )const; - - private: - std::shared_ptr my; - }; - - void listen( const fc::ip::endpoint& p ); - fc::ip::endpoint get_local_endpoint() const; - - /** - * Set the callback to be called for every http request made. - */ - void on_request( const std::function& cb ); - - private: - class impl; - std::unique_ptr my; - }; - typedef std::shared_ptr server_ptr; - -} } diff --git a/include/fc/rpc/http_api.hpp b/include/fc/rpc/http_api.hpp deleted file mode 100644 index a693655..0000000 --- a/include/fc/rpc/http_api.hpp +++ /dev/null @@ -1,35 +0,0 @@ -#pragma once -#include -#include -#include -#include -#include -#include - -namespace fc { namespace rpc { - - class http_api_connection : public api_connection - { - public: - http_api_connection(uint32_t max_conversion_depth); - ~http_api_connection(); - - virtual variant send_call( - api_id_type api_id, - string method_name, - variants args = variants() ) override; - virtual variant send_callback( - uint64_t callback_id, - variants args = variants() ) override; - virtual void send_notice( - uint64_t callback_id, - variants args = variants() ) override; - - void on_request( - const fc::http::request& req, - const fc::http::server::response& resp ); - - fc::rpc::state _rpc_state; - }; - -} } // namespace fc::rpc diff --git a/src/network/http/http_server.cpp b/src/network/http/http_server.cpp deleted file mode 100644 index af06c6f..0000000 --- a/src/network/http/http_server.cpp +++ /dev/null @@ -1,207 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include - - -namespace fc { namespace http { - - class server::response::impl - { - public: - impl( const fc::http::connection_ptr& c, const std::function& cont = std::function() ) - :body_bytes_sent(0),body_length(0),con(c),handle_next_req(cont) - {} - - void send_header() { - //ilog( "sending header..." ); - fc::stringstream ss; - ss << "HTTP/1.1 " << rep.status << " "; - switch( rep.status ) { - case fc::http::reply::OK: ss << "OK\r\n"; break; - case fc::http::reply::RecordCreated: ss << "Record Created\r\n"; break; - case fc::http::reply::NotFound: ss << "Not Found\r\n"; break; - case fc::http::reply::Found: ss << "Found\r\n"; break; - default: ss << "Internal Server Error\r\n"; break; - } - for( uint32_t i = 0; i < rep.headers.size(); ++i ) { - ss << rep.headers[i].key <<": "<get_socket().write( s.c_str(), s.size() ); - } - - http::reply rep; - int64_t body_bytes_sent; - uint64_t body_length; - http::connection_ptr con; - std::function handle_next_req; - }; - - - class server::impl - { - public: - impl(){} - - impl(const fc::ip::endpoint& p ) - { - tcp_serv.set_reuse_address(); - tcp_serv.listen(p); - accept_complete = fc::async([this](){ this->accept_loop(); }, "http_server accept_loop"); - } - - ~impl() - { - try - { - tcp_serv.close(); - if (accept_complete.valid()) - accept_complete.wait(); - } - catch (...) - { - } - - for (fc::future& request_in_progress : requests_in_progress) - { - try - { - request_in_progress.cancel_and_wait(); - } - catch (const fc::exception& e) - { - wlog("Caught exception while canceling http request task: ${error}", ("error", e)); - } - catch (const std::exception& e) - { - wlog("Caught exception while canceling http request task: ${error}", ("error", e.what())); - } - catch (...) - { - wlog("Caught unknown exception while canceling http request task"); - } - } - requests_in_progress.clear(); - } - - void accept_loop() - { - while( !accept_complete.canceled() ) - { - http::connection_ptr con = std::make_shared(); - tcp_serv.accept( con->get_socket() ); - //ilog( "Accept Connection" ); - // clean up futures for any completed requests - for (auto iter = requests_in_progress.begin(); iter != requests_in_progress.end();) - if (!iter->valid() || iter->ready()) - iter = requests_in_progress.erase(iter); - else - ++iter; - requests_in_progress.emplace_back(fc::async([=](){ handle_connection(con, on_req); }, "http_server handle_connection")); - } - } - - void handle_connection( const http::connection_ptr& c, - std::function do_on_req ) - { - try - { - http::server::response rep( std::shared_ptr( new response::impl(c) ) ); - request req = c->read_request(); - if( do_on_req ) - do_on_req( req, rep ); - c->get_socket().close(); - } - catch ( fc::exception& e ) - { - wlog( "unable to read request ${1}", ("1", e.to_detail_string() ) );//fc::except_str().c_str()); - } - //wlog( "done handle connection" ); - } - - fc::future accept_complete; - std::function on_req; - std::vector > requests_in_progress; - fc::tcp_server tcp_serv; - }; - - - - server::server():my( new impl() ){} - server::server( uint16_t port ) :my( new impl(fc::ip::endpoint( fc::ip::address(),port)) ){} - server::server( server&& s ):my(std::move(s.my)){} - - server& server::operator=(server&& s) { std::swap(my,s.my); return *this; } - - server::~server(){} - - void server::listen( const fc::ip::endpoint& p ) - { - my.reset( new impl(p) ); - } - - fc::ip::endpoint server::get_local_endpoint() const - { - return my->tcp_serv.get_local_endpoint(); - } - - - server::response::response(){} - server::response::response( const server::response& s ):my(s.my){} - server::response::response( server::response&& s ):my(std::move(s.my)){} - server::response::response( const std::shared_ptr& m ):my(m){} - - server::response& server::response::operator=(const server::response& s) { my = s.my; return *this; } - server::response& server::response::operator=(server::response&& s) { std::swap(my,s.my); return *this; } - - void server::response::add_header( const std::string& key, const std::string& val )const { - my->rep.headers.push_back( fc::http::header( key, val ) ); - } - void server::response::set_status( const http::reply::status_code& s )const { - if( my->body_bytes_sent != 0 ) { - wlog( "Attempt to set status after sending headers" ); - } - my->rep.status = s; - } - void server::response::set_length( uint64_t s )const { - if( my->body_bytes_sent != 0 ) { - wlog( "Attempt to set length after sending headers" ); - } - my->body_length = s; - } - void server::response::write( const char* data, uint64_t len )const { - if( my->body_bytes_sent + len > my->body_length ) { - wlog( "Attempt to send to many bytes.." ); - len = my->body_bytes_sent + len - my->body_length; - } - if( my->body_bytes_sent == 0 ) { - my->send_header(); - } - my->body_bytes_sent += len; - my->con->get_socket().write( data, static_cast(len) ); - if( my->body_bytes_sent == int64_t(my->body_length) ) { - if( false || my->handle_next_req ) { - ilog( "handle next request..." ); - //fc::async( std::function(my->handle_next_req) ); - fc::async( my->handle_next_req, "http_server handle_next_req" ); - } - } - } - - server::response::~response(){} - - void server::on_request( const std::function& cb ) - { - my->on_req = cb; - } - - - - -} } diff --git a/src/rpc/http_api.cpp b/src/rpc/http_api.cpp deleted file mode 100644 index b8a299a..0000000 --- a/src/rpc/http_api.cpp +++ /dev/null @@ -1,141 +0,0 @@ - -#include - -namespace fc { namespace rpc { - -http_api_connection::~http_api_connection() -{ -} - -http_api_connection::http_api_connection(uint32_t max_depth) -:api_connection(max_depth) -{ - _rpc_state.add_method( "call", [this]( const variants& args ) -> variant - { - // TODO: This logic is duplicated between http_api_connection and websocket_api_connection - // it should be consolidated into one place instead of copy-pasted - FC_ASSERT( args.size() == 3 && args[2].is_array() ); - api_id_type api_id; - if( args[0].is_string() ) - { - variant subresult = this->receive_call( 1, args[0].as_string() ); - api_id = subresult.as_uint64(); - } - else - api_id = args[0].as_uint64(); - - return this->receive_call( - api_id, - 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 ); - } ); -} - -variant http_api_connection::send_call( - api_id_type api_id, - string method_name, - variants args /* = variants() */ ) -{ - // HTTP has no way to do this, so do nothing - return variant(); -} - -variant http_api_connection::send_callback( - uint64_t callback_id, - variants args /* = variants() */ ) -{ - // HTTP has no way to do this, so do nothing - return variant(); -} - -void http_api_connection::send_notice( - uint64_t callback_id, - variants args /* = variants() */ ) -{ - // HTTP has no way to do this, so do nothing - return; -} - -void http_api_connection::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, fc::json::legacy_parser, _max_conversion_depth ); - const auto& var_obj = var.get_object(); - - if( var_obj.contains( "method" ) ) - { - auto call = var.as(_max_conversion_depth); - try - { - try - { - fc::variant result( _rpc_state.local_call( call.method, call.params ), _max_conversion_depth ); - resp_body = fc::json::to_string( fc::variant( fc::rpc::response( *call.id, result ), _max_conversion_depth), - fc::json::stringify_large_ints_and_doubles, _max_conversion_depth ); - resp_status = http::reply::OK; - } - FC_CAPTURE_AND_RETHROW( (call.method)(call.params) ); - } - catch ( const fc::exception& e ) - { - resp_body = fc::json::to_string( fc::variant( fc::rpc::response( *call.id, error_object{ 1, e.to_detail_string(), fc::variant(e, _max_conversion_depth)} ), _max_conversion_depth), - fc::json::stringify_large_ints_and_doubles, _max_conversion_depth ); - 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; -} - -} } // namespace fc::rpc