blob: fc58d3730f9292ac2ad95fabe036483bb438b39a [file] [log] [blame]
Ed Tanous911ac312017-08-15 09:37:42 -07001#pragma once
2
Ed Tanousc94ad492019-10-10 15:39:33 -07003#include <app.h>
4#include <http_request.h>
5#include <http_response.h>
6#include <routing.h>
Ed Tanous1abe55e2018-09-05 08:30:59 -07007
Ed Tanous911ac312017-08-15 09:37:42 -07008#include <boost/algorithm/string/replace.hpp>
9#include <boost/container/flat_set.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050010
James Feist4418c7f2019-04-15 11:09:15 -070011#include <filesystem>
Ed Tanous1abe55e2018-09-05 08:30:59 -070012#include <fstream>
13#include <string>
Ed Tanous911ac312017-08-15 09:37:42 -070014
Ed Tanous1abe55e2018-09-05 08:30:59 -070015namespace crow
16{
17namespace webassets
18{
Ed Tanous911ac312017-08-15 09:37:42 -070019
James Feistf6150402019-01-08 10:36:20 -080020namespace filesystem = std::filesystem;
Ed Tanousba9f9a62017-10-11 16:40:35 -070021
Ed Tanous1abe55e2018-09-05 08:30:59 -070022struct CmpStr
23{
24 bool operator()(const char* a, const char* b) const
25 {
26 return std::strcmp(a, b) < 0;
27 }
Ed Tanousba9f9a62017-10-11 16:40:35 -070028};
Ed Tanous911ac312017-08-15 09:37:42 -070029
30static boost::container::flat_set<std::string> routes;
31
Gunnar Mills1214b7e2020-06-04 10:11:30 -050032template <typename... Middlewares>
33void requestRoutes(Crow<Middlewares...>& app)
Ed Tanous1abe55e2018-09-05 08:30:59 -070034{
35 const static boost::container::flat_map<const char*, const char*, CmpStr>
36 contentTypes{
37 {{".css", "text/css;charset=UTF-8"},
38 {".html", "text/html;charset=UTF-8"},
Ed Tanous7e513892018-12-20 07:11:04 -080039 {".js", "application/javascript;charset=UTF-8"},
Ed Tanous1abe55e2018-09-05 08:30:59 -070040 {".png", "image/png;charset=UTF-8"},
41 {".woff", "application/x-font-woff"},
42 {".woff2", "application/x-font-woff2"},
43 {".gif", "image/gif"},
44 {".ico", "image/x-icon"},
45 {".ttf", "application/x-font-ttf"},
46 {".svg", "image/svg+xml"},
47 {".eot", "application/vnd.ms-fontobject"},
48 {".xml", "application/xml"},
Ed Tanous683f7272018-07-26 12:47:19 -070049 {".json", "application/json"},
Ed Tanous1abe55e2018-09-05 08:30:59 -070050 {".jpg", "image/jpeg"},
51 {".jpeg", "image/jpeg"},
52 {".json", "application/json"},
53 // dev tools don't care about map type, setting to json causes
54 // browser to show as text
55 // https://stackoverflow.com/questions/19911929/what-mime-type-should-i-use-for-javascript-source-map-files
56 {".map", "application/json"}}};
57 filesystem::path rootpath{"/usr/share/www/"};
58 filesystem::recursive_directory_iterator dirIter(rootpath);
59 // In certain cases, we might have both a gzipped version of the file AND a
60 // non-gzipped version. To avoid duplicated routes, we need to make sure we
61 // get the gzipped version first. Because the gzipped path should be longer
Ed Tanous8b156452018-10-12 11:09:26 -070062 // than the non gzipped path, if we sort in descending order, we should be
Ed Tanous1abe55e2018-09-05 08:30:59 -070063 // guaranteed to get the gzip version first.
64 std::vector<filesystem::directory_entry> paths(filesystem::begin(dirIter),
65 filesystem::end(dirIter));
66 std::sort(paths.rbegin(), paths.rend());
Ed Tanous9dc2c4d2018-03-07 14:51:27 -080067
Ed Tanous1abe55e2018-09-05 08:30:59 -070068 for (const filesystem::directory_entry& dir : paths)
69 {
70 filesystem::path absolutePath = dir.path();
71 filesystem::path relativePath{
72 absolutePath.string().substr(rootpath.string().size() - 1)};
73 if (filesystem::is_directory(dir))
74 {
75 // don't recurse into hidden directories or symlinks
76 if (boost::starts_with(dir.path().filename().string(), ".") ||
77 filesystem::is_symlink(dir))
78 {
79 dirIter.disable_recursion_pending();
80 }
Ed Tanousba9f9a62017-10-11 16:40:35 -070081 }
Ed Tanous1abe55e2018-09-05 08:30:59 -070082 else if (filesystem::is_regular_file(dir))
83 {
84 std::string extension = relativePath.extension();
85 filesystem::path webpath = relativePath;
86 const char* contentEncoding = nullptr;
Ed Tanous911ac312017-08-15 09:37:42 -070087
Ed Tanous1abe55e2018-09-05 08:30:59 -070088 if (extension == ".gz")
89 {
90 webpath = webpath.replace_extension("");
91 // Use the non-gzip version for determining content type
92 extension = webpath.extension().string();
93 contentEncoding = "gzip";
Ed Tanous911ac312017-08-15 09:37:42 -070094 }
Ed Tanousba9f9a62017-10-11 16:40:35 -070095
Ed Tanous1abe55e2018-09-05 08:30:59 -070096 if (boost::starts_with(webpath.filename().string(), "index."))
97 {
98 webpath = webpath.parent_path();
99 if (webpath.string().size() == 0 ||
100 webpath.string().back() != '/')
101 {
102 // insert the non-directory version of this path
103 routes.insert(webpath);
104 webpath += "/";
105 }
Ed Tanousba9f9a62017-10-11 16:40:35 -0700106 }
107
Ed Tanous1abe55e2018-09-05 08:30:59 -0700108 std::pair<boost::container::flat_set<std::string>::iterator, bool>
109 inserted = routes.insert(webpath);
110
111 if (!inserted.second)
112 {
113 // Got a duplicated path. This is expected in certain
114 // situations
115 BMCWEB_LOG_DEBUG << "Got duplicated path " << webpath;
116 continue;
117 }
118 const char* contentType = nullptr;
119
120 auto contentTypeIt = contentTypes.find(extension.c_str());
121 if (contentTypeIt == contentTypes.end())
122 {
123 BMCWEB_LOG_ERROR << "Cannot determine content-type for "
124 << absolutePath << " with extension "
125 << extension;
126 }
127 else
128 {
129 contentType = contentTypeIt->second;
Ed Tanous911ac312017-08-15 09:37:42 -0700130 }
131
Ed Tanous1abe55e2018-09-05 08:30:59 -0700132 app.routeDynamic(webpath)(
133 [absolutePath, contentType, contentEncoding](
134 const crow::Request& req, crow::Response& res) {
135 if (contentType != nullptr)
136 {
137 res.addHeader("Content-Type", contentType);
138 }
139
140 if (contentEncoding != nullptr)
141 {
142 res.addHeader("Content-Encoding", contentEncoding);
143 }
144
145 // res.set_header("Cache-Control", "public, max-age=86400");
146 std::ifstream inf(absolutePath);
147 if (!inf)
148 {
149 BMCWEB_LOG_DEBUG << "failed to read file";
150 res.result(
151 boost::beast::http::status::internal_server_error);
152 res.end();
153 return;
154 }
155
156 res.body() = {std::istreambuf_iterator<char>(inf),
157 std::istreambuf_iterator<char>()};
158 res.end();
159 });
160 }
Ed Tanous911ac312017-08-15 09:37:42 -0700161 }
Ed Tanous99dc9c72019-10-24 10:09:03 -0700162}
Ed Tanous1abe55e2018-09-05 08:30:59 -0700163} // namespace webassets
164} // namespace crow