1 #pragma once 2 3 #include <experimental/filesystem> 4 #include <fstream> 5 #include <string> 6 #include <crow/app.h> 7 #include <crow/http_request.h> 8 #include <crow/http_response.h> 9 #include <crow/routing.h> 10 #include <boost/algorithm/string/replace.hpp> 11 #include <boost/container/flat_set.hpp> 12 13 namespace crow { 14 namespace webassets { 15 16 namespace filesystem = std::experimental::filesystem; 17 18 struct CmpStr { 19 bool operator()(const char* a, const char* b) const { 20 return std::strcmp(a, b) < 0; 21 } 22 }; 23 24 static boost::container::flat_set<std::string> routes; 25 26 template <typename... Middlewares> 27 void requestRoutes(Crow<Middlewares...>& app) { 28 const static boost::container::flat_map<const char*, const char*, CmpStr> 29 contentTypes{ 30 {{".css", "text/css;charset=UTF-8"}, 31 {".html", "text/html;charset=UTF-8"}, 32 {".js", "text/html;charset=UTF-8"}, 33 {".png", "image/png;charset=UTF-8"}, 34 {".woff", "application/x-font-woff"}, 35 {".woff2", "application/x-font-woff2"}, 36 {".gif", "image/gif"}, 37 {".ico", "image/x-icon"}, 38 {".ttf", "application/x-font-ttf"}, 39 {".svg", "image/svg+xml"}, 40 {".eot", "application/vnd.ms-fontobject"}, 41 {".xml", "application/xml"}, 42 {".jpg", "image/jpeg"}, 43 {".jpeg", "image/jpeg"}, 44 {".json", "application/json"}, 45 // dev tools don't care about map type, setting to json causes 46 // browser to show as text 47 // https://stackoverflow.com/questions/19911929/what-mime-type-should-i-use-for-javascript-source-map-files 48 {".map", "application/json"}}}; 49 filesystem::path rootpath{"/usr/share/www/"}; 50 filesystem::recursive_directory_iterator dirIter(rootpath); 51 52 for (const filesystem::directory_entry& dir : dirIter) { 53 filesystem::path absolutePath = dir.path(); 54 filesystem::path relativePath{ 55 absolutePath.string().substr(rootpath.string().size() - 1)}; 56 // make sure we don't recurse into certain directories 57 // note: maybe check for is_directory() here as well... 58 59 if (filesystem::is_directory(dir)) { 60 // don't recurse into hidden directories or symlinks 61 if (boost::starts_with(dir.path().filename().string(), ".") || 62 filesystem::is_symlink(dir)) { 63 dirIter.disable_recursion_pending(); 64 } 65 } else if (filesystem::is_regular_file(dir)) { 66 std::string extension = relativePath.extension(); 67 filesystem::path webpath = relativePath; 68 const char* contentEncoding = nullptr; 69 70 if (extension == ".gz") { 71 webpath = webpath.replace_extension(""); 72 // Use the non-gzip version for determining content type 73 extension = webpath.extension().string(); 74 contentEncoding = "gzip"; 75 } 76 77 if (boost::starts_with(webpath.filename().string(), "index.")) { 78 webpath = webpath.parent_path(); 79 if (webpath.string().size() == 0 || webpath.string().back() != '/') { 80 // insert the non-directory version of this path 81 routes.insert(webpath); 82 webpath += "/"; 83 } 84 } 85 86 routes.insert(webpath); 87 const char* contentType = nullptr; 88 89 auto contentTypeIt = contentTypes.find(extension.c_str()); 90 if (contentTypeIt == contentTypes.end()) { 91 BMCWEB_LOG_ERROR << "Cannot determine content-type for " << absolutePath 92 << " with extension " << extension; 93 } else { 94 contentType = contentTypeIt->second; 95 } 96 97 app.routeDynamic(webpath)( 98 [absolutePath, contentType, contentEncoding](const crow::Request& req, 99 crow::Response& res) { 100 if (contentType != nullptr) { 101 res.addHeader("Content-Type", contentType); 102 } 103 104 if (contentEncoding != nullptr) { 105 res.addHeader("Content-Encoding", contentEncoding); 106 } 107 108 // res.set_header("Cache-Control", "public, max-age=86400"); 109 std::ifstream inf(absolutePath); 110 if (!inf) { 111 BMCWEB_LOG_DEBUG << "failed to read file"; 112 res.result(boost::beast::http::status::internal_server_error); 113 res.end(); 114 return; 115 } 116 117 res.body() = {std::istreambuf_iterator<char>(inf), 118 std::istreambuf_iterator<char>()}; 119 res.end(); 120 }); 121 } 122 } 123 } // namespace webassets 124 } // namespace webassets 125 } // namespace crow 126