mirror of
				https://github.com/ohwgiles/laminar.git
				synced 2025-06-13 12:54:29 +00:00 
			
		
		
		
	send correct Content-Type header for static resources
This commit is contained in:
		
							parent
							
								
									f42ef7aacf
								
							
						
					
					
						commit
						576159d8e9
					
				| @ -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 <string.h> | ||||
| 
 | ||||
| #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; | ||||
|     } | ||||
| 
 | ||||
|  | ||||
| @ -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<std::string, std::pair<const char*, const char*>> resources; | ||||
|     struct Resource { | ||||
|         const char* start; | ||||
|         const char* end; | ||||
|         const char* content_type; | ||||
|     }; | ||||
|     std::unordered_map<std::string, const Resource> resources; | ||||
| }; | ||||
| 
 | ||||
| #endif // _LAMINAR_RESOURCES_H_
 | ||||
|  | ||||
| @ -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); | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user