blob: c5c7228edea7a2918d6cc6a1fa3732ceaa7a382f [file] [log] [blame]
Ed Tanous911ac312017-08-15 09:37:42 -07001#pragma once
2
Ed Tanous3ccb3ad2023-01-13 17:40:03 -08003#include "app.hpp"
4#include "http_request.hpp"
5#include "http_response.hpp"
6#include "routing.hpp"
James Feist3909dc82020-04-03 10:58:55 -07007#include "webroutes.hpp"
8
Ed Tanous911ac312017-08-15 09:37:42 -07009#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
Ed Tanous1abe55e2018-09-05 08:30:59 -070020struct CmpStr
21{
22 bool operator()(const char* a, const char* b) const
23 {
24 return std::strcmp(a, b) < 0;
25 }
Ed Tanousba9f9a62017-10-11 16:40:35 -070026};
Ed Tanous911ac312017-08-15 09:37:42 -070027
Ed Tanous23a21a12020-07-25 04:45:05 +000028inline void requestRoutes(App& app)
Ed Tanous1abe55e2018-09-05 08:30:59 -070029{
Ed Tanous218bd472019-10-24 10:10:07 -070030 constexpr static std::array<std::pair<const char*, const char*>, 17>
Ed Tanous1abe55e2018-09-05 08:30:59 -070031 contentTypes{
32 {{".css", "text/css;charset=UTF-8"},
33 {".html", "text/html;charset=UTF-8"},
Ed Tanous7e513892018-12-20 07:11:04 -080034 {".js", "application/javascript;charset=UTF-8"},
Ed Tanous1abe55e2018-09-05 08:30:59 -070035 {".png", "image/png;charset=UTF-8"},
36 {".woff", "application/x-font-woff"},
37 {".woff2", "application/x-font-woff2"},
38 {".gif", "image/gif"},
39 {".ico", "image/x-icon"},
40 {".ttf", "application/x-font-ttf"},
41 {".svg", "image/svg+xml"},
42 {".eot", "application/vnd.ms-fontobject"},
43 {".xml", "application/xml"},
Ed Tanous683f7272018-07-26 12:47:19 -070044 {".json", "application/json"},
Ed Tanous1abe55e2018-09-05 08:30:59 -070045 {".jpg", "image/jpeg"},
46 {".jpeg", "image/jpeg"},
Ed Tanous1abe55e2018-09-05 08:30:59 -070047 // dev tools don't care about map type, setting to json causes
48 // browser to show as text
49 // https://stackoverflow.com/questions/19911929/what-mime-type-should-i-use-for-javascript-source-map-files
50 {".map", "application/json"}}};
Ed Tanousa47f32b2019-10-24 10:08:04 -070051
52 std::filesystem::path rootpath{"/usr/share/www/"};
Ed Tanous153fdcf2021-03-04 16:43:14 -080053
54 std::error_code ec;
55
56 std::filesystem::recursive_directory_iterator dirIter(rootpath, ec);
57 if (ec)
58 {
Ed Tanous62598e32023-07-17 17:06:25 -070059 BMCWEB_LOG_ERROR(
60 "Unable to find or open {} static file hosting disabled",
61 rootpath.string());
Ed Tanous153fdcf2021-03-04 16:43:14 -080062 return;
63 }
64
Ed Tanous1abe55e2018-09-05 08:30:59 -070065 // In certain cases, we might have both a gzipped version of the file AND a
66 // non-gzipped version. To avoid duplicated routes, we need to make sure we
67 // get the gzipped version first. Because the gzipped path should be longer
Ed Tanous8b156452018-10-12 11:09:26 -070068 // than the non gzipped path, if we sort in descending order, we should be
Ed Tanous1abe55e2018-09-05 08:30:59 -070069 // guaranteed to get the gzip version first.
Ed Tanousa47f32b2019-10-24 10:08:04 -070070 std::vector<std::filesystem::directory_entry> paths(
71 std::filesystem::begin(dirIter), std::filesystem::end(dirIter));
Ed Tanous1abe55e2018-09-05 08:30:59 -070072 std::sort(paths.rbegin(), paths.rend());
Ed Tanous9dc2c4d2018-03-07 14:51:27 -080073
Ed Tanousa47f32b2019-10-24 10:08:04 -070074 for (const std::filesystem::directory_entry& dir : paths)
Ed Tanous1abe55e2018-09-05 08:30:59 -070075 {
Ed Tanous3174e4d2020-10-07 11:41:22 -070076 const std::filesystem::path& absolutePath = dir.path();
Ed Tanousa47f32b2019-10-24 10:08:04 -070077 std::filesystem::path relativePath{
Ed Tanous1abe55e2018-09-05 08:30:59 -070078 absolutePath.string().substr(rootpath.string().size() - 1)};
Ed Tanousa47f32b2019-10-24 10:08:04 -070079 if (std::filesystem::is_directory(dir))
Ed Tanous1abe55e2018-09-05 08:30:59 -070080 {
81 // don't recurse into hidden directories or symlinks
Ed Tanous11ba3972022-07-11 09:50:41 -070082 if (dir.path().filename().string().starts_with(".") ||
Ed Tanousa47f32b2019-10-24 10:08:04 -070083 std::filesystem::is_symlink(dir))
Ed Tanous1abe55e2018-09-05 08:30:59 -070084 {
85 dirIter.disable_recursion_pending();
86 }
Ed Tanousba9f9a62017-10-11 16:40:35 -070087 }
Ed Tanousa47f32b2019-10-24 10:08:04 -070088 else if (std::filesystem::is_regular_file(dir))
Ed Tanous1abe55e2018-09-05 08:30:59 -070089 {
90 std::string extension = relativePath.extension();
Ed Tanousa47f32b2019-10-24 10:08:04 -070091 std::filesystem::path webpath = relativePath;
Ed Tanous1abe55e2018-09-05 08:30:59 -070092 const char* contentEncoding = nullptr;
Ed Tanous911ac312017-08-15 09:37:42 -070093
Ed Tanous1abe55e2018-09-05 08:30:59 -070094 if (extension == ".gz")
95 {
96 webpath = webpath.replace_extension("");
97 // Use the non-gzip version for determining content type
98 extension = webpath.extension().string();
99 contentEncoding = "gzip";
Ed Tanous911ac312017-08-15 09:37:42 -0700100 }
Ed Tanousba9f9a62017-10-11 16:40:35 -0700101
Ed Tanous11ba3972022-07-11 09:50:41 -0700102 if (webpath.filename().string().starts_with("index."))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700103 {
104 webpath = webpath.parent_path();
Ed Tanous26f69762022-01-25 09:49:11 -0800105 if (webpath.string().empty() || webpath.string().back() != '/')
Ed Tanous1abe55e2018-09-05 08:30:59 -0700106 {
107 // insert the non-directory version of this path
James Feist3909dc82020-04-03 10:58:55 -0700108 webroutes::routes.insert(webpath);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700109 webpath += "/";
110 }
Ed Tanousba9f9a62017-10-11 16:40:35 -0700111 }
112
Ed Tanous1abe55e2018-09-05 08:30:59 -0700113 std::pair<boost::container::flat_set<std::string>::iterator, bool>
James Feist3909dc82020-04-03 10:58:55 -0700114 inserted = webroutes::routes.insert(webpath);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700115
116 if (!inserted.second)
117 {
118 // Got a duplicated path. This is expected in certain
119 // situations
Ed Tanous62598e32023-07-17 17:06:25 -0700120 BMCWEB_LOG_DEBUG("Got duplicated path {}", webpath.string());
Ed Tanous1abe55e2018-09-05 08:30:59 -0700121 continue;
122 }
123 const char* contentType = nullptr;
124
Ed Tanous218bd472019-10-24 10:10:07 -0700125 for (const std::pair<const char*, const char*>& ext : contentTypes)
126 {
127 if (ext.first == nullptr || ext.second == nullptr)
128 {
129 continue;
130 }
131 if (extension == ext.first)
132 {
133 contentType = ext.second;
134 }
135 }
136
137 if (contentType == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700138 {
Ed Tanous62598e32023-07-17 17:06:25 -0700139 BMCWEB_LOG_ERROR(
140 "Cannot determine content-type for {} with extension {}",
141 absolutePath.string(), extension);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700142 }
Ed Tanous911ac312017-08-15 09:37:42 -0700143
Ed Tanousd4b6c662021-03-10 13:29:30 -0800144 if (webpath == "/")
145 {
146 forward_unauthorized::hasWebuiRoute = true;
147 }
148
Ed Tanous1abe55e2018-09-05 08:30:59 -0700149 app.routeDynamic(webpath)(
zhanghch058d1b46d2021-04-01 11:18:24 +0800150 [absolutePath, contentType, contentEncoding](
151 const crow::Request&,
152 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700153 if (contentType != nullptr)
154 {
Ed Tanousd9f6c622022-03-17 09:12:17 -0700155 asyncResp->res.addHeader(
156 boost::beast::http::field::content_type, contentType);
Ed Tanous002d39b2022-05-31 08:59:27 -0700157 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700158
Ed Tanous002d39b2022-05-31 08:59:27 -0700159 if (contentEncoding != nullptr)
160 {
Ed Tanousd9f6c622022-03-17 09:12:17 -0700161 asyncResp->res.addHeader(
162 boost::beast::http::field::content_encoding,
163 contentEncoding);
Ed Tanous002d39b2022-05-31 08:59:27 -0700164 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700165
Ed Tanous002d39b2022-05-31 08:59:27 -0700166 // res.set_header("Cache-Control", "public, max-age=86400");
Ed Tanous27b0cf92023-08-07 12:02:40 -0700167 if (!asyncResp->res.openFile(absolutePath))
Ed Tanous002d39b2022-05-31 08:59:27 -0700168 {
Ed Tanous62598e32023-07-17 17:06:25 -0700169 BMCWEB_LOG_DEBUG("failed to read file");
Ed Tanous002d39b2022-05-31 08:59:27 -0700170 asyncResp->res.result(
171 boost::beast::http::status::internal_server_error);
172 return;
173 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700174 });
Ed Tanous1abe55e2018-09-05 08:30:59 -0700175 }
Ed Tanous911ac312017-08-15 09:37:42 -0700176 }
Ed Tanous99dc9c72019-10-24 10:09:03 -0700177}
Ed Tanous1abe55e2018-09-05 08:30:59 -0700178} // namespace webassets
179} // namespace crow