summaryrefslogtreecommitdiffhomepage
path: root/http.cpp
diff options
context:
space:
mode:
authorRoland Reichwein <mail@reichwein.it>2020-04-09 18:30:32 +0200
committerRoland Reichwein <mail@reichwein.it>2020-04-09 18:30:32 +0200
commit0d157fb407a35f8afe6d6f0f4c2cc5cd5d5a1933 (patch)
tree86ccea82ebbe29197eacb9a85e8ec7548c5ae38c /http.cpp
parent2f42619303627db401e469e2fd65123cd794a378 (diff)
Prepared generate_page for static-files plugin
Diffstat (limited to 'http.cpp')
-rw-r--r--http.cpp52
1 files changed, 22 insertions, 30 deletions
diff --git a/http.cpp b/http.cpp
index eeff552..714bcc4 100644
--- a/http.cpp
+++ b/http.cpp
@@ -71,8 +71,7 @@ template<
class Send>
void
handle_request(
- const Config& config,
- const Socket& socket,
+ Server& server,
http::request<Body, http::basic_fields<Allocator>>&& req,
Send&& send)
{
@@ -115,9 +114,17 @@ handle_request(
return res;
};
- std::string res_data;
try {
- res_data = generate_response(req, config, socket);
+ http::response<http::string_body> res{http::status::ok, req.version()};
+ res.set(http::field::server, VersionString);
+ res.set(http::field::content_type, mime_type(extend_index_html(std::string(req.target()))));
+ res.keep_alive(req.keep_alive());
+ std::string res_data = generate_response(req, res, server);
+ if (req.method() != http::verb::head) {
+ res.body() = res_data;
+ res.content_length(res_data.size());
+ }
+ return send(std::move(res));
} catch(const bad_request_exception& ex) {
return send(bad_request(ex.what()));
} catch(const not_found_exception& ex) {
@@ -126,14 +133,6 @@ handle_request(
return send(server_error(ex.what()));
}
- http::response<http::string_body> res{http::status::ok, req.version()};
- res.set(http::field::server, VersionString);
- res.set(http::field::content_type, mime_type(extend_index_html(std::string(req.target()))));
- res.content_length(res_data.size());
- res.keep_alive(req.keep_alive());
- if (req.method() != http::verb::head)
- res.body() = res_data;
- return send(std::move(res));
}
//------------------------------------------------------------------------------
@@ -187,8 +186,7 @@ class session : public std::enable_shared_from_this<session>
beast::tcp_stream stream_;
beast::flat_buffer buffer_;
- const Config& m_config;
- const Socket& m_socket;
+ Server& m_server;
http::request<http::string_body> req_;
std::shared_ptr<void> res_;
send_lambda lambda_;
@@ -197,11 +195,9 @@ public:
// Take ownership of the stream
session(
tcp::socket&& socket,
- const Config& config,
- const Socket& config_socket)
+ Server& server)
: stream_(std::move(socket))
- , m_config(config)
- , m_socket(config_socket)
+ , m_server(server)
, lambda_(*this)
{
}
@@ -252,7 +248,7 @@ public:
return fail(ec, "read");
// Send the response
- handle_request(m_config, m_socket , std::move(req_), lambda_);
+ handle_request(m_server , std::move(req_), lambda_);
}
void
@@ -298,19 +294,16 @@ class listener : public std::enable_shared_from_this<listener>
{
net::io_context& ioc_;
tcp::acceptor acceptor_;
- const Config& m_config;
- const Socket& m_socket;
+ Server& m_server;
public:
listener(
net::io_context& ioc,
tcp::endpoint endpoint,
- const Config& config,
- const Socket& socket)
+ Server& server)
: ioc_(ioc)
, acceptor_(net::make_strand(ioc))
- , m_config(config)
- , m_socket(socket)
+ , m_server(server)
{
beast::error_code ec;
@@ -379,8 +372,7 @@ private:
// Create the session and run it
std::make_shared<session>(
std::move(socket),
- m_config,
- m_socket)->run();
+ m_server)->run();
}
// Accept another connection
@@ -394,7 +386,8 @@ private:
namespace HTTP {
- Server::Server(Config& config, boost::asio::io_context& ioc, const Socket& socket): ::Server(config, ioc), m_socket(socket)
+ Server::Server(Config& config, boost::asio::io_context& ioc, const Socket& socket, plugins_container_type& plugins)
+ : ::Server(config, ioc, socket, plugins)
{
}
@@ -411,8 +404,7 @@ namespace HTTP {
std::make_shared<listener>(
m_ioc,
tcp::endpoint{address, port},
- m_config,
- m_socket)->run();
+ *this)->run();
return EXIT_SUCCESS;
}