blob: 7f1c1f5701d72fd727b0b0d40478f4d8b65ccafc [file] [log] [blame]
Ed Tanous911ac312017-08-15 09:37:42 -07001#pragma once
2
Ed Tanous911ac312017-08-15 09:37:42 -07003#include <crow/app.h>
4#include <crow/http_request.h>
5#include <crow/http_response.h>
6#include <crow/routing.h>
Ed Tanous1abe55e2018-09-05 08:30:59 -07007
Ed Tanous911ac312017-08-15 09:37:42 -07008#include <boost/algorithm/string/replace.hpp>
9#include <boost/container/flat_set.hpp>
Ed Tanous1abe55e2018-09-05 08:30:59 -070010#include <experimental/filesystem>
11#include <fstream>
12#include <string>
Ed Tanous911ac312017-08-15 09:37:42 -070013
Ed Tanous1abe55e2018-09-05 08:30:59 -070014namespace crow
15{
16namespace webassets
17{
Ed Tanous911ac312017-08-15 09:37:42 -070018
19namespace filesystem = std::experimental::filesystem;
Ed Tanousba9f9a62017-10-11 16:40:35 -070020
Ed Tanous1abe55e2018-09-05 08:30:59 -070021struct CmpStr
22{
23 bool operator()(const char* a, const char* b) const
24 {
25 return std::strcmp(a, b) < 0;
26 }
Ed Tanousba9f9a62017-10-11 16:40:35 -070027};
Ed Tanous911ac312017-08-15 09:37:42 -070028
29static boost::container::flat_set<std::string> routes;
30
Ed Tanous1abe55e2018-09-05 08:30:59 -070031template <typename... Middlewares> void requestRoutes(Crow<Middlewares...>& app)
32{
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"},
37 {".js", "text/html;charset=UTF-8"},
38 {".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"},
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 filesystem::path rootpath{"/usr/share/www/"};
55 filesystem::recursive_directory_iterator dirIter(rootpath);
56 // In certain cases, we might have both a gzipped version of the file AND a
57 // non-gzipped version. To avoid duplicated routes, we need to make sure we
58 // get the gzipped version first. Because the gzipped path should be longer
59 // than the non gzipped path, if we sort in Ascending order, we should be
60 // guaranteed to get the gzip version first.
61 std::vector<filesystem::directory_entry> paths(filesystem::begin(dirIter),
62 filesystem::end(dirIter));
63 std::sort(paths.rbegin(), paths.rend());
Ed Tanous9dc2c4d2018-03-07 14:51:27 -080064
Ed Tanous1abe55e2018-09-05 08:30:59 -070065 for (const filesystem::directory_entry& dir : paths)
66 {
67 filesystem::path absolutePath = dir.path();
68 filesystem::path relativePath{
69 absolutePath.string().substr(rootpath.string().size() - 1)};
70 if (filesystem::is_directory(dir))
71 {
72 // don't recurse into hidden directories or symlinks
73 if (boost::starts_with(dir.path().filename().string(), ".") ||
74 filesystem::is_symlink(dir))
75 {
76 dirIter.disable_recursion_pending();
77 }
Ed Tanousba9f9a62017-10-11 16:40:35 -070078 }
Ed Tanous1abe55e2018-09-05 08:30:59 -070079 else if (filesystem::is_regular_file(dir))
80 {
81 std::string extension = relativePath.extension();
82 filesystem::path webpath = relativePath;
83 const char* contentEncoding = nullptr;
Ed Tanous911ac312017-08-15 09:37:42 -070084
Ed Tanous1abe55e2018-09-05 08:30:59 -070085 if (extension == ".gz")
86 {
87 webpath = webpath.replace_extension("");
88 // Use the non-gzip version for determining content type
89 extension = webpath.extension().string();
90 contentEncoding = "gzip";
Ed Tanous911ac312017-08-15 09:37:42 -070091 }
Ed Tanousba9f9a62017-10-11 16:40:35 -070092
Ed Tanous1abe55e2018-09-05 08:30:59 -070093 if (boost::starts_with(webpath.filename().string(), "index."))
94 {
95 webpath = webpath.parent_path();
96 if (webpath.string().size() == 0 ||
97 webpath.string().back() != '/')
98 {
99 // insert the non-directory version of this path
100 routes.insert(webpath);
101 webpath += "/";
102 }
Ed Tanousba9f9a62017-10-11 16:40:35 -0700103 }
104
Ed Tanous1abe55e2018-09-05 08:30:59 -0700105 std::pair<boost::container::flat_set<std::string>::iterator, bool>
106 inserted = routes.insert(webpath);
107
108 if (!inserted.second)
109 {
110 // Got a duplicated path. This is expected in certain
111 // situations
112 BMCWEB_LOG_DEBUG << "Got duplicated path " << webpath;
113 continue;
114 }
115 const char* contentType = nullptr;
116
117 auto contentTypeIt = contentTypes.find(extension.c_str());
118 if (contentTypeIt == contentTypes.end())
119 {
120 BMCWEB_LOG_ERROR << "Cannot determine content-type for "
121 << absolutePath << " with extension "
122 << extension;
123 }
124 else
125 {
126 contentType = contentTypeIt->second;
Ed Tanous911ac312017-08-15 09:37:42 -0700127 }
128
Ed Tanous1abe55e2018-09-05 08:30:59 -0700129 app.routeDynamic(webpath)(
130 [absolutePath, contentType, contentEncoding](
131 const crow::Request& req, crow::Response& res) {
132 if (contentType != nullptr)
133 {
134 res.addHeader("Content-Type", contentType);
135 }
136
137 if (contentEncoding != nullptr)
138 {
139 res.addHeader("Content-Encoding", contentEncoding);
140 }
141
142 // res.set_header("Cache-Control", "public, max-age=86400");
143 std::ifstream inf(absolutePath);
144 if (!inf)
145 {
146 BMCWEB_LOG_DEBUG << "failed to read file";
147 res.result(
148 boost::beast::http::status::internal_server_error);
149 res.end();
150 return;
151 }
152
153 res.body() = {std::istreambuf_iterator<char>(inf),
154 std::istreambuf_iterator<char>()};
155 res.end();
156 });
157 }
Ed Tanous911ac312017-08-15 09:37:42 -0700158 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700159} // namespace webassets
160} // namespace webassets
161} // namespace crow