user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH 1/5] daemon: use default address + well-known ports for scheme
Date: Mon,  8 Aug 2022 23:53:07 +0000	[thread overview]
Message-ID: <20220808235311.2006279-2-e@80x24.org> (raw)
In-Reply-To: <20220808235311.2006279-1-e@80x24.org>

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 | 45 +++++++++++++++++++++------------------
 1 file changed, 24 insertions(+), 21 deletions(-)

diff --git a/lib/PublicInbox/Daemon.pm b/lib/PublicInbox/Daemon.pm
index b6f4f9ed..0043d21e 100644
--- a/lib/PublicInbox/Daemon.pm
+++ b/lib/PublicInbox/Daemon.pm
@@ -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,

  reply	other threads:[~2022-08-08 23:53 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-08 23:53 [PATCH 0/5] daemon/netd-related improvements Eric Wong
2022-08-08 23:53 ` Eric Wong [this message]
2022-08-08 23:53 ` [PATCH 2/5] daemon: use per-listener SIG{__WARN__} callbacks Eric Wong
2022-08-08 23:53 ` [PATCH 3/5] daemon: cleanup internal data structures Eric Wong
2022-08-08 23:53 ` [PATCH 4/5] imap: mailboxes list across listeners Eric Wong
2022-08-08 23:53 ` [PATCH 5/5] imapd|nntpd: drop ->base_url preload Eric Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://public-inbox.org/README

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220808235311.2006279-2-e@80x24.org \
    --to=e@80x24.org \
    --cc=meta@public-inbox.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/public-inbox.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).