aboutsummaryrefslogtreecommitdiff
path: root/challenge-021/ruben-westerberg/perl5/ch-2.pl
blob: eda2228fa56198fd9e9891f9e4ba40372d19e776 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/usr/bin/env perl
use strict;
use warnings;
use v5.26;

my $URI=$ARGV[0]//'HTTP://a.uri/with/strange%65characters{}?query=true#frag<>';

print "Original URI:\t$URI\n";
#%Encode any character outside of restriced and unrestricted set except %
$URI=~s/([^0-9a-zA-Z-_.~%!*'();:@&=+\$,\/?#\[\]])/{sprintf("%%%2X",ord $1)}/ge;

#Normalize decode any encoded unresreved characters. Normalize encoding to upper case
$URI=~s/\%([a-fA-F0-9]{2,2})/{decode($1)}/ge;
sub decode {
	my $h=shift;
	my $c=chr hex $h;
	$c=~/[0-9a-zA-Z-_.~]/ ? $c:uc "%$h";
}

#Scheme to lower case
$URI=~s/^([a-zA-Z][a-zA-Z\-\.\+]+)/{lc $1}/e;

print "Normalized URI:\t$URI\n";