Skip to content

Commit

Permalink
extract port from the url
Browse files Browse the repository at this point in the history
  • Loading branch information
sergot committed Jul 24, 2014
1 parent b612395 commit ec7f0de
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/HTTP/UserAgent.pm6
Expand Up @@ -60,6 +60,7 @@ submethod BUILD(:$!useragent?) {
}

method get(Str $url is copy) {
my $port = _get-port($url);
$url = _clear-url($url);

my $response;
Expand All @@ -74,8 +75,8 @@ method get(Str $url is copy) {
$request.headers.header(User-Agent => $.useragent) if $.useragent.defined;

my $conn = $url.substr(4, 1) eq 's'
?? IO::Socket::SSL.new(:host(~$request.header('Host').values), :port(443), :timeout($.timeout))
!! IO::Socket::INET.new(:host(~$request.header('Host').values), :port(80), :timeout($.timeout));
?? IO::Socket::SSL.new(:host(~$request.header('Host').values), :port($port // 443), :timeout($.timeout))
!! IO::Socket::INET.new(:host(~$request.header('Host').values), :port($port // 80), :timeout($.timeout));

if $conn.send($request.Str ~ "\r\n") {
my $first-chunk;
Expand Down Expand Up @@ -207,3 +208,7 @@ sub _clear-url(Str $url is copy) {
$url;
}

sub _get-port(Str $url) {
(~$/[0]).Int if $url ~~ m/':' (\d+) \/?/;
}

0 comments on commit ec7f0de

Please sign in to comment.