2015-09-13 20:25:26 +00:00
|
|
|
///
|
2020-06-26 22:45:47 +00:00
|
|
|
/// Copyright 2015-2020 Oliver Giles
|
2015-09-13 20:25:26 +00:00
|
|
|
///
|
|
|
|
/// This file is part of Laminar
|
|
|
|
///
|
|
|
|
/// Laminar is free software: you can redistribute it and/or modify
|
|
|
|
/// it under the terms of the GNU General Public License as published by
|
|
|
|
/// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
/// (at your option) any later version.
|
|
|
|
///
|
2015-09-24 19:07:48 +00:00
|
|
|
/// Laminar is distributed in the hope that it will be useful,
|
2015-09-13 20:25:26 +00:00
|
|
|
/// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
/// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
/// GNU General Public License for more details.
|
|
|
|
///
|
|
|
|
/// You should have received a copy of the GNU General Public License
|
|
|
|
/// along with Laminar. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
///
|
|
|
|
#include "resources.h"
|
2019-03-29 19:43:16 +00:00
|
|
|
#include "log.h"
|
|
|
|
#include "index_html_size.h"
|
2015-09-13 20:25:26 +00:00
|
|
|
#include <string.h>
|
2019-03-29 19:43:16 +00:00
|
|
|
#include <zlib.h>
|
2015-09-13 20:25:26 +00:00
|
|
|
|
2017-10-31 18:07:12 +00:00
|
|
|
#define INIT_RESOURCE(route, name, content_type) \
|
2015-09-13 20:25:26 +00:00
|
|
|
extern const char _binary_##name##_z_start[];\
|
|
|
|
extern const char _binary_##name##_z_end[]; \
|
2017-10-31 18:07:12 +00:00
|
|
|
resources.emplace(route, Resource{_binary_ ## name ## _z_start, _binary_ ## name ## _z_end, content_type})
|
|
|
|
|
2020-06-26 22:45:47 +00:00
|
|
|
#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_JS "application/javascript; charset=utf-8"
|
|
|
|
#define CONTENT_TYPE_CSS "text/css; charset=utf-8"
|
|
|
|
#define CONTENT_TYPE_MANIFEST "application/manifest+json; charset=utf-8"
|
2015-09-13 20:25:26 +00:00
|
|
|
|
2019-03-29 19:43:16 +00:00
|
|
|
#define GZIP_FORMAT 16
|
|
|
|
|
2015-09-13 20:25:26 +00:00
|
|
|
Resources::Resources()
|
|
|
|
{
|
2017-10-31 18:07:12 +00:00
|
|
|
INIT_RESOURCE("/favicon.ico", favicon_ico, CONTENT_TYPE_ICO);
|
|
|
|
INIT_RESOURCE("/favicon-152.png", favicon_152_png, CONTENT_TYPE_PNG);
|
|
|
|
INIT_RESOURCE("/icon.png", icon_png, CONTENT_TYPE_PNG);
|
|
|
|
INIT_RESOURCE("/js/app.js", js_app_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/ansi_up.js", js_ansi_up_js, CONTENT_TYPE_JS);
|
|
|
|
INIT_RESOURCE("/js/Chart.min.js", js_Chart_min_js, CONTENT_TYPE_JS);
|
2020-06-26 22:45:47 +00:00
|
|
|
INIT_RESOURCE("/style.css", style_css, CONTENT_TYPE_CSS);
|
|
|
|
INIT_RESOURCE("/manifest.webmanifest", manifest_webmanifest, CONTENT_TYPE_MANIFEST);
|
2020-03-10 17:40:14 +00:00
|
|
|
// Configure the default template
|
|
|
|
setHtmlTemplate(std::string());
|
|
|
|
}
|
2019-03-29 19:43:16 +00:00
|
|
|
|
2020-03-10 17:40:14 +00:00
|
|
|
void Resources::setHtmlTemplate(std::string tmpl) {
|
|
|
|
extern const char _binary_index_html_z_start[];
|
|
|
|
extern const char _binary_index_html_z_end[];
|
|
|
|
|
|
|
|
z_stream strm;
|
|
|
|
memset(&strm, 0, sizeof(z_stream));
|
|
|
|
|
|
|
|
if(!tmpl.empty()) {
|
2019-03-29 19:43:16 +00:00
|
|
|
// deflate
|
2020-03-10 17:40:14 +00:00
|
|
|
index_html.resize(tmpl.size());
|
2019-03-29 19:43:16 +00:00
|
|
|
deflateInit2(&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED, MAX_WBITS|GZIP_FORMAT, 8, Z_DEFAULT_STRATEGY);
|
2020-03-10 17:40:14 +00:00
|
|
|
strm.next_in = (unsigned char*) tmpl.data();
|
|
|
|
strm.avail_in = tmpl.size();
|
2019-03-29 19:43:16 +00:00
|
|
|
strm.next_out = (unsigned char*) index_html.data();
|
2020-03-10 17:40:14 +00:00
|
|
|
strm.avail_out = tmpl.size();
|
2019-03-29 19:43:16 +00:00
|
|
|
if(deflate(&strm, Z_FINISH) != Z_STREAM_END) {
|
|
|
|
LLOG(FATAL, "Failed to compress index.html");
|
|
|
|
}
|
|
|
|
index_html.resize(strm.total_out);
|
2020-03-10 17:40:14 +00:00
|
|
|
} else {
|
|
|
|
// use the default template from compile-time asset
|
|
|
|
if(const char* baseUrl = getenv("LAMINAR_BASE_URL")) {
|
|
|
|
// The administrator needs to customize the <base href>. Unfortunately this seems
|
|
|
|
// to be the only thing that needs to be customizable but cannot be done via dynamic
|
|
|
|
// DOM manipulation without heavy compromises. So replace the static char array with
|
|
|
|
// a modified buffer accordingly.
|
|
|
|
std::string tmp;
|
|
|
|
tmp.resize(INDEX_HTML_UNCOMPRESSED_SIZE);
|
|
|
|
// inflate
|
|
|
|
inflateInit2(&strm, MAX_WBITS|GZIP_FORMAT);
|
|
|
|
strm.next_in = (unsigned char*) _binary_index_html_z_start;
|
|
|
|
strm.avail_in = _binary_index_html_z_end - _binary_index_html_z_start;
|
|
|
|
strm.next_out = (unsigned char*) tmp.data();
|
|
|
|
strm.avail_out = INDEX_HTML_UNCOMPRESSED_SIZE;
|
|
|
|
if(inflate(&strm, Z_FINISH) != Z_STREAM_END) {
|
|
|
|
LLOG(FATAL, "Failed to uncompress index_html");
|
|
|
|
}
|
|
|
|
// replace
|
|
|
|
// There's no validation on the replacement string, so you can completely mangle
|
|
|
|
// the html if you like. This isn't really an issue because if you can modify laminar's
|
|
|
|
// environment you already have elevated permissions
|
|
|
|
if(auto it = tmp.find("base href=\"/"))
|
|
|
|
tmp.replace(it+11, 1, baseUrl);
|
|
|
|
// deflate
|
|
|
|
index_html.resize(tmp.size());
|
|
|
|
deflateInit2(&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED, MAX_WBITS|GZIP_FORMAT, 8, Z_DEFAULT_STRATEGY);
|
|
|
|
strm.next_in = (unsigned char*) tmp.data();
|
|
|
|
strm.avail_in = tmp.size();
|
|
|
|
strm.next_out = (unsigned char*) index_html.data();
|
|
|
|
strm.avail_out = tmp.size();
|
|
|
|
if(deflate(&strm, Z_FINISH) != Z_STREAM_END) {
|
|
|
|
LLOG(FATAL, "Failed to compress index.html");
|
|
|
|
}
|
|
|
|
index_html.resize(strm.total_out);
|
|
|
|
} else {
|
|
|
|
index_html = std::string(_binary_index_html_z_start, _binary_index_html_z_end);
|
|
|
|
}
|
2019-03-29 19:43:16 +00:00
|
|
|
}
|
2020-03-10 17:40:14 +00:00
|
|
|
// update resource map
|
|
|
|
resources["/"] = Resource{index_html.data(), index_html.data() + index_html.size(), CONTENT_TYPE_HTML};
|
2015-09-13 20:25:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool beginsWith(std::string haystack, const char* needle) {
|
|
|
|
return strncmp(haystack.c_str(), needle, strlen(needle)) == 0;
|
|
|
|
}
|
|
|
|
|
2017-10-31 18:07:12 +00:00
|
|
|
bool Resources::handleRequest(std::string path, const char** start, const char** end, const char** content_type) {
|
2015-09-13 20:25:26 +00:00
|
|
|
// need to keep the list of "application links" synchronised with the angular
|
|
|
|
// application. We cannot return a 404 for any of these
|
2020-10-09 00:06:03 +00:00
|
|
|
auto it = beginsWith(path,"/jobs") || path == "/wallboard"
|
2015-09-13 20:25:26 +00:00
|
|
|
? resources.find("/")
|
|
|
|
: resources.find(path);
|
|
|
|
|
|
|
|
if(it != resources.end()) {
|
2017-10-31 18:07:12 +00:00
|
|
|
*start = it->second.start;
|
|
|
|
*end = it->second.end;
|
|
|
|
*content_type = it->second.content_type;
|
2015-09-13 20:25:26 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|