]> git.ipfire.org Git - thirdparty/public-inbox.git/commitdiff
httpd: psgix.io is the underlying IO object
authorEric Wong <e@80x24.org>
Wed, 7 May 2025 00:16:33 +0000 (00:16 +0000)
committerEric Wong <e@80x24.org>
Thu, 8 May 2025 18:13:04 +0000 (18:13 +0000)
PSGI::Extensions(3pm) states psgix.io should be "the raw IO
socket", so we shall conform to that specification.  For our
codebase to continue taking advantage of
public-inbox-daemon-specific features, we now expose the
PublicInbox::HTTP object via $env->{'pi-httpd.client'}.  The
`pi-httpd.async' boolean field is now gone, since we can just
test for the presence of `pi-httpd.app' or `pi-httpd.client'
to avoid occupying another hash table entry.

15 files changed:
lib/PublicInbox/ExtMsg.pm
lib/PublicInbox/GzipFilter.pm
lib/PublicInbox/HTTP.pm
lib/PublicInbox/HTTPD.pm
lib/PublicInbox/Limiter.pm
lib/PublicInbox/MailDiff.pm
lib/PublicInbox/ManifestJsGz.pm
lib/PublicInbox/Qspawn.pm
lib/PublicInbox/SolverGit.pm
lib/PublicInbox/ViewVCS.pm
lib/PublicInbox/WWW.pm
lib/PublicInbox/WwwAttach.pm
lib/PublicInbox/WwwListing.pm
lib/PublicInbox/WwwStatic.pm
t/httpd-corner.psgi

