blob: c2a6851e118f34d948619ff833b69e1998759dbf [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
Ed Tanous23a21a12020-07-25 04:45:05 +000030inline void requestRoutes(App& app)
Ed Tanous1abe55e2018-09-05 08:30:59 -070031{
Ed Tanous218bd472019-10-24 10:10:07 -070032 constexpr static std::array<std::pair<const char*, const char*>, 17>
Ed Tanous1abe55e2018-09-05 08:30:59 -070033 contentTypes{
34 {{".css", "text/css;charset=UTF-8"},
35 {".html", "text/html;charset=UTF-8"},
Ed Tanous7e513892018-12-20 07:11:04 -080036 {".js", "application/javascript;charset=UTF-8"},
Ed Tanous1abe55e2018-09-05 08:30:59 -070037 {".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"},
Ed Tanous683f7272018-07-26 12:47:19 -070046 {".json", "application/json"},
Ed Tanous1abe55e2018-09-05 08:30:59 -070047 {".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"}}};
Ed Tanousa47f32b2019-10-24 10:08:04 -070054
55 std::filesystem::path rootpath{"/usr/share/www/"};
56 std::filesystem::recursive_directory_iterator dirIter(rootpath);
Ed Tanous1abe55e2018-09-05 08:30:59 -070057 // 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
Ed Tanous8b156452018-10-12 11:09:26 -070060 // than the non gzipped path, if we sort in descending order, we should be
Ed Tanous1abe55e2018-09-05 08:30:59 -070061 // guaranteed to get the gzip version first.
Ed Tanousa47f32b2019-10-24 10:08:04 -070062 std::vector<std::filesystem::directory_entry> paths(
63 std::filesystem::begin(dirIter), std::filesystem::end(dirIter));
Ed Tanous1abe55e2018-09-05 08:30:59 -070064 std::sort(paths.rbegin(), paths.rend());
Ed Tanous9dc2c4d2018-03-07 14:51:27 -080065
Ed Tanousa47f32b2019-10-24 10:08:04 -070066 for (const std::filesystem::directory_entry& dir : paths)
Ed Tanous1abe55e2018-09-05 08:30:59 -070067 {
Ed Tanous3174e4d2020-10-07 11:41:22 -070068 const std::filesystem::path& absolutePath = dir.path();
Ed Tanousa47f32b2019-10-24 10:08:04 -070069 std::filesystem::path relativePath{
Ed Tanous1abe55e2018-09-05 08:30:59 -070070 absolutePath.string().substr(rootpath.string().size() - 1)};
Ed Tanousa47f32b2019-10-24 10:08:04 -070071 if (std::filesystem::is_directory(dir))
Ed Tanous1abe55e2018-09-05 08:30:59 -070072 {
73 // don't recurse into hidden directories or symlinks
74 if (boost::starts_with(dir.path().filename().string(), ".") ||
Ed Tanousa47f32b2019-10-24 10:08:04 -070075 std::filesystem::is_symlink(dir))
Ed Tanous1abe55e2018-09-05 08:30:59 -070076 {
77 dirIter.disable_recursion_pending();
78 }
Ed Tanousba9f9a62017-10-11 16:40:35 -070079 }
Ed Tanousa47f32b2019-10-24 10:08:04 -070080 else if (std::filesystem::is_regular_file(dir))
Ed Tanous1abe55e2018-09-05 08:30:59 -070081 {
82 std::string extension = relativePath.extension();
Ed Tanousa47f32b2019-10-24 10:08:04 -070083 std::filesystem::path webpath = relativePath;
Ed Tanous1abe55e2018-09-05 08:30:59 -070084 const char* contentEncoding = nullptr;
Ed Tanous911ac312017-08-15 09:37:42 -070085
Ed Tanous1abe55e2018-09-05 08:30:59 -070086 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";
Ed Tanous911ac312017-08-15 09:37:42 -070092 }
Ed Tanousba9f9a62017-10-11 16:40:35 -070093
Ed Tanous1abe55e2018-09-05 08:30:59 -070094 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
James Feist3909dc82020-04-03 10:58:55 -0700101 webroutes::routes.insert(webpath);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700102 webpath += "/";
103 }
Ed Tanousba9f9a62017-10-11 16:40:35 -0700104 }
105
Ed Tanous1abe55e2018-09-05 08:30:59 -0700106 std::pair<boost::container::flat_set<std::string>::iterator, bool>
James Feist3909dc82020-04-03 10:58:55 -0700107 inserted = webroutes::routes.insert(webpath);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700108
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
Ed Tanous218bd472019-10-24 10:10:07 -0700118 for (const std::pair<const char*, const char*>& ext : contentTypes)
119 {
120 if (ext.first == nullptr || ext.second == nullptr)
121 {
122 continue;
123 }
124 if (extension == ext.first)
125 {
126 contentType = ext.second;
127 }
128 }
129
130 if (contentType == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700131 {
132 BMCWEB_LOG_ERROR << "Cannot determine content-type for "
133 << absolutePath << " with extension "
134 << extension;
135 }
Ed Tanous911ac312017-08-15 09:37:42 -0700136
Ed Tanous1abe55e2018-09-05 08:30:59 -0700137 app.routeDynamic(webpath)(
Ed Tanouscb13a392020-07-25 19:02:03 +0000138 [absolutePath, contentType,
139 contentEncoding](const crow::Request&, crow::Response& res) {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700140 if (contentType != nullptr)
141 {
142 res.addHeader("Content-Type", contentType);
143 }
144
145 if (contentEncoding != nullptr)
146 {
147 res.addHeader("Content-Encoding", contentEncoding);
148 }
149
150 // res.set_header("Cache-Control", "public, max-age=86400");
151 std::ifstream inf(absolutePath);
152 if (!inf)
153 {
154 BMCWEB_LOG_DEBUG << "failed to read file";
155 res.result(
156 boost::beast::http::status::internal_server_error);
157 res.end();
158 return;
159 }
160
161 res.body() = {std::istreambuf_iterator<char>(inf),
162 std::istreambuf_iterator<char>()};
163 res.end();
164 });
165 }
Ed Tanous911ac312017-08-15 09:37:42 -0700166 }
Ed Tanous99dc9c72019-10-24 10:09:03 -0700167}
Ed Tanous1abe55e2018-09-05 08:30:59 -0700168} // namespace webassets
169} // namespace crow