blob: 816d5430c0b28e0e6661e88839e022b788f6cd66 [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>
10#include <boost/algorithm/string/replace.hpp>
11#include <boost/container/flat_set.hpp>
12
13namespace crow {
14namespace webassets {
15
16namespace filesystem = std::experimental::filesystem;
Ed Tanousba9f9a62017-10-11 16:40:35 -070017
Ed Tanous55c7b7a2018-05-22 15:27:24 -070018struct CmpStr {
Ed Tanousba9f9a62017-10-11 16:40:35 -070019 bool operator()(const char* a, const char* b) const {
20 return std::strcmp(a, b) < 0;
21 }
22};
Ed Tanous911ac312017-08-15 09:37:42 -070023
24static boost::container::flat_set<std::string> routes;
25
26template <typename... Middlewares>
Ed Tanous55c7b7a2018-05-22 15:27:24 -070027void requestRoutes(Crow<Middlewares...>& app) {
28 const static boost::container::flat_map<const char*, const char*, CmpStr>
29 contentTypes{
Ed Tanousba9f9a62017-10-11 16:40:35 -070030 {{".css", "text/css;charset=UTF-8"},
31 {".html", "text/html;charset=UTF-8"},
32 {".js", "text/html;charset=UTF-8"},
33 {".png", "image/png;charset=UTF-8"},
34 {".woff", "application/x-font-woff"},
35 {".woff2", "application/x-font-woff2"},
Ed Tanous5b6a1f92018-02-02 11:05:21 -080036 {".gif", "image/gif"},
37 {".ico", "image/x-icon"},
Ed Tanousba9f9a62017-10-11 16:40:35 -070038 {".ttf", "application/x-font-ttf"},
39 {".svg", "image/svg+xml"},
40 {".eot", "application/vnd.ms-fontobject"},
41 {".xml", "application/xml"},
Ed Tanous257f5792018-03-17 14:40:09 -070042 {".jpg", "image/jpeg"},
43 {".jpeg", "image/jpeg"},
Ed Tanousb39db7142018-08-14 11:20:42 -070044 {".json", "application/json"},
Ed Tanousba9f9a62017-10-11 16:40:35 -070045 // dev tools don't care about map type, setting to json causes
46 // browser to show as text
47 // https://stackoverflow.com/questions/19911929/what-mime-type-should-i-use-for-javascript-source-map-files
48 {".map", "application/json"}}};
Ed Tanous9dc2c4d2018-03-07 14:51:27 -080049 filesystem::path rootpath{"/usr/share/www/"};
Ed Tanous55c7b7a2018-05-22 15:27:24 -070050 filesystem::recursive_directory_iterator dirIter(rootpath);
Ed Tanous9dc2c4d2018-03-07 14:51:27 -080051
Ed Tanous55c7b7a2018-05-22 15:27:24 -070052 for (const filesystem::directory_entry& dir : dirIter) {
53 filesystem::path absolutePath = dir.path();
54 filesystem::path relativePath{
55 absolutePath.string().substr(rootpath.string().size() - 1)};
Ed Tanous911ac312017-08-15 09:37:42 -070056 // make sure we don't recurse into certain directories
57 // note: maybe check for is_directory() here as well...
Ed Tanous5b6a1f92018-02-02 11:05:21 -080058
Ed Tanous911ac312017-08-15 09:37:42 -070059 if (filesystem::is_directory(dir)) {
60 // don't recurse into hidden directories or symlinks
61 if (boost::starts_with(dir.path().filename().string(), ".") ||
62 filesystem::is_symlink(dir)) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -070063 dirIter.disable_recursion_pending();
Ed Tanous911ac312017-08-15 09:37:42 -070064 }
65 } else if (filesystem::is_regular_file(dir)) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -070066 std::string extension = relativePath.extension();
67 filesystem::path webpath = relativePath;
68 const char* contentEncoding = nullptr;
Ed Tanous5b6a1f92018-02-02 11:05:21 -080069
70 if (extension == ".gz") {
Ed Tanous911ac312017-08-15 09:37:42 -070071 webpath = webpath.replace_extension("");
Ed Tanous5b6a1f92018-02-02 11:05:21 -080072 // Use the non-gzip version for determining content type
73 extension = webpath.extension().string();
Ed Tanous55c7b7a2018-05-22 15:27:24 -070074 contentEncoding = "gzip";
Ed Tanous911ac312017-08-15 09:37:42 -070075 }
76
Ed Tanous9dc2c4d2018-03-07 14:51:27 -080077 if (boost::starts_with(webpath.filename().string(), "index.")) {
Ed Tanous911ac312017-08-15 09:37:42 -070078 webpath = webpath.parent_path();
Ed Tanous9dc2c4d2018-03-07 14:51:27 -080079 if (webpath.string().size() == 0 || webpath.string().back() != '/') {
Ed Tanous15aab542018-03-28 10:29:31 -070080 // insert the non-directory version of this path
81 routes.insert(webpath);
Ed Tanousba9f9a62017-10-11 16:40:35 -070082 webpath += "/";
83 }
Ed Tanous911ac312017-08-15 09:37:42 -070084 }
85
Ed Tanous9dc2c4d2018-03-07 14:51:27 -080086 routes.insert(webpath);
Ed Tanous55c7b7a2018-05-22 15:27:24 -070087 const char* contentType = nullptr;
Ed Tanous911ac312017-08-15 09:37:42 -070088
Ed Tanous55c7b7a2018-05-22 15:27:24 -070089 auto contentTypeIt = contentTypes.find(extension.c_str());
90 if (contentTypeIt == contentTypes.end()) {
91 BMCWEB_LOG_ERROR << "Cannot determine content-type for " << absolutePath
92 << " with extension " << extension;
Ed Tanous9dc2c4d2018-03-07 14:51:27 -080093 } else {
Ed Tanous55c7b7a2018-05-22 15:27:24 -070094 contentType = contentTypeIt->second;
Ed Tanous5b6a1f92018-02-02 11:05:21 -080095 }
96
Ed Tanous55c7b7a2018-05-22 15:27:24 -070097 app.routeDynamic(webpath)(
98 [absolutePath, contentType, contentEncoding](const crow::Request& req,
99 crow::Response& res) {
100 if (contentType != nullptr) {
101 res.addHeader("Content-Type", contentType);
Ed Tanous911ac312017-08-15 09:37:42 -0700102 }
Ed Tanousba9f9a62017-10-11 16:40:35 -0700103
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700104 if (contentEncoding != nullptr) {
105 res.addHeader("Content-Encoding", contentEncoding);
Ed Tanousba9f9a62017-10-11 16:40:35 -0700106 }
107
Ed Tanous911ac312017-08-15 09:37:42 -0700108 // res.set_header("Cache-Control", "public, max-age=86400");
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700109 std::ifstream inf(absolutePath);
Ed Tanous911ac312017-08-15 09:37:42 -0700110 if (!inf) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700111 BMCWEB_LOG_DEBUG << "failed to read file";
Ed Tanouse0d918b2018-03-27 17:41:04 -0700112 res.result(boost::beast::http::status::internal_server_error);
Ed Tanous911ac312017-08-15 09:37:42 -0700113 res.end();
114 return;
115 }
116
Ed Tanouse0d918b2018-03-27 17:41:04 -0700117 res.body() = {std::istreambuf_iterator<char>(inf),
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700118 std::istreambuf_iterator<char>()};
Ed Tanous911ac312017-08-15 09:37:42 -0700119 res.end();
120 });
121 }
122 }
Ed Tanous911ac312017-08-15 09:37:42 -0700123} // namespace webassets
Ed Tanous5b6a1f92018-02-02 11:05:21 -0800124} // namespace webassets
125} // namespace crow