Use a shared string for example HTTP server doc roots

fix #1109
This commit is contained in:
Vinnie Falco
2018-05-03 14:18:33 -07:00
parent a94efc8c92
commit f48b978044
12 changed files with 65 additions and 64 deletions

View File

@@ -267,7 +267,7 @@ class session
boost::asio::strand<
boost::asio::io_context::executor_type> strand_;
boost::beast::flat_buffer buffer_;
std::string const& doc_root_;
std::shared_ptr<std::string const> doc_root_;
http::request<http::string_body> req_;
std::shared_ptr<void> res_;
send_lambda lambda_;
@@ -278,7 +278,7 @@ public:
session(
tcp::socket socket,
ssl::context& ctx,
std::string const& doc_root)
std::shared_ptr<std::string const> const& doc_root)
: socket_(std::move(socket))
, stream_(socket_, ctx)
, strand_(socket_.get_executor())
@@ -343,7 +343,7 @@ public:
return fail(ec, "read");
// Send the response
yield handle_request(doc_root_, std::move(req_), lambda_);
yield handle_request(*doc_root_, std::move(req_), lambda_);
if(ec)
return fail(ec, "write");
if(close)
@@ -386,14 +386,14 @@ class listener
ssl::context& ctx_;
tcp::acceptor acceptor_;
tcp::socket socket_;
std::string const& doc_root_;
std::shared_ptr<std::string const> doc_root_;
public:
listener(
boost::asio::io_context& ioc,
ssl::context& ctx,
tcp::endpoint endpoint,
std::string const& doc_root)
std::shared_ptr<std::string const> const& doc_root)
: ctx_(ctx)
, acceptor_(ioc)
, socket_(ioc)
@@ -491,7 +491,7 @@ int main(int argc, char* argv[])
}
auto const address = boost::asio::ip::make_address(argv[1]);
auto const port = static_cast<unsigned short>(std::atoi(argv[2]));
std::string const doc_root = argv[3];
auto const doc_root = std::make_shared<std::string>(argv[3]);
auto const threads = std::max<int>(1, std::atoi(argv[4]));
// The io_context is required for all I/O