pixwhile/servehelper.cpp

103 lines
3.2 KiB
C++
Raw Normal View History

#include <regex>
2023-04-03 09:32:26 +00:00
#include "config.h"
#include "servehelper.h"
static inline std::string get_image_proxy_origin(const std::string& url);
2023-04-03 09:32:26 +00:00
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'; style-src "s + css_url
+ "; img-src " + get_image_proxy_origin(config.image_proxy_url));
2023-04-03 09:32:26 +00:00
Element html("html", {
Element("head", {
2023-04-03 14:40:34 +00:00
Element("meta", {{"charset", "utf-8"}}, {}),
2023-04-03 09:32:26 +00:00
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");
}
2023-04-03 15:27:07 +00:00
void serve_error(const httplib::Request& req, httplib::Response& res, const Config& config,
std::string title, std::optional<std::string> subtitle, std::optional<std::string> info) {
Element error_div("div", {{"class", "error"}}, {
Element("h2", {title})
});
if (subtitle) {
2023-04-04 08:33:48 +00:00
error_div.nodes.push_back(Element("p", {
std::move(*subtitle)
}));
2023-04-03 15:27:07 +00:00
}
if (info) {
error_div.nodes.push_back(Element("pre", {
Element("code", {std::move(*info)})
}));
}
Element body("body", {std::move(error_div)});
serve(req, res, config, std::move(title), std::move(body));
}
2023-04-04 08:33:48 +00:00
void serve_redirect(const httplib::Request& req, httplib::Response& res, const Config& config, std::string url) {
using namespace std::string_literals;
Element body("body", {
"Redirecting to ",
Element("a", {{"href", url}}, {url}),
""
});
res.set_redirect(url);
serve(req, res, config, "Redirecting to "s + std::move(url) + "", std::move(body));
}
2023-04-03 14:40:34 +00:00
std::string get_origin(const httplib::Request& req, const Config& config) {
2023-04-03 09:32:26 +00:00
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;
}
2023-04-05 16:36:20 +00:00
static std::regex remove_origin_regex(
"(?:https?://)?" // optional schema
"(?:.+?@)?" // optional username and pass
"(?:[^/]+[.:][^/]+(?:\\d+)?)" // host
"(/.*)");
std::string remove_origin(const std::string& url) {
std::smatch sm;
if (!std::regex_match(url, sm, remove_origin_regex)) {
return url;
}
return sm[1].str();
}
2023-04-05 16:36:20 +00:00
static std::regex image_proxy_regex(
"(https?://)?" // optional scheme
"(?:.+?@)?" // optional username and pass
"([^/]+(?::\\d+)?)" // host
"(?:/.*)?$");
static inline std::string get_image_proxy_origin(const std::string& url) {
std::smatch sm;
if (!std::regex_match(url, sm, image_proxy_regex)) {
return url;
}
return sm[1].str() + sm[2].str();
}