blob: b43366a6da5aa3f495f2adeba72f18cef6be86ef [file] [log] [blame]
Ed Tanous911ac312017-08-15 09:37:42 -07001#pragma once
2
Ed Tanous3ccb3ad2023-01-13 17:40:03 -08003#include "app.hpp"
4#include "http_request.hpp"
5#include "http_response.hpp"
6#include "routing.hpp"
James Feist3909dc82020-04-03 10:58:55 -07007#include "webroutes.hpp"
8
Ed Tanous911ac312017-08-15 09:37:42 -07009#include <boost/container/flat_set.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050010
Ed Tanousca89b202024-04-25 23:02:40 -070011#include <algorithm>
12#include <array>
James Feist4418c7f2019-04-15 11:09:15 -070013#include <filesystem>
Ed Tanous1abe55e2018-09-05 08:30:59 -070014#include <fstream>
15#include <string>
Ed Tanousca89b202024-04-25 23:02:40 -070016#include <string_view>
Ed Tanous911ac312017-08-15 09:37:42 -070017
Ed Tanous1abe55e2018-09-05 08:30:59 -070018namespace crow
19{
20namespace webassets
21{
Ed Tanous911ac312017-08-15 09:37:42 -070022
Ed Tanous499b5b42024-04-06 08:39:18 -070023inline 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 Tanous499b5b42024-04-06 08:39:18 -070031 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 Tanousca89b202024-04-25 23:02:40 -070052static constexpr std::string_view rootpath("/usr/share/www/");
53
54struct StaticFile
Ed Tanous1abe55e2018-09-05 08:30:59 -070055{
Ed Tanousca89b202024-04-25 23:02:40 -070056 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 Williamsbd79bce2024-08-16 15:22:20 -040063inline void handleStaticAsset(
64 const crow::Request& req,
65 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, const StaticFile& file)
Ed Tanousca89b202024-04-25 23:02:40 -070066{
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 Baed51c61b2024-09-13 10:35:34 -0500101 if (asyncResp->res.openFile(file.absolutePath) != crow::OpenCode::Success)
Ed Tanousca89b202024-04-25 23:02:40 -0700102 {
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
110inline 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 Tanous1abe55e2018-09-05 08:30:59 -0700114 contentTypes{
115 {{".css", "text/css;charset=UTF-8"},
Ed Tanous1abe55e2018-09-05 08:30:59 -0700116 {".eot", "application/vnd.ms-fontobject"},
Ed Tanousca89b202024-04-25 23:02:40 -0700117 {".gif", "image/gif"},
118 {".html", "text/html;charset=UTF-8"},
119 {".ico", "image/x-icon"},
Ed Tanous1abe55e2018-09-05 08:30:59 -0700120 {".jpeg", "image/jpeg"},
Ed Tanousca89b202024-04-25 23:02:40 -0700121 {".jpg", "image/jpeg"},
122 {".js", "application/javascript;charset=UTF-8"},
123 {".json", "application/json"},
Ed Tanous1abe55e2018-09-05 08:30:59 -0700124 // 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 Tanousca89b202024-04-25 23:02:40 -0700127 {".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 Tanousa47f32b2019-10-24 10:08:04 -0700134
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400135 const auto* contentType =
136 std::ranges::find_if(contentTypes, [&extension](const auto& val) {
137 return val.first == extension;
138 });
Ed Tanous153fdcf2021-03-04 16:43:14 -0800139
Ed Tanousca89b202024-04-25 23:02:40 -0700140 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
150inline 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 Tanous2b47f942024-03-12 15:43:16 -0700167 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 Tanousca89b202024-04-25 23:02:40 -0700174
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 Williamsbd79bce2024-08-16 15:22:20 -0400207 [file = std::move(
208 file)](const crow::Request& req,
209 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
210 handleStaticAsset(req, asyncResp, file);
211 });
Ed Tanousca89b202024-04-25 23:02:40 -0700212}
213
214inline void requestRoutes(App& app)
215{
Ed Tanous153fdcf2021-03-04 16:43:14 -0800216 std::error_code ec;
Ed Tanousca89b202024-04-25 23:02:40 -0700217 std::filesystem::recursive_directory_iterator dirIter({rootpath}, ec);
Ed Tanous153fdcf2021-03-04 16:43:14 -0800218 if (ec)
219 {
Ed Tanous62598e32023-07-17 17:06:25 -0700220 BMCWEB_LOG_ERROR(
Ed Tanousca89b202024-04-25 23:02:40 -0700221 "Unable to find or open {} static file hosting disabled", rootpath);
Ed Tanous153fdcf2021-03-04 16:43:14 -0800222 return;
223 }
224
Ed Tanous1abe55e2018-09-05 08:30:59 -0700225 // 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 Tanous8b156452018-10-12 11:09:26 -0700228 // than the non gzipped path, if we sort in descending order, we should be
Ed Tanous1abe55e2018-09-05 08:30:59 -0700229 // guaranteed to get the gzip version first.
Ed Tanousa47f32b2019-10-24 10:08:04 -0700230 std::vector<std::filesystem::directory_entry> paths(
231 std::filesystem::begin(dirIter), std::filesystem::end(dirIter));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700232 std::sort(paths.rbegin(), paths.rend());
Ed Tanous9dc2c4d2018-03-07 14:51:27 -0800233
Ed Tanousa47f32b2019-10-24 10:08:04 -0700234 for (const std::filesystem::directory_entry& dir : paths)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700235 {
Ed Tanousa47f32b2019-10-24 10:08:04 -0700236 if (std::filesystem::is_directory(dir))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700237 {
238 // don't recurse into hidden directories or symlinks
Ed Tanous11ba3972022-07-11 09:50:41 -0700239 if (dir.path().filename().string().starts_with(".") ||
Ed Tanousa47f32b2019-10-24 10:08:04 -0700240 std::filesystem::is_symlink(dir))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700241 {
242 dirIter.disable_recursion_pending();
243 }
Ed Tanousba9f9a62017-10-11 16:40:35 -0700244 }
Ed Tanousa47f32b2019-10-24 10:08:04 -0700245 else if (std::filesystem::is_regular_file(dir))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700246 {
Ed Tanousca89b202024-04-25 23:02:40 -0700247 addFile(app, dir);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700248 }
Ed Tanous911ac312017-08-15 09:37:42 -0700249 }
Ed Tanous99dc9c72019-10-24 10:09:03 -0700250}
Ed Tanous1abe55e2018-09-05 08:30:59 -0700251} // namespace webassets
252} // namespace crow