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 | c94ad49 | 2019-10-10 15:39:33 -0700 | [diff] [blame] | 5 | #include <app.h> |
| 6 | #include <http_request.h> |
| 7 | #include <http_response.h> |
| 8 | #include <routing.h> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 9 | |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 10 | #include <boost/algorithm/string/replace.hpp> |
| 11 | #include <boost/container/flat_set.hpp> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 12 | |
James Feist | 4418c7f | 2019-04-15 11:09:15 -0700 | [diff] [blame] | 13 | #include <filesystem> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 14 | #include <fstream> |
| 15 | #include <string> |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 16 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 17 | namespace crow |
| 18 | { |
| 19 | namespace webassets |
| 20 | { |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 21 | |
James Feist | f615040 | 2019-01-08 10:36:20 -0800 | [diff] [blame] | 22 | namespace filesystem = std::filesystem; |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 23 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 24 | struct CmpStr |
| 25 | { |
| 26 | bool operator()(const char* a, const char* b) const |
| 27 | { |
| 28 | return std::strcmp(a, b) < 0; |
| 29 | } |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 30 | }; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 31 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 32 | template <typename... Middlewares> |
| 33 | void requestRoutes(Crow<Middlewares...>& app) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 34 | { |
| 35 | const static boost::container::flat_map<const char*, const char*, CmpStr> |
| 36 | contentTypes{ |
| 37 | {{".css", "text/css;charset=UTF-8"}, |
| 38 | {".html", "text/html;charset=UTF-8"}, |
Ed Tanous | 7e51389 | 2018-12-20 07:11:04 -0800 | [diff] [blame] | 39 | {".js", "application/javascript;charset=UTF-8"}, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 40 | {".png", "image/png;charset=UTF-8"}, |
| 41 | {".woff", "application/x-font-woff"}, |
| 42 | {".woff2", "application/x-font-woff2"}, |
| 43 | {".gif", "image/gif"}, |
| 44 | {".ico", "image/x-icon"}, |
| 45 | {".ttf", "application/x-font-ttf"}, |
| 46 | {".svg", "image/svg+xml"}, |
| 47 | {".eot", "application/vnd.ms-fontobject"}, |
| 48 | {".xml", "application/xml"}, |
Ed Tanous | 683f727 | 2018-07-26 12:47:19 -0700 | [diff] [blame] | 49 | {".json", "application/json"}, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 50 | {".jpg", "image/jpeg"}, |
| 51 | {".jpeg", "image/jpeg"}, |
| 52 | {".json", "application/json"}, |
| 53 | // dev tools don't care about map type, setting to json causes |
| 54 | // browser to show as text |
| 55 | // https://stackoverflow.com/questions/19911929/what-mime-type-should-i-use-for-javascript-source-map-files |
| 56 | {".map", "application/json"}}}; |
| 57 | filesystem::path rootpath{"/usr/share/www/"}; |
| 58 | filesystem::recursive_directory_iterator dirIter(rootpath); |
| 59 | // In certain cases, we might have both a gzipped version of the file AND a |
| 60 | // non-gzipped version. To avoid duplicated routes, we need to make sure we |
| 61 | // get the gzipped version first. Because the gzipped path should be longer |
Ed Tanous | 8b15645 | 2018-10-12 11:09:26 -0700 | [diff] [blame] | 62 | // 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] | 63 | // guaranteed to get the gzip version first. |
| 64 | std::vector<filesystem::directory_entry> paths(filesystem::begin(dirIter), |
| 65 | filesystem::end(dirIter)); |
| 66 | std::sort(paths.rbegin(), paths.rend()); |
Ed Tanous | 9dc2c4d | 2018-03-07 14:51:27 -0800 | [diff] [blame] | 67 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 68 | for (const filesystem::directory_entry& dir : paths) |
| 69 | { |
| 70 | filesystem::path absolutePath = dir.path(); |
| 71 | filesystem::path relativePath{ |
| 72 | absolutePath.string().substr(rootpath.string().size() - 1)}; |
| 73 | if (filesystem::is_directory(dir)) |
| 74 | { |
| 75 | // don't recurse into hidden directories or symlinks |
| 76 | if (boost::starts_with(dir.path().filename().string(), ".") || |
| 77 | filesystem::is_symlink(dir)) |
| 78 | { |
| 79 | dirIter.disable_recursion_pending(); |
| 80 | } |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 81 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 82 | else if (filesystem::is_regular_file(dir)) |
| 83 | { |
| 84 | std::string extension = relativePath.extension(); |
| 85 | filesystem::path webpath = relativePath; |
| 86 | const char* contentEncoding = nullptr; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 87 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 88 | if (extension == ".gz") |
| 89 | { |
| 90 | webpath = webpath.replace_extension(""); |
| 91 | // Use the non-gzip version for determining content type |
| 92 | extension = webpath.extension().string(); |
| 93 | contentEncoding = "gzip"; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 94 | } |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 95 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 96 | if (boost::starts_with(webpath.filename().string(), "index.")) |
| 97 | { |
| 98 | webpath = webpath.parent_path(); |
| 99 | if (webpath.string().size() == 0 || |
| 100 | webpath.string().back() != '/') |
| 101 | { |
| 102 | // insert the non-directory version of this path |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame^] | 103 | webroutes::routes.insert(webpath); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 104 | webpath += "/"; |
| 105 | } |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 108 | std::pair<boost::container::flat_set<std::string>::iterator, bool> |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame^] | 109 | inserted = webroutes::routes.insert(webpath); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 110 | |
| 111 | if (!inserted.second) |
| 112 | { |
| 113 | // Got a duplicated path. This is expected in certain |
| 114 | // situations |
| 115 | BMCWEB_LOG_DEBUG << "Got duplicated path " << webpath; |
| 116 | continue; |
| 117 | } |
| 118 | const char* contentType = nullptr; |
| 119 | |
| 120 | auto contentTypeIt = contentTypes.find(extension.c_str()); |
| 121 | if (contentTypeIt == contentTypes.end()) |
| 122 | { |
| 123 | BMCWEB_LOG_ERROR << "Cannot determine content-type for " |
| 124 | << absolutePath << " with extension " |
| 125 | << extension; |
| 126 | } |
| 127 | else |
| 128 | { |
| 129 | contentType = contentTypeIt->second; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 132 | app.routeDynamic(webpath)( |
| 133 | [absolutePath, contentType, contentEncoding]( |
| 134 | const crow::Request& req, crow::Response& res) { |
| 135 | if (contentType != nullptr) |
| 136 | { |
| 137 | res.addHeader("Content-Type", contentType); |
| 138 | } |
| 139 | |
| 140 | if (contentEncoding != nullptr) |
| 141 | { |
| 142 | res.addHeader("Content-Encoding", contentEncoding); |
| 143 | } |
| 144 | |
| 145 | // res.set_header("Cache-Control", "public, max-age=86400"); |
| 146 | std::ifstream inf(absolutePath); |
| 147 | if (!inf) |
| 148 | { |
| 149 | BMCWEB_LOG_DEBUG << "failed to read file"; |
| 150 | res.result( |
| 151 | boost::beast::http::status::internal_server_error); |
| 152 | res.end(); |
| 153 | return; |
| 154 | } |
| 155 | |
| 156 | res.body() = {std::istreambuf_iterator<char>(inf), |
| 157 | std::istreambuf_iterator<char>()}; |
| 158 | res.end(); |
| 159 | }); |
| 160 | } |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 161 | } |
Ed Tanous | 99dc9c7 | 2019-10-24 10:09:03 -0700 | [diff] [blame] | 162 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 163 | } // namespace webassets |
| 164 | } // namespace crow |