index dd7240fb2249b44c6c9d3d8cf8b2f7a274b37106..251e430e826fd16c8554b7998f6b0cf5445d65ad 100644 (file)
@@ -141,7 +141,7 @@ sub ext_msg {
        ext_msg_ALL($ctx) // sub {
                $ctx->{-wcb} = $_[0]; # HTTP server write callback
 
-               if ($ctx->{env}->{'pi-httpd.async'}) {
+               if ($ctx->{env}->{'pi-httpd.app'}) {
                        require PublicInbox::ConfigIter;
                        my $iter = PublicInbox::ConfigIter->new(
                                                $ctx->{www}->{pi_cfg},
@@ -181,7 +181,7 @@ sub finalize_exact {
                push @{$ctx->{partial}}, [ $cur, $mids ];
        } elsif ($ctx->{again} && length($mid) >= $MIN_PARTIAL_LEN) {
                bless $ctx, __PACKAGE__;
-               if ($ctx->{env}->{'pi-httpd.async'}) {
+               if ($ctx->{env}->{'pi-httpd.app'}) {
                        $ctx->event_step;
                        return;
                }
index 2d164a4bf5cbb440f646fc33317893ccb22eef18..386f7b6b3f8b446ff08a12f86037287e8b226033 100644 (file)
@@ -31,7 +31,7 @@ my @GZIP_HDRS = qw(Vary Accept-Encoding Content-Encoding gzip);
 
 sub new { bless {}, shift } # qspawn filter
 
-# for Qspawn if using $env->{'pi-httpd.async'}
+# for Qspawn if using $env->{'pi-httpd.client'}
 sub attach {
        my ($self, $http_out) = @_;
        $self->{http_out} = $http_out; # PublicInbox::HTTP::{Chunked,Identity}
@@ -54,8 +54,7 @@ sub gzf_maybe ($$) { bless { gz => gz_or_noop(@_) }, __PACKAGE__ }
 sub psgi_response {
        # $code may be an HTTP response code (e.g. 200) or a CODE ref (mbox_hdr)
        my ($self, $code, $res_hdr) = @_;
-       if ($self->{env}->{'pi-httpd.async'}) {
-               my $http = $self->{env}->{'psgix.io'}; # PublicInbox::HTTP
+       if (my $http = $self->{env}->{'pi-httpd.client'}) {
                $http->{forward} = $self;
                sub {
                        my ($wcb) = @_; # -httpd provided write callback
@@ -98,7 +97,7 @@ sub gone { # what: search/over/mm
        undef;
 }
 
-# for GetlineResponse (via Qspawn) when NOT using $env->{'pi-httpd.async'}
+# for GetlineResponse (via Qspawn) when NOT using $env->{'pi-httpd.client'}
 # Also used for ->getline callbacks
 sub translate {
        my $self = shift; # $_[1] => input
@@ -181,7 +180,7 @@ sub bail {
        my $self = shift;
        carp @_;
        my $env = $self->{env} or return;
-       my $http = $env->{'psgix.io'} or return; # client abort
+       my $http = $env->{'pi-httpd.client'} or return; # client abort
        eval { $http->close }; # should hit our close
        carp "E: error in http->close: $@" if $@;
        eval { $self->close }; # just in case...
@@ -191,7 +190,7 @@ sub bail {
 # this is public-inbox-httpd-specific
 sub async_blob_cb { # git->cat_async callback
        my ($bref, $oid, $type, $size, $self) = @_;
-       my $http = $self->{env}->{'psgix.io'}; # PublicInbox::HTTP
+       my $http = $self->{env}->{'pi-httpd.client'}; # PublicInbox::HTTP
        $http->{forward} or return; # client aborted
        my $smsg = $self->{smsg} or return bail($self, 'BUG: no smsg');
        $type // return
index 407f476b315e86a0445797f00f3286bb1d0dbd01..01a686a4d550514206a3466934e8026de0857ff4 100644 (file)
@@ -157,7 +157,8 @@ sub app_dispatch {
        sysseek($input, 0, SEEK_SET) if defined $input;
        # note: NOT $self->{sock}, we want our close (+ PublicInbox::DS::close),
        # to do proper cleanup:
-       $env->{'psgix.io'} = $self; # for ->close or async_pass
+       $env->{'psgix.io'} = $self->{sock}; # for pi-httpd.ckhup
+       $env->{'pi-httpd.client'} = $self; # for ->close or async_pass
        my $res = Plack::Util::run_app($env->{'pi-httpd.app'}, $env);
        eval {
                if (ref($res) eq 'CODE') {
index 55d728cbd4bafba82f503526234e63d8e237fa46..68ffaebe97b9b9cc080d46c487fb6a4502776f02 100644 (file)
@@ -42,7 +42,7 @@ sub env_for ($$$) {
                # 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' => 1,
+               # 'pi-httpd.client' => PublicInbox::HTTP object
                'pi-httpd.app' => $self->{app},
                'pi-httpd.warn_cb' => $self->{warn_cb},
                'pi-httpd.ckhup' => $port ? \&PublicInbox::Daemon::tcp_hup :
index f90c8b568f07e9bd8de700fcd0530c9823ff37a7..27a7015cffd777c31e1b8ee43872280d0c090f01 100644 (file)
@@ -81,7 +81,7 @@ sub _start_next { # on_destroy cb
                $rec = shift @{$self->{run_queue}} or return;
                ($start_cb, $ctx, $fail_cb) = @$rec;
                $ck = $ctx->{env}->{'pi-httpd.ckhup'} or last;
-               $ck->($ctx->{env}->{'psgix.io'}->{sock}) or last;
+               $ck->($ctx->{env}->{'psgix.io'}) or last;
                $fail_cb->($ctx, 499, 'client disconnected');
        }
        _do_start $self, $start_cb, $ctx, $fail_cb;
index ce268bfb734dc654828d8c7ef556e3ff1777e741..a0e281c5ef9c9138d21f134f8069e67b5dbebe04 100644 (file)
@@ -60,7 +60,7 @@ sub next_smsg ($) {
                $ctx->write('</pre>', $ctx->_html_end);
                return $ctx->close;
        }
-       PublicInbox::DS::requeue($self) if $ctx->{env}->{'pi-httpd.async'};
+       PublicInbox::DS::requeue($self) if $ctx->{env}->{'pi-httpd.app'};
 }
 
 sub emit_msg_diff {
@@ -112,7 +112,7 @@ sub event_step {
        my ($self) = @_;
        eval {
                my $ctx = $self->{ctx};
-               if ($ctx->{env}->{'pi-httpd.async'}) {
+               if ($ctx->{env}->{'pi-httpd.app'}) {
                        ibx_async_cat($ctx->{ibx}, $self->{smsg}->{blob},
                                        \&diff_msg_i_async, $self);
                } else {
@@ -128,7 +128,7 @@ sub event_step {
 
 sub begin_mail_diff {
        my ($self) = @_;
-       if ($self->{ctx}->{env}->{'pi-httpd.async'}) {
+       if ($self->{ctx}->{env}->{'pi-httpd.app'}) {
                PublicInbox::DS::requeue($self);
        } else {
                event_step($self) while $self->{smsg};
index f912f81234b60c29988079f58d50d1746bf61a8b..ca37c1ad375ecd0a63a6fbe7dd0a44f50a9054fa 100644 (file)
@@ -83,7 +83,7 @@ sub response {
        sub {
                $ctx->{-wcb} = $_[0]; # HTTP server callback
                ($ctx->{www}->{pi_cfg}->ALL ||
-                                       !$ctx->{env}->{'pi-httpd.async'}) ?
+                                       !$ctx->{env}->{'pi-httpd.app'}) ?
                                $iter->each_section : $iter->event_step;
        }
 }
index bd8cd35b0e955172abf941442b0abd7f42eaa592..64a11cab12970459d33daeb69cd443a7b8165b25 100644 (file)
@@ -234,7 +234,7 @@ sub istream_cb { # InputStream callback
 
 sub _yield_start { # may run later, much later...
        my ($self) = @_;
-       if ($self->{env}->{'pi-httpd.async'}) {
+       if ($self->{env}->{'pi-httpd.app'}) {
                my $rpipe = $self->{rpipe};
                PublicInbox::InputStream::consume($rpipe, \&istream_cb, $self);
        } else {
@@ -254,7 +254,7 @@ sub _yield_start { # may run later, much later...
 #                          sub for the PSGI server to call
 #
 #   $env->{'qspawn.filter'} - filter object, responds to ->attach for
-#                             pi-httpd.async and ->translate for generic
+#                             pi-httpd.client and ->translate for generic
 #                             PSGI servers
 #
 # $limiter - the Limiter object to use (uses the def_limiter if not given)
@@ -264,7 +264,7 @@ sub _yield_start { # may run later, much later...
 #              and a string ref of the current buffer.  Returns an arrayref
 #              for PSGI responses.  2-element arrays in PSGI mean the
 #              body will be streamed, later, via writes (push-based) to
-#              psgix.io.  3-element arrays means the body is available
+#              pi-httpd.client.  3-element arrays means the body is available
 #              immediately (or streamed via ->getline (pull-based)).
 
 sub psgi_yield {
index a17e154225f9a3991d7d60dd30095873920cd438..cca1a5169c1d6e5cfca5612f79bb44002bc29e67 100644 (file)
@@ -175,7 +175,7 @@ sub solve_existing ($$) {
        my $try = $want->{try_gits} //= [ @{$self->{gits}} ]; # array copy
        my $git = $try->[0] // die 'BUG {try_gits} empty';
 
-       if ($self->{psgi_env}->{'pi-httpd.async'}) {
+       if ($self->{psgi_env}->{'pi-httpd.app'}) {
                async_check({ git => $git }, $want->{oid_b},
                                \&ck_existing_cb, [ $self, $want ]);
                $git->schedule_cleanup;
@@ -396,7 +396,7 @@ sub do_finish ($) {
 
        # let git disambiguate if oid_want was too short,
        # but long enough to be unambiguous:
-       if ($self->{psgi_env}->{'pi-httpd.async'}) {
+       if ($self->{psgi_env}->{'pi-httpd.app'}) {
                async_check { git => $self->{tmp_git} }, $oid_want,
                                \&finish_cb, $self
        } else {
@@ -451,7 +451,7 @@ sub event_step ($) {
 sub next_step ($) {
        # if outside of public-inbox-httpd, caller is expected to be
        # looping event_step, anyways
-       PublicInbox::DS::requeue($_[0]) if $_[0]->{psgi_env}->{'pi-httpd.async'}
+       PublicInbox::DS::requeue($_[0]) if $_[0]->{psgi_env}->{'pi-httpd.app'}
 }
 
 sub mark_found ($$$) {
@@ -495,7 +495,7 @@ sub parse_ls_files ($$) {
 
        dbg($self, "index at:\n$mode_b $oid_b_full\t".git_quote($file));
        my $tmp_git = $self->{tmp_git} or die 'no git working tree';
-       if ($self->{psgi_env}->{'pi-httpd.async'}) {
+       if ($self->{psgi_env}->{'pi-httpd.app'}) {
                async_check { git => $tmp_git }, $oid_b_full,
                        \&ck_size_cb, $self
        } else {
@@ -678,7 +678,7 @@ sub resolve_patch ($$) {
 
        if (my $msgs = $want->{try_smsgs}) {
                my $smsg = shift @$msgs;
-               if ($self->{psgi_env}->{'pi-httpd.async'}) {
+               if ($self->{psgi_env}->{'pi-httpd.app'}) {
                        return ibx_async_cat($want->{cur_ibx}, $smsg->{blob},
                                                \&extract_diff_async,
                                                [$self, $want, $smsg]);
@@ -744,7 +744,7 @@ sub solve ($$$$$) {
        $self->{found} = {}; # { abbr => [ ::Git, oid, type, size, $di ] }
 
        dbg($self, "solving $oid_want ...");
-       if ($env->{'pi-httpd.async'}) {
+       if ($env->{'pi-httpd.app'}) {
                PublicInbox::DS::requeue($self);
        } else {
                event_step($self) while $self->{user_cb};
index 3ad279a721dc671e155b8431594f307ea3bd92f5..76986f68cab7ca0f2dec993a0bf19389d21ec3c9 100644 (file)
@@ -134,7 +134,7 @@ sub do_cat_async {
        # favor git(1) over Lg2 (libgit2) for SHA-256 support
        my $ctx = ref $arg eq 'ARRAY' ? $arg->[0] : $arg;
        $ctx->{git}->cat_async($_, $cb, $arg) for @req;
-       if ($ctx->{env}->{'pi-httpd.async'}) {
+       if ($ctx->{env}->{'pi-httpd.app'}) {
                $ctx->{git}->watch_async;
        } else { # synchronous, generic PSGI
                $ctx->{git}->cat_async_wait;
@@ -143,7 +143,7 @@ sub do_cat_async {
 
 sub do_check_async {
        my ($ctx, $cb, @req) = @_;
-       if ($ctx->{env}->{'pi-httpd.async'}) {
+       if ($ctx->{env}->{'pi-httpd.app'}) {
                async_check($ctx, $_, $cb, $ctx) for @req;
        } else { # synchronous, generic PSGI
                $ctx->{git}->check_async($_, $cb, $ctx) for @req;
index 50f182af1d1f17426a5aae8b05a0299ff32e17c6..31099f09c153dd80f8a89930c28b322a2a3c442e 100644 (file)
@@ -774,7 +774,7 @@ sub event_step { # called via requeue
        # gzf = PublicInbox::GzipFilter == $ctx
        my $gzf = shift(@{$self->{-low_prio_q}}) // return;
        PublicInbox::DS::requeue($self) if scalar(@{$self->{-low_prio_q}});
-       my $http = $gzf->{env}->{'psgix.io'}; # PublicInbox::HTTP
+       my $http = $gzf->{env}->{'pi-httpd.client'}; # PublicInbox::HTTP
        $http->next_step($gzf->can('async_next'));
 }
 
index d297ce7ac659fd9f6045031eba32a80ce4691164..f93f32b05b0fee85b9837673b5290c04dce39b5a 100644 (file)
@@ -78,7 +78,7 @@ sub async_next {
 
 sub scan_attach ($) { # public-inbox-httpd only
        my ($ctx) = @_;
-       $ctx->{env}->{'psgix.io'}->{forward} = $ctx;
+       $ctx->{env}->{'pi-httpd.client'}->{forward} = $ctx;
        $ctx->smsg_blob($ctx->{smsg});
 }
 
@@ -94,7 +94,7 @@ sub get_attach ($$$) {
                return sub { # public-inbox-httpd-only
                        $ctx->{wcb} = $_[0];
                        scan_attach($ctx);
-               } if $ctx->{env}->{'pi-httpd.async'};
+               } if $ctx->{env}->{'pi-httpd.app'};
                # generic PSGI:
                $eml = $ctx->{ibx}->smsg_eml($ctx->{smsg});
        } elsif (!$ctx->{ibx}->over) {
index 101da9b7235305973bbdb59706d33e5e708b8d83..59b4741932076f42726d6d78385dfa8d23193189 100644 (file)
@@ -156,7 +156,7 @@ sub response {
                                                \&list_match_i, $re, $ctx);
                sub {
                        $ctx->{-wcb} = $_[0]; # HTTP server callback
-                       $ctx->{env}->{'pi-httpd.async'} ?
+                       $ctx->{env}->{'pi-httpd.app'} ?
                                        $iter->event_step : $iter->each_section;
                }
        }
index 37f95a11a426db86bf90a5c2917d1ba07acd8db8..68d11f75656ab88eba88a1464d1f845fbdca82e2 100644 (file)
@@ -55,9 +55,9 @@ sub r ($;$) {
 sub getline_response ($$$$$) {
        my ($env, $in, $off, $len, $path) = @_;
        my $r = bless {}, __PACKAGE__;
-       if ($env->{'pi-httpd.async'}) { # public-inbox-httpd-only mode
+       if (my $http = $env->{'pi-httpd.client'}) { # public-inbox-only mode
                $env->{'psgix.no-compress'} = 1; # do not chunk response
-               %$r = ( bypass => [$in, $off, $len, $env->{'psgix.io'}] );
+               %$r = ( bypass => [$in, $off, $len, $http] );
        } else {
                %$r = ( in => $in, off => $off, len => $len, path => $path );
        }
index 0dd840a013a97c90a103a121d8d1ef4a56d76ab6..b38ffd56ed67d44eac440351e778de85fea6b6f6 100644 (file)
@@ -81,8 +81,8 @@ my $app = sub {
                $code = 200;
                push @$body, "$env->{REMOTE_ADDR} $env->{REMOTE_PORT}";
        } elsif ($path eq '/session_reused') {
-               my $http = $env->{'psgix.io'}; # PublicInbox::HTTP
-               $body = [ $http->{sock}->get_session_reused ? "y\n" : "n\n" ];
+               my $sock = $env->{'psgix.io'}; # IO::Socket::SSL
+               $body = [ $sock->get_session_reused ? "y\n" : "n\n" ];
                $code = 200;
        } elsif ($path eq '/callback') {
                return sub {