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 << "Unable to find or open " << rootpath.string() 61 << " static file hosting disabled"; 62 return; 63 } 64 65 // In certain cases, we might have both a gzipped version of the file AND a 66 // non-gzipped version. To avoid duplicated routes, we need to make sure we 67 // get the gzipped version first. Because the gzipped path should be longer 68 // than the non gzipped path, if we sort in descending order, we should be 69 // guaranteed to get the gzip version first. 70 std::vector<std::filesystem::directory_entry> paths( 71 std::filesystem::begin(dirIter), std::filesystem::end(dirIter)); 72 std::sort(paths.rbegin(), paths.rend()); 73 74 for (const std::filesystem::directory_entry& dir : paths) 75 { 76 const std::filesystem::path& absolutePath = dir.path(); 77 std::filesystem::path relativePath{ 78 absolutePath.string().substr(rootpath.string().size() - 1)}; 79 if (std::filesystem::is_directory(dir)) 80 { 81 // don't recurse into hidden directories or symlinks 82 if (dir.path().filename().string().starts_with(".") || 83 std::filesystem::is_symlink(dir)) 84 { 85 dirIter.disable_recursion_pending(); 86 } 87 } 88 else if (std::filesystem::is_regular_file(dir)) 89 { 90 std::string extension = relativePath.extension(); 91 std::filesystem::path webpath = relativePath; 92 const char* contentEncoding = nullptr; 93 94 if (extension == ".gz") 95 { 96 webpath = webpath.replace_extension(""); 97 // Use the non-gzip version for determining content type 98 extension = webpath.extension().string(); 99 contentEncoding = "gzip"; 100 } 101 102 if (webpath.filename().string().starts_with("index.")) 103 { 104 webpath = webpath.parent_path(); 105 if (webpath.string().empty() || webpath.string().back() != '/') 106 { 107 // insert the non-directory version of this path 108 webroutes::routes.insert(webpath); 109 webpath += "/"; 110 } 111 } 112 113 std::pair<boost::container::flat_set<std::string>::iterator, bool> 114 inserted = webroutes::routes.insert(webpath); 115 116 if (!inserted.second) 117 { 118 // Got a duplicated path. This is expected in certain 119 // situations 120 BMCWEB_LOG_DEBUG << "Got duplicated path " << webpath.string(); 121 continue; 122 } 123 const char* contentType = nullptr; 124 125 for (const std::pair<const char*, const char*>& ext : contentTypes) 126 { 127 if (ext.first == nullptr || ext.second == nullptr) 128 { 129 continue; 130 } 131 if (extension == ext.first) 132 { 133 contentType = ext.second; 134 } 135 } 136 137 if (contentType == nullptr) 138 { 139 BMCWEB_LOG_ERROR << "Cannot determine content-type for " 140 << absolutePath.string() << " with extension " 141 << extension; 142 } 143 144 if (webpath == "/") 145 { 146 forward_unauthorized::hasWebuiRoute = true; 147 } 148 149 app.routeDynamic(webpath)( 150 [absolutePath, contentType, contentEncoding]( 151 const crow::Request&, 152 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 153 if (contentType != nullptr) 154 { 155 asyncResp->res.addHeader( 156 boost::beast::http::field::content_type, contentType); 157 } 158 159 if (contentEncoding != nullptr) 160 { 161 asyncResp->res.addHeader( 162 boost::beast::http::field::content_encoding, 163 contentEncoding); 164 } 165 166 // res.set_header("Cache-Control", "public, max-age=86400"); 167 std::ifstream inf(absolutePath); 168 if (!inf) 169 { 170 BMCWEB_LOG_DEBUG << "failed to read file"; 171 asyncResp->res.result( 172 boost::beast::http::status::internal_server_error); 173 return; 174 } 175 176 asyncResp->res.body() = {std::istreambuf_iterator<char>(inf), 177 std::istreambuf_iterator<char>()}; 178 }); 179 } 180 } 181 } 182 } // namespace webassets 183 } // namespace crow 184