1 #pragma once 2 3 #include "webroutes.hpp" 4 5 #include <app.hpp> 6 #include <boost/algorithm/string/replace.hpp> 7 #include <boost/container/flat_set.hpp> 8 #include <http_request.hpp> 9 #include <http_response.hpp> 10 #include <routing.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 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 (boost::starts_with(dir.path().filename().string(), ".") || 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 (boost::starts_with(webpath.filename().string(), "index.")) 103 { 104 webpath = webpath.parent_path(); 105 if (webpath.string().size() == 0 || 106 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; 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 << "Cannot determine content-type for " 141 << absolutePath << " with extension " 142 << extension; 143 } 144 145 app.routeDynamic(webpath)( 146 [absolutePath, contentType, contentEncoding]( 147 const crow::Request&, 148 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 149 if (contentType != nullptr) 150 { 151 asyncResp->res.addHeader("Content-Type", contentType); 152 } 153 154 if (contentEncoding != nullptr) 155 { 156 asyncResp->res.addHeader("Content-Encoding", 157 contentEncoding); 158 } 159 160 // res.set_header("Cache-Control", "public, max-age=86400"); 161 std::ifstream inf(absolutePath); 162 if (!inf) 163 { 164 BMCWEB_LOG_DEBUG << "failed to read file"; 165 asyncResp->res.result( 166 boost::beast::http::status::internal_server_error); 167 return; 168 } 169 170 asyncResp->res.body() = { 171 std::istreambuf_iterator<char>(inf), 172 std::istreambuf_iterator<char>()}; 173 }); 174 } 175 } 176 } 177 } // namespace webassets 178 } // namespace crow 179