24 #ifndef STATICLIB_PION_HTTP_SERVER_HPP
25 #define STATICLIB_PION_HTTP_SERVER_HPP
31 #include <unordered_map>
36 #include "staticlib/config.hpp"
37 #include "staticlib/support.hpp"
49 class http_request_reader;
61 using request_handler_type = std::function<void(http_request_ptr, response_writer_ptr)>;
66 using payload_handler_creator_type = std::function<http_parser::payload_handler_type(http_request_ptr&)>;
71 using handlers_map_type = std::unordered_map<std::string, request_handler_type>;
76 using payloads_map_type = std::unordered_map<std::string, payload_handler_creator_type>;
81 using websocket_map_type = std::unordered_map<std::string, websocket_handler_type>;
84 using websocket_conn_registry_type = std::multimap<std::string, std::pair<std::string, std::weak_ptr<tcp_connection>>>;
90 uint32_t read_timeout;
95 handlers_map_type get_handlers;
100 handlers_map_type post_handlers;
105 handlers_map_type put_handlers;
110 handlers_map_type delete_handlers;
115 handlers_map_type options_handlers;
120 request_handler_type bad_request_handler;
125 request_handler_type not_found_handler;
133 payloads_map_type get_payloads;
138 payloads_map_type post_payloads;
143 payloads_map_type put_payloads;
148 payloads_map_type delete_payloads;
153 payloads_map_type options_payloads;
158 websocket_map_type wsopen_handlers;
163 websocket_map_type wsmessage_handlers;
168 websocket_map_type wsclose_handlers;
170 websocket_conn_registry_type websocket_conn_registry;
172 std::mutex websocket_conn_registry_mtx;
192 http_server(uint32_t number_of_threads, uint16_t port,
193 asio::ip::address_v4 ip_address = asio::ip::address_v4::any(),
194 uint32_t read_timeout_millis = 10000,
195 const std::string& ssl_key_file = std::string(),
196 std::function<std::string(std::size_t, asio::ssl::context::password_purpose)> ssl_key_password_callback =
197 [](std::size_t, asio::ssl::context::password_purpose) {
return std::string(); },
198 const std::string& ssl_verify_file = std::string(),
199 std::function<bool(bool, asio::ssl::verify_context&)> ssl_verify_callback =
200 [](bool, asio::ssl::verify_context&) {
return true; });
209 void add_handler(
const std::string& method,
const std::string& resource,
210 request_handler_type request_handler);
218 bad_request_handler = std::move(handler);
227 not_found_handler = std::move(handler);
238 payload_handler_creator_type payload_handler);
248 websocket_handler_type handler);
261 sl::websocket::frame_type frame_type = sl::websocket::frame_type::text,
262 const std::set<std::string>& dest_ids = std::set<std::string>());
269 virtual void handle_connection(tcp_connection_ptr& conn)
override;
279 void handle_request_after_headers_parsed(http_request_ptr& request,
280 tcp_connection_ptr& conn,
const std::error_code& ec, sl::support::tribool& rc);
289 void handle_request(http_request_ptr request,
290 tcp_connection_ptr& conn,
const std::error_code& ec);