blob: a4ec58b07dcec145ed5988a26b29960650e2b0c3 [file] [log] [blame]
Ed Tanous911ac312017-08-15 09:37:42 -07001#pragma once
2
James Feist3909dc82020-04-03 10:58:55 -07003#include "webroutes.hpp"
4
Ed Tanous04e438c2020-10-03 08:06:26 -07005#include <app.hpp>
Ed Tanous911ac312017-08-15 09:37:42 -07006#include <boost/algorithm/string/replace.hpp>
7#include <boost/container/flat_set.hpp>
Ed Tanous04e438c2020-10-03 08:06:26 -07008#include <http_request.hpp>
9#include <http_response.hpp>
10#include <routing.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050011
James Feist4418c7f2019-04-15 11:09:15 -070012#include <filesystem>
Ed Tanous1abe55e2018-09-05 08:30:59 -070013#include <fstream>
14#include <string>
Ed Tanous911ac312017-08-15 09:37:42 -070015
Ed Tanous1abe55e2018-09-05 08:30:59 -070016namespace crow
17{
18namespace webassets
19{
Ed Tanous911ac312017-08-15 09:37:42 -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
Ed Tanous23a21a12020-07-25 04:45:05 +000029inline void requestRoutes(App& app)
Ed Tanous1abe55e2018-09-05 08:30:59 -070030{
Ed Tanous218bd472019-10-24 10:10:07 -070031 constexpr static std::array<std::pair<const char*, const char*>, 17>
Ed Tanous1abe55e2018-09-05 08:30:59 -070032 contentTypes{
33 {{".css", "text/css;charset=UTF-8"},
34 {".html", "text/html;charset=UTF-8"},
Ed Tanous7e513892018-12-20 07:11:04 -080035 {".js", "application/javascript;charset=UTF-8"},
Ed Tanous1abe55e2018-09-05 08:30:59 -070036 {".png", "image/png;charset=UTF-8"},
37 {".woff", "application/x-font-woff"},
38 {".woff2", "application/x-font-woff2"},
39 {".gif", "image/gif"},
40 {".ico", "image/x-icon"},
41 {".ttf", "application/x-font-ttf"},
42 {".svg", "image/svg+xml"},
43 {".eot", "application/vnd.ms-fontobject"},
44 {".xml", "application/xml"},
Ed Tanous683f7272018-07-26 12:47:19 -070045 {".json", "application/json"},
Ed Tanous1abe55e2018-09-05 08:30:59 -070046 {".jpg", "image/jpeg"},
47 {".jpeg", "image/jpeg"},
48 {".json", "application/json"},
49 // dev tools don't care about map type, setting to json causes
50 // browser to show as text
51 // https://stackoverflow.com/questions/19911929/what-mime-type-should-i-use-for-javascript-source-map-files
52 {".map", "application/json"}}};
Ed Tanousa47f32b2019-10-24 10:08:04 -070053
54 std::filesystem::path rootpath{"/usr/share/www/"};
Ed Tanous153fdcf2021-03-04 16:43:14 -080055
56 std::error_code ec;
57
58 std::filesystem::recursive_directory_iterator dirIter(rootpath, ec);
59 if (ec)
60 {
61 BMCWEB_LOG_ERROR << "Unable to find or open " << rootpath
62 << " static file hosting disabled";
63 return;
64 }
65
Ed Tanous1abe55e2018-09-05 08:30:59 -070066 // In certain cases, we might have both a gzipped version of the file AND a
67 // non-gzipped version. To avoid duplicated routes, we need to make sure we
68 // get the gzipped version first. Because the gzipped path should be longer
Ed Tanous8b156452018-10-12 11:09:26 -070069 // than the non gzipped path, if we sort in descending order, we should be
Ed Tanous1abe55e2018-09-05 08:30:59 -070070 // guaranteed to get the gzip version first.
Ed Tanousa47f32b2019-10-24 10:08:04 -070071 std::vector<std::filesystem::directory_entry> paths(
72 std::filesystem::begin(dirIter), std::filesystem::end(dirIter));
Ed Tanous1abe55e2018-09-05 08:30:59 -070073 std::sort(paths.rbegin(), paths.rend());
Ed Tanous9dc2c4d2018-03-07 14:51:27 -080074
Ed Tanousa47f32b2019-10-24 10:08:04 -070075 for (const std::filesystem::directory_entry& dir : paths)
Ed Tanous1abe55e2018-09-05 08:30:59 -070076 {
Ed Tanous3174e4d2020-10-07 11:41:22 -070077 const std::filesystem::path& absolutePath = dir.path();
Ed Tanousa47f32b2019-10-24 10:08:04 -070078 std::filesystem::path relativePath{
Ed Tanous1abe55e2018-09-05 08:30:59 -070079 absolutePath.string().substr(rootpath.string().size() - 1)};
Ed Tanousa47f32b2019-10-24 10:08:04 -070080 if (std::filesystem::is_directory(dir))
Ed Tanous1abe55e2018-09-05 08:30:59 -070081 {
82 // don't recurse into hidden directories or symlinks
83 if (boost::starts_with(dir.path().filename().string(), ".") ||
Ed Tanousa47f32b2019-10-24 10:08:04 -070084 std::filesystem::is_symlink(dir))
Ed Tanous1abe55e2018-09-05 08:30:59 -070085 {
86 dirIter.disable_recursion_pending();
87 }
Ed Tanousba9f9a62017-10-11 16:40:35 -070088 }
Ed Tanousa47f32b2019-10-24 10:08:04 -070089 else if (std::filesystem::is_regular_file(dir))
Ed Tanous1abe55e2018-09-05 08:30:59 -070090 {
91 std::string extension = relativePath.extension();
Ed Tanousa47f32b2019-10-24 10:08:04 -070092 std::filesystem::path webpath = relativePath;
Ed Tanous1abe55e2018-09-05 08:30:59 -070093 const char* contentEncoding = nullptr;
Ed Tanous911ac312017-08-15 09:37:42 -070094
Ed Tanous1abe55e2018-09-05 08:30:59 -070095 if (extension == ".gz")
96 {
97 webpath = webpath.replace_extension("");
98 // Use the non-gzip version for determining content type
99 extension = webpath.extension().string();
100 contentEncoding = "gzip";
Ed Tanous911ac312017-08-15 09:37:42 -0700101 }
Ed Tanousba9f9a62017-10-11 16:40:35 -0700102
Ed Tanous1abe55e2018-09-05 08:30:59 -0700103 if (boost::starts_with(webpath.filename().string(), "index."))
104 {
105 webpath = webpath.parent_path();
106 if (webpath.string().size() == 0 ||
107 webpath.string().back() != '/')
108 {
109 // insert the non-directory version of this path
James Feist3909dc82020-04-03 10:58:55 -0700110 webroutes::routes.insert(webpath);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700111 webpath += "/";
112 }
Ed Tanousba9f9a62017-10-11 16:40:35 -0700113 }
114
Ed Tanous1abe55e2018-09-05 08:30:59 -0700115 std::pair<boost::container::flat_set<std::string>::iterator, bool>
James Feist3909dc82020-04-03 10:58:55 -0700116 inserted = webroutes::routes.insert(webpath);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700117
118 if (!inserted.second)
119 {
120 // Got a duplicated path. This is expected in certain
121 // situations
122 BMCWEB_LOG_DEBUG << "Got duplicated path " << webpath;
123 continue;
124 }
125 const char* contentType = nullptr;
126
Ed Tanous218bd472019-10-24 10:10:07 -0700127 for (const std::pair<const char*, const char*>& ext : contentTypes)
128 {
129 if (ext.first == nullptr || ext.second == nullptr)
130 {
131 continue;
132 }
133 if (extension == ext.first)
134 {
135 contentType = ext.second;
136 }
137 }
138
139 if (contentType == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700140 {
141 BMCWEB_LOG_ERROR << "Cannot determine content-type for "
142 << absolutePath << " with extension "
143 << extension;
144 }
Ed Tanous911ac312017-08-15 09:37:42 -0700145
Ed Tanous1abe55e2018-09-05 08:30:59 -0700146 app.routeDynamic(webpath)(
Ed Tanouscb13a392020-07-25 19:02:03 +0000147 [absolutePath, contentType,
148 contentEncoding](const crow::Request&, crow::Response& res) {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700149 if (contentType != nullptr)
150 {
151 res.addHeader("Content-Type", contentType);
152 }
153
154 if (contentEncoding != nullptr)
155 {
156 res.addHeader("Content-Encoding", contentEncoding);
157 }
158
159 // res.set_header("Cache-Control", "public, max-age=86400");
160 std::ifstream inf(absolutePath);
161 if (!inf)
162 {
163 BMCWEB_LOG_DEBUG << "failed to read file";
164 res.result(
165 boost::beast::http::status::internal_server_error);
166 res.end();
167 return;
168 }
169
170 res.body() = {std::istreambuf_iterator<char>(inf),
171 std::istreambuf_iterator<char>()};
172 res.end();
173 });
174 }
Ed Tanous911ac312017-08-15 09:37:42 -0700175 }
Ed Tanous99dc9c72019-10-24 10:09:03 -0700176}
Ed Tanous1abe55e2018-09-05 08:30:59 -0700177} // namespace webassets
178} // namespace crow