FC Updates from BitShares and myself #21

Closed
nathanielhourt wants to merge 687 commits from dapp-support into latest-fc
4 changed files with 20 additions and 13 deletions
Showing only changes of commit d821e865ed - Show all commits

View file

@ -30,7 +30,7 @@ namespace fc { namespace http {
boost::any& get_session_data() { return _session_data; } boost::any& get_session_data() { return _session_data; }
virtual std::string get_request_header(const std::string& key) = 0; virtual std::string get_request_header(const std::string& key) = 0;
virtual std::string get_remote_hostname() = 0; virtual std::string get_remote_hostname(const std::string& forward_header_key) = 0;
fc::signal<void()> closed; fc::signal<void()> closed;
private: private:
@ -45,7 +45,7 @@ namespace fc { namespace http {
class websocket_server class websocket_server
{ {
public: public:
websocket_server(); websocket_server( std::string forward_header_key = std::string() );
~websocket_server(); ~websocket_server();
void on_connection( const on_connection_handler& handler); void on_connection( const on_connection_handler& handler);

View file

@ -165,11 +165,16 @@ namespace fc { namespace http {
return _ws_connection->get_request_header(key); return _ws_connection->get_request_header(key);
} }
virtual std::string get_remote_hostname() virtual std::string get_remote_hostname(const std::string& forward_header_key)
{ {
// TODO: check headers, revert to the raw connection details // TODO: check headers, revert to the raw connection details
// Notes: T is a pointer to a websocketpp::connection (see connection.hpp) if (!forward_header_key.empty())
return _ws_connection->get_uri()->get_host(); {
std::string header_value = _ws_connection->get_request_header(forward_header_key);
if (!header_value.empty())
return header_value;
}
return _ws_connection->get_remote_endpoint();
} }
T _ws_connection; T _ws_connection;
@ -180,7 +185,7 @@ namespace fc { namespace http {
class websocket_server_impl class websocket_server_impl
{ {
public: public:
websocket_server_impl() websocket_server_impl(const std::string& forward_header_key = std::string() )
:_server_thread( fc::thread::current() ) :_server_thread( fc::thread::current() )
{ {
@ -199,7 +204,8 @@ namespace fc { namespace http {
assert( current_con != _connections.end() ); assert( current_con != _connections.end() );
auto payload = msg->get_payload(); auto payload = msg->get_payload();
std::shared_ptr<websocket_connection> con = current_con->second; std::shared_ptr<websocket_connection> con = current_con->second;
wdump( ("server") ( con->get_remote_hostname() ) (msg->get_payload())); wlog( "Websocket Server Remote: ${host} Payload: ${body}",
("host", con->get_remote_hostname(forward_header_key)) ("body", msg->get_payload()));
++_pending_messages; ++_pending_messages;
auto f = fc::async([this,con,payload](){ if( _pending_messages ) --_pending_messages; con->on_message( payload ); }); auto f = fc::async([this,con,payload](){ if( _pending_messages ) --_pending_messages; con->on_message( payload ); });
if( _pending_messages > 100 ) if( _pending_messages > 100 )
@ -584,7 +590,8 @@ namespace fc { namespace http {
} // namespace detail } // namespace detail
websocket_server::websocket_server():my( new detail::websocket_server_impl() ) {} websocket_server::websocket_server(std::string forward_header_key)
:my( new detail::websocket_server_impl(forward_header_key) ) {}
websocket_server::~websocket_server(){} websocket_server::~websocket_server(){}
void websocket_server::on_connection( const on_connection_handler& handler ) void websocket_server::on_connection( const on_connection_handler& handler )

View file

@ -19,7 +19,7 @@ BOOST_AUTO_TEST_CASE(websocket_test)
fc::http::websocket_connection_ptr s_conn, c_conn; fc::http::websocket_connection_ptr s_conn, c_conn;
int port; int port;
{ {
fc::http::websocket_server server; fc::http::websocket_server server("MyProxyHeaderKey");
server.on_connection([&]( const fc::http::websocket_connection_ptr& c ){ server.on_connection([&]( const fc::http::websocket_connection_ptr& c ){
s_conn = c; s_conn = c;
c->on_message_handler([&](const std::string& s){ c->on_message_handler([&](const std::string& s){

View file

@ -17,9 +17,9 @@ int main(int argc, char** argv)
fc::http::websocket_client client; fc::http::websocket_client client;
fc::http::websocket_connection_ptr s_conn, c_conn; fc::http::websocket_connection_ptr s_conn, c_conn;
int port = std::stoi(argv[1]); std::string url = argv[1];
wlog( "Connecting to server at port ${port}", ("port", argv[1]) ); wlog( "Connecting to server at url ${url}", ("url", url) );
c_conn = client.connect( "ws://127.0.0.1:" + fc::to_string(port) ); c_conn = client.connect( "ws://" + url );
std::string echo; std::string echo;
c_conn->on_message_handler([&](const std::string& s){ c_conn->on_message_handler([&](const std::string& s){