xref: /openbmc/bmcweb/include/webassets.hpp (revision a0969c70)
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 <algorithm>
12 #include <array>
13 #include <filesystem>
14 #include <fstream>
15 #include <string>
16 #include <string_view>
17 
18 namespace crow
19 {
20 namespace webassets
21 {
22 
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(), '.');
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 
52 static constexpr std::string_view rootpath("/usr/share/www/");
53 
54 struct StaticFile
55 {
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 
63 inline void handleStaticAsset(
64     const crow::Request& req,
65     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, const StaticFile& file)
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 
101     if (asyncResp->res.openFile(file.absolutePath) != crow::OpenCode::Success)
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>
114         contentTypes{
115             {{".css", "text/css;charset=UTF-8"},
116              {".eot", "application/vnd.ms-fontobject"},
117              {".gif", "image/gif"},
118              {".html", "text/html;charset=UTF-8"},
119              {".ico", "image/x-icon"},
120              {".jpeg", "image/jpeg"},
121              {".jpg", "image/jpeg"},
122              {".js", "application/javascript;charset=UTF-8"},
123              {".json", "application/json"},
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
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"}}};
134 
135     const auto* contentType =
136         std::ranges::find_if(contentTypes, [&extension](const auto& val) {
137             return val.first == extension;
138         });
139 
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     }
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     }
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)(
207         [file = std::move(
208              file)](const crow::Request& req,
209                     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
210             handleStaticAsset(req, asyncResp, file);
211         });
212 }
213 
214 inline void requestRoutes(App& app)
215 {
216     std::error_code ec;
217     std::filesystem::recursive_directory_iterator dirIter({rootpath}, ec);
218     if (ec)
219     {
220         BMCWEB_LOG_ERROR(
221             "Unable to find or open {} static file hosting disabled", rootpath);
222         return;
223     }
224 
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
228     // than the non gzipped path, if we sort in descending order, we should be
229     // guaranteed to get the gzip version first.
230     std::vector<std::filesystem::directory_entry> paths(
231         std::filesystem::begin(dirIter), std::filesystem::end(dirIter));
232     std::sort(paths.rbegin(), paths.rend());
233 
234     for (const std::filesystem::directory_entry& dir : paths)
235     {
236         if (std::filesystem::is_directory(dir))
237         {
238             // don't recurse into hidden directories or symlinks
239             if (dir.path().filename().string().starts_with(".") ||
240                 std::filesystem::is_symlink(dir))
241             {
242                 dirIter.disable_recursion_pending();
243             }
244         }
245         else if (std::filesystem::is_regular_file(dir))
246         {
247             addFile(app, dir);
248         }
249     }
250 }
251 } // namespace webassets
252 } // namespace crow
253