xref: /openbmc/bmcweb/include/webassets.hpp (revision ac093318ac41bb5306d19d01824f79d0d3488840)
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            // dev tools don't care about map type, setting to json causes
44            // browser to show as text
45            // https://stackoverflow.com/questions/19911929/what-mime-type-should-i-use-for-javascript-source-map-files
46            {".map", "application/json"}}};
47   filesystem::path rootpath{"/usr/share/www/"};
48   filesystem::recursive_directory_iterator dir_iter(rootpath);
49 
50   for (const filesystem::directory_entry& dir : dir_iter) {
51     filesystem::path absolute_path = dir.path();
52     filesystem::path relative_path{
53         absolute_path.string().substr(rootpath.string().size() - 1)};
54     // make sure we don't recurse into certain directories
55     // note: maybe check for is_directory() here as well...
56 
57     if (filesystem::is_directory(dir)) {
58       // don't recurse into hidden directories or symlinks
59       if (boost::starts_with(dir.path().filename().string(), ".") ||
60           filesystem::is_symlink(dir)) {
61         dir_iter.disable_recursion_pending();
62       }
63     } else if (filesystem::is_regular_file(dir)) {
64       std::string extension = relative_path.extension();
65       filesystem::path webpath = relative_path;
66       const char* content_encoding = nullptr;
67 
68       if (extension == ".gz") {
69         webpath = webpath.replace_extension("");
70         // Use the non-gzip version for determining content type
71         extension = webpath.extension().string();
72         content_encoding = "gzip";
73       }
74 
75       if (boost::starts_with(webpath.filename().string(), "index.")) {
76         webpath = webpath.parent_path();
77         if (webpath.string().size() == 0 || webpath.string().back() != '/') {
78           webpath += "/";
79         }
80       }
81 
82       routes.insert(webpath);
83       const char* content_type = nullptr;
84 
85       auto content_type_it = content_types.find(extension.c_str());
86       if (content_type_it == content_types.end()) {
87         CROW_LOG_ERROR << "Cannot determine content-type for " << webpath
88                        << " with extension " << extension;
89       } else {
90         content_type = content_type_it->second;
91       }
92 
93       app.route_dynamic(webpath)(
94           [absolute_path, content_type, content_encoding](
95               const crow::request& req, crow::response& res) {
96             if (content_type != nullptr) {
97               res.add_header("Content-Type", content_type);
98             }
99 
100             if (content_encoding != nullptr) {
101               res.add_header("Content-Encoding", content_encoding);
102             }
103 
104             // res.set_header("Cache-Control", "public, max-age=86400");
105             std::ifstream inf(absolute_path);
106             if (!inf) {
107               CROW_LOG_DEBUG << "failed to read file";
108               res.code = static_cast<int>(HttpRespCode::NOT_FOUND);
109               res.code = static_cast<int>(HttpRespCode::INTERNAL_ERROR);
110               res.end();
111               return;
112             }
113 
114             res.body = {std::istreambuf_iterator<char>(inf),
115                         std::istreambuf_iterator<char>()};
116             res.end();
117           });
118     }
119   }
120 }  // namespace webassets
121 }  // namespace webassets
122 }  // namespace crow
123