From 50d2fad70aa3e1c768932d4c8a479a24ab6f5d7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BD=D1=82=D0=BE=D0=BD?= Date: Fri, 18 Apr 2025 17:36:48 +0300 Subject: [PATCH] =?UTF-8?q?=D0=92=D1=8B=D0=BD=D0=B5=D1=81=D0=B5=D0=BD?= =?UTF-8?q?=D0=B0=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D1=8F=202?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit (cherry picked from commit e23977fdf0c870d677f37902b3ad0de23dc67e12) --- src/helpers/helpers.cpp | 21 +++++++++++++++++++++ src/helpers/helpers.h | 2 ++ src/main.cpp | 42 ++++++----------------------------------- 3 files changed, 29 insertions(+), 36 deletions(-) diff --git a/src/helpers/helpers.cpp b/src/helpers/helpers.cpp index 5376ba3..5e6730d 100644 --- a/src/helpers/helpers.cpp +++ b/src/helpers/helpers.cpp @@ -53,4 +53,25 @@ boost::beast::string_view MimeType(boost::beast::string_view path) { return "image/svg+xml"; return "application/text"; } + +std::string PathCat(boost::beast::string_view base, boost::beast::string_view path){ + if (base.empty()) + return std::string(path); + std::string result(base); +#ifdef BOOST_MSVC + char constexpr path_separator = '\\'; + if (result.back() == path_separator) + result.resize(result.size() - 1); + result.append(path.data(), path.size()); + for (auto &c : result) + if (c == '/') + c = path_separator; +#else + char constexpr path_separator = '/'; + if (result.back() == path_separator) + result.resize(result.size() - 1); + result.append(path.data(), path.size()); +#endif + return result; +} } diff --git a/src/helpers/helpers.h b/src/helpers/helpers.h index cc1e4d8..37cb506 100644 --- a/src/helpers/helpers.h +++ b/src/helpers/helpers.h @@ -2,4 +2,6 @@ namespace uad { boost::beast::string_view MimeType(boost::beast::string_view path); + +std::string PathCat(boost::beast::string_view base, boost::beast::string_view path); } diff --git a/src/main.cpp b/src/main.cpp index a850df1..45fb3d8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -19,43 +19,13 @@ #include "./helpers/helpers.h" -namespace beast = boost::beast; // from -namespace http = beast::http; // from -namespace websocket = beast::websocket; // from -namespace net = boost::asio; // from -using tcp = boost::asio::ip::tcp; // from +namespace beast = boost::beast; +namespace http = beast::http; +namespace websocket = beast::websocket; +namespace net = boost::asio; +using tcp = boost::asio::ip::tcp; using namespace uad; -// Return a reasonable mime type based on the extension of a file. - - -// Append an HTTP rel-path to a local filesystem path. -// The returned path is normalized for the platform. -std::string path_cat(beast::string_view base, beast::string_view path) { - if (base.empty()) - return std::string(path); - std::string result(base); -#ifdef BOOST_MSVC - char constexpr path_separator = '\\'; - if (result.back() == path_separator) - result.resize(result.size() - 1); - result.append(path.data(), path.size()); - for (auto &c : result) - if (c == '/') - c = path_separator; -#else - char constexpr path_separator = '/'; - if (result.back() == path_separator) - result.resize(result.size() - 1); - result.append(path.data(), path.size()); -#endif - return result; -} - -// This function produces an HTTP response for the given -// request. The type of the response object depends on the -// contents of the request, so the interface requires the -// caller to pass a generic lambda for receiving the response. template void handle_request(beast::string_view doc_root, http::request> &&req, @@ -106,7 +76,7 @@ void handle_request(beast::string_view doc_root, return send(bad_request("Illegal request-target")); // Build the path to the requested file - std::string path = path_cat(doc_root, req.target()); + std::string path = PathCat(doc_root, req.target()); if (req.target().back() == '/') path.append("index.html");