From 9e8b9ce470c26d0578cf65db3cd3da7bab74b6d7 Mon Sep 17 00:00:00 2001 From: dakkar Date: Sat, 17 Jan 2015 16:30:02 +0000 Subject: Uri seems to pass a few tests also it now has a proper implementation --- lib/Net/Hawk/Utils.pm | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'lib/Net/Hawk/Utils.pm') 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 ?? '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; + + my %host; + unless %options{all()}.defined { + %host = parse_host(%req,%options); + Net::Hawk::Errors::BadRequest.new( + text => 'Invalid Host header', + value => %req, + ).throw unless %host; + }; + + my %request = ( + %req :p, + host => %options // %host, + port => %options // %host, + authorization => %req, + content_type => %req // '', + ); + return %request; + }; } 1; -- cgit v1.2.3