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