pixwhile/servehelper.cpp

209 lines
7.5 KiB
C++
Raw Permalink Normal View History

#include <regex>
2023-05-12 09:56:22 +00:00
#include <FastHash.h>
2023-04-03 09:32:26 +00:00
#include "config.h"
2023-04-28 09:00:21 +00:00
#include "pixivmodels.h"
2023-04-03 09:32:26 +00:00
#include "servehelper.h"
2023-05-12 09:56:22 +00:00
#include "routes/routes.h"
2023-04-03 09:32:26 +00:00
2023-04-28 09:00:21 +00:00
static Element generate_pager(const Illusts& illusts, size_t page, const char* id);
static inline Element generate_illusts_grid(const httplib::Request& req, const Config& config, const Illusts& illusts);
static inline Element generate_illusts_grid_item(const httplib::Request& req, const Config& config, const Illust& illust);
static inline Element generate_illust_badge(const Illust& illust, const std::string& illust_url);
2023-04-28 09:00:21 +00:00
2023-06-04 06:22:19 +00:00
void serve(const httplib::Request& req, httplib::Response& res, const Config& config, std::string title, Element element, Nodes extra_head) {
2023-04-03 09:32:26 +00:00
using namespace std::string_literals;
2023-04-06 12:24:09 +00:00
std::string css_url = get_origin(req, config) + "/style.css";
res.set_header("Content-Security-Policy", "default-src 'none'; style-src "s + css_url
+ "; img-src https://s.pximg.net " + config.image_proxy_url.get_origin());
2023-04-03 09:32:26 +00:00
2023-06-04 06:22:19 +00:00
Element head("head", {
Element("meta", {{"charset", "utf-8"}}, {}),
Element("title", {std::move(title)}),
Element("link", {{"rel", "stylesheet"}, {"href", std::move(css_url) + "?v=" + std::to_string(css_hash)}}, {}),
Element("meta", {{"name", "viewport"}, {"content", "width=device-width,initial-scale=1"}}, {})
});
head.nodes.reserve(head.nodes.size() + extra_head.size());
head.nodes.insert(head.nodes.end(), extra_head.begin(), extra_head.end());
2023-05-12 09:56:22 +00:00
std::string html = "<!DOCTYPE html>"s + Element("html", {
2023-06-04 06:22:19 +00:00
std::move(head),
2023-04-03 09:32:26 +00:00
std::move(element)
2023-05-12 09:56:22 +00:00
}).serialize();
uint64_t hash = FastHash(html.data(), html.size(), 0);
res.set_header("ETag", std::string(1, '"') + std::to_string(hash) + '"');
if (should_send_304(req, hash)) {
res.status = 304;
res.set_header("Content-Length", std::to_string(html.size()));
res.set_header("Content-Type", "text/html");
} else {
res.set_content(std::move(html), "text/html");
}
2023-04-03 09:32:26 +00:00
}
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-06-07 09:16:03 +00:00
void serve_redirect(const httplib::Request& req, httplib::Response& res, const Config& config, std::string url, bool permanent) {
2023-04-04 08:33:48 +00:00
using namespace std::string_literals;
Element body("body", {
"Redirecting to ",
Element("a", {{"href", url}}, {url}),
""
});
2023-06-07 09:16:03 +00:00
res.set_redirect(url, permanent ? 301 : 302);
2023-04-04 08:33:48 +00:00
serve(req, res, config, "Redirecting to "s + std::move(url) + "", std::move(body));
}
2023-04-28 09:00:21 +00:00
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");
}
2023-06-04 05:08:49 +00:00
if (config.canonical_origin) {
return *config.canonical_origin;
}
2023-04-03 09:32:26 +00:00
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-09 17:34:13 +00:00
std::string proxy_url(blankie::murl::Url base, blankie::murl::Url url) {
2023-04-06 12:24:09 +00:00
if (!url.path.empty() && url.path[0] != '/') {
2023-04-09 17:34:13 +00:00
base.path += '/';
}
base.path += blankie::murl::normalize_path(std::move(url.path));
if (!base.query.empty() && !url.query.empty()) {
base.query += '&';
2023-04-05 16:36:20 +00:00
}
2023-04-09 17:34:13 +00:00
base.query += std::move(url.query);
base.fragment = std::move(url.fragment);
return base.to_string();
}
2023-06-07 09:16:03 +00:00
std::string proxy_image_url(const httplib::Request& req, const Config& config, blankie::murl::Url url) {
if (!url.is_host_equal("i.pximg.net")) {
2023-04-09 17:34:13 +00:00
return url.to_string();
}
2023-06-07 09:16:03 +00:00
return url.fragment != "guess_extension"
? proxy_url(config.image_proxy_url, std::move(url))
: get_origin(req, config) + "/guess_extension/i.pximg.net" + url.path;
}
2023-04-28 09:00:21 +00:00
2023-05-12 09:56:22 +00:00
bool should_send_304(const httplib::Request& req, uint64_t hash) {
std::string header = req.get_header_value("If-None-Match");
if (header == "*") {
return true;
}
size_t pos = header.find(std::string(1, '"') + std::to_string(hash) + '"');
return pos != std::string::npos && (pos == 0 || header[pos - 1] != '/');
}
2023-04-28 09:00:21 +00:00
Element generate_illusts_pager(const httplib::Request& req, const Config& config, const Illusts& illusts, size_t page, const char* id) {
return Element("div", {{"id", id}}, {
generate_pager(illusts, page, id),
Element("br"),
generate_illusts_grid(req, config, illusts),
2023-04-28 09:00:21 +00:00
generate_pager(illusts, page, id)
});
}
static Element generate_pager(const Illusts& illusts, size_t page, const char* id) {
auto link = [&](size_t new_page, const char* text, bool add_link) {
using namespace std::string_literals;
Element b("b");
if (add_link) {
std::string href = "?p="s + std::to_string(new_page) + '#' + id;
b.nodes.push_back(Element("a", {{"href", std::move(href)}}, {text}));
} else {
b.nodes.push_back(text);
}
return b;
};
return Element("div", {{"class", "center"}}, {
link(1, "First", page != 0), " ",
link(page, "Prev", page != 0), " ",
std::to_string(page + 1), "/", std::to_string(illusts.total_pages), " ",
link(page + 2, "Next", page + 1 < illusts.total_pages), " ",
link(illusts.total_pages, "Last", page + 1 < illusts.total_pages)
});
}
static inline Element generate_illusts_grid(const httplib::Request& req, const Config& config, const Illusts& illusts) {
2023-05-12 10:13:55 +00:00
Element div("div", {{"class", "grid illusts_grid"}}, {});
2023-04-28 09:00:21 +00:00
div.nodes.reserve(illusts.illusts.size());
for (const Illust& i : illusts.illusts) {
div.nodes.push_back(generate_illusts_grid_item(req, config, i));
}
return div;
}
static inline Element generate_illusts_grid_item(const httplib::Request& req, const Config& config, const Illust& illust) {
std::string illust_url = get_origin(req, config) + "/artworks/" + std::to_string(illust.illust_id);
2023-06-07 09:16:03 +00:00
std::string image_url = proxy_image_url(req, config, illust.images[0].thumbnail_or_original(1).url);
2023-04-28 09:00:21 +00:00
2023-05-12 10:13:55 +00:00
Element div("div", {{"class", "illusts_grid-illust"}}, {
Element("a", {{"href", illust_url}}, {
2023-04-28 09:00:21 +00:00
Element("img", {{"loading", "lazy"}, {"src", std::move(image_url)}}, {}),
Element("p", {illust.title})
})
});
if (illust.page_count > 1 || illust.ai_generated) {
div.nodes.push_back(generate_illust_badge(illust, std::move(illust_url)));
2023-04-28 09:00:21 +00:00
}
return div;
}
static inline Element generate_illust_badge(const Illust& illust, const std::string& illust_url) {
2023-05-12 10:13:55 +00:00
const char* css_class = !illust.ai_generated ? "illusts_grid-illust_badge" : "illusts_grid-illust_badge ai";
if (illust.page_count > 1) {
return Element("a", {{"class", css_class}, {"href", illust_url + "?preview=1"}}, {
std::to_string(illust.page_count), " pages"
});
} else {
return Element("span", {{"class", css_class}}, {
"AI"
});
}
}