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
9 changed files with 53 additions and 86 deletions

View file

@ -1,4 +1,4 @@
FROM docker.io/rakudo-star:2023.08
FROM rakudo-star:2023.08
# Move it
WORKDIR /opt
@ -11,14 +11,15 @@ COPY templates/ ./templates/
RUN apt-get update
RUN apt-get -y upgrade
RUN apt-get -y install libssl-dev libarchive-dev
# Tests failing cope
RUN zef -v install --force-test IO::Socket::Async::SSL \
NativeHelpers::Callback \
Archive::Libarchive::Raw
# Stupid tests failing idk
RUN zef -v install --force-test IO::Socket::Async::SSL \
Archive::Libarchive::Raw NativeHelpers::Callback
# Get the latest and greatest for bug fix
# https://github.com/rawleyfowler/Humming-Bird/issues/60#issuecomment-1788351265
RUN zef install https://github.com/rawleyfowler/Humming-Bird.git --force-install
RUN zef -v install --deps-only .
# Finish setting up the environment
ENV HUMMING_BIRD_ENV='PROD'
EXPOSE 3000
CMD ["raku", "bin/hyperlink-redirect"]

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

View file

@ -11,4 +11,4 @@ use Humming-Bird::Core;
use lib 'lib';
use Hyperlink-Redirect;
listen 3000;
listen(3000);

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

@ -1,16 +1,20 @@
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>;
# Plugins
plugin 'Logger';
# Logging
middleware &middleware-logger;
advice &advice-logger;
# Must set the root path lest yet miss setting $!root
my $router = Router.new(root => '/');
@ -24,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);
@ -54,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>