Use redirects

This commit is contained in:
blankie 2023-04-04 15:33:48 +07:00
parent 0b7a3468a6
commit e3ee4a445d
Signed by: blankie
GPG Key ID: CC15FC822C7F61F5
3 changed files with 35 additions and 1 deletions

View File

@ -26,6 +26,25 @@ int main(int argc, char** argv) {
});
server.Get("/style.css", css_route);
server.Get("/member.php", [&](const httplib::Request& req, httplib::Response& res) {
std::string id = req.get_param_value("id");
if (id.empty() || id.find_first_not_of("1234567890") != std::string::npos) {
res.status = 400;
serve_error(req, res, config, "400: Bad Request", "Missing or invalid user ID");
return;
}
serve_redirect(req, res, config, get_origin(req, config) + "/users/" + id);
});
server.Get("/member_illust.php", [&](const httplib::Request& req, httplib::Response& res) {
std::string illust_id = req.get_param_value("illust_id");
if (illust_id.empty() || illust_id.find_first_not_of("1234567890") != std::string::npos) {
res.status = 400;
serve_error(req, res, config, "400: Bad Request", "Missing or invalid illust ID");
return;
}
serve_redirect(req, res, config, get_origin(req, config) + "/artworks/" + illust_id);
});
#ifndef NDEBUG
server.Get("/debug/exception/known", [](const httplib::Request& req, httplib::Response& res) {
throw std::runtime_error("awoo");

View File

@ -26,7 +26,9 @@ void serve_error(const httplib::Request& req, httplib::Response& res, const Conf
Element("h2", {title})
});
if (subtitle) {
error_div.nodes.push_back(std::move(*subtitle));
error_div.nodes.push_back(Element("p", {
std::move(*subtitle)
}));
}
if (info) {
error_div.nodes.push_back(Element("pre", {
@ -38,6 +40,18 @@ void serve_error(const httplib::Request& req, httplib::Response& res, const Conf
serve(req, res, config, std::move(title), std::move(body));
}
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));
}
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");

View File

@ -11,4 +11,5 @@ using Element = blankie::html::Element;
void serve(const httplib::Request& req, httplib::Response& res, const Config& config, std::string title, Element element);
void serve_error(const httplib::Request& req, httplib::Response& res, const Config& config,
std::string title, std::optional<std::string> subtitle = std::nullopt, std::optional<std::string> info = std::nullopt);
void serve_redirect(const httplib::Request& req, httplib::Response& res, const Config& config, std::string url);
std::string get_origin(const httplib::Request& req, const Config& config);