Skip to content

Commit 2d93c4a

Browse files
trwyantoalders
authored andcommitted
Accept PeerAddr of 0.
This turned up while messing with a little Dancer2 app. The startup notification said 'Accepting connections at http://0:5000/', and the curl program is fine with that. But LWP::UserAgent was not, complaining that 'No Host option provided'. I tracked this to Net::HTTP::Methods, specifically its http_configure() method, which was called with $cnf = { ..., PeerAddr => 0, ... }. It computes my $peer = $cnf->{PeerAddr} || $cnf->{PeerHost}; and dies with the given message if $peer is false. The patch iterates over PeerAddr and PeerHost and sets $peer to the first found which is defined and not the empty string. I have installed this on my system, and LWP::UserAgent is now happy with 'http://0:5000/'. Now, I do not claim to know if a host of '0' is official in any way, but the fact that Dancer2 requests it and curl accepts it looks to me like a pragmatic reason to handle it. I was unable to get Perl 5.6.2 configured for testing, but the modified code does pass under 5.8.4, at least on my box. For what it is worth, perlver says the syntax is good back to 5.6.0.
1 parent fdc8787 commit 2d93c4a

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

lib/Net/HTTP/Methods.pm

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,14 @@ sub http_configure {
3535
die "Listen option not allowed" if $cnf->{Listen};
3636
my $explicit_host = (exists $cnf->{Host});
3737
my $host = delete $cnf->{Host};
38-
my $peer = $cnf->{PeerAddr} || $cnf->{PeerHost};
39-
if (!$peer) {
38+
# All this because $cnf->{PeerAddr} = 0 is actually valid.
39+
my $peer;
40+
for my $key (qw{PeerAddr PeerHost}) {
41+
next if !defined($cnf->{$key}) || q{} eq $cnf->{$key};
42+
$peer = $cnf->{$key};
43+
last;
44+
}
45+
if (!defined $peer) {
4046
die "No Host option provided" unless $host;
4147
$cnf->{PeerAddr} = $peer = $host;
4248
}

t/http.t

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use strict;
22
use warnings;
33
use Test::More;
44

5-
plan tests => 37;
5+
plan tests => 38;
66
#use Data::Dump ();
77

88
my $CRLF = "\015\012";
@@ -204,3 +204,13 @@ eval {
204204
print "# $@";
205205
ok($@);
206206

207+
$@ = '';
208+
eval {
209+
$h = HTTP->new(
210+
PeerAddr => 0,
211+
PeerPort => 5000,
212+
Proto => 'tcp',
213+
);
214+
};
215+
print '# $@ is ', $@ ? $@ : "''\n";
216+
ok(!$@);

0 commit comments

Comments
 (0)