summaryrefslogtreecommitdiffhomepage
path: root/http.cpp
diff options
context:
space:
mode:
authorRoland Reichwein <mail@reichwein.it>2020-04-05 12:03:52 +0200
committerRoland Reichwein <mail@reichwein.it>2020-04-05 12:03:52 +0200
commit918685c1c09de1e3cd14c41bb8cc8b89a177ccd2 (patch)
treef803517e6a34be0216f00249dd62525eadce0d05 /http.cpp
parentcbfc28a946ded64e9402e1e6d32511150339ec72 (diff)
Different certificates for different hosts
Diffstat (limited to 'http.cpp')
-rw-r--r--http.cpp36
1 files changed, 22 insertions, 14 deletions
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<Body, http::basic_fields<Allocator>>&& 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<session>
beast::tcp_stream stream_;
beast::flat_buffer buffer_;
- std::shared_ptr<std::string const> doc_root_;
+ const Config& m_config;
+ const Socket& m_socket;
http::request<http::string_body> req_;
std::shared_ptr<void> res_;
send_lambda lambda_;
@@ -256,9 +259,11 @@ public:
// Take ownership of the stream
session(
tcp::socket&& socket,
- std::shared_ptr<std::string const> 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<listener>
{
net::io_context& ioc_;
tcp::acceptor acceptor_;
- std::shared_ptr<std::string const> doc_root_;
+ const Config& m_config;
+ const Socket& m_socket;
public:
listener(
net::io_context& ioc,
tcp::endpoint endpoint,
- std::shared_ptr<std::string const> 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<session>(
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<unsigned short>(std::atoi(m_socket.port.data()));
- auto const doc_root = std::make_shared<std::string>(m_config.Sites()[0].paths[0].params.at("target"));
// Create and launch a listening port
std::make_shared<listener>(
m_ioc,
tcp::endpoint{address, port},
- doc_root)->run();
+ m_config,
+ m_socket)->run();
return EXIT_SUCCESS;
}