@@ -7,8 +7,12 @@ use warnings;
77
88our $VERSION = ' 6.05' ;
99
10- use IO::Socket qw( AF_INET INADDR_ANY INADDR_LOOPBACK inet_ntoa) ;
11- our @ISA = qw( IO::Socket::INET) ;
10+ use Socket qw(
11+ AF_INET AF_INET6 INADDR_ANY IN6ADDR_ANY INADDR_LOOPBACK IN6ADDR_LOOPBACK
12+ inet_ntop sockaddr_family
13+ ) ;
14+ use IO::Socket::IP;
15+ our @ISA = qw( IO::Socket::IP) ;
1216
1317our $PROTO = " HTTP/1.1" ;
1418
@@ -38,15 +42,31 @@ sub url {
3842 my $self = shift ;
3943 my $url = $self -> _default_scheme . " ://" ;
4044 my $addr = $self -> sockaddr;
41- if (!$addr || $addr eq INADDR_ANY) {
45+ if (!$addr || $addr eq INADDR_ANY || $addr eq IN6ADDR_ANY ) {
4246 require Sys::Hostname;
4347 $url .= lc Sys::Hostname::hostname();
4448 }
4549 elsif ($addr eq INADDR_LOOPBACK) {
46- $url .= inet_ntoa($addr );
50+ $url .= inet_ntop(AF_INET, $addr );
51+ }
52+ elsif ($addr eq IN6ADDR_LOOPBACK) {
53+ $url .= ' [' . inet_ntop(AF_INET6, $addr ) . ' ]' ;
4754 }
4855 else {
49- $url .= gethostbyaddr ($addr , AF_INET) || inet_ntoa($addr );
56+ my $host = $self -> sockhostname;
57+ if (!defined $host ) {
58+ my $family = sockaddr_family($self -> sockname);
59+ if ($family && $family == AF_INET6) {
60+ $host = ' [' . inet_ntop(AF_INET6, $addr ) . ' ]' ;
61+ }
62+ elsif ($family && $family == AF_INET) {
63+ $host = inet_ntop(AF_INET, $addr );
64+ }
65+ else {
66+ die " Unknown family" ;
67+ }
68+ }
69+ $url .= $host ;
5070 }
5171 my $port = $self -> sockport;
5272 $url .= " :$port " if $port != $self -> _default_port;
@@ -72,8 +92,8 @@ package # hide from PAUSE
7292use strict;
7393use warnings;
7494
75- use IO::Socket ();
76- our @ISA = qw( IO::Socket::INET ) ;
95+ use IO::Socket::IP ();
96+ our @ISA = qw( IO::Socket::IP ) ;
7797our $DEBUG ;
7898*DEBUG = \$HTTP::Daemon::DEBUG ;
7999
@@ -613,12 +633,12 @@ __END__
613633
614634Instances of the C<HTTP::Daemon > class are HTTP/1.1 servers that
615635listen on a socket for incoming requests. The C<HTTP::Daemon > is a
616- subclass of C<IO::Socket::INET > , so you can perform socket operations
636+ subclass of C<IO::Socket::IP > , so you can perform socket operations
617637directly on it too.
618638
619639The accept() method will return when a connection from a client is
620640available. The returned value will be an C<HTTP::Daemon::ClientConn >
621- object which is another C<IO::Socket::INET > subclass. Calling the
641+ object which is another C<IO::Socket::IP > subclass. Calling the
622642get_request() method on this object will read data from the client and
623643return an C<HTTP::Request > object. The ClientConn object also provide
624644methods to send back various responses.
@@ -629,7 +649,7 @@ desirable. Also note that the user is responsible for generating
629649responses that conform to the HTTP/1.1 protocol.
630650
631651The following methods of C<HTTP::Daemon > are new (or enhanced) relative
632- to the C<IO::Socket::INET > base class:
652+ to the C<IO::Socket::IP > base class:
633653
634654=over 4
635655
@@ -638,7 +658,7 @@ to the C<IO::Socket::INET> base class:
638658=item $d = HTTP::Daemon->new( %opts )
639659
640660The constructor method takes the same arguments as the
641- C<IO::Socket::INET > constructor, but unlike its base class it can also
661+ C<IO::Socket::IP > constructor, but unlike its base class it can also
642662be called without any arguments. The daemon will then set up a listen
643663queue of 5 connections and allocate some random port number.
644664
@@ -650,7 +670,7 @@ HTTP port will be constructed like this:
650670 LocalPort => 80,
651671 );
652672
653- See L<IO::Socket::INET > for a description of other arguments that can
673+ See L<IO::Socket::IP > for a description of other arguments that can
654674be used configure the daemon during construction.
655675
656676=item $c = $d->accept
@@ -667,7 +687,7 @@ class a subclass of C<HTTP::Daemon::ClientConn>.
667687
668688The accept method will return C<undef > if timeouts have been enabled
669689and no connection is made within the given time. The timeout() method
670- is described in L<IO::Socket> .
690+ is described in L<IO::Socket::IP > .
671691
672692In list context both the client object and the peer address will be
673693returned; see the description of the accept method L<IO::Socket> for
@@ -689,7 +709,7 @@ replaced with the version number of this module.
689709
690710=back
691711
692- The C<HTTP::Daemon::ClientConn > is a C<IO::Socket::INET >
712+ The C<HTTP::Daemon::ClientConn > is a C<IO::Socket::IP >
693713subclass. Instances of this class are returned by the accept() method
694714of C<HTTP::Daemon > . The following methods are provided:
695715
@@ -863,6 +883,6 @@ Return a reference to the corresponding C<HTTP::Daemon> object.
863883
864884RFC 2616
865885
866- L<IO::Socket::INET > , L<IO::Socket>
886+ L<IO::Socket::IP > , L<IO::Socket>
867887
868888=cut
0 commit comments