Compare commits

..

1 commit

Author SHA1 Message Date
swagg boi ffaf379b55 https now automatically added if not present 2023-11-14 01:14:48 +00:00
7 changed files with 40 additions and 77 deletions

View file

@ -1,13 +1,12 @@
{
"name": "Hyperlink-Redirect",
"depends": [
"Humming-Bird",
"Template::Mustache",
"Base64",
"Libarchive::Filter"
],
"provides": {
"Hyperlink-Redirect": "lib/Hyperlink-Redirect.rakumod",
"Hyperlink-Redirect::Helpers": "lib/Hyperlink-Redirect/Helpers.rakumod"
}
"name": "Hyperlink-Redirect",
"depends": [
"Humming-Bird",
"Template::Mustache",
"Base64",
"Libarchive::Filter"
],
"provides": {
"Hyperlink-Redirect": "lib/Hyperlink-Redirect.rakumod"
}
}

View file

@ -42,7 +42,4 @@ A "useful" tool for turning hyperlinks into redirects in the name of shortening
## TODO
1. Charset should be `utf-8` not `utf8` (in Content-Type response header)
1. Verify status of meta-refresh; should it be 200 or 3xx?
1. Tests
1. Test UTF-8 chars in return-url
1. Validate submitted URLs

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 "https://" ~ $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

@ -2,11 +2,12 @@ use Humming-Bird::Core;
use Humming-Bird::Middleware;
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...
# UPDATE: We now have local lib (sry libs)
use Hyperlink-Redirect::Helpers;
# Set things up (config stuff would go here?)
my $template = Template::Mustache.new: :from<../templates>;
@ -27,29 +28,24 @@ $router.get(-> $request, $response {
$router.post(-> $request, $response {
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>;
my Str $url-scheme = $request.headers<X-Forwarded-Proto> || 'http';
my Str $url-host = $request.headers<Host>;
my (Str $base-url, Str $hyperlink, Str %stash);
$base-url = $url-scheme ~ '://' ~ $url-host ~
($meta-refresh ?? '/--meta-refresh/' !! '/');
$hyperlink = $base-url ~ hyperlink $return-url;
$hyperlink = $base-url ~ encode-base64(gzip($return-url), :str);
%stash = title => 'New hyperlink created', :$hyperlink;
$response.html($template.render: 'index', %stash);
});
# Don't try to process favicon as a hyperlink
$router.get('/favicon.ico', -> $request, $response {
$response.status(204)
});
# Process the hyperlink
$router.get('/--meta-refresh/**', -> $request, $response {
my Str $return-url = $request.path.subst: /^ '/--meta-refresh/'/, Empty;
my Str $redirect-url = redirect $return-url;
my Str $redirect-url = gunzip(decode-base64($return-url, :bin));
my Str %stash = title => 'Hyperlinking...', :$redirect-url;
$response.html($template.render: 'index', %stash);
@ -57,7 +53,8 @@ $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 = redirect $return-url;
my Str $redirect-url = gunzip(decode-base64($return-url, :bin));
$response.redirect($redirect-url);
});

View file

@ -1,32 +0,0 @@
use Base64;
use Libarchive::Filter :gzip;
my $starts-with-protocol = rx:i/ ^https? '://'/;
sub fix-protocol(Str $url) is export {
return "http://" ~ $url unless $url ~~ $starts-with-protocol;
return $url;
}
# 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
sub hyperlink(Str $return-url --> Str) is export {
encode-base64(gzip($return-url), :str)
}
sub redirect(Str $return-url --> Str) is export {
gunzip(decode-base64($return-url, :bin));
}

View file

@ -1,17 +0,0 @@
use v6.d;
use Humming-Bird::Core;
use Test;
# Local libs
use lib 'lib';
use Hyperlink-Redirect;
use Hyperlink-Redirect::Helpers;
plan 2;
ok fix-protocol("seriousbusiness.international") eq "http://seriousbusiness.international", \
"fix-protocol fixes bare domains correctly"\
or diag "\nfix-protocol should return the same string with 'http://' prefixed if protocol isn't specified";
ok fix-protocol("https://www.seriousbusiness.international") eq "https://www.seriousbusiness.international",\
"fix-protocol does not mangle correctly formatted domains"\
or diag "\nThe URL should remain unchanged when the protocol is specified";

View file

@ -3,7 +3,7 @@
<head>
<title>{{title}}</title>
{{#redirect-url}}
<meta http-equiv="Refresh" content="0; url={{redirect-url}}">
<meta http-equiv="Refresh" content="0; url='{{redirect-url}}'">
{{/redirect-url}}
</head>
<body>