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
+6 -6
View File
@@ -555,7 +555,7 @@ class http_session : public std::enable_shared_from_this<http_session>
boost::asio::io_context::executor_type> strand_;
boost::asio::steady_timer timer_;
boost::beast::flat_buffer buffer_;
std::string const& doc_root_;
std::shared_ptr<std::string const> doc_root_;
http::request<http::string_body> req_;
queue queue_;
@@ -564,7 +564,7 @@ public:
explicit
http_session(
tcp::socket socket,
std::string const& doc_root)
std::shared_ptr<std::string const> const& doc_root)
: socket_(std::move(socket))
, strand_(socket_.get_executor())
, timer_(socket_.get_executor().context(),
@@ -665,7 +665,7 @@ public:
}
// Send the response
handle_request(doc_root_, std::move(req_), queue_);
handle_request(*doc_root_, std::move(req_), queue_);
// If we aren't at the queue limit, try to pipeline another request
if(! queue_.is_full())
@@ -715,13 +715,13 @@ class listener : public std::enable_shared_from_this<listener>
{
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,
tcp::endpoint endpoint,
std::string const& doc_root)
std::shared_ptr<std::string const> const& doc_root)
: acceptor_(ioc)
, socket_(ioc)
, doc_root_(doc_root)
@@ -817,7 +817,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