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