diff --git a/servehelper.cpp b/servehelper.cpp index 4188f49..fb00969 100644 --- a/servehelper.cpp +++ b/servehelper.cpp @@ -1,12 +1,22 @@ +#include + #include "config.h" #include "servehelper.h" +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); + 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); + res.set_header("Content-Security-Policy", "default-src 'none'; style-src "s + css_url + + "; img-src " + get_image_proxy_origin(config.image_proxy_url)); Element html("html", { Element("head", { @@ -68,3 +78,12 @@ std::string get_origin(const httplib::Request& req, const Config& config) { } return origin; } + + +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(); +}