Subject: | Default port values are http/https rather than 80/443 |
Seems that 'http' and 'https' are used instead of the port numbers by
default, but there's a comparison to URI::http->default_port (which
*does* use the numerical value), giving:
$ perl t/26numerical_warning.t
1..2
Argument "http" isn't numeric in numeric ne (!=) at
/home/tom/perl5/perlbrew/perls/perl-5.14.2/lib/site_perl/5.14.2/Net/Async/HTTP.pm
line 414.
Test failed early - 127.0.0.1:http:80 not resolvable [Name or service
not known
] at t/26numerical_warning.t line 98.
# Looks like your test exited with -1 before it could output anything.
I've got a patch which fixes it for me, not sure if you'd prefer a
getservbyname approach instead or maybe even URI::http/URI::https
->default_port for consistency?
=== modified file 'lib/Net/Async/HTTP.pm'
--- lib/Net/Async/HTTP.pm 2012-02-29 21:45:31 +0000
+++ lib/Net/Async/HTTP.pm 2012-03-07 23:24:36 +0000
@@ -516,7 +523,9 @@
}
if( !defined $port ) {
- $port = delete $args{port} || ( $ssl ? "https" : "http" );
+ # using port numbers here since the var is called 'port', but
+ # maybe service + an async getservbyname would be more flexible
+ $port = delete $args{port} || ( $ssl ? 443 : 80 );
}
$timer->configure( notifier_name => "$host:$port/..." ) if $timer;
cheers,
Tom
Subject: | 26numerical_warning.t |
#!/usr/bin/perl
use strict;
use warnings;
use Test::More tests => 2;
use IO::Async::Test;
use IO::Async::Loop;
# Needed to fix 25uri_no_default_port.t
use URI::http;
use Net::Async::HTTP;
my $CRLF = "\x0d\x0a"; # because \r\n isn't portable
my $loop = IO::Async::Loop->new();
testing_loop( $loop );
my $http = Net::Async::HTTP->new(
user_agent => "", # Don't put one in request headers
);
$loop->add( $http );
my $port;
$loop->listen(
host => "127.0.0.1",
service => 0,
socktype => "stream",
on_listen => sub {
$port = shift->sockport;
},
on_stream => sub {
my ( $stream ) = @_;
$stream->configure(
on_read => sub {
my ( $self, $buffref ) = @_;
return 0 unless $$buffref =~ s/^(.*?)$CRLF$CRLF//s;
my $header = $1;
my $response = ( $header =~ m{^GET /redir} )
? "HTTP/1.1 301 Moved Permanently$CRLF" .
"Content-Length: 0$CRLF" .
"Location: /moved$CRLF" .
"Connection: close$CRLF" .
"$CRLF"
: "HTTP/1.1 200 OK$CRLF" .
"Content-Type: text/plain$CRLF" .
"Content-Length: 2$CRLF" .
"Connection: close$CRLF" .
"$CRLF" .
"OK";
$self->write( $response );
return 1;
},
);
$loop->add( $stream );
},
on_listen_error => sub { die "Test failed early - $_[-1]" },
on_resolve_error => sub { die "Test failed early - $_[-1]" },
);
wait_for { defined $port };
my $code = \&IO::Async::Protocol::connect;
no warnings 'redefine';
local *IO::Async::Protocol::connect = sub {
my $self = shift;
my %args = @_;
$args{service} = $port if $args{service} eq 'http';
$args{service} = $port if $args{service} == 80;
$code->($self, %args);
};
my $response;
my $req = HTTP::Request->new(GET => '/redir');
$req->protocol('HTTP/1.1');
$req->header(Host => '127.0.0.1');
$http->do_request(
method => "GET",
host => '127.0.0.1',
request => $req,
on_response => sub {
$response = $_[0];
},
on_error => sub { die "Test failed early - $_[-1]" },
);
wait_for { defined $response };
is( $response->content_type, "text/plain", '$response->content_type' );
is( $response->content, "OK", '$response->content' );