1 # Copyright (C) all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # XXX This is a totally unstable API for public-inbox internal use only
5 # This is exposed via the 'pi-httpd.async' key in the PSGI env hash.
6 # The name of this key is not even stable!
7 # Currently intended for use with read-only pipes with expensive
8 # processes such as git-http-backend(1), cgit(1)
11 # http: PublicInbox::HTTP ref
12 # fh: PublicInbox::HTTP::{Identity,Chunked} ref (can ->write + ->close)
13 # cb: initial read callback
15 # end_obj: CODE or object which responds to ->event_step when ->close is called
16 package PublicInbox::HTTPD::Async;
18 use parent qw(PublicInbox::DS);
20 use PublicInbox::Syscall qw(EPOLLIN);
22 # This is called via: $env->{'pi-httpd.async'}->()
23 # $io is a read-only pipe ($rpipe) for now, but may be a
24 # bidirectional socket in the future.
26 my ($class, $io, $cb, $arg, $end_obj) = @_;
28 # no $io? call $cb at the top of the next event loop to
30 unless (defined($io)) {
31 PublicInbox::DS::requeue($cb ? $cb : $arg);
32 die '$end_obj unsupported w/o $io' if $end_obj;
36 cb => $cb, # initial read callback
37 arg => $arg, # arg for $cb
38 end_obj => $end_obj, # like END{}, can ->event_step
40 my $pp = tied *$io; # ProcessPipe
41 $pp->{fh}->blocking(0) // die "$io->blocking(0): $!";
42 $self->SUPER::new($io, EPOLLIN);
47 if (defined $self->{cb}) {
48 # this may call async_pass when headers are done
49 $self->{cb}->($self->{arg});
50 } elsif (my $sock = $self->{sock}) {
51 # $http may be undef if discarding body output from cgit on 404
52 my $http = $self->{http} or return $self->close;
53 # $self->{sock} is a read pipe for git-http-backend or cgit
54 # and 65536 is the default Linux pipe size
55 my $r = sysread($sock, my $buf, 65536);
57 $self->{ofh}->write($buf); # may call $http->close
58 # let other clients get some work done, too
59 return if $http->{sock}; # !closed
61 # else: fall through to close below...
62 } elsif (!defined $r && $! == EAGAIN) {
63 return; # EPOLLIN means we'll be notified
66 # Done! Error handling will happen in $self->{ofh}->close
67 # called by end_obj->event_step handler
68 delete $http->{forward};
69 $self->close; # queues end_obj->event_step to be called
70 } # else { # we may've been requeued but closed by $http
73 # once this is called, all data we read is passed to the
74 # to the PublicInbox::HTTP instance ($http) via $ofh->write
75 # $ofh is typically PublicInbox::HTTP::{Chunked,Identity}, but
76 # may be PublicInbox::GzipFilter or $PublicInbox::Qspawn::qx_fh
78 my ($self, $http, $ofh, $bref) = @_;
79 delete @$self{qw(cb arg)};
80 # In case the client HTTP connection ($http) dies, it
81 # will automatically close this ($self) object.
82 $http->{forward} = $self;
84 # write anything we overread when we were reading headers.
85 # This is typically PublicInbox:HTTP::{chunked,identity}_wcb,
86 # but may be PublicInbox::GzipFilter::write. PSGI requires
87 # *_wcb methods respond to ->write (and ->close), not ->print
90 $self->{http} = $http;
94 # may be called as $forward->close in PublicInbox::HTTP or EOF (event_step)
97 $self->SUPER::close; # DS::close
98 delete @$self{qw(cb arg)};
100 # we defer this to the next timer loop since close is deferred
101 if (my $end_obj = delete $self->{end_obj}) {
102 # this calls $end_obj->event_step
103 # (likely PublicInbox::Qspawn::event_step,
104 # NOT PublicInbox::HTTPD::Async::event_step)
105 PublicInbox::DS::requeue($end_obj);