]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/HTTPD/Async.pm
8efa7a668ff027b02e78a1d9173487efb94e8423
[public-inbox.git] / lib / PublicInbox / HTTPD / Async.pm
1 # Copyright (C) 2016 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(Danga::Socket);
12 use fields qw(cb);
13
14 sub new {
15         my ($class, $io, $cb) = @_;
16         my $self = fields::new($class);
17         IO::Handle::blocking($io, 0);
18         $self->SUPER::new($io);
19         $self->{cb} = $cb;
20         $self->watch_read(1);
21         $self;
22 }
23
24 sub async_pass {
25         my ($self, $io, $fh) = @_;
26         my $restart_read = sub { $self->watch_read(1) };
27
28         # In case the client HTTP connection ($io) dies, it
29         # will automatically close this ($self) object.
30         $io->{forward} = $self;
31         $self->{cb} = sub {
32                 my $r = sysread($self->{sock}, my $buf, 8192);
33                 if ($r) {
34                         $fh->write($buf);
35                         if ($io->{write_buf_size}) {
36                                 $self->watch_read(0);
37                                 $io->write($restart_read);
38                         }
39                         return; # stay in watch_read
40                 } elsif (!defined $r) {
41                         return if $!{EAGAIN} || $!{EINTR};
42                 }
43
44                 # Done! Error handling will happen in $fh->close
45                 $io->{forward} = undef;
46                 $self->close;
47                 $fh->close;
48         }
49 }
50
51 sub event_read { $_[0]->{cb}->() }
52 sub event_hup { $_[0]->{cb}->() }
53 sub event_err { $_[0]->{cb}->() }
54 sub sysread { shift->{sock}->sysread(@_) }
55
56 sub close {
57         my $self = shift;
58         $self->{cb} = undef;
59         $self->SUPER::close(@_);
60 }
61
62 # do not let ourselves be closed during graceful termination
63 sub busy () { $_[0]->{cb} }
64
65 1;