]> Sergey Matveev's repositories - public-inbox.git/commitdiff
daemon: use default address + well-known ports for scheme
authorEric Wong <e@80x24.org>
Mon, 8 Aug 2022 23:53:07 +0000 (23:53 +0000)
committerEric Wong <e@80x24.org>
Tue, 9 Aug 2022 16:41:50 +0000 (16:41 +0000)
This ensures the "bound $URL" diagnostic message at startup
always shows the URL scheme handled if not relying on socket
inheritance.

This also avoids duplicate/unused data structures when binding
sockets ourselves, as bound socket names can expand from short
names to longer names (e.g. "0:119" => "0.0.0.0:119").

lib/PublicInbox/Daemon.pm

index b6f4f9ed06123b5555a7516e4591e8eaeae2e20d..0043d21eef4be44b7f391d6d1ce4099d2f091d20 100644 (file)
@@ -192,20 +192,23 @@ EOF
 
        foreach my $l (@cfg_listen) {
                my $orig = $l;
-               my $scheme = '';
-               my $port;
-               if ($l =~ s!\A([^:]+)://!!) { $scheme = $1 }
+               my ($scheme, $port, $opt);
+
+               $l =~ s!\A([a-z0-9]+)://!! and $scheme = $1;
+               (!$scheme && ($default_listen // '') =~ m!\A([^:]+)://!) and
+                       $scheme = $1;
                if ($l =~ /\A(?:\[[^\]]+\]|[^:]+):([0-9]+)/) {
                        $port = $1 + 0;
-                       my $s = $KNOWN_TLS{$port} // $KNOWN_STARTTLS{$port};
-                       $scheme //= $s if defined $s;
-               } elsif (index($l, '/') != 0) { # unix socket
-                       $port //= $SCHEME2PORT{$scheme} if $scheme;
-                       $port // die "no port in listen=$l\n";
+                       $scheme //= $KNOWN_TLS{$port} // $KNOWN_STARTTLS{$port};
+               }
+               $scheme or die "unable to determine URL scheme of $orig\n";
+               if (!defined($port) && index($l, '/') != 0) { # unix socket
+                       $port = $SCHEME2PORT{$scheme} //
+                               die "no port in listen=$orig\n";
                        $l =~ s!\A([^/]+)!$1:$port! or
                                die "unable to add port=$port to $l\n";
                }
-               my $opt; # non-TLS options
+               $l =~ s!/\z!!; # chop one trailing slash
                if ($l =~ s!/?\?(.+)\z!!) {
                        $opt = listener_opt($1);
                        $tls_opt{"$scheme://$l"} = accept_tls_opt($opt);
@@ -214,10 +217,10 @@ EOF
                } elsif ($scheme =~ /\A(?:https|imaps|nntps|pop3s)\z/) {
                        die "$orig specified w/o cert=\n";
                }
-               $scheme =~ /\A(?:http|imap|nntp|pop3)/ and
+               if ($listener_names->{$l}) { # already inherited
                        $xnetd->{$l} = load_mod($scheme, $opt, $l);
-
-               next if $listener_names->{$l}; # already inherited
+                       next;
+               }
                my (%o, $sock_pkg);
                if (index($l, '/') == 0) {
                        $sock_pkg = 'IO::Socket::UNIX';
@@ -244,16 +247,16 @@ EOF
                }
                $o{Listen} = 1024;
                my $prev = umask 0000;
-               my $s = eval { $sock_pkg->new(%o) };
-               warn "error binding $l: $! ($@)\n" unless $s;
+               my $s = eval { $sock_pkg->new(%o) } or
+                       warn "error binding $l: $! ($@)\n";
                umask $prev;
-               if ($s) {
-                       $s->blocking(0);
-                       my $k = sockname($s);
-                       warn "# bound $scheme://$k\n";
-                       $listener_names->{$k} = $s;
-                       push @listeners, $s;
-               }
+               $s // next;
+               $s->blocking(0);
+               my $sockname = sockname($s);
+               warn "# bound $scheme://$sockname\n";
+               $xnetd->{$sockname} //= load_mod($scheme);
+               $listener_names->{$sockname} = $s;
+               push @listeners, $s;
        }
 
        # cert/key options in @cfg_listen takes precedence when inheriting,