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