Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 3 | #include "app.hpp" |
| 4 | #include "http_request.hpp" |
| 5 | #include "http_response.hpp" |
| 6 | #include "routing.hpp" |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 7 | #include "webroutes.hpp" |
| 8 | |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 9 | #include <boost/algorithm/string/replace.hpp> |
| 10 | #include <boost/container/flat_set.hpp> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 11 | |
James Feist | 4418c7f | 2019-04-15 11:09:15 -0700 | [diff] [blame] | 12 | #include <filesystem> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 13 | #include <fstream> |
| 14 | #include <string> |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 15 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 16 | namespace crow |
| 17 | { |
| 18 | namespace webassets |
| 19 | { |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 20 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 21 | struct CmpStr |
| 22 | { |
| 23 | bool operator()(const char* a, const char* b) const |
| 24 | { |
| 25 | return std::strcmp(a, b) < 0; |
| 26 | } |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 27 | }; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 28 | |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 29 | inline void requestRoutes(App& app) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 30 | { |
Ed Tanous | 218bd47 | 2019-10-24 10:10:07 -0700 | [diff] [blame] | 31 | constexpr static std::array<std::pair<const char*, const char*>, 17> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 32 | contentTypes{ |
| 33 | {{".css", "text/css;charset=UTF-8"}, |
| 34 | {".html", "text/html;charset=UTF-8"}, |
Ed Tanous | 7e51389 | 2018-12-20 07:11:04 -0800 | [diff] [blame] | 35 | {".js", "application/javascript;charset=UTF-8"}, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 36 | {".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 Tanous | 683f727 | 2018-07-26 12:47:19 -0700 | [diff] [blame] | 45 | {".json", "application/json"}, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 46 | {".jpg", "image/jpeg"}, |
| 47 | {".jpeg", "image/jpeg"}, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 48 | // dev tools don't care about map type, setting to json causes |
| 49 | // browser to show as text |
| 50 | // https://stackoverflow.com/questions/19911929/what-mime-type-should-i-use-for-javascript-source-map-files |
| 51 | {".map", "application/json"}}}; |
Ed Tanous | a47f32b | 2019-10-24 10:08:04 -0700 | [diff] [blame] | 52 | |
| 53 | std::filesystem::path rootpath{"/usr/share/www/"}; |
Ed Tanous | 153fdcf | 2021-03-04 16:43:14 -0800 | [diff] [blame] | 54 | |
| 55 | std::error_code ec; |
| 56 | |
| 57 | std::filesystem::recursive_directory_iterator dirIter(rootpath, ec); |
| 58 | if (ec) |
| 59 | { |
Ed Tanous | 8cc8ede | 2022-02-28 10:20:59 -0800 | [diff] [blame] | 60 | BMCWEB_LOG_ERROR << "Unable to find or open " << rootpath.string() |
Ed Tanous | 153fdcf | 2021-03-04 16:43:14 -0800 | [diff] [blame] | 61 | << " static file hosting disabled"; |
| 62 | return; |
| 63 | } |
| 64 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 65 | // 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 Tanous | 8b15645 | 2018-10-12 11:09:26 -0700 | [diff] [blame] | 68 | // than the non gzipped path, if we sort in descending order, we should be |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 69 | // guaranteed to get the gzip version first. |
Ed Tanous | a47f32b | 2019-10-24 10:08:04 -0700 | [diff] [blame] | 70 | std::vector<std::filesystem::directory_entry> paths( |
| 71 | std::filesystem::begin(dirIter), std::filesystem::end(dirIter)); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 72 | std::sort(paths.rbegin(), paths.rend()); |
Ed Tanous | 9dc2c4d | 2018-03-07 14:51:27 -0800 | [diff] [blame] | 73 | |
Ed Tanous | a47f32b | 2019-10-24 10:08:04 -0700 | [diff] [blame] | 74 | for (const std::filesystem::directory_entry& dir : paths) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 75 | { |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 76 | const std::filesystem::path& absolutePath = dir.path(); |
Ed Tanous | a47f32b | 2019-10-24 10:08:04 -0700 | [diff] [blame] | 77 | std::filesystem::path relativePath{ |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 78 | absolutePath.string().substr(rootpath.string().size() - 1)}; |
Ed Tanous | a47f32b | 2019-10-24 10:08:04 -0700 | [diff] [blame] | 79 | if (std::filesystem::is_directory(dir)) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 80 | { |
| 81 | // don't recurse into hidden directories or symlinks |
Ed Tanous | 11ba397 | 2022-07-11 09:50:41 -0700 | [diff] [blame] | 82 | if (dir.path().filename().string().starts_with(".") || |
Ed Tanous | a47f32b | 2019-10-24 10:08:04 -0700 | [diff] [blame] | 83 | std::filesystem::is_symlink(dir)) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 84 | { |
| 85 | dirIter.disable_recursion_pending(); |
| 86 | } |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 87 | } |
Ed Tanous | a47f32b | 2019-10-24 10:08:04 -0700 | [diff] [blame] | 88 | else if (std::filesystem::is_regular_file(dir)) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 89 | { |
| 90 | std::string extension = relativePath.extension(); |
Ed Tanous | a47f32b | 2019-10-24 10:08:04 -0700 | [diff] [blame] | 91 | std::filesystem::path webpath = relativePath; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 92 | const char* contentEncoding = nullptr; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 93 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 94 | 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 Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 100 | } |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 101 | |
Ed Tanous | 11ba397 | 2022-07-11 09:50:41 -0700 | [diff] [blame] | 102 | if (webpath.filename().string().starts_with("index.")) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 103 | { |
| 104 | webpath = webpath.parent_path(); |
Ed Tanous | 26f6976 | 2022-01-25 09:49:11 -0800 | [diff] [blame] | 105 | if (webpath.string().empty() || webpath.string().back() != '/') |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 106 | { |
| 107 | // insert the non-directory version of this path |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 108 | webroutes::routes.insert(webpath); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 109 | webpath += "/"; |
| 110 | } |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 113 | std::pair<boost::container::flat_set<std::string>::iterator, bool> |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 114 | inserted = webroutes::routes.insert(webpath); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 115 | |
| 116 | if (!inserted.second) |
| 117 | { |
| 118 | // Got a duplicated path. This is expected in certain |
| 119 | // situations |
Ed Tanous | 8cc8ede | 2022-02-28 10:20:59 -0800 | [diff] [blame] | 120 | BMCWEB_LOG_DEBUG << "Got duplicated path " << webpath.string(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 121 | continue; |
| 122 | } |
| 123 | const char* contentType = nullptr; |
| 124 | |
Ed Tanous | 218bd47 | 2019-10-24 10:10:07 -0700 | [diff] [blame] | 125 | 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 Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 138 | { |
| 139 | BMCWEB_LOG_ERROR << "Cannot determine content-type for " |
Ed Tanous | 8cc8ede | 2022-02-28 10:20:59 -0800 | [diff] [blame] | 140 | << absolutePath.string() << " with extension " |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 141 | << extension; |
| 142 | } |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 143 | |
Ed Tanous | d4b6c66 | 2021-03-10 13:29:30 -0800 | [diff] [blame] | 144 | if (webpath == "/") |
| 145 | { |
| 146 | forward_unauthorized::hasWebuiRoute = true; |
| 147 | } |
| 148 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 149 | app.routeDynamic(webpath)( |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 150 | [absolutePath, contentType, contentEncoding]( |
| 151 | const crow::Request&, |
| 152 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 153 | if (contentType != nullptr) |
| 154 | { |
Ed Tanous | d9f6c62 | 2022-03-17 09:12:17 -0700 | [diff] [blame] | 155 | asyncResp->res.addHeader( |
| 156 | boost::beast::http::field::content_type, contentType); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 157 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 158 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 159 | if (contentEncoding != nullptr) |
| 160 | { |
Ed Tanous | d9f6c62 | 2022-03-17 09:12:17 -0700 | [diff] [blame] | 161 | asyncResp->res.addHeader( |
| 162 | boost::beast::http::field::content_encoding, |
| 163 | contentEncoding); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 164 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 165 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 166 | // res.set_header("Cache-Control", "public, max-age=86400"); |
| 167 | std::ifstream inf(absolutePath); |
| 168 | if (!inf) |
| 169 | { |
| 170 | BMCWEB_LOG_DEBUG << "failed to read file"; |
| 171 | asyncResp->res.result( |
| 172 | boost::beast::http::status::internal_server_error); |
| 173 | return; |
| 174 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 175 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 176 | asyncResp->res.body() = {std::istreambuf_iterator<char>(inf), |
| 177 | std::istreambuf_iterator<char>()}; |
| 178 | }); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 179 | } |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 180 | } |
Ed Tanous | 99dc9c7 | 2019-10-24 10:09:03 -0700 | [diff] [blame] | 181 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 182 | } // namespace webassets |
| 183 | } // namespace crow |