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