From 6b91026eddfa739b6637b18285041063e0fd0348 Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Mon, 13 Apr 2020 19:36:15 +0200 Subject: Fix webbox (WIP) --- plugins/static-files/static-files.cpp | 55 +++++++++++++++++++++++++++++++---- 1 file changed, 50 insertions(+), 5 deletions(-) (limited to 'plugins/static-files') diff --git a/plugins/static-files/static-files.cpp b/plugins/static-files/static-files.cpp index b137cb8..011e37f 100644 --- a/plugins/static-files/static-files.cpp +++ b/plugins/static-files/static-files.cpp @@ -1,5 +1,7 @@ #include "static-files.h" +#include + #include #include #include @@ -10,6 +12,42 @@ namespace fs = std::filesystem; namespace { +// Return a reasonable mime type based on the extension of a file. +std::string +mime_type(fs::path path) +{ + using boost::algorithm::iequals; + auto const ext = [&path] + { + size_t pos = path.string().rfind("."); + if (pos == std::string::npos) + return std::string{}; + return path.string().substr(pos); + }(); + if(iequals(ext, ".htm")) return "text/html"; // TODO: unordered_map + 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"; +} + std::string getFile(const fs::path& filename) { std::ifstream file(filename.string(), std::ios::in | std::ios::binary | std::ios::ate); @@ -28,10 +66,10 @@ std::string getFile(const fs::path& filename) } } -std::string extend_index_html(std::string path) +fs::path extend_index_html(fs::path path) { - if (path.size() == 0 || (path.size() && path.back() == '/')) - path.append("index.html"); + if (path.string().size() == 0 || path.string().back() == '/') + return path / "index.html"; return path; } @@ -74,14 +112,21 @@ std::string static_files_plugin::generate_page( // Request path must not contain "..". std::string rel_target{GetRequestParam("rel_target")}; + std::string target{GetRequestParam("target")}; if (rel_target.find("..") != std::string::npos) { - std::string target{GetRequestParam("target")}; return HttpStatus("400", "Illegal request: "s + target, SetResponseHeader); } // Build the path to the requested file std::string doc_root{GetRequestParam("doc_root")}; - std::string path {fs::path{doc_root} / extend_index_html(rel_target)}; + fs::path path {fs::path{doc_root} / rel_target}; + if (target.size() && target.back() != '/' && fs::is_directory(path)) { + std::string location{GetRequestParam("location") + "/"s}; + SetResponseHeader("location", location); + return HttpStatus("301", "Correcting directory path", SetResponseHeader); + } + path = {extend_index_html(path)}; + SetResponseHeader("content_type", mime_type(path)); try { return getFile(path); -- cgit v1.2.3