-# Copyright (C) 2016-2018 all contributors <meta@public-inbox.org>
+# Copyright (C) 2016-2019 all contributors <meta@public-inbox.org>
# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
#
# Generic PSGI server for convenience. It aims to provide
use strict;
use warnings;
use base qw(PublicInbox::DS);
-use fields qw(httpd env input_left remote_addr remote_port forward pull);
+use fields qw(httpd env input_left remote_addr remote_port forward);
use bytes (); # only for bytes::length
use Fcntl qw(:seek);
use Plack::HTTPParser qw(parse_http_request); # XS or pure Perl
require PublicInbox::EvCleanup;
use PublicInbox::DS qw(msg_more);
use PublicInbox::Syscall qw(EPOLLIN EPOLLONESHOT);
+use PublicInbox::Tmpfile;
use constant {
CHUNK_START => -1, # [a-f0-9]+\r\n
CHUNK_END => -2, # \r\n
}
}
-sub getline_cb ($$$) {
+sub getline_response ($$$) {
my ($self, $write, $close) = @_;
- local $/ = \8192;
- my $forward = $self->{forward};
- # limit our own running time for fairness with other
- # clients and to avoid buffering too much:
- if ($forward) {
- my $buf = eval { $forward->getline };
+ my $pull; # DANGER: self-referential
+ $pull = sub {
+ my $forward = $self->{forward};
+ # limit our own running time for fairness with other
+ # clients and to avoid buffering too much:
+ my $buf = eval {
+ local $/ = \8192;
+ $forward->getline;
+ } if $forward;
+
if (defined $buf) {
$write->($buf); # may close in PublicInbox::DS::write
+
if ($self->{sock}) {
- my $next = $self->{pull};
- if ($self->{wbuf}) {
- $self->write($next);
- } else {
- PublicInbox::DS::requeue($next);
- }
- return;
+ my $wbuf = $self->{wbuf} ||= [];
+ push @$wbuf, $pull;
+
+ # wbuf may be populated by $write->($buf),
+ # no need to rearm if so:
+ $self->requeue if scalar(@$wbuf) == 1;
+ return; # likely
}
} elsif ($@) {
err($self, "response ->getline error: $@");
- $forward = undef;
$self->close;
}
- }
- delete @$self{qw(forward pull)};
- # avoid recursion
- if ($forward) {
- eval { $forward->close };
- if ($@) {
- err($self, "response ->close error: $@");
- $self->close; # idempotent
+ $pull = undef; # all done!
+ # avoid recursion
+ if (delete $self->{forward}) {
+ eval { $forward->close };
+ if ($@) {
+ err($self, "response ->close error: $@");
+ $self->close; # idempotent
+ }
}
- }
- $close->();
-}
+ $forward = undef;
+ $close->(); # call response_done_cb
+ };
-sub getline_response ($$$) {
- my ($self, $write, $close) = @_;
- my $pull = $self->{pull} = sub { getline_cb($self, $write, $close) };
- $pull->();
+ $pull->(); # kick-off!
}
sub response_write {
}
sub input_tmpfile ($) {
- open($_[0], '+>', undef);
- $_[0]->autoflush(1);
+ my $input = tmpfile('http.input', $_[0]->{sock}) or return;
+ $input->autoflush(1);
+ $input;
}
sub input_prepare {
quit($self, 413);
return;
}
- input_tmpfile($input);
+ $input = input_tmpfile($self);
} elsif (env_chunked($env)) {
$len = CHUNK_START;
- input_tmpfile($input);
+ $input = input_tmpfile($self);
} else {
$input = $null_io;
}
if (my $env = delete $self->{env}) {
delete $env->{'psgix.io'}; # prevent circular references
}
- delete $self->{pull};
if (my $forward = delete $self->{forward}) {
eval { $forward->close };
err($self, "forward ->close error: $@") if $@;