From 4fff8146f107747811b07c1424c8b6d4bbea2368 Mon Sep 17 00:00:00 2001 From: na-trium-144 <100704180+na-trium-144@users.noreply.github.com> Date: Fri, 12 Apr 2024 03:14:59 +0900 Subject: [PATCH 01/10] add unix domain socket --- include/crow.h | 1 + include/crow/app.h | 55 ++++++++++++++++++---- include/crow/http_connection.h | 2 +- include/crow/http_server.h | 19 ++++---- include/crow/routing.h | 5 ++ include/crow/socket_acceptors.h | 53 +++++++++++++++++++++ include/crow/socket_adaptors.h | 82 +++++++++++++++++++++++++++++++++ 7 files changed, 197 insertions(+), 20 deletions(-) create mode 100644 include/crow/socket_acceptors.h diff --git a/include/crow.h b/include/crow.h index 25c136ad6..cea87c94f 100644 --- a/include/crow.h +++ b/include/crow.h @@ -5,6 +5,7 @@ #include "crow/TinySHA1.hpp" #include "crow/settings.h" #include "crow/socket_adaptors.h" +#include "crow/socket_acceptors.h" #include "crow/json.h" #include "crow/mustache.h" #include "crow/logging.h" diff --git a/include/crow/app.h b/include/crow/app.h index a91e992a6..8eef77543 100644 --- a/include/crow/app.h +++ b/include/crow/app.h @@ -52,10 +52,11 @@ namespace crow /// This crow application using self_t = Crow; /// The HTTP server - using server_t = Server; + using server_t = Server; + using unix_server_t = Server; #ifdef CROW_ENABLE_SSL /// An HTTP server that runs on SSL with an SSLAdaptor - using ssl_server_t = Server; + using ssl_server_t = Server; #endif Crow() {} @@ -194,6 +195,20 @@ namespace crow return bindaddr_; } + /// Disable tcp/ip and use unix domain socket instead + self_t& unix_path(std::string path) + { + bindaddr_ = path; + use_unix_ = true; + return *this; + } + + /// Get the unix domain socket path + std::string unix_path() + { + return bindaddr_; + } + /// Run the server on multiple threads using all available threads self_t& multithreaded() { @@ -353,7 +368,8 @@ namespace crow #ifdef CROW_ENABLE_SSL if (ssl_used_) { - ssl_server_ = std::move(std::unique_ptr(new ssl_server_t(this, bindaddr_, port_, server_name_, &middlewares_, concurrency_, timeout_, &ssl_context_))); + TCPAcceptor::endpoint endpoint(asio::ip::address::from_string(bindaddr_), port_); + ssl_server_ = std::move(std::unique_ptr(new ssl_server_t(this, endpoint, server_name_, &middlewares_, concurrency_, timeout_, &ssl_context_))); ssl_server_->set_tick_function(tick_interval_, tick_function_); ssl_server_->signal_clear(); for (auto snum : signals_) @@ -366,14 +382,30 @@ namespace crow else #endif { - server_ = std::move(std::unique_ptr(new server_t(this, bindaddr_, port_, server_name_, &middlewares_, concurrency_, timeout_, nullptr))); - server_->set_tick_function(tick_interval_, tick_function_); - for (auto snum : signals_) + if (use_unix_) { - server_->signal_add(snum); + UnixSocketAcceptor::endpoint endpoint(bindaddr_); + unix_server_ = std::move(std::unique_ptr(new unix_server_t(this, endpoint, server_name_, &middlewares_, concurrency_, timeout_, nullptr))); + unix_server_->set_tick_function(tick_interval_, tick_function_); + for (auto snum : signals_) + { + unix_server_->signal_add(snum); + } + notify_server_start(); + unix_server_->run(); + } + else + { + TCPAcceptor::endpoint endpoint(asio::ip::address::from_string(bindaddr_), port_); + server_ = std::move(std::unique_ptr(new server_t(this, endpoint, server_name_, &middlewares_, concurrency_, timeout_, nullptr))); + server_->set_tick_function(tick_interval_, tick_function_); + for (auto snum : signals_) + { + server_->signal_add(snum); + } + notify_server_start(); + server_->run(); } - notify_server_start(); - server_->run(); } } @@ -407,6 +439,7 @@ namespace crow websocket->close("Server Application Terminated"); } if (server_) { server_->stop(); } + if (unix_server_) { unix_server_->stop(); } } } @@ -546,6 +579,8 @@ namespace crow } if (server_) server_->wait_for_start(); + else if (unix_server_) + unix_server_->wait_for_start(); #ifdef CROW_ENABLE_SSL else if (ssl_server_) ssl_server_->wait_for_start(); @@ -585,6 +620,7 @@ namespace crow uint64_t max_payload_{UINT64_MAX}; std::string server_name_ = std::string("Crow/") + VERSION; std::string bindaddr_ = "0.0.0.0"; + bool use_unix_ = false; size_t res_stream_threshold_ = 1048576; Router router_; bool static_routes_added_{false}; @@ -606,6 +642,7 @@ namespace crow #endif std::unique_ptr server_; + std::unique_ptr unix_server_; std::vector signals_{SIGINT, SIGTERM}; diff --git a/include/crow/http_connection.h b/include/crow/http_connection.h index cb82df5f9..a2852ec10 100644 --- a/include/crow/http_connection.h +++ b/include/crow/http_connection.h @@ -134,7 +134,7 @@ namespace crow req_.middleware_container = static_cast(middlewares_); req_.io_service = &adaptor_.get_io_service(); - req_.remote_ip_address = adaptor_.remote_endpoint().address().to_string(); + req_.remote_ip_address = adaptor_.address(); add_keep_alive_ = req_.keep_alive; close_connection_ = req_.close_connection; diff --git a/include/crow/http_server.h b/include/crow/http_server.h index c53ce70c7..9d8683f0a 100644 --- a/include/crow/http_server.h +++ b/include/crow/http_server.h @@ -22,21 +22,20 @@ namespace crow { using tcp = asio::ip::tcp; + using stream_protocol = asio::local::stream_protocol; - template + template class Server { public: - Server(Handler* handler, std::string bindaddr, uint16_t port, std::string server_name = std::string("Crow/") + VERSION, std::tuple* middlewares = nullptr, uint16_t concurrency = 1, uint8_t timeout = 5, typename Adaptor::context* adaptor_ctx = nullptr): - acceptor_(io_service_, tcp::endpoint(asio::ip::address::from_string(bindaddr), port)), + Server(Handler* handler, typename Acceptor::endpoint endpoint, std::string server_name = std::string("Crow/") + VERSION, std::tuple* middlewares = nullptr, uint16_t concurrency = 1, uint8_t timeout = 5, typename Adaptor::context* adaptor_ctx = nullptr): + acceptor_(io_service_, endpoint), signals_(io_service_), tick_timer_(io_service_), handler_(handler), concurrency_(concurrency), timeout_(timeout), server_name_(server_name), - port_(port), - bindaddr_(bindaddr), task_queue_length_pool_(concurrency_ - 1), middlewares_(middlewares), adaptor_ctx_(adaptor_ctx) @@ -135,11 +134,10 @@ namespace crow }); } - port_ = acceptor_.local_endpoint().port(); + port_ = acceptor_.port(); handler_->port(port_); - - CROW_LOG_INFO << server_name_ << " server is running at " << (handler_->ssl_used() ? "https://" : "http://") << bindaddr_ << ":" << acceptor_.local_endpoint().port() << " using " << concurrency_ << " threads"; + CROW_LOG_INFO << server_name_ << " server is running at " << acceptor_.url_display(handler_->ssl_used()) << " using " << concurrency_ << " threads"; CROW_LOG_INFO << "Call `app.loglevel(crow::LogLevel::Warning)` to hide Info level logs."; signals_.async_wait( @@ -225,7 +223,7 @@ namespace crow is, handler_, server_name_, middlewares_, get_cached_date_str_pool_[service_idx], *task_timer_pool_[service_idx], adaptor_ctx_, task_queue_length_pool_[service_idx]); - acceptor_.async_accept( + acceptor_.raw_acceptor().async_accept( p->socket(), [this, p, &is, service_idx](asio::error_code ec) { if (!ec) @@ -258,7 +256,7 @@ namespace crow asio::io_service io_service_; std::vector task_timer_pool_; std::vector> get_cached_date_str_pool_; - tcp::acceptor acceptor_; + Acceptor acceptor_; bool shutting_down_ = false; bool server_started_{false}; std::condition_variable cv_started_; @@ -273,6 +271,7 @@ namespace crow std::string server_name_; uint16_t port_; std::string bindaddr_; + bool use_unix_; std::vector> task_queue_length_pool_; std::chrono::milliseconds tick_interval_; diff --git a/include/crow/routing.h b/include/crow/routing.h index 3ec90b959..63025d3d6 100644 --- a/include/crow/routing.h +++ b/include/crow/routing.h @@ -118,6 +118,11 @@ namespace crow res = response(404); res.end(); } + virtual void handle_upgrade(const request&, response& res, UnixSocketAdaptor&&) + { + res = response(404); + res.end(); + } #ifdef CROW_ENABLE_SSL virtual void handle_upgrade(const request&, response& res, SSLAdaptor&&) { diff --git a/include/crow/socket_acceptors.h b/include/crow/socket_acceptors.h new file mode 100644 index 000000000..e2376452b --- /dev/null +++ b/include/crow/socket_acceptors.h @@ -0,0 +1,53 @@ +#pragma once +#ifndef ASIO_STANDALONE +#define ASIO_STANDALONE +#endif +#include + +namespace crow +{ + using tcp = asio::ip::tcp; + using stream_protocol = asio::local::stream_protocol; + + struct TCPAcceptor + { + using endpoint = tcp::endpoint; + tcp::acceptor acceptor_; + TCPAcceptor(asio::io_service& io_service, const endpoint& endpoint): + acceptor_(io_service, endpoint) {} + + int16_t port() const + { + return acceptor_.local_endpoint().port(); + } + std::string url_display(bool ssl_used) const + { + return (ssl_used ? "https://" : "http://") + acceptor_.local_endpoint().address().to_string() + ":" + std::to_string(acceptor_.local_endpoint().port()); + } + tcp::acceptor& raw_acceptor() + { + return acceptor_; + } + }; + + struct UnixSocketAcceptor + { + using endpoint = stream_protocol::endpoint; + stream_protocol::acceptor acceptor_; + UnixSocketAcceptor(asio::io_service& io_service, const endpoint& endpoint): + acceptor_(io_service, endpoint) {} + + int16_t port() const + { + return 0; + } + std::string url_display(bool) const + { + return acceptor_.local_endpoint().path(); + } + stream_protocol::acceptor& raw_acceptor() + { + return acceptor_; + } + }; +} // namespace crow \ No newline at end of file diff --git a/include/crow/socket_adaptors.h b/include/crow/socket_adaptors.h index 478840ef4..ffa629a45 100644 --- a/include/crow/socket_adaptors.h +++ b/include/crow/socket_adaptors.h @@ -16,6 +16,7 @@ namespace crow { using tcp = asio::ip::tcp; + using stream_protocol = asio::local::stream_protocol; /// A wrapper for the asio::ip::tcp::socket and asio::ssl::stream struct SocketAdaptor @@ -47,6 +48,11 @@ namespace crow return socket_.remote_endpoint(); } + std::string address() const + { + return socket_.remote_endpoint().address().to_string(); + } + bool is_open() { return socket_.is_open(); @@ -85,6 +91,77 @@ namespace crow tcp::socket socket_; }; + struct UnixSocketAdaptor + { + using context = void; + UnixSocketAdaptor(asio::io_service& io_service, context*): + socket_(io_service) + { + } + + asio::io_service& get_io_service() + { + return GET_IO_SERVICE(socket_); + } + + stream_protocol::socket& raw_socket() + { + return socket_; + } + + stream_protocol::socket& socket() + { + return socket_; + } + + stream_protocol::endpoint remote_endpoint() + { + return socket_.local_endpoint(); + } + + std::string address() const + { + return ""; + } + + bool is_open() + { + return socket_.is_open(); + } + + void close() + { + asio::error_code ec; + socket_.close(ec); + } + + void shutdown_readwrite() + { + asio::error_code ec; + socket_.shutdown(asio::socket_base::shutdown_type::shutdown_both, ec); + } + + void shutdown_write() + { + asio::error_code ec; + socket_.shutdown(asio::socket_base::shutdown_type::shutdown_send, ec); + } + + void shutdown_read() + { + asio::error_code ec; + socket_.shutdown(asio::socket_base::shutdown_type::shutdown_receive, ec); + } + + template + void start(F f) + { + f(asio::error_code()); + } + + stream_protocol::socket socket_; + }; + #ifdef CROW_ENABLE_SSL struct SSLAdaptor { @@ -110,6 +187,11 @@ namespace crow return raw_socket().remote_endpoint(); } + std::string address() const + { + return socket_.remote_endpoint().address().to_string(); + } + bool is_open() { return ssl_socket_ ? raw_socket().is_open() : false; From d962b01dec3fbdf0df9cf9e81ff6a340125e7efa Mon Sep 17 00:00:00 2001 From: na-trium-144 <100704180+na-trium-144@users.noreply.github.com> Date: Fri, 12 Apr 2024 18:46:18 +0900 Subject: [PATCH 02/10] add handle_upgrade for unix socket --- include/crow/routing.h | 5 +++++ include/crow/websocket.h | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/include/crow/routing.h b/include/crow/routing.h index 63025d3d6..b559cd04c 100644 --- a/include/crow/routing.h +++ b/include/crow/routing.h @@ -468,6 +468,11 @@ namespace crow max_payload_ = max_payload_override_ ? max_payload_ : app_->websocket_max_payload(); new crow::websocket::Connection(req, std::move(adaptor), app_, max_payload_, open_handler_, message_handler_, close_handler_, error_handler_, accept_handler_); } + void handle_upgrade(const request& req, response&, UnixSocketAdaptor&& adaptor) override + { + max_payload_ = max_payload_override_ ? max_payload_ : app_->websocket_max_payload(); + new crow::websocket::Connection(req, std::move(adaptor), app_, max_payload_, open_handler_, message_handler_, close_handler_, error_handler_, accept_handler_); + } #ifdef CROW_ENABLE_SSL void handle_upgrade(const request& req, response&, SSLAdaptor&& adaptor) override { diff --git a/include/crow/websocket.h b/include/crow/websocket.h index c7d8f0638..46116700b 100644 --- a/include/crow/websocket.h +++ b/include/crow/websocket.h @@ -218,7 +218,7 @@ namespace crow std::string get_remote_ip() override { - return adaptor_.remote_endpoint().address().to_string(); + return adaptor_.address(); } void set_max_payload_size(uint64_t payload) From 190cf3db13fd896dfafccfc04b2fd9f633583129 Mon Sep 17 00:00:00 2001 From: na-trium-144 <100704180+na-trium-144@users.noreply.github.com> Date: Tue, 16 Apr 2024 14:15:16 +0900 Subject: [PATCH 03/10] set reuse addr false --- include/crow/socket_acceptors.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/include/crow/socket_acceptors.h b/include/crow/socket_acceptors.h index e2376452b..91e62ceb3 100644 --- a/include/crow/socket_acceptors.h +++ b/include/crow/socket_acceptors.h @@ -35,7 +35,8 @@ namespace crow using endpoint = stream_protocol::endpoint; stream_protocol::acceptor acceptor_; UnixSocketAcceptor(asio::io_service& io_service, const endpoint& endpoint): - acceptor_(io_service, endpoint) {} + acceptor_(io_service, endpoint, false) {} + // reuse addr must be false (https://github.com/chriskohlhoff/asio/issues/622) int16_t port() const { From 76f8c587c7ce5e69af57d0a885cd9fff701bfdf0 Mon Sep 17 00:00:00 2001 From: na-trium-144 <100704180+na-trium-144@users.noreply.github.com> Date: Sat, 20 Apr 2024 22:50:20 +0900 Subject: [PATCH 04/10] add example for unix socket --- examples/CMakeLists.txt | 4 ++++ examples/example_unix_socket.cpp | 27 +++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 examples/example_unix_socket.cpp diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index eed7bc5f5..f564ae532 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -93,6 +93,10 @@ add_executable(example_file_upload example_file_upload.cpp) add_warnings_optimizations(example_file_upload) target_link_libraries(example_file_upload PUBLIC Crow::Crow) +add_executable(example_unix_socket example_unix_socket.cpp) +add_warnings_optimizations(example_unix_socket) +target_link_libraries(example_unix_socket PUBLIC Crow::Crow) + if(MSVC) add_executable(example_vs example_vs.cpp) add_warnings_optimizations(example_vs) diff --git a/examples/example_unix_socket.cpp b/examples/example_unix_socket.cpp new file mode 100644 index 000000000..a893236be --- /dev/null +++ b/examples/example_unix_socket.cpp @@ -0,0 +1,27 @@ +#include "crow.h" + +#ifdef WIN32 +#include +#else +#include +#include +#endif + +int main() +{ + crow::SimpleApp app; + + CROW_ROUTE(app, "/") + ([]() { + return "Hello, world!"; + }); + + std::string unix_path = "example.sock"; +#ifdef WIN32 + DeleteFileA(unix_path.c_str()); +#else + unlink(unix_path.c_str()); +#endif + app.unix_path(unix_path).run(); + +} From 89df0493a90374676446f3d2141c9087487fae28 Mon Sep 17 00:00:00 2001 From: na-trium-144 <100704180+na-trium-144@users.noreply.github.com> Date: Sun, 21 Apr 2024 00:36:02 +0900 Subject: [PATCH 05/10] fix unittesterror --- tests/unittest.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/unittest.cpp b/tests/unittest.cpp index 7e2b4011d..e50dc2e05 100644 --- a/tests/unittest.cpp +++ b/tests/unittest.cpp @@ -1464,7 +1464,8 @@ struct NullSimpleMiddleware TEST_CASE("middleware_simple") { App app; - decltype(app)::server_t server(&app, LOCALHOST_ADDRESS, 45451); + TCPAcceptor::endpoint endpoint(asio::ip::address::from_string(LOCALHOST_ADDRESS), 45451); + decltype(app)::server_t server(&app, endpoint); CROW_ROUTE(app, "/") ([&](const crow::request& req) { app.get_context(req); From fc8a00dcd8aa3c6a45bc5256f7e4fa423355b5c7 Mon Sep 17 00:00:00 2001 From: na-trium-144 <100704180+na-trium-144@users.noreply.github.com> Date: Sun, 21 Apr 2024 00:51:05 +0900 Subject: [PATCH 06/10] add unix_socket test --- tests/unittest.cpp | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/tests/unittest.cpp b/tests/unittest.cpp index e50dc2e05..72f8bbe19 100644 --- a/tests/unittest.cpp +++ b/tests/unittest.cpp @@ -3674,3 +3674,30 @@ TEST_CASE("http2_upgrade_is_ignored") CHECK(res.find("http2 upgrade is not supported so body is parsed") != std::string::npos); app.stop(); } + +TEST_CASE("unix_socket") +{ + static char buf[2048]; + SimpleApp app; + CROW_ROUTE(app, "/").methods("GET"_method)([] { + return "A"; + }); + + constexpr const char* socket_path = "unittest.sock"; + unlink(socket_path); + auto _ = app.unix_path(socket_path).run_async(); + app.wait_for_server_start(); + + std::string sendmsg = "GET / HTTP/1.0\r\n\r\n"; + { + asio::io_service is; + asio::local::stream_protocol::socket c(is); + c.connect(asio::local::stream_protocol::endpoint(socket_path)); + + c.send(asio::buffer(sendmsg)); + + size_t recved = c.receive(asio::buffer(buf, 2048)); + CHECK('A' == buf[recved - 1]); + } + app.stop(); +} // unix_socket From a0ce00ce0ee89b4f54aeb3ec9ca576d0c77b2181 Mon Sep 17 00:00:00 2001 From: na-trium-144 <100704180+na-trium-144@users.noreply.github.com> Date: Sun, 21 Apr 2024 01:16:11 +0900 Subject: [PATCH 07/10] no need to use windows api --- examples/example_unix_socket.cpp | 9 --------- 1 file changed, 9 deletions(-) diff --git a/examples/example_unix_socket.cpp b/examples/example_unix_socket.cpp index a893236be..e3bcb6046 100644 --- a/examples/example_unix_socket.cpp +++ b/examples/example_unix_socket.cpp @@ -1,11 +1,6 @@ #include "crow.h" -#ifdef WIN32 -#include -#else -#include #include -#endif int main() { @@ -17,11 +12,7 @@ int main() }); std::string unix_path = "example.sock"; -#ifdef WIN32 - DeleteFileA(unix_path.c_str()); -#else unlink(unix_path.c_str()); -#endif app.unix_path(unix_path).run(); } From a117692cce0b844f28eab3f7e3f4c6cbc5b03afc Mon Sep 17 00:00:00 2001 From: na-trium-144 <100704180+na-trium-144@users.noreply.github.com> Date: Sun, 21 Apr 2024 13:13:20 +0900 Subject: [PATCH 08/10] fix ssladaptor --- include/crow/socket_adaptors.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/crow/socket_adaptors.h b/include/crow/socket_adaptors.h index ffa629a45..9173ad278 100644 --- a/include/crow/socket_adaptors.h +++ b/include/crow/socket_adaptors.h @@ -189,7 +189,7 @@ namespace crow std::string address() const { - return socket_.remote_endpoint().address().to_string(); + return ssl_socket_->lowest_layer().remote_endpoint().address().to_string(); } bool is_open() From 0bdbeaaa52afd976019afa72329bb23fc042c94d Mon Sep 17 00:00:00 2001 From: na-trium-144 <100704180+na-trium-144@users.noreply.github.com> Date: Thu, 6 Jun 2024 03:28:16 +0900 Subject: [PATCH 09/10] rename unix_path to local_socket_path --- examples/example_unix_socket.cpp | 6 +++--- include/crow/app.h | 4 ++-- tests/unittest.cpp | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/examples/example_unix_socket.cpp b/examples/example_unix_socket.cpp index e3bcb6046..2c86621db 100644 --- a/examples/example_unix_socket.cpp +++ b/examples/example_unix_socket.cpp @@ -11,8 +11,8 @@ int main() return "Hello, world!"; }); - std::string unix_path = "example.sock"; - unlink(unix_path.c_str()); - app.unix_path(unix_path).run(); + std::string local_socket_path = "example.sock"; + unlink(local_socket_path.c_str()); + app.local_socket_path(local_socket_path).run(); } diff --git a/include/crow/app.h b/include/crow/app.h index 6c8c21fd7..09768d38b 100644 --- a/include/crow/app.h +++ b/include/crow/app.h @@ -346,7 +346,7 @@ namespace crow } /// \brief Disable tcp/ip and use unix domain socket instead - self_t& unix_path(std::string path) + self_t& local_socket_path(std::string path) { bindaddr_ = path; use_unix_ = true; @@ -354,7 +354,7 @@ namespace crow } /// \brief Get the unix domain socket path - std::string unix_path() + std::string local_socket_path() { return bindaddr_; } diff --git a/tests/unittest.cpp b/tests/unittest.cpp index b5c0ee5e0..82e37613d 100644 --- a/tests/unittest.cpp +++ b/tests/unittest.cpp @@ -3692,7 +3692,7 @@ TEST_CASE("unix_socket") constexpr const char* socket_path = "unittest.sock"; unlink(socket_path); - auto _ = app.unix_path(socket_path).run_async(); + auto _ = app.local_socket_path(socket_path).run_async(); app.wait_for_server_start(); std::string sendmsg = "GET / HTTP/1.0\r\n\r\n"; From f563217a8d649c951e5f2962411247dd452bb9d3 Mon Sep 17 00:00:00 2001 From: na-trium-144 <100704180+na-trium-144@users.noreply.github.com> Date: Tue, 17 Dec 2024 04:45:31 +0900 Subject: [PATCH 10/10] fix build errors and warnings --- include/crow/routing.h | 2 +- include/crow/socket_acceptors.h | 16 ++++++++++++---- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/include/crow/routing.h b/include/crow/routing.h index 536c9ad95..aaaa0df35 100644 --- a/include/crow/routing.h +++ b/include/crow/routing.h @@ -473,7 +473,7 @@ namespace crow // NOTE: Already documented in "crow/app.h" void handle_upgrade(const request& req, response&, UnixSocketAdaptor&& adaptor) override { max_payload_ = max_payload_override_ ? max_payload_ : app_->websocket_max_payload(); - new crow::websocket::Connection(req, std::move(adaptor), app_, max_payload_, open_handler_, message_handler_, close_handler_, error_handler_, accept_handler_); + new crow::websocket::Connection(req, std::move(adaptor), app_, max_payload_, subprotocols_, open_handler_, message_handler_, close_handler_, error_handler_, accept_handler_); } #ifdef CROW_ENABLE_SSL void handle_upgrade(const request& req, response&, SSLAdaptor&& adaptor) override diff --git a/include/crow/socket_acceptors.h b/include/crow/socket_acceptors.h index 91e62ceb3..db31f3133 100644 --- a/include/crow/socket_acceptors.h +++ b/include/crow/socket_acceptors.h @@ -13,8 +13,8 @@ namespace crow { using endpoint = tcp::endpoint; tcp::acceptor acceptor_; - TCPAcceptor(asio::io_service& io_service, const endpoint& endpoint): - acceptor_(io_service, endpoint) {} + TCPAcceptor(asio::io_service& io_service, const endpoint& endpoint_): + acceptor_(io_service, endpoint_) {} int16_t port() const { @@ -28,14 +28,18 @@ namespace crow { return acceptor_; } + endpoint local_endpoint() const + { + return acceptor_.local_endpoint(); + } }; struct UnixSocketAcceptor { using endpoint = stream_protocol::endpoint; stream_protocol::acceptor acceptor_; - UnixSocketAcceptor(asio::io_service& io_service, const endpoint& endpoint): - acceptor_(io_service, endpoint, false) {} + UnixSocketAcceptor(asio::io_service& io_service, const endpoint& endpoint_): + acceptor_(io_service, endpoint_, false) {} // reuse addr must be false (https://github.com/chriskohlhoff/asio/issues/622) int16_t port() const @@ -50,5 +54,9 @@ namespace crow { return acceptor_; } + endpoint local_endpoint() const + { + return acceptor_.local_endpoint(); + } }; } // namespace crow \ No newline at end of file