blob: 9bd40dbf8fd8d84dd1960fb168705239b10f13b9 [file] [log] [blame]
Ed Tanous911ac312017-08-15 09:37:42 -07001#pragma once
2
James Feistf6150402019-01-08 10:36:20 -08003#include "filesystem.hpp"
4
Ed Tanous911ac312017-08-15 09:37:42 -07005#include <crow/app.h>
6#include <crow/http_request.h>
7#include <crow/http_response.h>
8#include <crow/routing.h>
Ed Tanous1abe55e2018-09-05 08:30:59 -07009
Ed Tanous911ac312017-08-15 09:37:42 -070010#include <boost/algorithm/string/replace.hpp>
11#include <boost/container/flat_set.hpp>
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
Ed Tanous1abe55e2018-09-05 08:30:59 -070032template <typename... Middlewares> void requestRoutes(Crow<Middlewares...>& app)
33{
34 const static boost::container::flat_map<const char*, const char*, CmpStr>
35 contentTypes{
36 {{".css", "text/css;charset=UTF-8"},
37 {".html", "text/html;charset=UTF-8"},
Ed Tanous7e513892018-12-20 07:11:04 -080038 {".js", "application/javascript;charset=UTF-8"},
Ed Tanous1abe55e2018-09-05 08:30:59 -070039 {".png", "image/png;charset=UTF-8"},
40 {".woff", "application/x-font-woff"},
41 {".woff2", "application/x-font-woff2"},
42 {".gif", "image/gif"},
43 {".ico", "image/x-icon"},
44 {".ttf", "application/x-font-ttf"},
45 {".svg", "image/svg+xml"},
46 {".eot", "application/vnd.ms-fontobject"},
47 {".xml", "application/xml"},
Ed Tanous683f7272018-07-26 12:47:19 -070048 {".json", "application/json"},
Ed Tanous1abe55e2018-09-05 08:30:59 -070049 {".jpg", "image/jpeg"},
50 {".jpeg", "image/jpeg"},
51 {".json", "application/json"},
52 // dev tools don't care about map type, setting to json causes
53 // browser to show as text
54 // https://stackoverflow.com/questions/19911929/what-mime-type-should-i-use-for-javascript-source-map-files
55 {".map", "application/json"}}};
56 filesystem::path rootpath{"/usr/share/www/"};
57 filesystem::recursive_directory_iterator dirIter(rootpath);
58 // In certain cases, we might have both a gzipped version of the file AND a
59 // non-gzipped version. To avoid duplicated routes, we need to make sure we
60 // get the gzipped version first. Because the gzipped path should be longer
Ed Tanous8b156452018-10-12 11:09:26 -070061 // than the non gzipped path, if we sort in descending order, we should be
Ed Tanous1abe55e2018-09-05 08:30:59 -070062 // guaranteed to get the gzip version first.
63 std::vector<filesystem::directory_entry> paths(filesystem::begin(dirIter),
64 filesystem::end(dirIter));
65 std::sort(paths.rbegin(), paths.rend());
Ed Tanous9dc2c4d2018-03-07 14:51:27 -080066
Ed Tanous1abe55e2018-09-05 08:30:59 -070067 for (const filesystem::directory_entry& dir : paths)
68 {
69 filesystem::path absolutePath = dir.path();
70 filesystem::path relativePath{
71 absolutePath.string().substr(rootpath.string().size() - 1)};
72 if (filesystem::is_directory(dir))
73 {
74 // don't recurse into hidden directories or symlinks
75 if (boost::starts_with(dir.path().filename().string(), ".") ||
76 filesystem::is_symlink(dir))
77 {
78 dirIter.disable_recursion_pending();
79 }
Ed Tanousba9f9a62017-10-11 16:40:35 -070080 }
Ed Tanous1abe55e2018-09-05 08:30:59 -070081 else if (filesystem::is_regular_file(dir))
82 {
83 std::string extension = relativePath.extension();
84 filesystem::path webpath = relativePath;
85 const char* contentEncoding = nullptr;
Ed Tanous911ac312017-08-15 09:37:42 -070086
Ed Tanous1abe55e2018-09-05 08:30:59 -070087 if (extension == ".gz")
88 {
89 webpath = webpath.replace_extension("");
90 // Use the non-gzip version for determining content type
91 extension = webpath.extension().string();
92 contentEncoding = "gzip";
Ed Tanous911ac312017-08-15 09:37:42 -070093 }
Ed Tanousba9f9a62017-10-11 16:40:35 -070094
Ed Tanous1abe55e2018-09-05 08:30:59 -070095 if (boost::starts_with(webpath.filename().string(), "index."))
96 {
97 webpath = webpath.parent_path();
98 if (webpath.string().size() == 0 ||
99 webpath.string().back() != '/')
100 {
101 // insert the non-directory version of this path
102 routes.insert(webpath);
103 webpath += "/";
104 }
Ed Tanousba9f9a62017-10-11 16:40:35 -0700105 }
106
Ed Tanous1abe55e2018-09-05 08:30:59 -0700107 std::pair<boost::container::flat_set<std::string>::iterator, bool>
108 inserted = routes.insert(webpath);
109
110 if (!inserted.second)
111 {
112 // Got a duplicated path. This is expected in certain
113 // situations
114 BMCWEB_LOG_DEBUG << "Got duplicated path " << webpath;
115 continue;
116 }
117 const char* contentType = nullptr;
118
119 auto contentTypeIt = contentTypes.find(extension.c_str());
120 if (contentTypeIt == contentTypes.end())
121 {
122 BMCWEB_LOG_ERROR << "Cannot determine content-type for "
123 << absolutePath << " with extension "
124 << extension;
125 }
126 else
127 {
128 contentType = contentTypeIt->second;
Ed Tanous911ac312017-08-15 09:37:42 -0700129 }
130
Ed Tanous1abe55e2018-09-05 08:30:59 -0700131 app.routeDynamic(webpath)(
132 [absolutePath, contentType, contentEncoding](
133 const crow::Request& req, crow::Response& res) {
134 if (contentType != nullptr)
135 {
136 res.addHeader("Content-Type", contentType);
137 }
138
139 if (contentEncoding != nullptr)
140 {
141 res.addHeader("Content-Encoding", contentEncoding);
142 }
143
144 // res.set_header("Cache-Control", "public, max-age=86400");
145 std::ifstream inf(absolutePath);
146 if (!inf)
147 {
148 BMCWEB_LOG_DEBUG << "failed to read file";
149 res.result(
150 boost::beast::http::status::internal_server_error);
151 res.end();
152 return;
153 }
154
155 res.body() = {std::istreambuf_iterator<char>(inf),
156 std::istreambuf_iterator<char>()};
157 res.end();
158 });
159 }
Ed Tanous911ac312017-08-15 09:37:42 -0700160 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700161} // namespace webassets
162} // namespace webassets
163} // namespace crow