39 lines
1.3 KiB
C++
39 lines
1.3 KiB
C++
#include "config.h"
|
|
#include "servehelper.h"
|
|
|
|
static inline std::string get_origin(const httplib::Request& req, const Config& config);
|
|
|
|
void serve(const httplib::Request& req, httplib::Response& res, const Config& config, std::string title, Element element) {
|
|
using namespace std::string_literals;
|
|
|
|
std::string origin = get_origin(req, config);
|
|
std::string css_url = origin + "/style.css";
|
|
res.set_header("Content-Security-Policy", "default-src 'none'; img-src 'self'; style-src "s + css_url);
|
|
|
|
Element html("html", {
|
|
Element("head", {
|
|
Element("title", {std::move(title)}),
|
|
Element("link", {{"rel", "stylesheet"}, {"href", std::move(css_url)}}, {})
|
|
}),
|
|
std::move(element)
|
|
});
|
|
res.set_content("<!DOCTYPE html>"s + html.serialize(), "text/html");
|
|
}
|
|
|
|
static inline std::string get_origin(const httplib::Request& req, const Config& config) {
|
|
if (req.has_header("X-Canonical-Origin")) {
|
|
return req.get_header_value("X-Canonical-Origin");
|
|
}
|
|
|
|
std::string origin = "http://";
|
|
if (req.has_header("Host")) {
|
|
origin += req.get_header_value("Host");
|
|
} else {
|
|
origin += config.bind_host;
|
|
if (config.bind_port != 80) {
|
|
origin += ':' + std::to_string(config.bind_port);
|
|
}
|
|
}
|
|
return origin;
|
|
}
|