develooper Front page | perl.libwww | Postings from March 2001

Re: HTTP::Daemon url (httpd_server_name) bug? (+fix)

From:
Gisle Aas
Date:
March 8, 2001 10:19
Subject:
Re: HTTP::Daemon url (httpd_server_name) bug? (+fix)
Message ID:
lrr9086s46.fsf@caliper.ActiveState.com
Ben Low <ben@snrc.uow.edu.au> writes:

> I'm using your HTTP::Daemon module (thanks!), and have a query regarding
> the value returned by the url method.
> 
> As at version 1.22, HTTP::Daemon's constructor fills in an invalid
> httpd_server_name by using the IO::Socket LocalAddr option (which may
> include the port number).
> 
>  $d = new HTTP::Daemon LocalAddr => "blah:8080";
> 
>  $d = new HTTP::Daemon LocalAddr => ":8080";
> 
> which return a url of "http://blah:8080:8080/", and
> "http://:8080:8080/", respectively.
> 
> The attached patch (against v1.22) corrects this behaviour by not
> assuming that the given LocalAddr argument is a hostname - it looks up
> the hostname via the socket's sockhost method, further defaulting to the
> hostname in lieu of the 0.0.0.0 address.

Ok.  It seems to turn your first example into a URL with the
IP-address in the name.  This is the patch that I checked in:

===================================================================
RCS file: /cvsroot/libwww-perl/lwp5/lib/HTTP/Daemon.pm,v
retrieving revision 1.22
retrieving revision 1.23
diff -u -p -d -u -r1.22 -r1.23
--- lib/HTTP/Daemon.pm  2001/01/04 21:43:10     1.22
+++ lib/HTTP/Daemon.pm  2001/03/08 18:13:37     1.23
@@ -60,9 +60,9 @@ to the I<IO::Socket::INET> base class.
 
 use vars qw($VERSION @ISA $PROTO $DEBUG);
 
-$VERSION = sprintf("%d.%02d", q$Revision: 1.22 $ =~ /(\d+)\.(\d+)/);
+$VERSION = sprintf("%d.%02d", q$Revision: 1.23 $ =~ /(\d+)\.(\d+)/);
 
-use IO::Socket ();
+use IO::Socket qw(AF_INET INADDR_ANY inet_ntoa);
 @ISA=qw(IO::Socket::INET);
 
 $PROTO = "HTTP/1.1";
@@ -87,16 +87,7 @@ sub new
     my($class, %args) = @_;
     $args{Listen} ||= 5;
     $args{Proto}  ||= 'tcp';
-    my $self = $class->SUPER::new(%args);
-    return undef unless $self;
-
-    my $host = $args{LocalAddr};
-    unless ($host) {
-       require Sys::Hostname;
-       $host = lc Sys::Hostname::hostname();
-    }
-    ${*$self}{'httpd_server_name'} = $host;
-    $self;
+    return $class->SUPER::new(%args);
 }
 
 
@@ -129,7 +120,14 @@ sub url
 {
     my $self = shift;
     my $url = "http://";
-    $url .= ${*$self}{'httpd_server_name'};
+    my $addr = $self->sockaddr;
+    if ($addr eq INADDR_ANY) {
+       require Sys::Hostname;
+       $url .= lc Sys::Hostname::hostname();
+    }
+    else {
+       $url .= gethostbyaddr($addr, AF_INET) || inet_ntoa($addr);
+    }
     my $port = $self->sockport;
     $url .= ":$port" if $port != 80;
     $url .= "/";

> 
> Regards,
> 
> Ben Low--- libwww-perl-5.50/lib/HTTP/Daemon.pm	Fri Jan  5 08:43:10 2001
> +++ /tmp/HTTP/Daemon.pm	Thu Mar  8 21:58:45 2001
> @@ -90,8 +90,10 @@
>      my $self = $class->SUPER::new(%args);
>      return undef unless $self;
>  
> -    my $host = $args{LocalAddr};
> -    unless ($host) {
> +	# LocalAddr may include (just the) port number, ask the socket for the 
> +	# actual bound address (tell the user 'hostname' for INADDR_ANY)
> +	my $host = $self->sockhost;
> +    unless ($host and  $host ne '0.0.0.0') {
>  	require Sys::Hostname;
>  	$host = lc Sys::Hostname::hostname();
>      }



nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About