xref: /openbmc/bmcweb/include/webassets.hpp (revision 7d95f5f6db1df37fe6438aa0e87479275c82807a)
1 #pragma once
2 
3 #include <experimental/filesystem>
4 #include <fstream>
5 #include <string>
6 #include <crow/app.h>
7 #include <crow/http_codes.h>
8 #include <crow/http_request.h>
9 #include <crow/http_response.h>
10 #include <crow/routing.h>
11 #include <boost/algorithm/string/replace.hpp>
12 #include <boost/container/flat_set.hpp>
13 
14 namespace crow {
15 namespace webassets {
16 
17 namespace filesystem = std::experimental::filesystem;
18 
19 struct cmp_str {
20   bool operator()(const char* a, const char* b) const {
21     return std::strcmp(a, b) < 0;
22   }
23 };
24 
25 static boost::container::flat_set<std::string> routes;
26 
27 template <typename... Middlewares>
28 void request_routes(Crow<Middlewares...>& app) {
29   const static boost::container::flat_map<const char*, const char*, cmp_str>
30       content_types{
31           {{".css", "text/css;charset=UTF-8"},
32            {".html", "text/html;charset=UTF-8"},
33            {".js", "text/html;charset=UTF-8"},
34            {".png", "image/png;charset=UTF-8"},
35            {".woff", "application/x-font-woff"},
36            {".woff2", "application/x-font-woff2"},
37            {".gif", "image/gif"},
38            {".ico", "image/x-icon"},
39            {".ttf", "application/x-font-ttf"},
40            {".svg", "image/svg+xml"},
41            {".eot", "application/vnd.ms-fontobject"},
42            {".xml", "application/xml"},
43            {".jpg", "image/jpeg"},
44            {".jpeg", "image/jpeg"},
45            // dev tools don't care about map type, setting to json causes
46            // browser to show as text
47            // https://stackoverflow.com/questions/19911929/what-mime-type-should-i-use-for-javascript-source-map-files
48            {".map", "application/json"}}};
49   filesystem::path rootpath{"/usr/share/www/"};
50   filesystem::recursive_directory_iterator dir_iter(rootpath);
51 
52   for (const filesystem::directory_entry& dir : dir_iter) {
53     filesystem::path absolute_path = dir.path();
54     filesystem::path relative_path{
55         absolute_path.string().substr(rootpath.string().size() - 1)};
56     // make sure we don't recurse into certain directories
57     // note: maybe check for is_directory() here as well...
58 
59     if (filesystem::is_directory(dir)) {
60       // don't recurse into hidden directories or symlinks
61       if (boost::starts_with(dir.path().filename().string(), ".") ||
62           filesystem::is_symlink(dir)) {
63         dir_iter.disable_recursion_pending();
64       }
65     } else if (filesystem::is_regular_file(dir)) {
66       std::string extension = relative_path.extension();
67       filesystem::path webpath = relative_path;
68       const char* content_encoding = nullptr;
69 
70       if (extension == ".gz") {
71         webpath = webpath.replace_extension("");
72         // Use the non-gzip version for determining content type
73         extension = webpath.extension().string();
74         content_encoding = "gzip";
75       }
76 
77       if (boost::starts_with(webpath.filename().string(), "index.")) {
78         webpath = webpath.parent_path();
79         if (webpath.string().size() == 0 || webpath.string().back() != '/') {
80           // insert the non-directory version of this path
81           routes.insert(webpath);
82           webpath += "/";
83         }
84       }
85 
86       routes.insert(webpath);
87       const char* content_type = nullptr;
88 
89       auto content_type_it = content_types.find(extension.c_str());
90       if (content_type_it == content_types.end()) {
91         CROW_LOG_ERROR << "Cannot determine content-type for " << webpath
92                        << " with extension " << extension;
93       } else {
94         content_type = content_type_it->second;
95       }
96 
97       app.route_dynamic(webpath)(
98           [absolute_path, content_type, content_encoding](
99               const crow::request& req, crow::response& res) {
100             if (content_type != nullptr) {
101               res.add_header("Content-Type", content_type);
102             }
103 
104             if (content_encoding != nullptr) {
105               res.add_header("Content-Encoding", content_encoding);
106             }
107 
108             // res.set_header("Cache-Control", "public, max-age=86400");
109             std::ifstream inf(absolute_path);
110             if (!inf) {
111               CROW_LOG_DEBUG << "failed to read file";
112               res.code = static_cast<int>(HttpRespCode::NOT_FOUND);
113               res.code = static_cast<int>(HttpRespCode::INTERNAL_ERROR);
114               res.end();
115               return;
116             }
117 
118             res.body = {std::istreambuf_iterator<char>(inf),
119                         std::istreambuf_iterator<char>()};
120             res.end();
121           });
122     }
123   }
124 }  // namespace webassets
125 }  // namespace webassets
126 }  // namespace crow
127