Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A handful of fc changes to support unix sockets for HTTP RPC #12

Merged
merged 6 commits into from
Aug 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 2 additions & 48 deletions include/fc/network/url.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ namespace fc {
explicit url( const string& u );
url( const url& c );
url( url&& c );
url( mutable_url&& c );
url( const mutable_url& c );
url( const string& proto, const ostring& host, const ostring& user, const ostring& pass,
const opath& path, const ostring& query, const ovariant_object& args, const fc::optional<uint16_t>& port);
~url();

url& operator=( const url& c );
Expand Down Expand Up @@ -59,51 +59,5 @@ namespace fc {
void to_variant( const url& u, fc::variant& v );
void from_variant( const fc::variant& v, url& u );

/**
* Used to create / manipulate a URL
*/
class mutable_url
{
public:
mutable_url();
explicit mutable_url( const string& mutable_url );
mutable_url( const mutable_url& c );
mutable_url( const url& c );
mutable_url( mutable_url&& c );
~mutable_url();

mutable_url& operator=( const url& c );
mutable_url& operator=( const mutable_url& c );
mutable_url& operator=( mutable_url&& c );

bool operator==( const mutable_url& cmp )const;
bool operator==( const url& cmp )const;

operator string()const;

//// file, ssh, tcp, http, ssl, etc...
string proto()const;
ostring host()const;
ostring user()const;
ostring pass()const;
opath path()const;
ostring query()const;
ovariant_object args()const;
fc::optional<uint16_t> port()const;

void set_proto( string );
void set_host( string );
void set_user( string );
void set_pass( string );
void set_path( fc::path p );
void set_query( string );
void set_args( variant_object );
void set_port( uint16_t );

private:
friend class url;
std::unique_ptr<detail::url_impl> my;
};

} // namespace fc

68 changes: 66 additions & 2 deletions src/network/http/http_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/ssl/error.hpp>
#include <boost/asio/ssl/stream.hpp>
#include <boost/asio/local/stream_protocol.hpp>
#include <boost/filesystem.hpp>

using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
namespace http = boost::beast::http; // from <boost/beast/http.hpp>
namespace ssl = boost::asio::ssl; // from <boost/asio/ssl.hpp>
namespace local = boost::asio::local;

namespace fc {

Expand All @@ -29,8 +32,10 @@ class http_client_impl {
using host_key = std::tuple<std::string, std::string, uint16_t>;
using raw_socket_ptr = std::unique_ptr<tcp::socket>;
using ssl_socket_ptr = std::unique_ptr<ssl::stream<tcp::socket>>;
using connection = static_variant<raw_socket_ptr, ssl_socket_ptr>;
using unix_socket_ptr = std::unique_ptr<local::stream_protocol::socket>;
using connection = static_variant<raw_socket_ptr, ssl_socket_ptr, unix_socket_ptr>;
using connection_map = std::map<host_key, connection>;
using unix_url_split_map = std::map<string, fc::url>;
using error_code = boost::system::error_code;
using deadline_type = boost::posix_time::ptime;

Expand Down Expand Up @@ -153,6 +158,21 @@ class http_client_impl {
return std::make_tuple(dest.proto(), *dest.host(), port);
}

connection_map::iterator create_unix_connection( const url& dest, const deadline_type& deadline) {
auto key = url_to_host_key(dest);
auto socket = std::make_unique<local::stream_protocol::socket>(_ioc);

error_code ec;
socket->connect(local::stream_protocol::endpoint(*dest.host()), ec);
FC_ASSERT(!ec, "Failed to connect: ${message}", ("message",ec.message()));

auto res = _connections.emplace(std::piecewise_construct,
std::forward_as_tuple(key),
std::forward_as_tuple(std::move(socket)));

return res.first;
}

connection_map::iterator create_raw_connection( const url& dest, const deadline_type& deadline ) {
auto key = url_to_host_key(dest);
auto socket = std::make_unique<tcp::socket>(_ioc);
Expand Down Expand Up @@ -200,6 +220,8 @@ class http_client_impl {
return create_raw_connection(dest, deadline);
} else if (dest.proto() == "https") {
return create_ssl_connection(dest, deadline);
} else if (dest.proto() == "unix") {
return create_unix_connection(dest, deadline);
} else {
FC_THROW("Unknown protocol ${proto}", ("proto", dest.proto()));
}
Expand All @@ -213,6 +235,10 @@ class http_client_impl {
bool operator() ( const ssl_socket_ptr& ptr ) const {
return !ptr->lowest_layer().is_open();
}

bool operator() ( const unix_socket_ptr& ptr) const {
return !ptr->is_open();
}
};

bool check_closed( const connection_map::iterator& conn_itr ) {
Expand Down Expand Up @@ -350,10 +376,45 @@ class http_client_impl {
return result;
}

/*
Unix URLs work a little special here. They'll originally be in the format of
unix:///home/username/eosio-wallet/keosd.sock/v1/wallet/sign_digest
for example. When the fc::url is given to http_client in post_sync(), this will
have proto=unix and host=/home/username/eosio-wallet/keosd.sock/v1/wallet/sign_digest

At this point we still don't know what part of the above string is the unix socket path
and which part is the path to access on the server. This function discovers that
host=/home/username/eosio-wallet/keosd.sock and path=/v1/wallet/sign_digest
and creates another fc::url that will be used downstream of the http_client::post_sync()
call.
*/
const fc::url& get_unix_url(const std::string& full_url) {
unix_url_split_map::const_iterator found = _unix_url_paths.find(full_url);
if(found != _unix_url_paths.end())
return found->second;

boost::filesystem::path socket_file(full_url);
if(socket_file.is_relative())
FC_THROW_EXCEPTION( parse_error_exception, "socket url cannot be relative (${url})", ("url", socket_file.string()));
if(socket_file.empty())
FC_THROW_EXCEPTION( parse_error_exception, "missing socket url");
boost::filesystem::path url_path;
do {
if(boost::filesystem::status(socket_file).type() == boost::filesystem::socket_file)
break;
url_path = socket_file.filename() / url_path;
socket_file = socket_file.remove_filename();
} while(!socket_file.empty());
if(socket_file.empty())
FC_THROW_EXCEPTION( parse_error_exception, "couldn't discover socket path");
url_path = "/" / url_path;
return _unix_url_paths.emplace(full_url, fc::url("unix", socket_file.string(), ostring(), ostring(), url_path.string(), ostring(), ovariant_object(), fc::optional<uint16_t>())).first->second;
}

boost::asio::io_context _ioc;
ssl::context _sslc;
connection_map _connections;
unix_url_split_map _unix_url_paths;
};


Expand All @@ -364,7 +425,10 @@ http_client::http_client()
}

variant http_client::post_sync(const url& dest, const variant& payload, const fc::time_point& deadline) {
return _my->post_sync(dest, payload, deadline);
if(dest.proto() == "unix")
return _my->post_sync(_my->get_unix_url(*dest.host()), payload, deadline);
else
return _my->post_sync(dest, payload, deadline);
}

void http_client::add_cert(const std::string& cert_pem_string) {
Expand Down
Loading