blob: 21da368155e2ed9fc139cd5e330e8b3c06674713 [file] [log] [blame]
Ed Tanous911ac312017-08-15 09:37:42 -07001#pragma once
2
3#include <experimental/filesystem>
4#include <fstream>
5#include <string>
6#include <crow/app.h>
7#include <crow/http_request.h>
8#include <crow/http_response.h>
9#include <crow/routing.h>
Ed Tanous5b6a1f92018-02-02 11:05:21 -080010#include <crow/http_codes.h>
Ed Tanous911ac312017-08-15 09:37:42 -070011#include <boost/algorithm/string/replace.hpp>
12#include <boost/container/flat_set.hpp>
13
14namespace crow {
15namespace webassets {
16
17namespace filesystem = std::experimental::filesystem;
Ed Tanousba9f9a62017-10-11 16:40:35 -070018
19struct cmp_str {
20 bool operator()(const char* a, const char* b) const {
21 return std::strcmp(a, b) < 0;
22 }
23};
Ed Tanous911ac312017-08-15 09:37:42 -070024
25static boost::container::flat_set<std::string> routes;
26
27template <typename... Middlewares>
28void request_routes(Crow<Middlewares...>& app) {
Ed Tanousba9f9a62017-10-11 16:40:35 -070029 const static boost::container::flat_map<const char*, const char*, cmp_str>
30 content_types{
31 {{".css", "text/css;charset=UTF-8"},
32 {".html", "text/html;charset=UTF-8"},
33 {".js", "text/html;charset=UTF-8"},
34 {".png", "image/png;charset=UTF-8"},
35 {".woff", "application/x-font-woff"},
36 {".woff2", "application/x-font-woff2"},
Ed Tanous5b6a1f92018-02-02 11:05:21 -080037 {".gif", "image/gif"},
38 {".ico", "image/x-icon"},
Ed Tanousba9f9a62017-10-11 16:40:35 -070039 {".ttf", "application/x-font-ttf"},
40 {".svg", "image/svg+xml"},
41 {".eot", "application/vnd.ms-fontobject"},
42 {".xml", "application/xml"},
43 // dev tools don't care about map type, setting to json causes
44 // browser to show as text
45 // https://stackoverflow.com/questions/19911929/what-mime-type-should-i-use-for-javascript-source-map-files
46 {".map", "application/json"}}};
Ed Tanous911ac312017-08-15 09:37:42 -070047 auto rootpath = filesystem::path("/usr/share/www/");
48 auto dir_iter = filesystem::recursive_directory_iterator(rootpath);
49 for (auto& dir : dir_iter) {
50 auto absolute_path = dir.path();
51 auto relative_path = filesystem::path(
52 absolute_path.string().substr(rootpath.string().size() - 1));
53 // make sure we don't recurse into certain directories
54 // note: maybe check for is_directory() here as well...
Ed Tanous5b6a1f92018-02-02 11:05:21 -080055
Ed Tanous911ac312017-08-15 09:37:42 -070056 if (filesystem::is_directory(dir)) {
57 // don't recurse into hidden directories or symlinks
58 if (boost::starts_with(dir.path().filename().string(), ".") ||
59 filesystem::is_symlink(dir)) {
60 dir_iter.disable_recursion_pending();
61 }
62 } else if (filesystem::is_regular_file(dir)) {
Ed Tanous5b6a1f92018-02-02 11:05:21 -080063 std::string extension = relative_path.extension();
Ed Tanous911ac312017-08-15 09:37:42 -070064 auto webpath = relative_path;
Ed Tanous5b6a1f92018-02-02 11:05:21 -080065 const char* content_encoding = nullptr;
Ed Tanous911ac312017-08-15 09:37:42 -070066 bool is_gzip = false;
Ed Tanous5b6a1f92018-02-02 11:05:21 -080067
68 if (webpath.filename() == "$metadata") {
69 // TODO, this endpoint should really be generated based on the redfish
70 // data. Once that is done, remove the type hardcode.
71 extension = ".xml";
72 }
73
74 if (extension == ".gz") {
Ed Tanous911ac312017-08-15 09:37:42 -070075 webpath = webpath.replace_extension("");
Ed Tanous5b6a1f92018-02-02 11:05:21 -080076 // Use the non-gzip version for determining content type
77 extension = webpath.extension().string();
78 content_encoding = "gzip";
Ed Tanous911ac312017-08-15 09:37:42 -070079 is_gzip = true;
80 }
81
82 if (webpath.filename() == "index.html") {
83 webpath = webpath.parent_path();
Ed Tanousba9f9a62017-10-11 16:40:35 -070084 if (webpath.string() != "/") {
85 webpath += "/";
86 }
Ed Tanous911ac312017-08-15 09:37:42 -070087 }
88
89 routes.insert(webpath.string());
90
Ed Tanous911ac312017-08-15 09:37:42 -070091 const char* content_type = nullptr;
Ed Tanous911ac312017-08-15 09:37:42 -070092
Ed Tanous5b6a1f92018-02-02 11:05:21 -080093 // if the filename has an extension, look up the type
94 if (extension != "") {
95 auto content_type_it = content_types.find(extension.c_str());
96
97 if (content_type_it == content_types.end()) {
98 CROW_LOG_ERROR << "Cannot determine content-type for " << webpath;
99 continue;
100 }
101 content_type = content_type_it->second;
102 }
103
104 app.route_dynamic(webpath.string())(
105 [absolute_path, content_type, content_encoding](
106 const crow::request& req, crow::response& res) {
Ed Tanous911ac312017-08-15 09:37:42 -0700107 if (content_type != nullptr) {
Ed Tanous5b6a1f92018-02-02 11:05:21 -0800108 res.add_header("Content-Type", content_type);
Ed Tanous911ac312017-08-15 09:37:42 -0700109 }
Ed Tanousba9f9a62017-10-11 16:40:35 -0700110
Ed Tanous5b6a1f92018-02-02 11:05:21 -0800111 if (content_encoding != nullptr) {
112 res.add_header("Content-Encoding", content_encoding);
Ed Tanousba9f9a62017-10-11 16:40:35 -0700113 }
114
Ed Tanous911ac312017-08-15 09:37:42 -0700115 // res.set_header("Cache-Control", "public, max-age=86400");
Ed Tanous5b6a1f92018-02-02 11:05:21 -0800116 std::ifstream inf(absolute_path);
Ed Tanous911ac312017-08-15 09:37:42 -0700117 if (!inf) {
118 CROW_LOG_DEBUG << "failed to read file";
Ed Tanous5b6a1f92018-02-02 11:05:21 -0800119 res.code = static_cast<int>(HttpRespCode::NOT_FOUND);
120 res.code = static_cast<int>(HttpRespCode::INTERNAL_ERROR);
Ed Tanous911ac312017-08-15 09:37:42 -0700121 res.end();
122 return;
123 }
124
Ed Tanous5b6a1f92018-02-02 11:05:21 -0800125 res.body = {std::istreambuf_iterator<char>(inf),
126 std::istreambuf_iterator<char>()};
Ed Tanous911ac312017-08-15 09:37:42 -0700127 res.end();
128 });
129 }
130 }
Ed Tanous911ac312017-08-15 09:37:42 -0700131} // namespace webassets
Ed Tanous5b6a1f92018-02-02 11:05:21 -0800132} // namespace webassets
133} // namespace crow