aboutsummaryrefslogtreecommitdiff
path: root/lib/Net/Hawk/Utils.pm
diff options
context:
space:
mode:
authordakkar <dakkar@thenautilus.net>2015-01-17 16:30:02 +0000
committerdakkar <dakkar@thenautilus.net>2015-01-17 16:30:02 +0000
commit9e8b9ce470c26d0578cf65db3cd3da7bab74b6d7 (patch)
tree3051e09d349f784dd8b3cb90ac0fd7c3af5ad20a /lib/Net/Hawk/Utils.pm
parentadd subtest names (diff)
downloadnet-hawk-master.tar.gz
net-hawk-master.tar.bz2
net-hawk-master.zip
Uri seems to pass a few testsHEADmaster
also it now has a proper implementation
Diffstat (limited to 'lib/Net/Hawk/Utils.pm')
-rw-r--r--lib/Net/Hawk/Utils.pm36
1 files changed, 36 insertions, 0 deletions
diff --git a/lib/Net/Hawk/Utils.pm b/lib/Net/Hawk/Utils.pm
index bf81b10..21fc33f 100644
--- a/lib/Net/Hawk/Utils.pm
+++ b/lib/Net/Hawk/Utils.pm
@@ -65,6 +65,42 @@ package Net::Hawk::Utils {
return %attributes;
}
+
+ sub parse_host(%req,Str $header_host_name) {
+ $header_host_name //= 'host';
+ $header_host_name .= lc;
+ my $host = %req<$header_host_name>;
+ return Nil unless $host;
+ my $scheme = %req<connection><encrypted> ?? 'https' !! 'http';
+ my $uri = try { URI.new("{$scheme}://{$host}",:is_validating) };
+ return Nil unless $uri;
+ return {
+ name => $uri.host,
+ port => $uri.port,
+ };
+ };
+
+ sub parse_request(%req,%options) is export {
+ return %req unless %req<headers>;
+
+ my %host;
+ unless %options{all(<host port>)}.defined {
+ %host = parse_host(%req,%options<host_header_name>);
+ Net::Hawk::Errors::BadRequest.new(
+ text => 'Invalid Host header',
+ value => %req,
+ ).throw unless %host;
+ };
+
+ my %request = (
+ %req<method url> :p,
+ host => %options<host> // %host<name>,
+ port => %options<port> // %host<port>,
+ authorization => %req<headers><authorization>,
+ content_type => %req<headers><content_type> // '',
+ );
+ return %request;
+ };
}
1;