1 #pragma once 2 3 #include "webroutes.hpp" 4 5 #include <app.h> 6 #include <http_request.h> 7 #include <http_response.h> 8 #include <routing.h> 9 10 #include <boost/algorithm/string/replace.hpp> 11 #include <boost/container/flat_set.hpp> 12 13 #include <filesystem> 14 #include <fstream> 15 #include <string> 16 17 namespace crow 18 { 19 namespace webassets 20 { 21 22 namespace filesystem = std::filesystem; 23 24 struct CmpStr 25 { 26 bool operator()(const char* a, const char* b) const 27 { 28 return std::strcmp(a, b) < 0; 29 } 30 }; 31 32 template <typename... Middlewares> 33 void requestRoutes(Crow<Middlewares...>& app) 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"}, 39 {".js", "application/javascript;charset=UTF-8"}, 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"}, 49 {".json", "application/json"}, 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 62 // than the non gzipped path, if we sort in descending order, we should be 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()); 67 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 } 81 } 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; 87 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"; 94 } 95 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 103 webroutes::routes.insert(webpath); 104 webpath += "/"; 105 } 106 } 107 108 std::pair<boost::container::flat_set<std::string>::iterator, bool> 109 inserted = webroutes::routes.insert(webpath); 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; 130 } 131 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 } 161 } 162 } 163 } // namespace webassets 164 } // namespace crow 165