Вынесена функция 2

(cherry picked from commit e23977fdf0c870d677f37902b3ad0de23dc67e12)
This commit is contained in:
Антон
2025-04-18 17:36:48 +03:00
parent e6823ce506
commit b6c914f6f8
3 changed files with 29 additions and 36 deletions
+21
View File
@@ -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;
}
}
+2
View File
@@ -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);
}
+6 -36
View File
@@ -19,43 +19,13 @@
#include "./helpers/helpers.h"
namespace beast = boost::beast; // from <boost/beast.hpp>
namespace http = beast::http; // from <boost/beast/http.hpp>
namespace websocket = beast::websocket; // from <boost/beast/websocket.hpp>
namespace net = boost::asio; // from <boost/asio.hpp>
using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
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 <class Body, class Allocator, class Send>
void handle_request(beast::string_view doc_root,
http::request<Body, http::basic_fields<Allocator>> &&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");