summaryrefslogtreecommitdiffhomepage
path: root/source/web.d
diff options
context:
space:
mode:
authorChloe Kudryavtsev <toast@toastin.space>2019-07-09 06:35:37 -0400
committerChloe Kudryavtsev <toast@toastin.space>2019-07-09 06:35:37 -0400
commit66decb7136c33a8745db2003b605518349991205 (patch)
treedad6395ba71e354d8adb98b03babe54c115d09c5 /source/web.d
parenteco: update vibe.d - partial fix for sockets (diff)
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
Diffstat (limited to 'source/web.d')
-rw-r--r--source/web.d30
1 files 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;