Allow responses from image proxy server

This commit is contained in:
blankie 2023-04-05 00:01:24 +07:00
parent 5f004f5b45
commit e657432ac1
Signed by: blankie
GPG Key ID: CC15FC822C7F61F5
1 changed files with 20 additions and 1 deletions

View File

@ -1,12 +1,22 @@
#include <regex>
#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();
}