]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/HTTPD/Async.pm
httpd/async: stop running command if client disconnects
[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(Danga::Socket);
12 use fields qw(cb cleanup);
13 require PublicInbox::EvCleanup;
14
15 sub new {
16         my ($class, $io, $cb, $cleanup) = @_;
17         my $self = fields::new($class);
18         IO::Handle::blocking($io, 0);
19         $self->SUPER::new($io);
20         $self->{cb} = $cb;
21         $self->{cleanup} = $cleanup;
22         $self->watch_read(1);
23         $self;
24 }
25
26 # fires after pending writes are complete:
27 sub restart_read_cb ($) {
28         my ($self) = @_;
29         sub { $self->watch_read(1) }
30 }
31
32 sub main_cb ($$$) {
33         my ($http, $fh, $bref) = @_;
34         sub {
35                 my ($self) = @_;
36                 my $r = sysread($self->{sock}, $$bref, 8192);
37                 if ($r) {
38                         $fh->write($$bref);
39                         unless ($http->{closed}) { # Danga::Socket sets this
40                                 if ($http->{write_buf_size}) {
41                                         $self->watch_read(0);
42                                         $http->write(restart_read_cb($self));
43                                 }
44                                 # stay in watch_read, but let other clients
45                                 # get some work done, too.
46                                 return;
47                         }
48                         # fall through to close below...
49                 } elsif (!defined $r) {
50                         return if $!{EAGAIN} || $!{EINTR};
51                 }
52
53                 # Done! Error handling will happen in $fh->close
54                 # called by the {cleanup} handler
55                 $http->{forward} = undef;
56                 $self->close;
57         }
58 }
59
60 sub async_pass {
61         my ($self, $http, $fh, $bref) = @_;
62         # In case the client HTTP connection ($http) dies, it
63         # will automatically close this ($self) object.
64         $http->{forward} = $self;
65         $fh->write($$bref); # PublicInbox:HTTP::{chunked,identity}_wcb
66         $self->{cb} = main_cb($http, $fh, $bref);
67 }
68
69 sub event_read { $_[0]->{cb}->(@_) }
70 sub event_hup { $_[0]->{cb}->(@_) }
71 sub event_err { $_[0]->{cb}->(@_) }
72
73 sub close {
74         my $self = shift;
75         my $cleanup = $self->{cleanup};
76         $self->{cleanup} = $self->{cb} = undef;
77         $self->SUPER::close(@_);
78
79         # we defer this to the next timer loop since close is deferred
80         PublicInbox::EvCleanup::next_tick($cleanup) if $cleanup;
81 }
82
83 1;