diff --git a/CMakeLists.txt b/CMakeLists.txt index 3a2b2c6..e5c280e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/src/helpers/helpers.cpp b/src/helpers/helpers.cpp new file mode 100644 index 0000000..5376ba3 --- /dev/null +++ b/src/helpers/helpers.cpp @@ -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"; +} +} diff --git a/src/helpers/helpers.h b/src/helpers/helpers.h new file mode 100644 index 0000000..cc1e4d8 --- /dev/null +++ b/src/helpers/helpers.h @@ -0,0 +1,5 @@ +#include + +namespace uad { +boost::beast::string_view MimeType(boost::beast::string_view path); +} diff --git a/src/main.cpp b/src/main.cpp index 62ee94e..a850df1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -17,65 +17,17 @@ #include #include +#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 +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 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));