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