1 #pragma once 2 3 #include "app.hpp" 4 #include "http_request.hpp" 5 #include "http_response.hpp" 6 #include "routing.hpp" 7 #include "webroutes.hpp" 8 9 #include <boost/algorithm/string/replace.hpp> 10 #include <boost/container/flat_set.hpp> 11 12 #include <filesystem> 13 #include <fstream> 14 #include <string> 15 16 namespace crow 17 { 18 namespace webassets 19 { 20 21 struct CmpStr 22 { 23 bool operator()(const char* a, const char* b) const 24 { 25 return std::strcmp(a, b) < 0; 26 } 27 }; 28 29 inline void requestRoutes(App& app) 30 { 31 constexpr static std::array<std::pair<const char*, const char*>, 17> 32 contentTypes{ 33 {{".css", "text/css;charset=UTF-8"}, 34 {".html", "text/html;charset=UTF-8"}, 35 {".js", "application/javascript;charset=UTF-8"}, 36 {".png", "image/png;charset=UTF-8"}, 37 {".woff", "application/x-font-woff"}, 38 {".woff2", "application/x-font-woff2"}, 39 {".gif", "image/gif"}, 40 {".ico", "image/x-icon"}, 41 {".ttf", "application/x-font-ttf"}, 42 {".svg", "image/svg+xml"}, 43 {".eot", "application/vnd.ms-fontobject"}, 44 {".xml", "application/xml"}, 45 {".json", "application/json"}, 46 {".jpg", "image/jpeg"}, 47 {".jpeg", "image/jpeg"}, 48 // dev tools don't care about map type, setting to json causes 49 // browser to show as text 50 // https://stackoverflow.com/questions/19911929/what-mime-type-should-i-use-for-javascript-source-map-files 51 {".map", "application/json"}}}; 52 53 std::filesystem::path rootpath{"/usr/share/www/"}; 54 55 std::error_code ec; 56 57 std::filesystem::recursive_directory_iterator dirIter(rootpath, ec); 58 if (ec) 59 { 60 BMCWEB_LOG_ERROR( 61 "Unable to find or open {} static file hosting disabled", 62 rootpath.string()); 63 return; 64 } 65 66 // In certain cases, we might have both a gzipped version of the file AND a 67 // non-gzipped version. To avoid duplicated routes, we need to make sure we 68 // get the gzipped version first. Because the gzipped path should be longer 69 // than the non gzipped path, if we sort in descending order, we should be 70 // guaranteed to get the gzip version first. 71 std::vector<std::filesystem::directory_entry> paths( 72 std::filesystem::begin(dirIter), std::filesystem::end(dirIter)); 73 std::sort(paths.rbegin(), paths.rend()); 74 75 for (const std::filesystem::directory_entry& dir : paths) 76 { 77 const std::filesystem::path& absolutePath = dir.path(); 78 std::filesystem::path relativePath{ 79 absolutePath.string().substr(rootpath.string().size() - 1)}; 80 if (std::filesystem::is_directory(dir)) 81 { 82 // don't recurse into hidden directories or symlinks 83 if (dir.path().filename().string().starts_with(".") || 84 std::filesystem::is_symlink(dir)) 85 { 86 dirIter.disable_recursion_pending(); 87 } 88 } 89 else if (std::filesystem::is_regular_file(dir)) 90 { 91 std::string extension = relativePath.extension(); 92 std::filesystem::path webpath = relativePath; 93 const char* contentEncoding = nullptr; 94 95 if (extension == ".gz") 96 { 97 webpath = webpath.replace_extension(""); 98 // Use the non-gzip version for determining content type 99 extension = webpath.extension().string(); 100 contentEncoding = "gzip"; 101 } 102 103 if (webpath.filename().string().starts_with("index.")) 104 { 105 webpath = webpath.parent_path(); 106 if (webpath.string().empty() || webpath.string().back() != '/') 107 { 108 // insert the non-directory version of this path 109 webroutes::routes.insert(webpath); 110 webpath += "/"; 111 } 112 } 113 114 std::pair<boost::container::flat_set<std::string>::iterator, bool> 115 inserted = webroutes::routes.insert(webpath); 116 117 if (!inserted.second) 118 { 119 // Got a duplicated path. This is expected in certain 120 // situations 121 BMCWEB_LOG_DEBUG("Got duplicated path {}", webpath.string()); 122 continue; 123 } 124 const char* contentType = nullptr; 125 126 for (const std::pair<const char*, const char*>& ext : contentTypes) 127 { 128 if (ext.first == nullptr || ext.second == nullptr) 129 { 130 continue; 131 } 132 if (extension == ext.first) 133 { 134 contentType = ext.second; 135 } 136 } 137 138 if (contentType == nullptr) 139 { 140 BMCWEB_LOG_ERROR( 141 "Cannot determine content-type for {} with extension {}", 142 absolutePath.string(), extension); 143 } 144 145 if (webpath == "/") 146 { 147 forward_unauthorized::hasWebuiRoute = true; 148 } 149 150 app.routeDynamic(webpath)( 151 [absolutePath, contentType, contentEncoding]( 152 const crow::Request&, 153 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 154 if (contentType != nullptr) 155 { 156 asyncResp->res.addHeader( 157 boost::beast::http::field::content_type, contentType); 158 } 159 160 if (contentEncoding != nullptr) 161 { 162 asyncResp->res.addHeader( 163 boost::beast::http::field::content_encoding, 164 contentEncoding); 165 } 166 167 // res.set_header("Cache-Control", "public, max-age=86400"); 168 std::ifstream inf(absolutePath); 169 if (!inf) 170 { 171 BMCWEB_LOG_DEBUG("failed to read file"); 172 asyncResp->res.result( 173 boost::beast::http::status::internal_server_error); 174 return; 175 } 176 177 asyncResp->res.body() = {std::istreambuf_iterator<char>(inf), 178 std::istreambuf_iterator<char>()}; 179 }); 180 } 181 } 182 } 183 } // namespace webassets 184 } // namespace crow 185