]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/HTTP.pm
http: move empty string check into write callback
[public-inbox.git] / lib / PublicInbox / HTTP.pm
index c7fb954ebd6c0f0b472dce57bf1a07601a01866c..bbcb0898612c905f4df1094ce8057c4b4547b832 100644 (file)
@@ -118,11 +118,11 @@ sub event_read_input ($) {
                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};
@@ -131,10 +131,13 @@ sub app_dispatch ($) {
                $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') {
@@ -186,7 +189,9 @@ sub response_header_write {
 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
@@ -220,6 +225,15 @@ sub more ($$) {
        $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) = @_;
@@ -229,7 +243,8 @@ sub event_write {
        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);
        }
 }
 
@@ -251,7 +266,6 @@ sub input_prepare {
        # TODO: expire idle clients on ENFILE / EMFILE
        return unless $input;
 
-       binmode $input;
        $env->{'psgi.input'} = $input;
        $self->{env} = $env;
        $self->{input_left} = $len || 0;
@@ -303,7 +317,8 @@ sub event_read_input_chunked { # unlikely...
 
        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) {
@@ -371,11 +386,10 @@ sub quit {
 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: