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

(cherry picked from commit 4814cdcabb06c47fb6258b0031d97e2d5bff24b2)
This commit is contained in:
Антон
2025-04-18 17:30:16 +03:00
parent 8752a8bd85
commit e6823ce506
4 changed files with 70 additions and 55 deletions
+3 -1
View File
@@ -13,7 +13,9 @@ endif ()
find_package(Boost REQUIRED)
add_executable(App ./src/main.cpp)
add_executable(App ./src/main.cpp
./src/helpers/helpers.h
./src/helpers/helpers.cpp)
target_link_libraries(App PRIVATE Boost::boost)
+56
View File
@@ -0,0 +1,56 @@
#include "helpers.h"
namespace uad {
boost::beast::string_view MimeType(boost::beast::string_view path) {
using boost::beast::iequals;
auto const ext = [&path] {
auto const pos = path.rfind(".");
if (pos == boost::beast::string_view::npos)
return boost::beast::string_view{};
return path.substr(pos);
}();
if (iequals(ext, ".htm"))
return "text/html";
if (iequals(ext, ".html"))
return "text/html";
if (iequals(ext, ".php"))
return "text/html";
if (iequals(ext, ".css"))
return "text/css";
if (iequals(ext, ".txt"))
return "text/plain";
if (iequals(ext, ".js"))
return "application/javascript";
if (iequals(ext, ".json"))
return "application/json";
if (iequals(ext, ".xml"))
return "application/xml";
if (iequals(ext, ".swf"))
return "application/x-shockwave-flash";
if (iequals(ext, ".flv"))
return "video/x-flv";
if (iequals(ext, ".png"))
return "image/png";
if (iequals(ext, ".jpe"))
return "image/jpeg";
if (iequals(ext, ".jpeg"))
return "image/jpeg";
if (iequals(ext, ".jpg"))
return "image/jpeg";
if (iequals(ext, ".gif"))
return "image/gif";
if (iequals(ext, ".bmp"))
return "image/bmp";
if (iequals(ext, ".ico"))
return "image/vnd.microsoft.icon";
if (iequals(ext, ".tiff"))
return "image/tiff";
if (iequals(ext, ".tif"))
return "image/tiff";
if (iequals(ext, ".svg"))
return "image/svg+xml";
if (iequals(ext, ".svgz"))
return "image/svg+xml";
return "application/text";
}
}
+5
View File
@@ -0,0 +1,5 @@
#include <boost/beast.hpp>
namespace uad {
boost::beast::string_view MimeType(boost::beast::string_view path);
}
+6 -54
View File
@@ -17,65 +17,17 @@
#include <thread>
#include <vector>
#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>
using namespace uad;
// Return a reasonable mime type based on the extension of a file.
beast::string_view mime_type(beast::string_view path) {
using beast::iequals;
auto const ext = [&path] {
auto const pos = path.rfind(".");
if (pos == beast::string_view::npos)
return beast::string_view{};
return path.substr(pos);
}();
if (iequals(ext, ".htm"))
return "text/html";
if (iequals(ext, ".html"))
return "text/html";
if (iequals(ext, ".php"))
return "text/html";
if (iequals(ext, ".css"))
return "text/css";
if (iequals(ext, ".txt"))
return "text/plain";
if (iequals(ext, ".js"))
return "application/javascript";
if (iequals(ext, ".json"))
return "application/json";
if (iequals(ext, ".xml"))
return "application/xml";
if (iequals(ext, ".swf"))
return "application/x-shockwave-flash";
if (iequals(ext, ".flv"))
return "video/x-flv";
if (iequals(ext, ".png"))
return "image/png";
if (iequals(ext, ".jpe"))
return "image/jpeg";
if (iequals(ext, ".jpeg"))
return "image/jpeg";
if (iequals(ext, ".jpg"))
return "image/jpeg";
if (iequals(ext, ".gif"))
return "image/gif";
if (iequals(ext, ".bmp"))
return "image/bmp";
if (iequals(ext, ".ico"))
return "image/vnd.microsoft.icon";
if (iequals(ext, ".tiff"))
return "image/tiff";
if (iequals(ext, ".tif"))
return "image/tiff";
if (iequals(ext, ".svg"))
return "image/svg+xml";
if (iequals(ext, ".svgz"))
return "image/svg+xml";
return "application/text";
}
// Append an HTTP rel-path to a local filesystem path.
// The returned path is normalized for the platform.
@@ -178,7 +130,7 @@ void handle_request(beast::string_view doc_root,
if (req.method() == http::verb::head) {
http::response<http::empty_body> res{http::status::ok, req.version()};
res.set(http::field::server, BOOST_BEAST_VERSION_STRING);
res.set(http::field::content_type, mime_type(path));
res.set(http::field::content_type, MimeType(path));
res.content_length(size);
res.keep_alive(req.keep_alive());
return send(std::move(res));
@@ -189,7 +141,7 @@ void handle_request(beast::string_view doc_root,
std::piecewise_construct, std::make_tuple(std::move(body)),
std::make_tuple(http::status::ok, req.version())};
res.set(http::field::server, BOOST_BEAST_VERSION_STRING);
res.set(http::field::content_type, mime_type(path));
res.set(http::field::content_type, MimeType(path));
res.content_length(size);
res.keep_alive(req.keep_alive());
return send(std::move(res));