summaryrefslogtreecommitdiff
path: root/include/webassets.hpp
diff options
context:
space:
mode:
authorEd Tanous <ed.tanous@intel.com>2017-10-12 02:40:35 +0300
committerEd Tanous <ed.tanous@intel.com>2017-10-25 00:19:43 +0300
commitba9f9a6cebfbb7a12ecf869afa0cdc5aef9c8372 (patch)
tree67345df8b98aecb87480b827b10e1ca2ece5876a /include/webassets.hpp
parent911ac31759cb7b77a856af8806b4e064d50d7422 (diff)
downloadbmcweb-ba9f9a6cebfbb7a12ecf869afa0cdc5aef9c8372.tar.xz
Update Webserver
Upate get_routes to use the correct constness for its use case crow to set json_mode if json value is populated Delete std::array bytes API due to major efficiency issues. To be replaced with span API in near future Implement a catch block for handlers that can throw exceptions Implement direct handling of routes that end with / to better support redfish. /foo and /foo/ now invoke the same handler insead of issuing a 301 redirect Update nlohmann to latest version Implement one nlohmann endpoint with exceptions disabled Implement first pass at a IBM style rest-dbus interface Fix pam authentication to call dropbear auth methods Implements first pass at redfish interface. Shemas avaialble pass redfish validation 100% Use response json object rather than request json object. Update authorization middleware to be redfish compliant UPdate random token generation to be more efficient, and not base64 bytes, generate bytes directly Change-Id: I63cc2005c1a21f5c2f5168777a4e09f3c965a34f
Diffstat (limited to 'include/webassets.hpp')
-rw-r--r--include/webassets.hpp66
1 files changed, 36 insertions, 30 deletions
diff --git a/include/webassets.hpp b/include/webassets.hpp
index 988af767ba..7845d712ad 100644
--- a/include/webassets.hpp
+++ b/include/webassets.hpp
@@ -14,35 +14,38 @@ namespace crow {
namespace webassets {
namespace filesystem = std::experimental::filesystem;
-static const char* gzip_string = "gzip";
-static const char* none_string = "none";
-static const char* if_none_match_string = "If-None-Match";
-static const char* content_encoding_string = "Content-Encoding";
-static const char* content_type_string = "Content-Type";
-static const char* etag_string = "ETag";
+
+struct cmp_str {
+ bool operator()(const char* a, const char* b) const {
+ return std::strcmp(a, b) < 0;
+ }
+};
static boost::container::flat_set<std::string> routes;
template <typename... Middlewares>
void request_routes(Crow<Middlewares...>& app) {
- const static boost::container::flat_map<const char*, const char*> content_types{
- {{".css", "text/css;charset=UTF-8"},
- {".html", "text/html;charset=UTF-8"},
- {".js", "text/html;charset=UTF-8"},
- {".png", "image/png;charset=UTF-8"},
- {".woff", "application/x-font-woff"},
- {".woff2", "application/x-font-woff2"},
- {".ttf", "application/x-font-ttf"},
- {".svg", "image/svg+xml"},
- {".eot", "application/vnd.ms-fontobject"},
- // dev tools don't care about map type, setting to json causes
- // browser to show as text
- // https://stackoverflow.com/questions/19911929/what-mime-type-should-i-use-for-javascript-source-map-files
- {".map", "application/json"}}};
+ const static boost::container::flat_map<const char*, const char*, cmp_str>
+ content_types{
+ {{".css", "text/css;charset=UTF-8"},
+ {".html", "text/html;charset=UTF-8"},
+ {".js", "text/html;charset=UTF-8"},
+ {".png", "image/png;charset=UTF-8"},
+ {".woff", "application/x-font-woff"},
+ {".woff2", "application/x-font-woff2"},
+ {".ttf", "application/x-font-ttf"},
+ {".svg", "image/svg+xml"},
+ {".eot", "application/vnd.ms-fontobject"},
+ {".xml", "application/xml"},
+ // dev tools don't care about map type, setting to json causes
+ // browser to show as text
+ // https://stackoverflow.com/questions/19911929/what-mime-type-should-i-use-for-javascript-source-map-files
+ {".map", "application/json"}}};
auto rootpath = filesystem::path("/usr/share/www/");
auto dir_iter = filesystem::recursive_directory_iterator(rootpath);
for (auto& dir : dir_iter) {
auto absolute_path = dir.path();
+ auto absolute_path_str = dir.path().string();
auto relative_path = filesystem::path(
absolute_path.string().substr(rootpath.string().size() - 1));
// make sure we don't recurse into certain directories
@@ -63,39 +66,42 @@ void request_routes(Crow<Middlewares...>& app) {
if (webpath.filename() == "index.html") {
webpath = webpath.parent_path();
+ if (webpath.string() != "/") {
+ webpath += "/";
+ }
}
routes.insert(webpath.string());
std::string absolute_path_str = absolute_path.string();
const char* content_type = nullptr;
- auto content_type_it =
- content_types.find(relative_path.extension().c_str());
+ auto content_type_it = content_types.find(webpath.extension().c_str());
if (content_type_it != content_types.end()) {
content_type = content_type_it->second;
}
app.route_dynamic(std::string(webpath.string()))(
[is_gzip, absolute_path_str, content_type](const crow::request& req,
crow::response& res) {
- if (is_gzip) {
- res.add_header(content_encoding_string, gzip_string);
- } else {
- res.add_header(content_encoding_string, none_string);
- }
+ static const char* content_type_string = "Content-Type";
// std::string sha1("a576dc96a5c605b28afb032f3103630d61ac1068");
- // res.add_header(etag_string, sha1);
+ // res.add_header("ETag", sha1);
- // if (req.get_header_value(if_none_match_string) == sha1) {
+ // if (req.get_header_value("If-None-Match") == sha1) {
// res.code = 304;
//} else {
// res.code = 200;
// TODO, if you have a browser from the dark ages that doesn't
// support
// gzip, unzip it before sending based on Accept-Encoding header
- // res.add_header(content_encoding_string, gzip_string);
+ // res.add_header("Content-Encoding", gzip_string);
if (content_type != nullptr) {
res.add_header(content_type_string, content_type);
}
+
+ if (is_gzip) {
+ res.add_header("Content-Encoding", "gzip");
+ }
+
// res.set_header("Cache-Control", "public, max-age=86400");
std::ifstream inf(absolute_path_str);
if (!inf) {