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 {".json", "application/json"}, 49 // dev tools don't care about map type, setting to json causes 50 // browser to show as text 51 // https://stackoverflow.com/questions/19911929/what-mime-type-should-i-use-for-javascript-source-map-files 52 {".map", "application/json"}}}; 53 54 std::filesystem::path rootpath{"/usr/share/www/"}; 55 56 std::error_code ec; 57 58 std::filesystem::recursive_directory_iterator dirIter(rootpath, ec); 59 if (ec) 60 { 61 BMCWEB_LOG_ERROR << "Unable to find or open " << rootpath 62 << " static file hosting disabled"; 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 (boost::starts_with(dir.path().filename().string(), ".") || 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 (boost::starts_with(webpath.filename().string(), "index.")) 104 { 105 webpath = webpath.parent_path(); 106 if (webpath.string().size() == 0 || 107 webpath.string().back() != '/') 108 { 109 // insert the non-directory version of this path 110 webroutes::routes.insert(webpath); 111 webpath += "/"; 112 } 113 } 114 115 std::pair<boost::container::flat_set<std::string>::iterator, bool> 116 inserted = webroutes::routes.insert(webpath); 117 118 if (!inserted.second) 119 { 120 // Got a duplicated path. This is expected in certain 121 // situations 122 BMCWEB_LOG_DEBUG << "Got duplicated path " << webpath; 123 continue; 124 } 125 const char* contentType = nullptr; 126 127 for (const std::pair<const char*, const char*>& ext : contentTypes) 128 { 129 if (ext.first == nullptr || ext.second == nullptr) 130 { 131 continue; 132 } 133 if (extension == ext.first) 134 { 135 contentType = ext.second; 136 } 137 } 138 139 if (contentType == nullptr) 140 { 141 BMCWEB_LOG_ERROR << "Cannot determine content-type for " 142 << absolutePath << " with extension " 143 << extension; 144 } 145 146 app.routeDynamic(webpath)( 147 [absolutePath, contentType, 148 contentEncoding](const crow::Request&, crow::Response& res) { 149 if (contentType != nullptr) 150 { 151 res.addHeader("Content-Type", contentType); 152 } 153 154 if (contentEncoding != nullptr) 155 { 156 res.addHeader("Content-Encoding", contentEncoding); 157 } 158 159 // res.set_header("Cache-Control", "public, max-age=86400"); 160 std::ifstream inf(absolutePath); 161 if (!inf) 162 { 163 BMCWEB_LOG_DEBUG << "failed to read file"; 164 res.result( 165 boost::beast::http::status::internal_server_error); 166 res.end(); 167 return; 168 } 169 170 res.body() = {std::istreambuf_iterator<char>(inf), 171 std::istreambuf_iterator<char>()}; 172 res.end(); 173 }); 174 } 175 } 176 } 177 } // namespace webassets 178 } // namespace crow 179