FC Updates from BitShares and myself #21

Closed
nathanielhourt wants to merge 687 commits from dapp-support into latest-fc
2 changed files with 23 additions and 12 deletions
Showing only changes of commit 94200c1b57 - 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(const std::string& forward_header_key) = 0; virtual std::string get_remote_ip(const std::string& forward_header_key) = 0;
fc::signal<void()> closed; fc::signal<void()> closed;
private: private:

View file

@ -171,18 +171,28 @@ namespace fc { namespace http {
* @param forward_header_key the key to look at in the request header * @param forward_header_key the key to look at in the request header
* @returns the value in the header, otherwise the remote endpoint * @returns the value in the header, otherwise the remote endpoint
*/ */
virtual std::string get_remote_hostname(const std::string& forward_header_key) virtual std::string get_remote_ip(const std::string& forward_header_key) override
{ {
if (last_ip.empty() || last_forward_header_key != forward_header_key)
{
// refresh the cache
last_forward_header_key = forward_header_key;
if (!forward_header_key.empty()) if (!forward_header_key.empty())
{ {
std::string header_value = _ws_connection->get_request_header(forward_header_key); last_ip = get_request_header(forward_header_key);
if (!header_value.empty()) if (!header_value.empty())
return header_value; last_ip = header_value;
} }
return _ws_connection->get_remote_endpoint(); if (last_ip.empty())
last_ip = _ws_connection->get_remote_endpoint();
}
return last_ip;
} }
T _ws_connection; T _ws_connection;
// cache the value of the remote IP
std::string last_forward_header_key;
std::string last_ip;
}; };
typedef websocketpp::lib::shared_ptr<boost::asio::ssl::context> context_ptr; typedef websocketpp::lib::shared_ptr<boost::asio::ssl::context> context_ptr;
@ -197,8 +207,9 @@ namespace fc { namespace http {
_server.init_asio(&fc::asio::default_io_service()); _server.init_asio(&fc::asio::default_io_service());
_server.set_reuse_addr(true); _server.set_reuse_addr(true);
_server.set_open_handler( [&]( connection_hdl hdl ){ _server.set_open_handler( [&]( connection_hdl hdl ){
_server_thread.async( [&](){ _server_thread.async( [_server, _connections](){
auto new_con = std::make_shared<websocket_connection_impl<websocket_server_type::connection_ptr>>( _server.get_con_from_hdl(hdl) ); auto new_con = std::make_shared<websocket_connection_impl<
websocket_server_type::connection_ptr>>( _server.get_con_from_hdl(hdl) );
_on_connection( _connections[hdl] = new_con ); _on_connection( _connections[hdl] = new_con );
}).wait(); }).wait();
}); });
@ -209,7 +220,7 @@ namespace fc { namespace http {
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;
wlog( "Websocket Server Remote: ${host} Payload: ${body}", wlog( "Websocket Server Remote: ${host} Payload: ${body}",
("host", con->get_remote_hostname(fwd_header_key)) ("body", msg->get_payload())); ("host", con->get_remote_ip(fwd_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 )
@ -346,7 +357,7 @@ namespace fc { namespace http {
auto received = msg->get_payload(); auto received = msg->get_payload();
std::shared_ptr<websocket_connection> con = current_con->second; std::shared_ptr<websocket_connection> con = current_con->second;
wlog( "Websocket TLS Server Remote: ${host} Payload: ${body}", wlog( "Websocket TLS Server Remote: ${host} Payload: ${body}",
("host", con->get_remote_hostname(fwd_header_key)) ("body", msg->get_payload())); ("host", con->get_remote_ip(fwd_header_key)) ("body", msg->get_payload()));
fc::async([con,received](){ con->on_message( received ); }); fc::async([con,received](){ con->on_message( received ); });
}).wait(); }).wait();
}); });