user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
Search results ordered by [date|relevance]  view[summary|nested|Atom feed]
thread overview below | download mbox.gz: |
* [PATCH 1/6] httpd: make internals slightly more generic
  2022-08-01 21:24  7% [PATCH 0/6] flesh out more -netd funcionality Eric Wong
@ 2022-08-01 21:24  7% ` Eric Wong
  0 siblings, 0 replies; 2+ results
From: Eric Wong @ 2022-08-01 21:24 UTC (permalink / raw)
  To: meta

This brings the HTTP server closer to the IMAP/NNTP/POP3
implementations and eliminates package-wide globals in
PublicInbox::HTTPD.  The end goal is to be able to host
completely different PSGI applications on different listen
ports.
---
 lib/PublicInbox/Daemon.pm | 10 +++----
 lib/PublicInbox/HTTP.pm   | 10 +++----
 lib/PublicInbox/HTTPD.pm  | 55 ++++++++++++++++++++++-----------------
 3 files changed, 41 insertions(+), 34 deletions(-)

diff --git a/lib/PublicInbox/Daemon.pm b/lib/PublicInbox/Daemon.pm
index bceae6e5..1af03cc4 100644
--- a/lib/PublicInbox/Daemon.pm
+++ b/lib/PublicInbox/Daemon.pm
@@ -81,11 +81,11 @@ sub load_mod ($) {
 	my $mod = $modc.'D';
 	eval "require $mod"; # IMAPD|HTTPD|NNTPD|POP3D
 	die $@ if $@;
-	my %xn = map { $_ => $mod->can($_) } qw(refresh post_accept);
-	$xn{tlsd} = $mod->new if $mod->can('refresh_groups'); #!HTTPD
-	my $tlsd = $xn{tlsd};
-	$xn{refresh} //= sub { $tlsd->refresh_groups(@_) };
-	$xn{post_accept} //= sub { $modc->new($_[0], $tlsd) };
+	my %xn;
+	my $tlsd = $xn{tlsd} = $mod->new;
+	$xn{refresh} = sub { $tlsd->refresh_groups(@_) };
+	$xn{post_accept} = $tlsd->can('post_accept_cb') ?
+			$tlsd->post_accept_cb : sub { $modc->new($_[0], $tlsd) };
 	$xn{af_default} = 'httpready' if $modc eq 'PublicInbox::HTTP';
 	\%xn;
 }
diff --git a/lib/PublicInbox/HTTP.pm b/lib/PublicInbox/HTTP.pm
index 76e978a2..669211e3 100644
--- a/lib/PublicInbox/HTTP.pm
+++ b/lib/PublicInbox/HTTP.pm
@@ -1,4 +1,4 @@
-# Copyright (C) 2016-2021 all contributors <meta@public-inbox.org>
+# Copyright (C) all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 #
 # Generic PSGI server for convenience.  It aims to provide
@@ -52,8 +52,8 @@ sub http_date () {
 }
 
 sub new ($$$) {
-	my ($class, $sock, $addr, $httpd) = @_;
-	my $self = bless { httpd => $httpd }, $class;
+	my ($class, $sock, $addr, $srv_env) = @_;
+	my $self = bless { srv_env => $srv_env }, $class;
 	my $ev = EPOLLIN;
 	my $wbuf;
 	if ($sock->can('accept_SSL') && !$sock->accept_SSL) {
@@ -78,7 +78,7 @@ sub event_step { # called by PublicInbox::DS
 	return read_input($self) if ref($self->{env});
 
 	my $rbuf = $self->{rbuf} // (\(my $x = ''));
-	my %env = %{$self->{httpd}->{env}}; # full hash copy
+	my %env = %{$self->{srv_env}}; # full hash copy
 	my $r;
 	while (($r = parse_http_request($$rbuf, \%env)) < 0) {
 		# We do not support Trailers in chunked requests, for
@@ -145,7 +145,7 @@ sub app_dispatch {
 	# note: NOT $self->{sock}, we want our close (+ PublicInbox::DS::close),
 	# to do proper cleanup:
 	$env->{'psgix.io'} = $self; # for ->close or async_pass
-	my $res = Plack::Util::run_app($self->{httpd}->{app}, $env);
+	my $res = Plack::Util::run_app($env->{'pi-httpd.app'}, $env);
 	eval {
 		if (ref($res) eq 'CODE') {
 			$res->(sub { response_write($self, $env, $_[0]) });
diff --git a/lib/PublicInbox/HTTPD.pm b/lib/PublicInbox/HTTPD.pm
index 715e4538..bcdbb9f9 100644
--- a/lib/PublicInbox/HTTPD.pm
+++ b/lib/PublicInbox/HTTPD.pm
@@ -13,12 +13,17 @@ use PublicInbox::HTTPD::Async;
 
 sub pi_httpd_async { PublicInbox::HTTPD::Async->new(@_) }
 
-sub new {
-	my ($class, $sock, $app, $client) = @_;
-	my $n = getsockname($sock) or die "not a socket: $sock $!\n";
-	my ($host, $port) = PublicInbox::Daemon::host_with_port($n);
+# we have a different env for ever listener socket for
+# SERVER_NAME, SERVER_PORT and psgi.url_scheme
+# envs: listener FD => PSGI env
+sub new { bless { envs => {} }, __PACKAGE__ }
 
-	my %env = (
+# this becomes {srv_env} in PublicInbox::HTTP
+sub env_for ($$$) {
+	my ($self, $srv, $client) = @_;
+	my $n = getsockname($srv) or die "not a socket: $srv $!\n";
+	my ($host, $port) = PublicInbox::Daemon::host_with_port($n);
+	{
 		SERVER_NAME => $host,
 		SERVER_PORT => $port,
 		SCRIPT_NAME => '',
@@ -40,26 +45,24 @@ sub new {
 		# this to limit git-http-backend(1) parallelism.
 		# We also check for the truthiness of this to
 		# detect when to use async paths for slow blobs
-		'pi-httpd.async' => \&pi_httpd_async
-	);
-	bless { app => $app, env => \%env }, $class;
+		'pi-httpd.async' => \&pi_httpd_async,
+		'pi-httpd.app' => $self->{app},
+	}
 }
 
-my %httpds; # per-listen-FD mapping for HTTPD->{env}->{SERVER_<NAME|PORT>}
-my $default_app; # ugh...
-
-sub refresh {
+sub refresh_groups {
+	my ($self) = @_;
+	my $app;
 	if (@main::ARGV) {
-		eval { $default_app = Plack::Util::load_psgi(@ARGV) };
-		if ($@) {
-			die $@,
-"$0 runs in /, command-line paths must be absolute\n";
-		}
+		eval { $app = Plack::Util::load_psgi(@ARGV) };
+		die $@, <<EOM if $@;
+$0 runs in /, command-line paths must be absolute
+EOM
 	} else {
 		require PublicInbox::WWW;
 		my $www = PublicInbox::WWW->new;
 		$www->preload;
-		$default_app = builder {
+		$app = builder {
 			eval { enable 'ReverseProxy' };
 			$@ and warn <<EOM;
 Plack::Middleware::ReverseProxy missing,
@@ -69,14 +72,18 @@ EOM
 			sub { $www->call(@_) };
 		};
 	}
-	%httpds = (); # invalidate cache
+	$_->{'pi-httpd.app'} = $app for values %{$self->{envs}};
+	$self->{app} = $app;
 }
 
-sub post_accept { # Listener->{post_accept}
-	my ($client, $addr, $srv) = @_; # $_[3] - tls_wrap (unused)
-	my $httpd = $httpds{fileno($srv)} //=
-				__PACKAGE__->new($srv, $default_app, $client);
-	PublicInbox::HTTP->new($client, $addr, $httpd),
+sub post_accept_cb { # for Listener->{post_accept}
+	my ($self) = @_;
+	sub {
+		my ($client, $addr, $srv) = @_; # $_[4] - tls_wrap (unused)
+		PublicInbox::HTTP->new($client, $addr,
+				$self->{envs}->{fileno($srv)} //=
+					env_for($self, $srv, $client));
+	}
 }
 
 1;

^ permalink raw reply related	[relevance 7%]

* [PATCH 0/6] flesh out more -netd funcionality
@ 2022-08-01 21:24  7% Eric Wong
  2022-08-01 21:24  7% ` [PATCH 1/6] httpd: make internals slightly more generic Eric Wong
  0 siblings, 1 reply; 2+ results
