]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/HTTPD/Async.pm
54b62451e884730294b7d9f22fef739261aeac9b
[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 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 sub restart_read_cb ($) {
27         my ($self) = @_;
28         sub { $self->watch_read(1) }
29 }
30
31 sub main_cb ($$$) {
32         my ($http, $fh, $bref) = @_;
33         sub {
34                 my ($self) = @_;
35                 my $r = sysread($self->{sock}, $$bref, 8192);
36                 if ($r) {
37                         $fh->write($$bref);
38                         return if $http->{closed};
39                         if ($http->{write_buf_size}) {
40                                 $self->watch_read(0);
41                                 $http->write(restart_read_cb($self));
42                         }
43                         # stay in watch_read, but let other clients
44                         # get some work done, too.
45                         return;
46                 } elsif (!defined $r) {
47                         return if $!{EAGAIN} || $!{EINTR};
48                 }
49
50                 # Done! Error handling will happen in $fh->close
51                 # called by the {cleanup} handler
52                 $http->{forward} = undef;
53                 $self->close;
54         }
55 }
56
57 sub async_pass {
58         my ($self, $http, $fh, $bref) = @_;
59         # In case the client HTTP connection ($http) dies, it
60         # will automatically close this ($self) object.
61         $http->{forward} = $self;
62         $fh->write($$bref); # PublicInbox:HTTP::{chunked,identity}_wcb
63         $self->{cb} = main_cb($http, $fh, $bref);
64 }
65
66 sub event_read { $_[0]->{cb}->(@_) }
67 sub event_hup { $_[0]->{cb}->(@_) }
68 sub event_err { $_[0]->{cb}->(@_) }
69 sub sysread { shift->{sock}->sysread(@_) }
70
71 sub close {
72         my $self = shift;
73         my $cleanup = $self->{cleanup};
74         $self->{cleanup} = $self->{cb} = undef;
75         $self->SUPER::close(@_);
76
77         # we defer this to the next timer loop since close is deferred
78         PublicInbox::EvCleanup::next_tick($cleanup) if $cleanup;
79 }
80
81 1;