From 66decb7136c33a8745db2003b605518349991205 Mon Sep 17 00:00:00 2001 From: Chloe Kudryavtsev Date: Tue, 9 Jul 2019 06:35:37 -0400 Subject: feat: redirect real browsers to content - Use 303: otherwise they just POST again - Make the response by hand, redirect() not usable in this context - Matching browsers is done via UA --- source/web.d | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/source/web.d b/source/web.d index c71f592..b2cf430 100644 --- a/source/web.d +++ b/source/web.d @@ -6,6 +6,7 @@ import brpaste.storage; import vibe.vibe; import std.functional; +import std.regex; RedisStorage store; @@ -39,10 +40,18 @@ void insert(bool put, HTTPServerRequest req, HTTPServerResponse res) { string data = req.form["data"]; enforceHTTP(data.isValid, HTTPStatus.unsupportedMediaType, "Content contains binary."); auto hash = put ? req.params["id"] : data.hash; - store.put(hash, data, put); - res.statusCode = HTTPStatus.created; - res.writeBody(hash); + + auto ua = req.headers.get("User-Agent", ""); + if(ua.isBrowser) { + // TODO: eventually move back to registerWebInterface for redirect() + res.statusCode = HTTPStatus.seeOther; + res.headers["Location"] = "/%s".format(hash); + res.writeBody(""); + } else { + res.statusCode = HTTPStatus.created; + res.writeBody(hash); + } } void health(HTTPServerRequest req, HTTPServerResponse res) { @@ -53,6 +62,21 @@ void health(HTTPServerRequest req, HTTPServerResponse res) { store.isDown; } +// tries to match User-Agent against known browsers +static bool isBrowser(string ua) { + foreach (r; [ + "Firefox/", + "Chrome/", + "Safari/", + "OPR/", + "Edge/", + "Trident/" + ]) { + if(ua.matchFirst(r)) return true; + } + return false; +} + shared static this() { // setup redis string path; -- cgit v1.2.3