From: Eric Wong @ 2022-08-01 21:24 UTC (permalink / raw)
  To: meta

These changes will allow public-inbox-netd to host multiple,
completely-unrelated .psgi apps within the same process via
psgi= as a per-listener option.  Having separate stdout/stderr
facsimiles is also supported via err= and out= keys (HTTP(S)
only has err= for $env->{'psgi.errors'}).

(public-inbox-{nntp,imap,pop3,http}d can actually do all that
-netd can do, too, the only difference is -netd has no default
port/protocol).

Further optimizations (PublicInbox::Config object sharing)
and reload improvements (TLS cert reload on SIGHUP) are on
the way...

Eric Wong (6):
  httpd: make internals slightly more generic
  daemon: support per-listener env, .psgi, out, err
  daemon: require absolute cert/key paths with --daemonize
  daemon: add diagnostics about inherited/bound listeners
  daemon: allow listening on well-known ports based on protocol
  daemon: share FDs for identical log paths

 Documentation/public-inbox-daemon.pod |  51 ++++++--
 Documentation/public-inbox-netd.pod   |  34 ++++--
 MANIFEST                              |   1 +
 lib/PublicInbox/Daemon.pm             | 168 +++++++++++++++++---------
 lib/PublicInbox/HTTP.pm               |  10 +-
 lib/PublicInbox/HTTPD.pm              |  60 +++++----
 lib/PublicInbox/IMAPD.pm              |   3 +-
 lib/PublicInbox/NNTPD.pm              |  25 ++--
 lib/PublicInbox/POP3D.pm              |  36 +++---
 t/alt.psgi                            |  17 +++
 t/httpd-corner.psgi                   |   8 +-
 t/httpd-corner.t                      |  39 +++++-
 12 files changed, 304 insertions(+), 148 deletions(-)
 create mode 100644 t/alt.psgi

^ permalink raw reply	[relevance 7%]

Results 1-2 of 2 | reverse | options above
-- pct% links below jump to the message on this page, permalinks otherwise --
2022-08-01 21:24  7% [PATCH 0/6] flesh out more -netd funcionality Eric Wong
2022-08-01 21:24  7% ` [PATCH 1/6] httpd: make internals slightly more generic Eric Wong

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).