]> 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 15db1390f78530832c302cf519facc41b93d9b3d..bbcb0898612c905f4df1094ce8057c4b4547b832 100644 (file)
@@ -13,7 +13,7 @@ use warnings;
 use base qw(Danga::Socket);
 use fields qw(httpd env rbuf input_left remote_addr remote_port);
 use Fcntl qw(:seek);
-use HTTP::Parser::XS qw(parse_http_request); # supports pure Perl fallback
+use Plack::HTTPParser qw(parse_http_request); # XS or pure Perl
 use HTTP::Status qw(status_message);
 use HTTP::Date qw(time2str);
 use IO::File;
@@ -24,6 +24,12 @@ use constant {
        CHUNK_MAX_HDR => 256,
 };
 
+# Use the same configuration parameter as git since this is primarily
+# a slow-client sponge for git-http-backend
+# TODO: support per-respository http.maxRequestBuffer somehow...
+our $MAX_REQUEST_BUFFER = $ENV{GIT_HTTP_MAX_REQUEST_BUFFER} ||
+                       (10 * 1024 * 1024);
+
 my $null_io = IO::File->new('/dev/null', '<');
 my $http_date;
 my $prev = 0;
@@ -112,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};
@@ -125,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') {
@@ -157,14 +166,14 @@ sub response_header_write {
                if ($k =~ /\ATransfer-Encoding\z/i && $v =~ /\bchunked\b/i) {
                        $chunked = 1;
                }
-
                $h .= "$k: $v\r\n";
        }
 
        my $conn = $env->{HTTP_CONNECTION} || '';
-       my $alive = (defined($len) || $chunked) &&
-                       ($proto eq 'HTTP/1.1' && $conn !~ /\bclose\b/i) ||
-                       ($conn =~ /\bkeep-alive\b/i);
+       my $term = defined($len) || $chunked;
+       my $alive = $term &&
+                       (($proto eq 'HTTP/1.1' && $conn !~ /\bclose\b/i) ||
+                        ($conn =~ /\bkeep-alive\b/i));
 
        $h .= 'Connection: ' . ($alive ? 'keep-alive' : 'close');
        $h .= "\r\nDate: " . http_date() . "\r\n\r\n";
@@ -174,13 +183,15 @@ sub response_header_write {
        } else {
                $self->write($h);
        }
-       ($alive, $chunked);
+       $alive;
 }
 
 sub response_write {
        my ($self, $env, $res) = @_;
-       my ($alive, $chunked) = response_header_write($self, $env, $res);
-       my $write = sub { $self->write($_[0]) };
+       my $alive = response_header_write($self, $env, $res);
+
+       # 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
@@ -214,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) = @_;
@@ -223,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);
        }
 }
 
@@ -232,6 +253,10 @@ sub input_prepare {
        my $input = $null_io;
        my $len = $env->{CONTENT_LENGTH};
        if ($len) {
+               if ($len > $MAX_REQUEST_BUFFER) {
+                       quit($self, 413);
+                       return;
+               }
                $input = IO::File->new_tmpfile;
        } elsif (env_chunked($env)) {
                $len = CHUNK_START;
@@ -241,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;
@@ -293,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) {
@@ -306,6 +331,9 @@ sub event_read_input_chunked { # unlikely...
                if ($len == CHUNK_START) {
                        if ($$rbuf =~ s/\A([a-f0-9]+).*?\r\n//i) {
                                $len = hex $1;
+                               if (($len + -s $input) > $MAX_REQUEST_BUFFER) {
+                                       return quit($self, 413);
+                               }
                        } elsif (length($$rbuf) > CHUNK_MAX_HDR) {
                                return quit($self, 400);
                        }
@@ -358,6 +386,12 @@ sub quit {
 sub event_hup { $_[0]->close }
 sub event_err { $_[0]->close }
 
+sub close {
+       my $self = shift;
+       $self->{env} = undef;
+       $self->SUPER::close(@_);
+}
+
 # for graceful shutdown in PublicInbox::Daemon:
 sub busy () {
        my ($self) = @_;