Merge pull request 'https now automatically added if not present' (#1) from drudgesentinel/Hyperlink-Redirect:main into main

Reviewed-on: swaggboi/Hyperlink-Redirect#1
This commit is contained in:
swagg boi 2023-11-14 01:58:29 +00:00
commit 78b81897c9
2 changed files with 23 additions and 2 deletions

19
lib/Helpers.rakumod Normal file
View file

@ -0,0 +1,19 @@
my $starts-with-protocol = rx:i/ ^https? '://'/;
sub fix-protocol ($url) is export {
return "http://" ~ $url unless $url ~~ $starts-with-protocol;
}
# I use a dedicated method to remove all debugging messages prior to commit
sub dbug($message) {
say "$message";
}
#error types
# Just string is provided:
# ERROR: Malformed UTF-8 near byte 81 at line 1 col 2
# TYPE: X::AdHoc
# ERROR: Stringification of a Buf is not done with 'Stringy'. The 'decode'
# method should be used to convert a Buf to a Str.
# TYPE: X::Buf::AsStr

View file

@ -4,6 +4,7 @@ use Humming-Bird::Advice;
use Template::Mustache;
use Base64;
use Libarchive::Filter :gzip;
use Helpers;
# Normally would 'use' local libs here for Controller and Model and
# what not but keeping it simple for now...
@ -25,7 +26,7 @@ $router.get(-> $request, $response {
});
$router.post(-> $request, $response {
my Str $return-url = $request.content<hyperlink>;
my Str $return-url = fix-protocol($request.content<hyperlink>);
my Bool $meta-refresh = $request.content<meta-refresh>.defined;
my Str $url-scheme = $request.headers<X-Forwarded-Proto> || 'http';
my Str $url-host = $request.headers<Host>;
@ -52,6 +53,7 @@ $router.get('/--meta-refresh/**', -> $request, $response {
$router.get('/**', -> $request, $response {
my Str $return-url = $request.path.substr(1); # Omits the leading slash
my Str $redirect-url = gunzip(decode-base64($return-url, :bin));
$response.redirect($redirect-url);