blob: 847696a325fd203da035144b2c2e129dffee7c6a [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>
James Feist4418c7f2019-04-15 11:09:15 -070010#include <filesystem>
Ed Tanous1abe55e2018-09-05 08:30:59 -070011#include <fstream>
12#include <string>
Ed Tanous911ac312017-08-15 09:37:42 -070013
Ed Tanous1abe55e2018-09-05 08:30:59 -070014namespace crow
15{
16namespace webassets
17{
Ed Tanous911ac312017-08-15 09:37:42 -070018
James Feistf6150402019-01-08 10:36:20 -080019namespace filesystem = std::filesystem;
Ed Tanousba9f9a62017-10-11 16:40:35 -070020
Ed Tanous1abe55e2018-09-05 08:30:59 -070021struct CmpStr
22{
23 bool operator()(const char* a, const char* b) const
24 {
25 return std::strcmp(a, b) < 0;
26 }
Ed Tanousba9f9a62017-10-11 16:40:35 -070027};
Ed Tanous911ac312017-08-15 09:37:42 -070028
29static boost::container::flat_set<std::string> routes;
30
Ed Tanous1abe55e2018-09-05 08:30:59 -070031template <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"},
Ed Tanous7e513892018-12-20 07:11:04 -080037 {".js", "application/javascript;charset=UTF-8"},
Ed Tanous1abe55e2018-09-05 08:30:59 -070038 {".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"},
Ed Tanous683f7272018-07-26 12:47:19 -070047 {".json", "application/json"},
Ed Tanous1abe55e2018-09-05 08:30:59 -070048 {".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
Ed Tanous8b156452018-10-12 11:09:26 -070060 // than the non gzipped path, if we sort in descending order, we should be
Ed Tanous1abe55e2018-09-05 08:30:59 -070061 // 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());
Ed Tanous9dc2c4d2018-03-07 14:51:27 -080065
Ed Tanous1abe55e2018-09-05 08:30:59 -070066 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 }
Ed Tanousba9f9a62017-10-11 16:40:35 -070079 }
Ed Tanous1abe55e2018-09-05 08:30:59 -070080 else if (filesystem::is_regular_file(dir))
81 {
82 std::string extension = relativePath.extension();
83 filesystem::path webpath = relativePath;
84 const char* contentEncoding = nullptr;
Ed Tanous911ac312017-08-15 09:37:42 -070085
Ed Tanous1abe55e2018-09-05 08:30:59 -070086 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";
Ed Tanous911ac312017-08-15 09:37:42 -070092 }
Ed Tanousba9f9a62017-10-11 16:40:35 -070093
Ed Tanous1abe55e2018-09-05 08:30:59 -070094 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 }
Ed Tanousba9f9a62017-10-11 16:40:35 -0700104 }
105
Ed Tanous1abe55e2018-09-05 08:30:59 -0700106 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;
Ed Tanous911ac312017-08-15 09:37:42 -0700128 }
129
Ed Tanous1abe55e2018-09-05 08:30:59 -0700130 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 }
Ed Tanous911ac312017-08-15 09:37:42 -0700159 }
Ed Tanous99dc9c72019-10-24 10:09:03 -0700160}
Ed Tanous1abe55e2018-09-05 08:30:59 -0700161} // namespace webassets
162} // namespace crow