xref: /openbmc/bmcweb/include/webassets.hpp (revision 530520ea)
1 #pragma once
2 
3 #include <crow/app.h>
4 #include <crow/http_request.h>
5 #include <crow/http_response.h>
6 #include <crow/routing.h>
7 
8 #include <boost/algorithm/string/replace.hpp>
9 #include <boost/container/flat_set.hpp>
10 #include <experimental/filesystem>
11 #include <fstream>
12 #include <string>
13 
14 namespace crow
15 {
16 namespace webassets
17 {
18 
19 namespace filesystem = std::experimental::filesystem;
20 
21 struct CmpStr
22 {
23     bool operator()(const char* a, const char* b) const
24     {
25         return std::strcmp(a, b) < 0;
26     }
27 };
28 
29 static boost::container::flat_set<std::string> routes;
30 
31 template <typename... Middlewares> void requestRoutes(Crow<Middlewares...>& app)
32 {
33     const static boost::container::flat_map<const char*, const char*, CmpStr>
34         contentTypes{
35             {{".css", "text/css;charset=UTF-8"},
36              {".html", "text/html;charset=UTF-8"},
37              {".js", "application/javascript;charset=UTF-8"},
38              {".png", "image/png;charset=UTF-8"},
39              {".woff", "application/x-font-woff"},
40              {".woff2", "application/x-font-woff2"},
41              {".gif", "image/gif"},
42              {".ico", "image/x-icon"},
43              {".ttf", "application/x-font-ttf"},
44              {".svg", "image/svg+xml"},
45              {".eot", "application/vnd.ms-fontobject"},
46              {".xml", "application/xml"},
47              {".json", "application/json"},
48              {".jpg", "image/jpeg"},
49              {".jpeg", "image/jpeg"},
50              {".json", "application/json"},
51              // dev tools don't care about map type, setting to json causes
52              // browser to show as text
53              // https://stackoverflow.com/questions/19911929/what-mime-type-should-i-use-for-javascript-source-map-files
54              {".map", "application/json"}}};
55     filesystem::path rootpath{"/usr/share/www/"};
56     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<filesystem::directory_entry> paths(filesystem::begin(dirIter),
63                                                    filesystem::end(dirIter));
64     std::sort(paths.rbegin(), paths.rend());
65 
66     for (const filesystem::directory_entry& dir : paths)
67     {
68         filesystem::path absolutePath = dir.path();
69         filesystem::path relativePath{
70             absolutePath.string().substr(rootpath.string().size() - 1)};
71         if (filesystem::is_directory(dir))
72         {
73             // don't recurse into hidden directories or symlinks
74             if (boost::starts_with(dir.path().filename().string(), ".") ||
75                 filesystem::is_symlink(dir))
76             {
77                 dirIter.disable_recursion_pending();
78             }
79         }
80         else if (filesystem::is_regular_file(dir))
81         {
82             std::string extension = relativePath.extension();
83             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                     routes.insert(webpath);
102                     webpath += "/";
103                 }
104             }
105 
106             std::pair<boost::container::flat_set<std::string>::iterator, bool>
107                 inserted = 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             auto contentTypeIt = contentTypes.find(extension.c_str());
119             if (contentTypeIt == contentTypes.end())
120             {
121                 BMCWEB_LOG_ERROR << "Cannot determine content-type for "
122                                  << absolutePath << " with extension "
123                                  << extension;
124             }
125             else
126             {
127                 contentType = contentTypeIt->second;
128             }
129 
130             app.routeDynamic(webpath)(
131                 [absolutePath, contentType, contentEncoding](
132                     const crow::Request& req, crow::Response& res) {
133                     if (contentType != nullptr)
134                     {
135                         res.addHeader("Content-Type", contentType);
136                     }
137 
138                     if (contentEncoding != nullptr)
139                     {
140                         res.addHeader("Content-Encoding", contentEncoding);
141                     }
142 
143                     // res.set_header("Cache-Control", "public, max-age=86400");
144                     std::ifstream inf(absolutePath);
145                     if (!inf)
146                     {
147                         BMCWEB_LOG_DEBUG << "failed to read file";
148                         res.result(
149                             boost::beast::http::status::internal_server_error);
150                         res.end();
151                         return;
152                     }
153 
154                     res.body() = {std::istreambuf_iterator<char>(inf),
155                                   std::istreambuf_iterator<char>()};
156                     res.end();
157                 });
158         }
159     }
160 } // namespace webassets
161 } // namespace webassets
162 } // namespace crow
163