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