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

@@ -264,7 +264,7 @@ class session : public std::enable_shared_from_this<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_;
@@ -275,7 +275,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())
@@ -341,7 +341,7 @@ public:
return fail(ec, "read");
// Send the response
handle_request(doc_root_, std::move(req_), lambda_);
handle_request(*doc_root_, std::move(req_), lambda_);
}
void
@@ -400,14 +400,14 @@ class listener : public std::enable_shared_from_this<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)
@@ -505,7 +505,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