Removed broken http_server + unused http_api

This commit is contained in:
Peter Conrad 2019-06-06 14:54:48 +02:00
parent 8196eb98c8
commit 7649f1f47d
5 changed files with 0 additions and 446 deletions

View file

@ -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

View file

@ -1,61 +0,0 @@
#pragma once
#include <fc/network/http/connection.hpp>
#include <functional>
#include <memory>
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<impl>& 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<impl> 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<void(const http::request&, const server::response& s )>& cb );
private:
class impl;
std::unique_ptr<impl> my;
};
typedef std::shared_ptr<server> server_ptr;
} }

View file

@ -1,35 +0,0 @@
#pragma once
#include <fc/io/json.hpp>
#include <fc/network/http/connection.hpp>
#include <fc/network/http/server.hpp>
#include <fc/reflect/variant.hpp>
#include <fc/rpc/api_connection.hpp>
#include <fc/rpc/state.hpp>
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

View file

@ -1,207 +0,0 @@
#include <fc/network/http/server.hpp>
#include <fc/thread/thread.hpp>
#include <fc/network/tcp_socket.hpp>
#include <fc/io/sstream.hpp>
#include <fc/network/ip.hpp>
#include <fc/io/stdio.hpp>
#include <fc/log/logger.hpp>
namespace fc { namespace http {
class server::response::impl
{
public:
impl( const fc::http::connection_ptr& c, const std::function<void()>& cont = std::function<void()>() )
: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 <<": "<<rep.headers[i].val <<"\r\n";
}
ss << "Content-Length: "<<body_length<<"\r\n\r\n";
auto s = ss.str();
//fc::cerr<<s<<"\n";
con->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<void()> 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<void>& 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<http::connection>();
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<void(const http::request&, const server::response& s )> do_on_req )
{
try
{
http::server::response rep( std::shared_ptr<response::impl>( 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<void> accept_complete;
std::function<void(const http::request&, const server::response& s)> on_req;
std::vector<fc::future<void> > 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<server::response::impl>& 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<size_t>(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<void()>(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<void(const http::request&, const server::response& s )>& cb )
{
my->on_req = cb;
}
} }

View file

@ -1,141 +0,0 @@
#include <fc/rpc/http_api.hpp>
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<fc::rpc::request>(_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