Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 3 | #include "app.hpp" |
| 4 | #include "http_request.hpp" |
| 5 | #include "http_response.hpp" |
| 6 | #include "routing.hpp" |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 7 | #include "webroutes.hpp" |
| 8 | |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 9 | #include <boost/container/flat_set.hpp> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 10 | |
Ed Tanous | ca89b20 | 2024-04-25 23:02:40 -0700 | [diff] [blame] | 11 | #include <algorithm> |
| 12 | #include <array> |
James Feist | 4418c7f | 2019-04-15 11:09:15 -0700 | [diff] [blame] | 13 | #include <filesystem> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 14 | #include <fstream> |
| 15 | #include <string> |
Ed Tanous | ca89b20 | 2024-04-25 23:02:40 -0700 | [diff] [blame] | 16 | #include <string_view> |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 17 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 18 | namespace crow |
| 19 | { |
| 20 | namespace webassets |
| 21 | { |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 22 | |
Ed Tanous | 499b5b4 | 2024-04-06 08:39:18 -0700 | [diff] [blame] | 23 | inline std::string getStaticEtag(const std::filesystem::path& webpath) |
| 24 | { |
| 25 | // webpack outputs production chunks in the form: |
| 26 | // <filename>.<hash>.<extension> |
| 27 | // For example app.63e2c453.css |
| 28 | // Try to detect this, so we can use the hash as the ETAG |
| 29 | std::vector<std::string> split; |
| 30 | bmcweb::split(split, webpath.filename().string(), '.'); |
Ed Tanous | 499b5b4 | 2024-04-06 08:39:18 -0700 | [diff] [blame] | 31 | if (split.size() < 3) |
| 32 | { |
| 33 | return ""; |
| 34 | } |
| 35 | |
| 36 | // get the second to last element |
| 37 | std::string hash = split.rbegin()[1]; |
| 38 | |
| 39 | // Webpack hashes are 8 characters long |
| 40 | if (hash.size() != 8) |
| 41 | { |
| 42 | return ""; |
| 43 | } |
| 44 | // Webpack hashes only include hex printable characters |
| 45 | if (hash.find_first_not_of("0123456789abcdefABCDEF") != std::string::npos) |
| 46 | { |
| 47 | return ""; |
| 48 | } |
| 49 | return std::format("\"{}\"", hash); |
| 50 | } |
| 51 | |
Ed Tanous | ca89b20 | 2024-04-25 23:02:40 -0700 | [diff] [blame] | 52 | static constexpr std::string_view rootpath("/usr/share/www/"); |
| 53 | |
| 54 | struct StaticFile |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 55 | { |
Ed Tanous | ca89b20 | 2024-04-25 23:02:40 -0700 | [diff] [blame] | 56 | std::filesystem::path absolutePath; |
| 57 | std::string_view contentType; |
| 58 | std::string_view contentEncoding; |
| 59 | std::string etag; |
| 60 | bool renamed = false; |
| 61 | }; |
| 62 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 63 | inline void handleStaticAsset( |
| 64 | const crow::Request& req, |
| 65 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, const StaticFile& file) |
Ed Tanous | ca89b20 | 2024-04-25 23:02:40 -0700 | [diff] [blame] | 66 | { |
| 67 | if (!file.contentType.empty()) |
| 68 | { |
| 69 | asyncResp->res.addHeader(boost::beast::http::field::content_type, |
| 70 | file.contentType); |
| 71 | } |
| 72 | |
| 73 | if (!file.contentEncoding.empty()) |
| 74 | { |
| 75 | asyncResp->res.addHeader(boost::beast::http::field::content_encoding, |
| 76 | file.contentEncoding); |
| 77 | } |
| 78 | |
| 79 | if (!file.etag.empty()) |
| 80 | { |
| 81 | asyncResp->res.addHeader(boost::beast::http::field::etag, file.etag); |
| 82 | // Don't cache paths that don't have the etag in them, like |
| 83 | // index, which gets transformed to / |
| 84 | if (!file.renamed) |
| 85 | { |
| 86 | // Anything with a hash can be cached forever and is |
| 87 | // immutable |
| 88 | asyncResp->res.addHeader(boost::beast::http::field::cache_control, |
| 89 | "max-age=31556926, immutable"); |
| 90 | } |
| 91 | |
| 92 | std::string_view cachedEtag = |
| 93 | req.getHeaderValue(boost::beast::http::field::if_none_match); |
| 94 | if (cachedEtag == file.etag) |
| 95 | { |
| 96 | asyncResp->res.result(boost::beast::http::status::not_modified); |
| 97 | return; |
| 98 | } |
| 99 | } |
| 100 | |
Myung Bae | d51c61b | 2024-09-13 10:35:34 -0500 | [diff] [blame] | 101 | if (asyncResp->res.openFile(file.absolutePath) != crow::OpenCode::Success) |
Ed Tanous | ca89b20 | 2024-04-25 23:02:40 -0700 | [diff] [blame] | 102 | { |
| 103 | BMCWEB_LOG_DEBUG("failed to read file"); |
| 104 | asyncResp->res.result( |
| 105 | boost::beast::http::status::internal_server_error); |
| 106 | return; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | inline std::string_view getFiletypeForExtension(std::string_view extension) |
| 111 | { |
| 112 | constexpr static std::array<std::pair<std::string_view, std::string_view>, |
| 113 | 17> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 114 | contentTypes{ |
| 115 | {{".css", "text/css;charset=UTF-8"}, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 116 | {".eot", "application/vnd.ms-fontobject"}, |
Ed Tanous | ca89b20 | 2024-04-25 23:02:40 -0700 | [diff] [blame] | 117 | {".gif", "image/gif"}, |
| 118 | {".html", "text/html;charset=UTF-8"}, |
| 119 | {".ico", "image/x-icon"}, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 120 | {".jpeg", "image/jpeg"}, |
Ed Tanous | ca89b20 | 2024-04-25 23:02:40 -0700 | [diff] [blame] | 121 | {".jpg", "image/jpeg"}, |
| 122 | {".js", "application/javascript;charset=UTF-8"}, |
| 123 | {".json", "application/json"}, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 124 | // dev tools don't care about map type, setting to json causes |
| 125 | // browser to show as text |
| 126 | // https://stackoverflow.com/questions/19911929/what-mime-type-should-i-use-for-javascript-source-map-files |
Ed Tanous | ca89b20 | 2024-04-25 23:02:40 -0700 | [diff] [blame] | 127 | {".map", "application/json"}, |
| 128 | {".png", "image/png;charset=UTF-8"}, |
| 129 | {".svg", "image/svg+xml"}, |
| 130 | {".ttf", "application/x-font-ttf"}, |
| 131 | {".woff", "application/x-font-woff"}, |
| 132 | {".woff2", "application/x-font-woff2"}, |
| 133 | {".xml", "application/xml"}}}; |
Ed Tanous | a47f32b | 2019-10-24 10:08:04 -0700 | [diff] [blame] | 134 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 135 | const auto* contentType = |
| 136 | std::ranges::find_if(contentTypes, [&extension](const auto& val) { |
| 137 | return val.first == extension; |
| 138 | }); |
Ed Tanous | 153fdcf | 2021-03-04 16:43:14 -0800 | [diff] [blame] | 139 | |
Ed Tanous | ca89b20 | 2024-04-25 23:02:40 -0700 | [diff] [blame] | 140 | if (contentType == contentTypes.end()) |
| 141 | { |
| 142 | BMCWEB_LOG_ERROR( |
| 143 | "Cannot determine content-type for file with extension {}", |
| 144 | extension); |
| 145 | return ""; |
| 146 | } |
| 147 | return contentType->second; |
| 148 | } |
| 149 | |
| 150 | inline void addFile(App& app, const std::filesystem::directory_entry& dir) |
| 151 | { |
| 152 | StaticFile file; |
| 153 | file.absolutePath = dir.path(); |
| 154 | std::filesystem::path relativePath( |
| 155 | file.absolutePath.string().substr(rootpath.size() - 1)); |
| 156 | |
| 157 | std::string extension = relativePath.extension(); |
| 158 | std::filesystem::path webpath = relativePath; |
| 159 | |
| 160 | if (extension == ".gz") |
| 161 | { |
| 162 | webpath = webpath.replace_extension(""); |
| 163 | // Use the non-gzip version for determining content type |
| 164 | extension = webpath.extension().string(); |
| 165 | file.contentEncoding = "gzip"; |
| 166 | } |
Ed Tanous | 2b47f94 | 2024-03-12 15:43:16 -0700 | [diff] [blame] | 167 | else if (extension == ".zstd") |
| 168 | { |
| 169 | webpath = webpath.replace_extension(""); |
| 170 | // Use the non-zstd version for determining content type |
| 171 | extension = webpath.extension().string(); |
| 172 | file.contentEncoding = "zstd"; |
| 173 | } |
Ed Tanous | ca89b20 | 2024-04-25 23:02:40 -0700 | [diff] [blame] | 174 | |
| 175 | file.etag = getStaticEtag(webpath); |
| 176 | |
| 177 | if (webpath.filename().string().starts_with("index.")) |
| 178 | { |
| 179 | webpath = webpath.parent_path(); |
| 180 | if (webpath.string().empty() || webpath.string().back() != '/') |
| 181 | { |
| 182 | // insert the non-directory version of this path |
| 183 | webroutes::routes.insert(webpath); |
| 184 | webpath += "/"; |
| 185 | file.renamed = true; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | std::pair<boost::container::flat_set<std::string>::iterator, bool> |
| 190 | inserted = webroutes::routes.insert(webpath); |
| 191 | |
| 192 | if (!inserted.second) |
| 193 | { |
| 194 | // Got a duplicated path. This is expected in certain |
| 195 | // situations |
| 196 | BMCWEB_LOG_DEBUG("Got duplicated path {}", webpath.string()); |
| 197 | return; |
| 198 | } |
| 199 | file.contentType = getFiletypeForExtension(extension); |
| 200 | |
| 201 | if (webpath == "/") |
| 202 | { |
| 203 | forward_unauthorized::hasWebuiRoute = true; |
| 204 | } |
| 205 | |
| 206 | app.routeDynamic(webpath)( |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 207 | [file = std::move( |
| 208 | file)](const crow::Request& req, |
| 209 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { |
| 210 | handleStaticAsset(req, asyncResp, file); |
| 211 | }); |
Ed Tanous | ca89b20 | 2024-04-25 23:02:40 -0700 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | inline void requestRoutes(App& app) |
| 215 | { |
Ed Tanous | 153fdcf | 2021-03-04 16:43:14 -0800 | [diff] [blame] | 216 | std::error_code ec; |
Ed Tanous | ca89b20 | 2024-04-25 23:02:40 -0700 | [diff] [blame] | 217 | std::filesystem::recursive_directory_iterator dirIter({rootpath}, ec); |
Ed Tanous | 153fdcf | 2021-03-04 16:43:14 -0800 | [diff] [blame] | 218 | if (ec) |
| 219 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 220 | BMCWEB_LOG_ERROR( |
Ed Tanous | ca89b20 | 2024-04-25 23:02:40 -0700 | [diff] [blame] | 221 | "Unable to find or open {} static file hosting disabled", rootpath); |
Ed Tanous | 153fdcf | 2021-03-04 16:43:14 -0800 | [diff] [blame] | 222 | return; |
| 223 | } |
| 224 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 225 | // In certain cases, we might have both a gzipped version of the file AND a |
| 226 | // non-gzipped version. To avoid duplicated routes, we need to make sure we |
| 227 | // get the gzipped version first. Because the gzipped path should be longer |
Ed Tanous | 8b15645 | 2018-10-12 11:09:26 -0700 | [diff] [blame] | 228 | // than the non gzipped path, if we sort in descending order, we should be |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 229 | // guaranteed to get the gzip version first. |
Ed Tanous | a47f32b | 2019-10-24 10:08:04 -0700 | [diff] [blame] | 230 | std::vector<std::filesystem::directory_entry> paths( |
| 231 | std::filesystem::begin(dirIter), std::filesystem::end(dirIter)); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 232 | std::sort(paths.rbegin(), paths.rend()); |
Ed Tanous | 9dc2c4d | 2018-03-07 14:51:27 -0800 | [diff] [blame] | 233 | |
Ed Tanous | a47f32b | 2019-10-24 10:08:04 -0700 | [diff] [blame] | 234 | for (const std::filesystem::directory_entry& dir : paths) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 235 | { |
Ed Tanous | a47f32b | 2019-10-24 10:08:04 -0700 | [diff] [blame] | 236 | if (std::filesystem::is_directory(dir)) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 237 | { |
| 238 | // don't recurse into hidden directories or symlinks |
Ed Tanous | 11ba397 | 2022-07-11 09:50:41 -0700 | [diff] [blame] | 239 | if (dir.path().filename().string().starts_with(".") || |
Ed Tanous | a47f32b | 2019-10-24 10:08:04 -0700 | [diff] [blame] | 240 | std::filesystem::is_symlink(dir)) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 241 | { |
| 242 | dirIter.disable_recursion_pending(); |
| 243 | } |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 244 | } |
Ed Tanous | a47f32b | 2019-10-24 10:08:04 -0700 | [diff] [blame] | 245 | else if (std::filesystem::is_regular_file(dir)) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 246 | { |
Ed Tanous | ca89b20 | 2024-04-25 23:02:40 -0700 | [diff] [blame] | 247 | addFile(app, dir); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 248 | } |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 249 | } |
Ed Tanous | 99dc9c7 | 2019-10-24 10:09:03 -0700 | [diff] [blame] | 250 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 251 | } // namespace webassets |
| 252 | } // namespace crow |