From 918685c1c09de1e3cd14c41bb8cc8b89a177ccd2 Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Sun, 5 Apr 2020 12:03:52 +0200 Subject: Different certificates for different hosts --- http.cpp | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) (limited to 'http.cpp') diff --git a/http.cpp b/http.cpp index eb4dc8c..2203ffe 100644 --- a/http.cpp +++ b/http.cpp @@ -97,7 +97,8 @@ template< class Send> void handle_request( - beast::string_view doc_root, + const Config& config, + const Socket& socket, http::request>&& req, Send&& send) { @@ -152,8 +153,9 @@ handle_request( return send(bad_request("Illegal request-target")); // Build the path to the requested file - std::string path = path_cat(doc_root, req.target()); - std::cout << "DEBUG: " << req["host"] << "|" << req.target() << std::endl; + std::string host{req["host"]}; // TODO: just use string_view + std::string target{req.target()}; + std::string path = path_cat(config.DocRoot(socket, host, target), req.target()); if(req.target().back() == '/') path.append("index.html"); @@ -247,7 +249,8 @@ class session : public std::enable_shared_from_this beast::tcp_stream stream_; beast::flat_buffer buffer_; - std::shared_ptr doc_root_; + const Config& m_config; + const Socket& m_socket; http::request req_; std::shared_ptr res_; send_lambda lambda_; @@ -256,9 +259,11 @@ public: // Take ownership of the stream session( tcp::socket&& socket, - std::shared_ptr const& doc_root) + const Config& config, + const Socket& config_socket) : stream_(std::move(socket)) - , doc_root_(doc_root) + , m_config(config) + , m_socket(config_socket) , lambda_(*this) { } @@ -309,7 +314,7 @@ public: return fail(ec, "read"); // Send the response - handle_request(*doc_root_, std::move(req_), lambda_); + handle_request(m_config, m_socket , std::move(req_), lambda_); } void @@ -355,16 +360,19 @@ class listener : public std::enable_shared_from_this { net::io_context& ioc_; tcp::acceptor acceptor_; - std::shared_ptr doc_root_; + const Config& m_config; + const Socket& m_socket; public: listener( net::io_context& ioc, tcp::endpoint endpoint, - std::shared_ptr const& doc_root) + const Config& config, + const Socket& socket) : ioc_(ioc) , acceptor_(net::make_strand(ioc)) - , doc_root_(doc_root) + , m_config(config) + , m_socket(socket) { beast::error_code ec; @@ -433,7 +441,8 @@ private: // Create the session and run it std::make_shared( std::move(socket), - doc_root_)->run(); + m_config, + m_socket)->run(); } // Accept another connection @@ -457,16 +466,15 @@ namespace HTTP { int Server::start() { - // TODO: Config auto const address = net::ip::make_address(m_socket.address); auto const port = static_cast(std::atoi(m_socket.port.data())); - auto const doc_root = std::make_shared(m_config.Sites()[0].paths[0].params.at("target")); // Create and launch a listening port std::make_shared( m_ioc, tcp::endpoint{address, port}, - doc_root)->run(); + m_config, + m_socket)->run(); return EXIT_SUCCESS; } -- cgit v1.2.3