1 # Copyright (C) 2016-2019 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)
9 package PublicInbox::HTTPD::Async;
12 use base qw(PublicInbox::DS);
13 use fields qw(cb arg end_obj);
15 use PublicInbox::Syscall qw(EPOLLIN EPOLLET);
17 # This is called via: $env->{'pi-httpd.async'}->()
18 # $io is a read-only pipe ($rpipe) for now, but may be a
19 # bidirectional socket in the future.
21 my ($class, $io, $cb, $arg, $end_obj) = @_;
23 # no $io? call $cb at the top of the next event loop to
25 unless (defined($io)) {
26 PublicInbox::DS::requeue($cb ? $cb : $arg);
27 die '$end_obj unsupported w/o $io' if $end_obj;
31 my $self = fields::new($class);
32 IO::Handle::blocking($io, 0);
33 $self->SUPER::new($io, EPOLLIN | EPOLLET);
34 $self->{cb} = $cb; # initial read callback, later replaced by main_cb
35 $self->{arg} = $arg; # arg for $cb
36 $self->{end_obj} = $end_obj; # like END{}, can ->event_step
44 # $self->{sock} is a read pipe for git-http-backend or cgit
45 # and 65536 is the default Linux pipe size
46 my $r = sysread($self->{sock}, my $buf, 65536);
48 $fh->write($buf); # may call $http->close
49 if ($http->{sock}) { # !closed
51 # let other clients get some work done, too
55 # else: fall through to close below...
56 } elsif (!defined $r && $! == EAGAIN) {
57 return; # EPOLLET means we'll be notified
60 # Done! Error handling will happen in $fh->close
61 # called by the {end} handler
62 delete $http->{forward};
63 $self->close; # queues ->{end} to be called
67 # once this is called, all data we read is passed to the
68 # to the PublicInbox::HTTP instance ($http) via $fh->write
70 my ($self, $http, $fh, $bref) = @_;
71 # In case the client HTTP connection ($http) dies, it
72 # will automatically close this ($self) object.
73 $http->{forward} = $self;
75 # write anything we overread when we were reading headers
76 $fh->write($$bref); # PublicInbox:HTTP::{chunked,identity}_wcb
78 # we're done with this, free this memory up ASAP since the
79 # calls after this may use much memory:
82 # replace the header read callback with the main one
83 my $cb = $self->{cb} = main_cb($http, $fh);
84 $cb->($self); # either hit EAGAIN or ->requeue to keep EPOLLET happy
88 # {cb} may be undef after ->requeue due to $http->close happening
89 my $cb = $_[0]->{cb} or return;
93 # may be called as $forward->close in PublicInbox::HTTP or EOF (main_cb)
97 $self->SUPER::close; # DS::close
99 # we defer this to the next timer loop since close is deferred
100 if (my $end_obj = delete $self->{end_obj}) {
101 # this calls $end_obj->event_step
102 # (likely PublicInbox::Qspawn::event_step,
103 # NOT PublicInbox::HTTPD::Async::event_step)
104 PublicInbox::DS::requeue($end_obj);