]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/HTTPD/Async.pm
http|nntp: favor "$! == EFOO" over $!{EFOO} checks
[public-inbox.git] / lib / PublicInbox / HTTPD / Async.pm
1 # Copyright (C) 2016-2018 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 #
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 is is intended for use with read-only pipes.
8 package PublicInbox::HTTPD::Async;
9 use strict;
10 use warnings;
11 use base qw(PublicInbox::DS);
12 use fields qw(cb cleanup);
13 require PublicInbox::EvCleanup;
14 use Errno qw(EAGAIN);
15
16 sub new {
17         my ($class, $io, $cb, $cleanup) = @_;
18
19         # no $io? call $cb at the top of the next event loop to
20         # avoid recursion:
21         unless (defined($io)) {
22                 PublicInbox::EvCleanup::asap($cb) if $cb;
23                 PublicInbox::EvCleanup::next_tick($cleanup) if $cleanup;
24                 return;
25         }
26
27         my $self = fields::new($class);
28         IO::Handle::blocking($io, 0);
29         $self->SUPER::new($io, PublicInbox::DS::EPOLLIN());
30         $self->{cb} = $cb;
31         $self->{cleanup} = $cleanup;
32         $self;
33 }
34
35 sub restart_read ($) { $_[0]->watch(PublicInbox::DS::EPOLLIN()) }
36
37 # fires after pending writes are complete:
38 sub restart_read_cb ($) {
39         my ($self) = @_;
40         sub { restart_read($self) }
41 }
42
43 sub main_cb ($$$) {
44         my ($http, $fh, $bref) = @_;
45         sub {
46                 my ($self) = @_;
47                 my $r = sysread($self->{sock}, $$bref, 8192);
48                 if ($r) {
49                         $fh->write($$bref);
50                         if ($http->{sock}) { # !closed
51                                 if ($http->{wbuf}) {
52                                         $self->watch(0);
53                                         $http->write(restart_read_cb($self));
54                                 }
55                                 # stay in EPOLLIN, but let other clients
56                                 # get some work done, too.
57                                 return;
58                         }
59                         # fall through to close below...
60                 } elsif (!defined $r) {
61                         return restart_read($self) if $! == EAGAIN;
62                 }
63
64                 # Done! Error handling will happen in $fh->close
65                 # called by the {cleanup} handler
66                 $http->{forward} = undef;
67                 $self->close;
68         }
69 }
70
71 sub async_pass {
72         my ($self, $http, $fh, $bref) = @_;
73         # In case the client HTTP connection ($http) dies, it
74         # will automatically close this ($self) object.
75         $http->{forward} = $self;
76         $fh->write($$bref); # PublicInbox:HTTP::{chunked,identity}_wcb
77         $self->{cb} = main_cb($http, $fh, $bref);
78 }
79
80 sub event_step { $_[0]->{cb}->(@_) }
81
82 sub close {
83         my $self = shift;
84         my $cleanup = $self->{cleanup};
85         $self->{cleanup} = $self->{cb} = undef;
86         $self->SUPER::close(@_);
87
88         # we defer this to the next timer loop since close is deferred
89         PublicInbox::EvCleanup::next_tick($cleanup) if $cleanup;
90 }
91
92 1;