blob: bb9ab7f9e6ea2d04b049b92dc1ea618fdccc8748 [file] [log] [blame]
Ed Tanous911ac312017-08-15 09:37:42 -07001#pragma once
2
James Feist3909dc82020-04-03 10:58:55 -07003#include "webroutes.hpp"
4
Ed Tanousc94ad492019-10-10 15:39:33 -07005#include <app.h>
6#include <http_request.h>
7#include <http_response.h>
8#include <routing.h>
Ed Tanous1abe55e2018-09-05 08:30:59 -07009
Ed Tanous911ac312017-08-15 09:37:42 -070010#include <boost/algorithm/string/replace.hpp>
11#include <boost/container/flat_set.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050012
James Feist4418c7f2019-04-15 11:09:15 -070013#include <filesystem>
Ed Tanous1abe55e2018-09-05 08:30:59 -070014#include <fstream>
15#include <string>
Ed Tanous911ac312017-08-15 09:37:42 -070016
Ed Tanous1abe55e2018-09-05 08:30:59 -070017namespace crow
18{
19namespace webassets
20{
Ed Tanous911ac312017-08-15 09:37:42 -070021
Ed Tanous1abe55e2018-09-05 08:30:59 -070022struct CmpStr
23{
24 bool operator()(const char* a, const char* b) const
25 {
26 return std::strcmp(a, b) < 0;
27 }
Ed Tanousba9f9a62017-10-11 16:40:35 -070028};
Ed Tanous911ac312017-08-15 09:37:42 -070029
Gunnar Mills1214b7e2020-06-04 10:11:30 -050030template <typename... Middlewares>
31void requestRoutes(Crow<Middlewares...>& app)
Ed Tanous1abe55e2018-09-05 08:30:59 -070032{
33 const static boost::container::flat_map<const char*, const char*, CmpStr>
34 contentTypes{
35 {{".css", "text/css;charset=UTF-8"},
36 {".html", "text/html;charset=UTF-8"},
Ed Tanous7e513892018-12-20 07:11:04 -080037 {".js", "application/javascript;charset=UTF-8"},
Ed Tanous1abe55e2018-09-05 08:30:59 -070038 {".png", "image/png;charset=UTF-8"},
39 {".woff", "application/x-font-woff"},
40 {".woff2", "application/x-font-woff2"},
41 {".gif", "image/gif"},
42 {".ico", "image/x-icon"},
43 {".ttf", "application/x-font-ttf"},
44 {".svg", "image/svg+xml"},
45 {".eot", "application/vnd.ms-fontobject"},
46 {".xml", "application/xml"},
Ed Tanous683f7272018-07-26 12:47:19 -070047 {".json", "application/json"},
Ed Tanous1abe55e2018-09-05 08:30:59 -070048 {".jpg", "image/jpeg"},
49 {".jpeg", "image/jpeg"},
50 {".json", "application/json"},
51 // dev tools don't care about map type, setting to json causes
52 // browser to show as text
53 // https://stackoverflow.com/questions/19911929/what-mime-type-should-i-use-for-javascript-source-map-files
54 {".map", "application/json"}}};
Ed Tanousa47f32b2019-10-24 10:08:04 -070055
56 std::filesystem::path rootpath{"/usr/share/www/"};
57 std::filesystem::recursive_directory_iterator dirIter(rootpath);
Ed Tanous1abe55e2018-09-05 08:30:59 -070058 // In certain cases, we might have both a gzipped version of the file AND a
59 // non-gzipped version. To avoid duplicated routes, we need to make sure we
60 // get the gzipped version first. Because the gzipped path should be longer
Ed Tanous8b156452018-10-12 11:09:26 -070061 // than the non gzipped path, if we sort in descending order, we should be
Ed Tanous1abe55e2018-09-05 08:30:59 -070062 // guaranteed to get the gzip version first.
Ed Tanousa47f32b2019-10-24 10:08:04 -070063 std::vector<std::filesystem::directory_entry> paths(
64 std::filesystem::begin(dirIter), std::filesystem::end(dirIter));
Ed Tanous1abe55e2018-09-05 08:30:59 -070065 std::sort(paths.rbegin(), paths.rend());
Ed Tanous9dc2c4d2018-03-07 14:51:27 -080066
Ed Tanousa47f32b2019-10-24 10:08:04 -070067 for (const std::filesystem::directory_entry& dir : paths)
Ed Tanous1abe55e2018-09-05 08:30:59 -070068 {
Ed Tanousa47f32b2019-10-24 10:08:04 -070069 std::filesystem::path absolutePath = dir.path();
70 std::filesystem::path relativePath{
Ed Tanous1abe55e2018-09-05 08:30:59 -070071 absolutePath.string().substr(rootpath.string().size() - 1)};
Ed Tanousa47f32b2019-10-24 10:08:04 -070072 if (std::filesystem::is_directory(dir))
Ed Tanous1abe55e2018-09-05 08:30:59 -070073 {
74 // don't recurse into hidden directories or symlinks
75 if (boost::starts_with(dir.path().filename().string(), ".") ||
Ed Tanousa47f32b2019-10-24 10:08:04 -070076 std::filesystem::is_symlink(dir))
Ed Tanous1abe55e2018-09-05 08:30:59 -070077 {
78 dirIter.disable_recursion_pending();
79 }
Ed Tanousba9f9a62017-10-11 16:40:35 -070080 }
Ed Tanousa47f32b2019-10-24 10:08:04 -070081 else if (std::filesystem::is_regular_file(dir))
Ed Tanous1abe55e2018-09-05 08:30:59 -070082 {
83 std::string extension = relativePath.extension();
Ed Tanousa47f32b2019-10-24 10:08:04 -070084 std::filesystem::path webpath = relativePath;
Ed Tanous1abe55e2018-09-05 08:30:59 -070085 const char* contentEncoding = nullptr;
Ed Tanous911ac312017-08-15 09:37:42 -070086
Ed Tanous1abe55e2018-09-05 08:30:59 -070087 if (extension == ".gz")
88 {
89 webpath = webpath.replace_extension("");
90 // Use the non-gzip version for determining content type
91 extension = webpath.extension().string();
92 contentEncoding = "gzip";
Ed Tanous911ac312017-08-15 09:37:42 -070093 }
Ed Tanousba9f9a62017-10-11 16:40:35 -070094
Ed Tanous1abe55e2018-09-05 08:30:59 -070095 if (boost::starts_with(webpath.filename().string(), "index."))
96 {
97 webpath = webpath.parent_path();
98 if (webpath.string().size() == 0 ||
99 webpath.string().back() != '/')
100 {
101 // insert the non-directory version of this path
James Feist3909dc82020-04-03 10:58:55 -0700102 webroutes::routes.insert(webpath);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700103 webpath += "/";
104 }
Ed Tanousba9f9a62017-10-11 16:40:35 -0700105 }
106
Ed Tanous1abe55e2018-09-05 08:30:59 -0700107 std::pair<boost::container::flat_set<std::string>::iterator, bool>
James Feist3909dc82020-04-03 10:58:55 -0700108 inserted = webroutes::routes.insert(webpath);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700109
110 if (!inserted.second)
111 {
112 // Got a duplicated path. This is expected in certain
113 // situations
114 BMCWEB_LOG_DEBUG << "Got duplicated path " << webpath;
115 continue;
116 }
117 const char* contentType = nullptr;
118
119 auto contentTypeIt = contentTypes.find(extension.c_str());
120 if (contentTypeIt == contentTypes.end())
121 {
122 BMCWEB_LOG_ERROR << "Cannot determine content-type for "
123 << absolutePath << " with extension "
124 << extension;
125 }
126 else
127 {
128 contentType = contentTypeIt->second;
Ed Tanous911ac312017-08-15 09:37:42 -0700129 }
130
Ed Tanous1abe55e2018-09-05 08:30:59 -0700131 app.routeDynamic(webpath)(
132 [absolutePath, contentType, contentEncoding](
133 const crow::Request& req, crow::Response& res) {
134 if (contentType != nullptr)
135 {
136 res.addHeader("Content-Type", contentType);
137 }
138
139 if (contentEncoding != nullptr)
140 {
141 res.addHeader("Content-Encoding", contentEncoding);
142 }
143
144 // res.set_header("Cache-Control", "public, max-age=86400");
145 std::ifstream inf(absolutePath);
146 if (!inf)
147 {
148 BMCWEB_LOG_DEBUG << "failed to read file";
149 res.result(
150 boost::beast::http::status::internal_server_error);
151 res.end();
152 return;
153 }
154
155 res.body() = {std::istreambuf_iterator<char>(inf),
156 std::istreambuf_iterator<char>()};
157 res.end();
158 });
159 }
Ed Tanous911ac312017-08-15 09:37:42 -0700160 }
Ed Tanous99dc9c72019-10-24 10:09:03 -0700161}
Ed Tanous1abe55e2018-09-05 08:30:59 -0700162} // namespace webassets
163} // namespace crow