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 // dev tools don't care about map type, setting to json causes 45 // browser to show as text 46 // https://stackoverflow.com/questions/19911929/what-mime-type-should-i-use-for-javascript-source-map-files 47 {".map", "application/json"}}}; 48 filesystem::path rootpath{"/usr/share/www/"}; 49 filesystem::recursive_directory_iterator dirIter(rootpath); 50 51 for (const filesystem::directory_entry& dir : dirIter) { 52 filesystem::path absolutePath = dir.path(); 53 filesystem::path relativePath{ 54 absolutePath.string().substr(rootpath.string().size() - 1)}; 55 // make sure we don't recurse into certain directories 56 // note: maybe check for is_directory() here as well... 57 58 if (filesystem::is_directory(dir)) { 59 // don't recurse into hidden directories or symlinks 60 if (boost::starts_with(dir.path().filename().string(), ".") || 61 filesystem::is_symlink(dir)) { 62 dirIter.disable_recursion_pending(); 63 } 64 } else if (filesystem::is_regular_file(dir)) { 65 std::string extension = relativePath.extension(); 66 filesystem::path webpath = relativePath; 67 const char* contentEncoding = nullptr; 68 69 if (extension == ".gz") { 70 webpath = webpath.replace_extension(""); 71 // Use the non-gzip version for determining content type 72 extension = webpath.extension().string(); 73 contentEncoding = "gzip"; 74 } 75 76 if (boost::starts_with(webpath.filename().string(), "index.")) { 77 webpath = webpath.parent_path(); 78 if (webpath.string().size() == 0 || webpath.string().back() != '/') { 79 // insert the non-directory version of this path 80 routes.insert(webpath); 81 webpath += "/"; 82 } 83 } 84 85 routes.insert(webpath); 86 const char* contentType = nullptr; 87 88 auto contentTypeIt = contentTypes.find(extension.c_str()); 89 if (contentTypeIt == contentTypes.end()) { 90 BMCWEB_LOG_ERROR << "Cannot determine content-type for " << absolutePath 91 << " with extension " << extension; 92 } else { 93 contentType = contentTypeIt->second; 94 } 95 96 app.routeDynamic(webpath)( 97 [absolutePath, contentType, contentEncoding](const crow::Request& req, 98 crow::Response& res) { 99 if (contentType != nullptr) { 100 res.addHeader("Content-Type", contentType); 101 } 102 103 if (contentEncoding != nullptr) { 104 res.addHeader("Content-Encoding", contentEncoding); 105 } 106 107 // res.set_header("Cache-Control", "public, max-age=86400"); 108 std::ifstream inf(absolutePath); 109 if (!inf) { 110 BMCWEB_LOG_DEBUG << "failed to read file"; 111 res.result(boost::beast::http::status::internal_server_error); 112 res.end(); 113 return; 114 } 115 116 res.body() = {std::istreambuf_iterator<char>(inf), 117 std::istreambuf_iterator<char>()}; 118 res.end(); 119 }); 120 } 121 } 122 } // namespace webassets 123 } // namespace webassets 124 } // namespace crow 125