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> |
Ed Tanous | 5b6a1f9 | 2018-02-02 11:05:21 -0800 | [diff] [blame] | 10 | #include <crow/http_codes.h> |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 11 | #include <boost/algorithm/string/replace.hpp> |
| 12 | #include <boost/container/flat_set.hpp> |
| 13 | |
| 14 | namespace crow { |
| 15 | namespace webassets { |
| 16 | |
| 17 | namespace filesystem = std::experimental::filesystem; |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 18 | |
| 19 | struct cmp_str { |
| 20 | bool operator()(const char* a, const char* b) const { |
| 21 | return std::strcmp(a, b) < 0; |
| 22 | } |
| 23 | }; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 24 | |
| 25 | static boost::container::flat_set<std::string> routes; |
| 26 | |
| 27 | template <typename... Middlewares> |
| 28 | void request_routes(Crow<Middlewares...>& app) { |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 29 | const static boost::container::flat_map<const char*, const char*, cmp_str> |
| 30 | content_types{ |
| 31 | {{".css", "text/css;charset=UTF-8"}, |
| 32 | {".html", "text/html;charset=UTF-8"}, |
| 33 | {".js", "text/html;charset=UTF-8"}, |
| 34 | {".png", "image/png;charset=UTF-8"}, |
| 35 | {".woff", "application/x-font-woff"}, |
| 36 | {".woff2", "application/x-font-woff2"}, |
Ed Tanous | 5b6a1f9 | 2018-02-02 11:05:21 -0800 | [diff] [blame] | 37 | {".gif", "image/gif"}, |
| 38 | {".ico", "image/x-icon"}, |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 39 | {".ttf", "application/x-font-ttf"}, |
| 40 | {".svg", "image/svg+xml"}, |
| 41 | {".eot", "application/vnd.ms-fontobject"}, |
| 42 | {".xml", "application/xml"}, |
| 43 | // dev tools don't care about map type, setting to json causes |
| 44 | // browser to show as text |
| 45 | // https://stackoverflow.com/questions/19911929/what-mime-type-should-i-use-for-javascript-source-map-files |
| 46 | {".map", "application/json"}}}; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 47 | auto rootpath = filesystem::path("/usr/share/www/"); |
| 48 | auto dir_iter = filesystem::recursive_directory_iterator(rootpath); |
| 49 | for (auto& dir : dir_iter) { |
| 50 | auto absolute_path = dir.path(); |
| 51 | auto relative_path = filesystem::path( |
| 52 | absolute_path.string().substr(rootpath.string().size() - 1)); |
| 53 | // make sure we don't recurse into certain directories |
| 54 | // note: maybe check for is_directory() here as well... |
Ed Tanous | 5b6a1f9 | 2018-02-02 11:05:21 -0800 | [diff] [blame] | 55 | |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 56 | if (filesystem::is_directory(dir)) { |
| 57 | // don't recurse into hidden directories or symlinks |
| 58 | if (boost::starts_with(dir.path().filename().string(), ".") || |
| 59 | filesystem::is_symlink(dir)) { |
| 60 | dir_iter.disable_recursion_pending(); |
| 61 | } |
| 62 | } else if (filesystem::is_regular_file(dir)) { |
Ed Tanous | 5b6a1f9 | 2018-02-02 11:05:21 -0800 | [diff] [blame] | 63 | std::string extension = relative_path.extension(); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 64 | auto webpath = relative_path; |
Ed Tanous | 5b6a1f9 | 2018-02-02 11:05:21 -0800 | [diff] [blame] | 65 | const char* content_encoding = nullptr; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 66 | bool is_gzip = false; |
Ed Tanous | 5b6a1f9 | 2018-02-02 11:05:21 -0800 | [diff] [blame] | 67 | |
| 68 | if (webpath.filename() == "$metadata") { |
| 69 | // TODO, this endpoint should really be generated based on the redfish |
| 70 | // data. Once that is done, remove the type hardcode. |
| 71 | extension = ".xml"; |
| 72 | } |
| 73 | |
| 74 | if (extension == ".gz") { |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 75 | webpath = webpath.replace_extension(""); |
Ed Tanous | 5b6a1f9 | 2018-02-02 11:05:21 -0800 | [diff] [blame] | 76 | // Use the non-gzip version for determining content type |
| 77 | extension = webpath.extension().string(); |
| 78 | content_encoding = "gzip"; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 79 | is_gzip = true; |
| 80 | } |
| 81 | |
| 82 | if (webpath.filename() == "index.html") { |
| 83 | webpath = webpath.parent_path(); |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 84 | if (webpath.string() != "/") { |
| 85 | webpath += "/"; |
| 86 | } |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | routes.insert(webpath.string()); |
| 90 | |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 91 | const char* content_type = nullptr; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 92 | |
Ed Tanous | 5b6a1f9 | 2018-02-02 11:05:21 -0800 | [diff] [blame] | 93 | // if the filename has an extension, look up the type |
| 94 | if (extension != "") { |
| 95 | auto content_type_it = content_types.find(extension.c_str()); |
| 96 | |
| 97 | if (content_type_it == content_types.end()) { |
| 98 | CROW_LOG_ERROR << "Cannot determine content-type for " << webpath; |
| 99 | continue; |
| 100 | } |
| 101 | content_type = content_type_it->second; |
| 102 | } |
| 103 | |
| 104 | app.route_dynamic(webpath.string())( |
| 105 | [absolute_path, content_type, content_encoding]( |
| 106 | const crow::request& req, crow::response& res) { |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 107 | if (content_type != nullptr) { |
Ed Tanous | 5b6a1f9 | 2018-02-02 11:05:21 -0800 | [diff] [blame] | 108 | res.add_header("Content-Type", content_type); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 109 | } |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 110 | |
Ed Tanous | 5b6a1f9 | 2018-02-02 11:05:21 -0800 | [diff] [blame] | 111 | if (content_encoding != nullptr) { |
| 112 | res.add_header("Content-Encoding", content_encoding); |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 115 | // res.set_header("Cache-Control", "public, max-age=86400"); |
Ed Tanous | 5b6a1f9 | 2018-02-02 11:05:21 -0800 | [diff] [blame] | 116 | std::ifstream inf(absolute_path); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 117 | if (!inf) { |
| 118 | CROW_LOG_DEBUG << "failed to read file"; |
Ed Tanous | 5b6a1f9 | 2018-02-02 11:05:21 -0800 | [diff] [blame] | 119 | res.code = static_cast<int>(HttpRespCode::NOT_FOUND); |
| 120 | res.code = static_cast<int>(HttpRespCode::INTERNAL_ERROR); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 121 | res.end(); |
| 122 | return; |
| 123 | } |
| 124 | |
Ed Tanous | 5b6a1f9 | 2018-02-02 11:05:21 -0800 | [diff] [blame] | 125 | res.body = {std::istreambuf_iterator<char>(inf), |
| 126 | std::istreambuf_iterator<char>()}; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 127 | res.end(); |
| 128 | }); |
| 129 | } |
| 130 | } |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 131 | } // namespace webassets |
Ed Tanous | 5b6a1f9 | 2018-02-02 11:05:21 -0800 | [diff] [blame] | 132 | } // namespace webassets |
| 133 | } // namespace crow |