#pragma once #include #include #include #include #include #include #include #include #include #include #include #include "../types.h" namespace beast = boost::beast; // from namespace http = beast::http; // from namespace boost_net = boost::asio; // from namespace ssl = boost::asio::ssl; // from using tcp = boost_net::ip::tcp; // from class WebServer { public: WebServer(const std::string& address, unsigned short port, const std::string& cert_file, const std::string& key_file, const std::string& key_password, const std::string& hardcode_user, const std::string& hardcode_password); ~WebServer(); void run(); void start(); void register_route(const std::string& uri, std::function&, http::response&)> handler, bool auth_required = true); private: bool is_ssl; static std::string my_password_callback(std::size_t max_length, ssl::context::password_purpose purpose); void do_accept(); void on_accept(beast::error_code ec, tcp::socket socket); void do_session_ssl(tcp::socket socket); void do_session(tcp::socket socket); template void handle_request(http::request>&& req, std::function&&)> send); std::string authenticate(const http::request& req, int32* user_status = 0); std::string generate_session_id(); boost_net::io_context ioc_; ssl::context ssl_ctx_; tcp::acceptor acceptor_; std::unordered_map sessions_; // session_id -> username std::unordered_map sessions_status_; // session_id -> status std::unordered_map credentials_; // username -> password std::unordered_map route_required_status_; // route -> status std::unordered_map&, http::response&)>> routes_; std::unordered_map&, http::response&)>> noauth_routes_; };