2015-03-26 20:51:10 +00:00
|
|
|
#pragma once
|
|
|
|
|
#include <functional>
|
|
|
|
|
#include <memory>
|
|
|
|
|
#include <string>
|
2019-04-04 11:46:38 +00:00
|
|
|
#include <boost/any.hpp>
|
2015-03-30 20:56:28 +00:00
|
|
|
#include <fc/network/ip.hpp>
|
2019-05-24 12:33:46 +00:00
|
|
|
#include <fc/network/http/connection.hpp>
|
2015-04-01 14:25:57 +00:00
|
|
|
#include <fc/signals.hpp>
|
2015-03-26 20:51:10 +00:00
|
|
|
|
|
|
|
|
namespace fc { namespace http {
|
|
|
|
|
namespace detail {
|
2016-04-20 02:58:37 +00:00
|
|
|
class websocket_server_impl;
|
|
|
|
|
class websocket_tls_server_impl;
|
2015-03-26 20:51:10 +00:00
|
|
|
class websocket_client_impl;
|
2015-05-12 20:16:10 +00:00
|
|
|
class websocket_tls_client_impl;
|
2019-05-24 12:33:46 +00:00
|
|
|
} // namespace detail
|
2015-03-26 20:51:10 +00:00
|
|
|
|
2020-05-01 23:43:29 +00:00
|
|
|
// TODO refactor code, move stuff used by server or client only to derived class(es),
|
|
|
|
|
// E.G. it seems get_request_header and on_http* are for server only.
|
2015-03-26 20:51:10 +00:00
|
|
|
class websocket_connection
|
|
|
|
|
{
|
|
|
|
|
public:
|
2015-05-27 20:15:49 +00:00
|
|
|
virtual ~websocket_connection(){}
|
2015-03-26 20:51:10 +00:00
|
|
|
virtual void send_message( const std::string& message ) = 0;
|
2015-03-27 20:29:33 +00:00
|
|
|
virtual void close( int64_t code, const std::string& reason ){};
|
|
|
|
|
void on_message( const std::string& message ) { _on_message(message); }
|
2019-05-24 12:33:46 +00:00
|
|
|
fc::http::reply on_http( const std::string& message ) { return _on_http(message); }
|
2015-03-26 20:51:10 +00:00
|
|
|
|
2015-03-27 20:29:33 +00:00
|
|
|
void on_message_handler( const std::function<void(const std::string&)>& h ) { _on_message = h; }
|
2019-05-24 12:33:46 +00:00
|
|
|
void on_http_handler( const std::function<fc::http::reply(const std::string&)>& h ) { _on_http = h; }
|
2015-03-26 20:51:10 +00:00
|
|
|
|
2019-04-04 11:46:38 +00:00
|
|
|
void set_session_data( boost::any d ){ _session_data = std::move(d); }
|
|
|
|
|
boost::any& get_session_data() { return _session_data; }
|
2015-04-01 14:25:57 +00:00
|
|
|
|
2017-01-16 19:55:17 +00:00
|
|
|
virtual std::string get_request_header(const std::string& key) = 0;
|
2020-05-01 21:18:31 +00:00
|
|
|
|
2020-05-02 00:51:47 +00:00
|
|
|
const std::string& get_remote_endpoint_string()const { return _remote_endpoint; }
|
2017-01-16 19:55:17 +00:00
|
|
|
|
2015-04-01 14:25:57 +00:00
|
|
|
fc::signal<void()> closed;
|
2020-05-01 21:18:31 +00:00
|
|
|
protected:
|
|
|
|
|
std::string _remote_endpoint; // for logging
|
2015-03-26 20:51:10 +00:00
|
|
|
private:
|
2019-04-04 11:46:38 +00:00
|
|
|
boost::any _session_data;
|
2015-05-12 18:50:08 +00:00
|
|
|
std::function<void(const std::string&)> _on_message;
|
2019-05-24 12:33:46 +00:00
|
|
|
std::function<fc::http::reply(const std::string&)> _on_http;
|
2015-03-26 20:51:10 +00:00
|
|
|
};
|
2015-03-27 20:29:33 +00:00
|
|
|
typedef std::shared_ptr<websocket_connection> websocket_connection_ptr;
|
2015-03-26 20:51:10 +00:00
|
|
|
|
2015-03-27 20:29:33 +00:00
|
|
|
typedef std::function<void(const websocket_connection_ptr&)> on_connection_handler;
|
2015-03-26 20:51:10 +00:00
|
|
|
|
2020-06-20 17:01:22 +00:00
|
|
|
// TODO websocket_tls_server and websocket_server have almost the same interface and implementation,
|
|
|
|
|
// better refactor to remove duplicate code and to avoid undesired or unnecessary differences
|
2015-05-27 20:15:49 +00:00
|
|
|
class websocket_server
|
2015-03-26 20:51:10 +00:00
|
|
|
{
|
|
|
|
|
public:
|
2020-05-01 21:18:31 +00:00
|
|
|
websocket_server(const std::string& forward_header_key);
|
2015-03-26 20:51:10 +00:00
|
|
|
~websocket_server();
|
|
|
|
|
|
2015-03-27 20:29:33 +00:00
|
|
|
void on_connection( const on_connection_handler& handler);
|
2015-03-26 20:51:10 +00:00
|
|
|
void listen( uint16_t port );
|
2015-03-30 20:56:28 +00:00
|
|
|
void listen( const fc::ip::endpoint& ep );
|
2019-05-04 22:44:33 +00:00
|
|
|
uint16_t get_listening_port();
|
2015-03-26 20:51:10 +00:00
|
|
|
void start_accept();
|
|
|
|
|
|
2019-05-04 22:44:33 +00:00
|
|
|
void stop_listening();
|
|
|
|
|
void close();
|
|
|
|
|
|
2015-03-26 20:51:10 +00:00
|
|
|
private:
|
2016-04-20 02:58:37 +00:00
|
|
|
friend class detail::websocket_server_impl;
|
|
|
|
|
std::unique_ptr<detail::websocket_server_impl> my;
|
2015-03-26 20:51:10 +00:00
|
|
|
};
|
|
|
|
|
|
2015-05-12 18:50:08 +00:00
|
|
|
|
2020-06-20 17:01:22 +00:00
|
|
|
// TODO websocket_tls_server and websocket_server have almost the same interface and implementation,
|
|
|
|
|
// better refactor to remove duplicate code and to avoid undesired or unnecessary differences
|
2015-05-27 20:15:49 +00:00
|
|
|
class websocket_tls_server
|
2015-05-12 18:50:08 +00:00
|
|
|
{
|
|
|
|
|
public:
|
2020-05-01 21:18:31 +00:00
|
|
|
websocket_tls_server( const std::string& server_pem,
|
|
|
|
|
const std::string& ssl_password,
|
|
|
|
|
const std::string& forward_header_key );
|
2015-05-12 18:50:08 +00:00
|
|
|
~websocket_tls_server();
|
|
|
|
|
|
|
|
|
|
void on_connection( const on_connection_handler& handler);
|
|
|
|
|
void listen( uint16_t port );
|
|
|
|
|
void listen( const fc::ip::endpoint& ep );
|
2020-06-20 17:01:22 +00:00
|
|
|
uint16_t get_listening_port();
|
2015-05-12 18:50:08 +00:00
|
|
|
void start_accept();
|
2020-06-20 17:01:22 +00:00
|
|
|
|
|
|
|
|
void stop_listening();
|
|
|
|
|
void close();
|
|
|
|
|
|
2015-05-12 18:50:08 +00:00
|
|
|
private:
|
2016-04-20 02:58:37 +00:00
|
|
|
friend class detail::websocket_tls_server_impl;
|
|
|
|
|
std::unique_ptr<detail::websocket_tls_server_impl> my;
|
2015-05-12 18:50:08 +00:00
|
|
|
};
|
|
|
|
|
|
2015-03-26 20:51:10 +00:00
|
|
|
class websocket_client
|
|
|
|
|
{
|
|
|
|
|
public:
|
2016-05-18 17:28:52 +00:00
|
|
|
websocket_client( const std::string& ca_filename = "_default" );
|
2015-03-26 20:51:10 +00:00
|
|
|
~websocket_client();
|
|
|
|
|
|
2015-03-27 20:29:33 +00:00
|
|
|
websocket_connection_ptr connect( const std::string& uri );
|
2015-05-12 20:16:10 +00:00
|
|
|
websocket_connection_ptr secure_connect( const std::string& uri );
|
2019-05-04 22:44:33 +00:00
|
|
|
|
|
|
|
|
void close();
|
|
|
|
|
void synchronous_close();
|
2019-05-24 15:41:21 +00:00
|
|
|
void append_header(const std::string& key, const std::string& value);
|
2015-03-26 20:51:10 +00:00
|
|
|
private:
|
|
|
|
|
std::unique_ptr<detail::websocket_client_impl> my;
|
2015-05-12 20:16:10 +00:00
|
|
|
std::unique_ptr<detail::websocket_tls_client_impl> smy;
|
2020-05-02 15:17:07 +00:00
|
|
|
fc::http::headers _headers;
|
2015-05-12 20:16:10 +00:00
|
|
|
};
|
2015-03-26 20:51:10 +00:00
|
|
|
|
|
|
|
|
} }
|