diff --git a/CHANGELOG.md b/CHANGELOG.md index c5a54828..77e5bf05 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ Version 170: * Add test::error to experimental * Add test::fail_count to experimental * Add test::stream to experimental +* Use a shared string for example HTTP server doc roots -------------------------------------------------------------------------------- diff --git a/example/advanced/server-flex/advanced_server_flex.cpp b/example/advanced/server-flex/advanced_server_flex.cpp index 75637079..29a43cc1 100644 --- a/example/advanced/server-flex/advanced_server_flex.cpp +++ b/example/advanced/server-flex/advanced_server_flex.cpp @@ -756,7 +756,7 @@ class http_session } }; - std::string const& doc_root_; + std::shared_ptr doc_root_; http::request req_; queue queue_; @@ -771,7 +771,7 @@ public: http_session( boost::asio::io_context& ioc, boost::beast::flat_buffer buffer, - std::string const& doc_root) + std::shared_ptr const& doc_root) : doc_root_(doc_root) , queue_(*this) , timer_(ioc, @@ -849,7 +849,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()) @@ -896,7 +896,7 @@ public: plain_http_session( tcp::socket socket, boost::beast::flat_buffer buffer, - std::string const& doc_root) + std::shared_ptr const& doc_root) : http_session( socket.get_executor().context(), std::move(buffer), @@ -977,7 +977,7 @@ public: tcp::socket socket, ssl::context& ctx, boost::beast::flat_buffer buffer, - std::string const& doc_root) + std::shared_ptr const& doc_root) : http_session( socket.get_executor().context(), std::move(buffer), @@ -1107,7 +1107,7 @@ class detect_session : public std::enable_shared_from_this ssl::context& ctx_; boost::asio::strand< boost::asio::io_context::executor_type> strand_; - std::string const& doc_root_; + std::shared_ptr doc_root_; boost::beast::flat_buffer buffer_; public: @@ -1115,7 +1115,7 @@ public: detect_session( tcp::socket socket, ssl::context& ctx, - std::string const& doc_root) + std::shared_ptr const& doc_root) : socket_(std::move(socket)) , ctx_(ctx) , strand_(socket_.get_executor()) @@ -1171,14 +1171,14 @@ class listener : public std::enable_shared_from_this ssl::context& ctx_; tcp::acceptor acceptor_; tcp::socket socket_; - std::string const& doc_root_; + std::shared_ptr doc_root_; public: listener( boost::asio::io_context& ioc, ssl::context& ctx, tcp::endpoint endpoint, - std::string const& doc_root) + std::shared_ptr const& doc_root) : ctx_(ctx) , acceptor_(ioc) , socket_(ioc) @@ -1276,7 +1276,7 @@ int main(int argc, char* argv[]) } auto const address = boost::asio::ip::make_address(argv[1]); auto const port = static_cast(std::atoi(argv[2])); - std::string const doc_root = argv[3]; + auto const doc_root = std::make_shared(argv[3]); auto const threads = std::max(1, std::atoi(argv[4])); // The io_context is required for all I/O diff --git a/example/advanced/server/advanced_server.cpp b/example/advanced/server/advanced_server.cpp index 7b9980f8..2d3e5f24 100644 --- a/example/advanced/server/advanced_server.cpp +++ b/example/advanced/server/advanced_server.cpp @@ -555,7 +555,7 @@ class http_session : public std::enable_shared_from_this 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 doc_root_; http::request req_; queue queue_; @@ -564,7 +564,7 @@ public: explicit http_session( tcp::socket socket, - std::string const& doc_root) + std::shared_ptr 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 { tcp::acceptor acceptor_; tcp::socket socket_; - std::string const& doc_root_; + std::shared_ptr doc_root_; public: listener( boost::asio::io_context& ioc, tcp::endpoint endpoint, - std::string const& doc_root) + std::shared_ptr 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(std::atoi(argv[2])); - std::string const doc_root = argv[3]; + auto const doc_root = std::make_shared(argv[3]); auto const threads = std::max(1, std::atoi(argv[4])); // The io_context is required for all I/O diff --git a/example/http/server/async-ssl/http_server_async_ssl.cpp b/example/http/server/async-ssl/http_server_async_ssl.cpp index 9c767152..9473cd84 100644 --- a/example/http/server/async-ssl/http_server_async_ssl.cpp +++ b/example/http/server/async-ssl/http_server_async_ssl.cpp @@ -264,7 +264,7 @@ class session : public std::enable_shared_from_this boost::asio::strand< boost::asio::io_context::executor_type> strand_; boost::beast::flat_buffer buffer_; - std::string const& doc_root_; + std::shared_ptr doc_root_; http::request req_; std::shared_ptr res_; send_lambda lambda_; @@ -275,7 +275,7 @@ public: session( tcp::socket socket, ssl::context& ctx, - std::string const& doc_root) + std::shared_ptr 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 ssl::context& ctx_; tcp::acceptor acceptor_; tcp::socket socket_; - std::string const& doc_root_; + std::shared_ptr doc_root_; public: listener( boost::asio::io_context& ioc, ssl::context& ctx, tcp::endpoint endpoint, - std::string const& doc_root) + std::shared_ptr 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(std::atoi(argv[2])); - std::string const doc_root = argv[3]; + auto const doc_root = std::make_shared(argv[3]); auto const threads = std::max(1, std::atoi(argv[4])); // The io_context is required for all I/O diff --git a/example/http/server/async/http_server_async.cpp b/example/http/server/async/http_server_async.cpp index 33177599..84272bee 100644 --- a/example/http/server/async/http_server_async.cpp +++ b/example/http/server/async/http_server_async.cpp @@ -259,7 +259,7 @@ class session : public std::enable_shared_from_this boost::asio::strand< boost::asio::io_context::executor_type> strand_; boost::beast::flat_buffer buffer_; - std::string const& doc_root_; + std::shared_ptr doc_root_; http::request req_; std::shared_ptr res_; send_lambda lambda_; @@ -269,7 +269,7 @@ public: explicit session( tcp::socket socket, - std::string const& doc_root) + std::shared_ptr const& doc_root) : socket_(std::move(socket)) , strand_(socket_.get_executor()) , doc_root_(doc_root) @@ -317,7 +317,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 @@ -363,13 +363,13 @@ class listener : public std::enable_shared_from_this { tcp::acceptor acceptor_; tcp::socket socket_; - std::string const& doc_root_; + std::shared_ptr doc_root_; public: listener( boost::asio::io_context& ioc, tcp::endpoint endpoint, - std::string const& doc_root) + std::shared_ptr const& doc_root) : acceptor_(ioc) , socket_(ioc) , doc_root_(doc_root) @@ -465,7 +465,7 @@ int main(int argc, char* argv[]) } auto const address = boost::asio::ip::make_address(argv[1]); auto const port = static_cast(std::atoi(argv[2])); - std::string const doc_root = argv[3]; + auto const doc_root = std::make_shared(argv[3]); auto const threads = std::max(1, std::atoi(argv[4])); // The io_context is required for all I/O diff --git a/example/http/server/coro-ssl/http_server_coro_ssl.cpp b/example/http/server/coro-ssl/http_server_coro_ssl.cpp index e748d3d1..e9b40300 100644 --- a/example/http/server/coro-ssl/http_server_coro_ssl.cpp +++ b/example/http/server/coro-ssl/http_server_coro_ssl.cpp @@ -256,7 +256,7 @@ void do_session( tcp::socket& socket, ssl::context& ctx, - std::string const& doc_root, + std::shared_ptr const& doc_root, boost::asio::yield_context yield) { bool close = false; @@ -287,7 +287,7 @@ do_session( return fail(ec, "read"); // Send the response - handle_request(doc_root, std::move(req), lambda); + handle_request(*doc_root, std::move(req), lambda); if(ec) return fail(ec, "write"); if(close) @@ -314,7 +314,7 @@ do_listen( boost::asio::io_context& ioc, ssl::context& ctx, tcp::endpoint endpoint, - std::string const& doc_root, + std::shared_ptr const& doc_root, boost::asio::yield_context yield) { boost::system::error_code ec; @@ -371,7 +371,7 @@ int main(int argc, char* argv[]) } auto const address = boost::asio::ip::make_address(argv[1]); auto const port = static_cast(std::atoi(argv[2])); - std::string const doc_root = argv[3]; + auto const doc_root = std::make_shared(argv[3]); auto const threads = std::max(1, std::atoi(argv[4])); // The io_context is required for all I/O diff --git a/example/http/server/coro/http_server_coro.cpp b/example/http/server/coro/http_server_coro.cpp index 988a0221..cf2cd5cc 100644 --- a/example/http/server/coro/http_server_coro.cpp +++ b/example/http/server/coro/http_server_coro.cpp @@ -251,7 +251,7 @@ struct send_lambda void do_session( tcp::socket& socket, - std::string const& doc_root, + std::shared_ptr const& doc_root, boost::asio::yield_context yield) { bool close = false; @@ -274,7 +274,7 @@ do_session( return fail(ec, "read"); // Send the response - handle_request(doc_root, std::move(req), lambda); + handle_request(*doc_root, std::move(req), lambda); if(ec) return fail(ec, "write"); if(close) @@ -298,7 +298,7 @@ void do_listen( boost::asio::io_context& ioc, tcp::endpoint endpoint, - std::string const& doc_root, + std::shared_ptr const& doc_root, boost::asio::yield_context yield) { boost::system::error_code ec; @@ -354,7 +354,7 @@ int main(int argc, char* argv[]) } auto const address = boost::asio::ip::make_address(argv[1]); auto const port = static_cast(std::atoi(argv[2])); - std::string const doc_root = argv[3]; + auto const doc_root = std::make_shared(argv[3]); auto const threads = std::max(1, std::atoi(argv[4])); // The io_context is required for all I/O diff --git a/example/http/server/flex/http_server_flex.cpp b/example/http/server/flex/http_server_flex.cpp index f13f7a17..eb4f5faa 100644 --- a/example/http/server/flex/http_server_flex.cpp +++ b/example/http/server/flex/http_server_flex.cpp @@ -270,7 +270,7 @@ class session } }; - std::string const& doc_root_; + std::shared_ptr doc_root_; http::request req_; std::shared_ptr res_; send_lambda lambda_; @@ -286,7 +286,7 @@ public: session( boost::asio::io_context& ioc, boost::beast::flat_buffer buffer, - std::string const& doc_root) + std::shared_ptr const& doc_root) : doc_root_(doc_root) , lambda_(*this) , strand_(ioc.get_executor()) @@ -326,7 +326,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 @@ -369,7 +369,7 @@ public: plain_session( tcp::socket socket, boost::beast::flat_buffer buffer, - std::string const& doc_root) + std::shared_ptr const& doc_root) : session( socket.get_executor().context(), std::move(buffer), @@ -420,7 +420,7 @@ public: tcp::socket socket, ssl::context& ctx, boost::beast::flat_buffer buffer, - std::string const& doc_root) + std::shared_ptr const& doc_root) : session( socket.get_executor().context(), std::move(buffer), @@ -501,7 +501,7 @@ class detect_session : public std::enable_shared_from_this ssl::context& ctx_; boost::asio::strand< boost::asio::io_context::executor_type> strand_; - std::string const& doc_root_; + std::shared_ptr doc_root_; boost::beast::flat_buffer buffer_; public: @@ -509,7 +509,7 @@ public: detect_session( tcp::socket socket, ssl::context& ctx, - std::string const& doc_root) + std::shared_ptr const& doc_root) : socket_(std::move(socket)) , ctx_(ctx) , strand_(socket_.get_executor()) @@ -567,14 +567,14 @@ class listener : public std::enable_shared_from_this boost::asio::io_context::executor_type> strand_; tcp::acceptor acceptor_; tcp::socket socket_; - std::string const& doc_root_; + std::shared_ptr doc_root_; public: listener( boost::asio::io_context& ioc, ssl::context& ctx, tcp::endpoint endpoint, - std::string const& doc_root) + std::shared_ptr const& doc_root) : ctx_(ctx) , strand_(ioc.get_executor()) , acceptor_(ioc) @@ -673,7 +673,7 @@ int main(int argc, char* argv[]) } auto const address = boost::asio::ip::make_address(argv[1]); auto const port = static_cast(std::atoi(argv[2])); - std::string const doc_root = argv[3]; + auto const doc_root = std::make_shared(argv[3]); auto const threads = std::max(1, std::atoi(argv[4])); // The io_context is required for all I/O diff --git a/example/http/server/stackless-ssl/http_server_stackless_ssl.cpp b/example/http/server/stackless-ssl/http_server_stackless_ssl.cpp index a0fbac4e..2555ed95 100644 --- a/example/http/server/stackless-ssl/http_server_stackless_ssl.cpp +++ b/example/http/server/stackless-ssl/http_server_stackless_ssl.cpp @@ -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 doc_root_; http::request req_; std::shared_ptr res_; send_lambda lambda_; @@ -278,7 +278,7 @@ public: session( tcp::socket socket, ssl::context& ctx, - std::string const& doc_root) + std::shared_ptr 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 doc_root_; public: listener( boost::asio::io_context& ioc, ssl::context& ctx, tcp::endpoint endpoint, - std::string const& doc_root) + std::shared_ptr 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(std::atoi(argv[2])); - std::string const doc_root = argv[3]; + auto const doc_root = std::make_shared(argv[3]); auto const threads = std::max(1, std::atoi(argv[4])); // The io_context is required for all I/O diff --git a/example/http/server/stackless/http_server_stackless.cpp b/example/http/server/stackless/http_server_stackless.cpp index e92d0ad2..71627ea1 100644 --- a/example/http/server/stackless/http_server_stackless.cpp +++ b/example/http/server/stackless/http_server_stackless.cpp @@ -263,7 +263,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 doc_root_; http::request req_; std::shared_ptr res_; send_lambda lambda_; @@ -273,7 +273,7 @@ public: explicit session( tcp::socket socket, - std::string const& doc_root) + std::shared_ptr const& doc_root) : socket_(std::move(socket)) , strand_(socket_.get_executor()) , doc_root_(doc_root) @@ -323,7 +323,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) @@ -355,13 +355,13 @@ class listener { tcp::acceptor acceptor_; tcp::socket socket_; - std::string const& doc_root_; + std::shared_ptr doc_root_; public: listener( boost::asio::io_context& ioc, tcp::endpoint endpoint, - std::string const& doc_root) + std::shared_ptr const& doc_root) : acceptor_(ioc) , socket_(ioc) , doc_root_(doc_root) @@ -456,7 +456,7 @@ int main(int argc, char* argv[]) } auto const address = boost::asio::ip::make_address(argv[1]); auto const port = static_cast(std::atoi(argv[2])); - std::string const doc_root = argv[3]; + auto const doc_root = std::make_shared(argv[3]); auto const threads = std::max(1, std::atoi(argv[4])); // The io_context is required for all I/O diff --git a/example/http/server/sync-ssl/http_server_sync_ssl.cpp b/example/http/server/sync-ssl/http_server_sync_ssl.cpp index 51498628..76b9d8df 100644 --- a/example/http/server/sync-ssl/http_server_sync_ssl.cpp +++ b/example/http/server/sync-ssl/http_server_sync_ssl.cpp @@ -250,7 +250,7 @@ void do_session( tcp::socket& socket, ssl::context& ctx, - std::string const& doc_root) + std::shared_ptr const& doc_root) { bool close = false; boost::system::error_code ec; @@ -280,7 +280,7 @@ do_session( return fail(ec, "read"); // Send the response - handle_request(doc_root, std::move(req), lambda); + handle_request(*doc_root, std::move(req), lambda); if(ec) return fail(ec, "write"); if(close) @@ -316,7 +316,7 @@ int main(int argc, char* argv[]) } auto const address = boost::asio::ip::make_address(argv[1]); auto const port = static_cast(std::atoi(argv[2])); - std::string const doc_root = argv[3]; + auto const doc_root = std::make_shared(argv[3]); // The io_context is required for all I/O boost::asio::io_context ioc{1}; diff --git a/example/http/server/sync/http_server_sync.cpp b/example/http/server/sync/http_server_sync.cpp index 327aa54e..31b1a14d 100644 --- a/example/http/server/sync/http_server_sync.cpp +++ b/example/http/server/sync/http_server_sync.cpp @@ -247,7 +247,7 @@ struct send_lambda void do_session( tcp::socket& socket, - std::string const& doc_root) + std::shared_ptr const& doc_root) { bool close = false; boost::system::error_code ec; @@ -269,7 +269,7 @@ do_session( return fail(ec, "read"); // Send the response - handle_request(doc_root, std::move(req), lambda); + handle_request(*doc_root, std::move(req), lambda); if(ec) return fail(ec, "write"); if(close) @@ -303,7 +303,7 @@ int main(int argc, char* argv[]) } auto const address = boost::asio::ip::make_address(argv[1]); auto const port = static_cast(std::atoi(argv[2])); - std::string const doc_root = argv[3]; + auto const doc_root = std::make_shared(argv[3]); // The io_context is required for all I/O boost::asio::io_context ioc{1};