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