From a6a4391123a91fff12317802a12bc522e0b3245c Mon Sep 17 00:00:00 2001 From: Vladislav Ostapov Date: Wed, 2 Apr 2025 13:36:54 +0300 Subject: [PATCH] =?UTF-8?q?=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=B4=D0=BB=D1=8F=20=D0=B7=D0=B0?= =?UTF-8?q?=D1=89=D0=B8=D1=82=D1=8B=20=D0=B7=D0=B0=D0=BF=D1=83=D1=81=D0=BA?= =?UTF-8?q?=D0=B0=20=D0=B2=D0=B5=D0=B1=D0=BA=D0=B8=20=D1=81=20=D0=BD=D0=B5?= =?UTF-8?q?=D0=BA=D0=BE=D1=80=D1=80=D0=B5=D0=BA=D1=82=D0=BD=D1=8B=D0=BC?= =?UTF-8?q?=D0=B8=20=D0=B0=D1=80=D0=B3=D1=83=D0=BC=D0=B5=D0=BD=D1=82=D0=B0?= =?UTF-8?q?=D0=BC=D0=B8=20=D0=BA=D0=BE=D0=BC=D0=B0=D0=BD=D0=B4=D0=BD=D0=BE?= =?UTF-8?q?=D0=B9=20=D1=81=D1=82=D1=80=D0=BE=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.cpp | 32 +++++++++++++++++++++++++------- src/server/server.cpp | 8 ++++---- src/server/server.hpp | 4 ++-- 3 files changed, 31 insertions(+), 13 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 3dc34de..13b1a66 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -538,6 +538,26 @@ int main(int argc, char *argv[]) { return 1; } + if (strcmp(argv[1], "nossl") != 0 && strcmp(argv[1], "ssl") != 0) { + std::cerr << "Unsupported ssl mode: " << argv[1] << std::endl; + return 1; + } + + int serverPort; + try { + size_t idx = 0; + serverPort = std::stoi(std::string(argv[3]), &idx); + if (serverPort < 0 || serverPort > 0xffff) { + throw std::invalid_argument("Out of range"); + } + if (idx != strlen(argv[3])) { + throw std::invalid_argument("Invalid number"); + } + } catch (std::exception& e) { + std::cerr << "Wrong server port `" << argv[3] << "`: " << e.what() << std::endl; + return 1; + } + init_logging(); boost::log::core::get()->add_thread_attribute("Scope", boost::log::attributes::named_scope()); @@ -563,11 +583,11 @@ int main(int argc, char *argv[]) { std::unique_ptr s; if (strcmp(argv[1], "nossl") == 0) { - s = std::make_unique(argv[2], argv[3]); + BOOST_LOG_TRIVIAL(info) << "Run server on " << argv[2] << ":" << serverPort; + s = std::make_unique(argv[2], serverPort); resources.registerResources(*s); s->run(); - - } else if (strcmp(argv[1], "ssl") == 0) { + } else { std::vector cert; http::resource::loadFile("cert.pem", cert); std::vector key; http::resource::loadFile("key.pem", key); std::vector dh; http::resource::loadFile("dh.pem", dh); @@ -584,12 +604,10 @@ int main(int argc, char *argv[]) { ctx->use_private_key(boost::asio::buffer(key), ssl::context::file_format::pem); ctx->use_tmp_dh(boost::asio::buffer(dh)); - s = std::make_unique(argv[2], argv[3], ctx); + BOOST_LOG_TRIVIAL(info) << "Run server on " << argv[2] << ":" << serverPort; + s = std::make_unique(argv[2], serverPort, ctx); resources.registerResources(*s); s->run(); - } else { - std::cerr << "Unsupported ssl mode: " << argv[1] << std::endl; - return 1; } } catch (std::exception &e) { BOOST_LOG_TRIVIAL(error) << e.what() << std::endl; diff --git a/src/server/server.cpp b/src/server/server.cpp index 5e5068c..3562367 100644 --- a/src/server/server.cpp +++ b/src/server/server.cpp @@ -23,7 +23,7 @@ namespace http::server { connections_.clear(); } - Server::Server(const std::string &address, const std::string &port) + Server::Server(const std::string &address, int port) : io_context_(1), signals_(io_context_), acceptor_(io_context_) { // Register to handle the signals that indicate when the server should exit. // It is safe to register for the same signal multiple times in a program, @@ -39,7 +39,7 @@ namespace http::server { // Open the acceptor with the option to reuse the address (i.e. SO_REUSEADDR). boost::asio::ip::tcp::resolver resolver(io_context_); boost::asio::ip::tcp::endpoint endpoint = - *resolver.resolve(address, port).begin(); + *resolver.resolve(address, std::to_string(port)).begin(); acceptor_.open(endpoint.protocol()); acceptor_.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true)); acceptor_.bind(endpoint); @@ -48,7 +48,7 @@ namespace http::server { doAccept(); } - Server::Server(const std::string &address, const std::string &port, std::shared_ptr ctx): + Server::Server(const std::string &address, int port, std::shared_ptr ctx): ssl_ctx(std::move(ctx)), io_context_(1), signals_(io_context_), acceptor_(io_context_) { // Register to handle the signals that indicate when the server should exit. // It is safe to register for the same signal multiple times in a program, @@ -63,7 +63,7 @@ namespace http::server { // Open the acceptor with the option to reuse the address (i.e. SO_REUSEADDR). boost::asio::ip::tcp::resolver resolver(io_context_); - boost::asio::ip::tcp::endpoint endpoint = *resolver.resolve(address, port).begin(); + boost::asio::ip::tcp::endpoint endpoint = *resolver.resolve(address, std::to_string(port)).begin(); acceptor_.open(endpoint.protocol()); acceptor_.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true)); acceptor_.bind(endpoint); diff --git a/src/server/server.hpp b/src/server/server.hpp index 0ad4f5b..557be00 100644 --- a/src/server/server.hpp +++ b/src/server/server.hpp @@ -44,8 +44,8 @@ namespace http::server { Server &operator=(const Server &) = delete; /// Construct the server to listen on the specified TCP address and port - explicit Server(const std::string &address, const std::string &port); - explicit Server(const std::string &address, const std::string &port, std::shared_ptr ctx); + explicit Server(const std::string &address, int port); + explicit Server(const std::string &address, int port, std::shared_ptr ctx); std::vector> resources;