From 576159d8e9822ce715da07204877f8f3e89b32b6 Mon Sep 17 00:00:00 2001 From: Oliver Giles Date: Tue, 31 Oct 2017 20:07:12 +0200 Subject: [PATCH] send correct Content-Type header for static resources --- src/resources.cpp | 49 +++++++++++++++++++++++++++-------------------- src/resources.h | 15 ++++++++++----- src/server.cpp | 5 +++-- 3 files changed, 41 insertions(+), 28 deletions(-) diff --git a/src/resources.cpp b/src/resources.cpp index e5d86d2..90e3662 100644 --- a/src/resources.cpp +++ b/src/resources.cpp @@ -1,5 +1,5 @@ /// -/// Copyright 2016 Oliver Giles +/// Copyright 2016-2017 Oliver Giles /// /// This file is part of Laminar /// @@ -19,35 +19,41 @@ #include "resources.h" #include -#define INIT_RESOURCE(route, name) \ +#define INIT_RESOURCE(route, name, content_type) \ extern const char _binary_##name##_z_start[];\ extern const char _binary_##name##_z_end[]; \ - resources[route] = std::make_pair(_binary_ ## name ## _z_start, _binary_ ## name ## _z_end) + resources.emplace(route, Resource{_binary_ ## name ## _z_start, _binary_ ## name ## _z_end, content_type}) + +#define CONTENT_TYPE_HTML "text/html; charset=utf-8" +#define CONTENT_TYPE_ICO "image/x-icon" +#define CONTENT_TYPE_PNG "image/png" +#define CONTENT_TYPE_GIF "image/gif" +#define CONTENT_TYPE_JS "application/javascript; charset=utf-8" +#define CONTENT_TYPE_CSS "text/css; charset=utf-8" Resources::Resources() { - // TODO: Content-type - INIT_RESOURCE("/", index_html); - INIT_RESOURCE("/favicon.ico", favicon_ico); - INIT_RESOURCE("/favicon-152.png", favicon_152_png); - INIT_RESOURCE("/progress.gif", progress_gif); - INIT_RESOURCE("/icon.png", icon_png); - INIT_RESOURCE("/js/app.js", js_app_js); - INIT_RESOURCE("/js/Chart.HorizontalBar.js", js_Chart_HorizontalBar_js); - INIT_RESOURCE("/js/ansi_up.js", js_ansi_up_js); - INIT_RESOURCE("/js/vue.min.js", js_vue_min_js); - INIT_RESOURCE("/js/vue-router.min.js", js_vue_router_min_js); - INIT_RESOURCE("/js/ansi_up.js", js_ansi_up_js); - INIT_RESOURCE("/js/Chart.min.js", js_Chart_min_js); - INIT_RESOURCE("/js/Chart.HorizontalBar.js", js_Chart_HorizontalBar_js); - INIT_RESOURCE("/css/bootstrap.min.css", css_bootstrap_min_css); + INIT_RESOURCE("/", index_html, CONTENT_TYPE_HTML); + INIT_RESOURCE("/favicon.ico", favicon_ico, CONTENT_TYPE_ICO); + INIT_RESOURCE("/favicon-152.png", favicon_152_png, CONTENT_TYPE_PNG); + INIT_RESOURCE("/progress.gif", progress_gif, CONTENT_TYPE_GIF); + INIT_RESOURCE("/icon.png", icon_png, CONTENT_TYPE_PNG); + INIT_RESOURCE("/js/app.js", js_app_js, CONTENT_TYPE_JS); + INIT_RESOURCE("/js/Chart.HorizontalBar.js", js_Chart_HorizontalBar_js, CONTENT_TYPE_JS); + INIT_RESOURCE("/js/ansi_up.js", js_ansi_up_js, CONTENT_TYPE_JS); + INIT_RESOURCE("/js/vue.min.js", js_vue_min_js, CONTENT_TYPE_JS); + INIT_RESOURCE("/js/vue-router.min.js", js_vue_router_min_js, CONTENT_TYPE_JS); + INIT_RESOURCE("/js/ansi_up.js", js_ansi_up_js, CONTENT_TYPE_JS); + INIT_RESOURCE("/js/Chart.min.js", js_Chart_min_js, CONTENT_TYPE_JS); + INIT_RESOURCE("/js/Chart.HorizontalBar.js", js_Chart_HorizontalBar_js, CONTENT_TYPE_JS); + INIT_RESOURCE("/css/bootstrap.min.css", css_bootstrap_min_css, CONTENT_TYPE_CSS); } inline bool beginsWith(std::string haystack, const char* needle) { return strncmp(haystack.c_str(), needle, strlen(needle)) == 0; } -bool Resources::handleRequest(std::string path, const char **start, const char **end) { +bool Resources::handleRequest(std::string path, const char** start, const char** end, const char** content_type) { // need to keep the list of "application links" synchronised with the angular // application. We cannot return a 404 for any of these auto it = beginsWith(path,"/jobs") @@ -55,8 +61,9 @@ bool Resources::handleRequest(std::string path, const char **start, const char * : resources.find(path); if(it != resources.end()) { - *start = it->second.first; - *end = it->second.second; + *start = it->second.start; + *end = it->second.end; + *content_type = it->second.content_type; return true; } diff --git a/src/resources.h b/src/resources.h index 7a3a1ce..31e93df 100644 --- a/src/resources.h +++ b/src/resources.h @@ -1,5 +1,5 @@ /// -/// Copyright 2016 Oliver Giles +/// Copyright 2016-2017 Oliver Giles /// /// This file is part of Laminar /// @@ -30,12 +30,17 @@ public: Resources(); // If a resource is known for the given path, set start and end to the - // binary data to send to the client. Function returns false if no resource - // for the given path is known (404) - bool handleRequest(std::string path, const char** start, const char** end); + // binary data to send to the client, and content_type to its MIME + // type. Function returns false if no resource for the given path exists + bool handleRequest(std::string path, const char** start, const char** end, const char** content_type); private: - std::unordered_map> resources; + struct Resource { + const char* start; + const char* end; + const char* content_type; + }; + std::unordered_map resources; }; #endif // _LAMINAR_RESOURCES_H_ diff --git a/src/server.cpp b/src/server.cpp index ea4e423..4f88aa0 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -199,7 +199,7 @@ public: // Handle plain HTTP requests by delivering the binary resource wss.set_http_handler([this](websocketpp::connection_hdl hdl){ websocket::connection_ptr c = wss.get_con_from_hdl(hdl); - const char* start, *end; + const char* start, *end, *content_type; std::string resource = c->get_resource(); if(resource.compare(0, strlen("/archive/"), "/archive/") == 0) { std::string file(resource.substr(strlen("/archive/"))); @@ -211,8 +211,9 @@ public: } else { c->set_status(websocketpp::http::status_code::not_found); } - } else if(resources.handleRequest(resource, &start, &end)) { + } else if(resources.handleRequest(resource, &start, &end, &content_type)) { c->set_status(websocketpp::http::status_code::ok); + c->append_header("Content-Type", content_type); c->append_header("Content-Encoding", "gzip"); c->append_header("Content-Transfer-Encoding", "binary"); std::string response(start,end);