return recv_err($self, $r, $len) unless $r;
# continue looping if $r > 0;
}
- app_dispatch($self);
+ app_dispatch($self, $input);
}
-sub app_dispatch ($) {
- my ($self) = @_;
+sub app_dispatch {
+ my ($self, $input) = @_;
$self->watch_read(0);
my $env = $self->{env};
$env->{REMOTE_ADDR} = $self->{remote_addr};
$host =~ s/:(\d+)\z// and $env->{SERVER_PORT} = $1;
$env->{SERVER_NAME} = $host;
}
-
- sysseek($env->{'psgi.input'}, 0, SEEK_SET) or
+ if (defined $input) {
+ sysseek($input, 0, SEEK_SET) or
die "BUG: psgi.input seek failed: $!";
-
+ }
+ # note: NOT $self->{sock}, we want our close (+ Danga::Socket::close),
+ # to do proper cleanup:
+ $env->{'psgix.io'} = $self; # only for ->close
my $res = Plack::Util::run_app($self->{httpd}->{app}, $env);
eval {
if (ref($res) eq 'CODE') {
sub response_write {
my ($self, $env, $res) = @_;
my $alive = response_header_write($self, $env, $res);
- my $write = sub { $self->write($_[0]) };
+
+ # middlewares such as Deflater may write empty strings
+ my $write = sub { $self->write($_[0]) if $_[0] ne '' };
my $close = sub {
if ($alive) {
$self->event_write; # watch for readability if done
$self->write($_[1]);
}
+my $pipelineq = [];
+my $next_tick;
+sub process_pipelineq () {
+ $next_tick = undef;
+ my $q = $pipelineq;
+ $pipelineq = [];
+ rbuf_process($_) foreach @$q;
+}
+
# overrides existing Danga::Socket method
sub event_write {
my ($self) = @_;
if ($self->{rbuf} eq '') { # wait for next request
$self->watch_read(1);
} else { # avoid recursion for pipelined requests
- Danga::Socket->AddTimer(0, sub { rbuf_process($self) });
+ push @$pipelineq, $self;
+ $next_tick ||= Danga::Socket->AddTimer(0, *process_pipelineq);
}
}
# TODO: expire idle clients on ENFILE / EMFILE
return unless $input;
- binmode $input;
$env->{'psgi.input'} = $input;
$self->{env} = $env;
$self->{input_left} = $len || 0;
while (1) { # chunk start
if ($len == CHUNK_ZEND) {
- return app_dispatch($self) if $$rbuf =~ s/\A\r\n//s;
+ $$rbuf =~ s/\A\r\n//s and
+ return app_dispatch($self, $input);
return quit($self, 400) if length($$rbuf) > 2;
}
if ($len == CHUNK_END) {
sub event_hup { $_[0]->close }
sub event_err { $_[0]->close }
-sub write ($$) : method {
- my PublicInbox::HTTP $self = $_[0];
- return 1 if (defined($_[1]) && ref($_[1]) eq '' && $_[1] eq '');
-
- $self->SUPER::write($_[1]);
+sub close {
+ my $self = shift;
+ $self->{env} = undef;
+ $self->SUPER::close(@_);
}
# for graceful shutdown in PublicInbox::Daemon: