Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 3 | #include "webroutes.hpp" |
| 4 | |
Ed Tanous | 04e438c | 2020-10-03 08:06:26 -0700 | [diff] [blame^] | 5 | #include <app.hpp> |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 6 | #include <boost/algorithm/string/replace.hpp> |
| 7 | #include <boost/container/flat_set.hpp> |
Ed Tanous | 04e438c | 2020-10-03 08:06:26 -0700 | [diff] [blame^] | 8 | #include <http_request.hpp> |
| 9 | #include <http_response.hpp> |
| 10 | #include <routing.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"}, |
| 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 Tanous | a47f32b | 2019-10-24 10:08:04 -0700 | [diff] [blame] | 53 | |
| 54 | std::filesystem::path rootpath{"/usr/share/www/"}; |
| 55 | std::filesystem::recursive_directory_iterator dirIter(rootpath); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 56 | // In certain cases, we might have both a gzipped version of the file AND a |
| 57 | // non-gzipped version. To avoid duplicated routes, we need to make sure we |
| 58 | // get the gzipped version first. Because the gzipped path should be longer |
Ed Tanous | 8b15645 | 2018-10-12 11:09:26 -0700 | [diff] [blame] | 59 | // 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] | 60 | // guaranteed to get the gzip version first. |
Ed Tanous | a47f32b | 2019-10-24 10:08:04 -0700 | [diff] [blame] | 61 | std::vector<std::filesystem::directory_entry> paths( |
| 62 | std::filesystem::begin(dirIter), std::filesystem::end(dirIter)); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 63 | std::sort(paths.rbegin(), paths.rend()); |
Ed Tanous | 9dc2c4d | 2018-03-07 14:51:27 -0800 | [diff] [blame] | 64 | |
Ed Tanous | a47f32b | 2019-10-24 10:08:04 -0700 | [diff] [blame] | 65 | for (const std::filesystem::directory_entry& dir : paths) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 66 | { |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 67 | const std::filesystem::path& absolutePath = dir.path(); |
Ed Tanous | a47f32b | 2019-10-24 10:08:04 -0700 | [diff] [blame] | 68 | std::filesystem::path relativePath{ |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 69 | absolutePath.string().substr(rootpath.string().size() - 1)}; |
Ed Tanous | a47f32b | 2019-10-24 10:08:04 -0700 | [diff] [blame] | 70 | if (std::filesystem::is_directory(dir)) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 71 | { |
| 72 | // don't recurse into hidden directories or symlinks |
| 73 | if (boost::starts_with(dir.path().filename().string(), ".") || |
Ed Tanous | a47f32b | 2019-10-24 10:08:04 -0700 | [diff] [blame] | 74 | std::filesystem::is_symlink(dir)) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 75 | { |
| 76 | dirIter.disable_recursion_pending(); |
| 77 | } |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 78 | } |
Ed Tanous | a47f32b | 2019-10-24 10:08:04 -0700 | [diff] [blame] | 79 | else if (std::filesystem::is_regular_file(dir)) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 80 | { |
| 81 | std::string extension = relativePath.extension(); |
Ed Tanous | a47f32b | 2019-10-24 10:08:04 -0700 | [diff] [blame] | 82 | std::filesystem::path webpath = relativePath; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 83 | const char* contentEncoding = nullptr; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 84 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 85 | if (extension == ".gz") |
| 86 | { |
| 87 | webpath = webpath.replace_extension(""); |
| 88 | // Use the non-gzip version for determining content type |
| 89 | extension = webpath.extension().string(); |
| 90 | contentEncoding = "gzip"; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 91 | } |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 92 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 93 | if (boost::starts_with(webpath.filename().string(), "index.")) |
| 94 | { |
| 95 | webpath = webpath.parent_path(); |
| 96 | if (webpath.string().size() == 0 || |
| 97 | webpath.string().back() != '/') |
| 98 | { |
| 99 | // insert the non-directory version of this path |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 100 | webroutes::routes.insert(webpath); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 101 | webpath += "/"; |
| 102 | } |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 105 | std::pair<boost::container::flat_set<std::string>::iterator, bool> |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 106 | inserted = webroutes::routes.insert(webpath); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 107 | |
| 108 | if (!inserted.second) |
| 109 | { |
| 110 | // Got a duplicated path. This is expected in certain |
| 111 | // situations |
| 112 | BMCWEB_LOG_DEBUG << "Got duplicated path " << webpath; |
| 113 | continue; |
| 114 | } |
| 115 | const char* contentType = nullptr; |
| 116 | |
Ed Tanous | 218bd47 | 2019-10-24 10:10:07 -0700 | [diff] [blame] | 117 | for (const std::pair<const char*, const char*>& ext : contentTypes) |
| 118 | { |
| 119 | if (ext.first == nullptr || ext.second == nullptr) |
| 120 | { |
| 121 | continue; |
| 122 | } |
| 123 | if (extension == ext.first) |
| 124 | { |
| 125 | contentType = ext.second; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | if (contentType == nullptr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 130 | { |
| 131 | BMCWEB_LOG_ERROR << "Cannot determine content-type for " |
| 132 | << absolutePath << " with extension " |
| 133 | << extension; |
| 134 | } |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 135 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 136 | app.routeDynamic(webpath)( |
Ed Tanous | cb13a39 | 2020-07-25 19:02:03 +0000 | [diff] [blame] | 137 | [absolutePath, contentType, |
| 138 | contentEncoding](const crow::Request&, crow::Response& res) { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 139 | if (contentType != nullptr) |
| 140 | { |
| 141 | res.addHeader("Content-Type", contentType); |
| 142 | } |
| 143 | |
| 144 | if (contentEncoding != nullptr) |
| 145 | { |
| 146 | res.addHeader("Content-Encoding", contentEncoding); |
| 147 | } |
| 148 | |
| 149 | // res.set_header("Cache-Control", "public, max-age=86400"); |
| 150 | std::ifstream inf(absolutePath); |
| 151 | if (!inf) |
| 152 | { |
| 153 | BMCWEB_LOG_DEBUG << "failed to read file"; |
| 154 | res.result( |
| 155 | boost::beast::http::status::internal_server_error); |
| 156 | res.end(); |
| 157 | return; |
| 158 | } |
| 159 | |
| 160 | res.body() = {std::istreambuf_iterator<char>(inf), |
| 161 | std::istreambuf_iterator<char>()}; |
| 162 | res.end(); |
| 163 | }); |
| 164 | } |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 165 | } |
Ed Tanous | 99dc9c7 | 2019-10-24 10:09:03 -0700 | [diff] [blame] | 166 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 167 | } // namespace webassets |
| 168 | } // namespace crow |