From 5b0db04d340f91d0edbc58f1fadbb167caff837d Mon Sep 17 00:00:00 2001 From: swaggboi Date: Sat, 7 Oct 2023 22:48:48 -0400 Subject: [PATCH] Got the basic thing doing the thing --- README.md | 3 +-- lib/Hyperlink-Redirect.rakumod | 13 ++++++++----- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 1d64107..7c1ecd9 100644 --- a/README.md +++ b/README.md @@ -14,5 +14,4 @@ A "useful" tool for turning hyperlinks into redirects in the name of shortening ## TODO -1. Process the form input -1. Make the return url gzip/base64'd +1. Why does 'hyperlink' linger even for subsequent GET requests from different clients?? diff --git a/lib/Hyperlink-Redirect.rakumod b/lib/Hyperlink-Redirect.rakumod index d7c086d..a5b6819 100644 --- a/lib/Hyperlink-Redirect.rakumod +++ b/lib/Hyperlink-Redirect.rakumod @@ -18,16 +18,19 @@ $router.get(-> $request, $response { }); $router.post(-> $request, $response { - my $hyperlink = $request.content.{'hyperlink'}; - - say encode-base64(gzip($hyperlink), :str); + my $return-url = $request.content.{'hyperlink'}; + my $url-scheme = $request.headers.{'X-Forwarded-Proto'} || 'http'; + my $url-host = $request.headers.{'Host'}; + my $base-url = $url-scheme ~ '://' ~ $url-host ~ '/'; + my $hyperlink = $base-url ~ encode-base64(gzip($return-url), :str); $response.html($templates.process: 'index', :$hyperlink); }); # Try a wildcard to catch 'all' path $router.get('/**', -> $request, $response { - my $url = $request.path.substr(1); # Omits the leading slash + my $return-url = $request.path.substr(1); # Omits the leading slash + my $redirect-url = gunzip(decode-base64($return-url, :bin)); - $response.redirect($url); + $response.redirect($redirect-url); });