]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/HTTP.pm
www: use WwwStream for dumping thread and search views
[public-inbox.git] / lib / PublicInbox / HTTP.pm
index 393451221112478d832c82142d37e7c3faf57757..e19c592c99612ae29960676e5112166d5f5642eb 100644 (file)
@@ -4,18 +4,19 @@
 # Generic PSGI server for convenience.  It aims to provide
 # a consistent experience for public-inbox admins so they don't have
 # to learn different ways to admin both NNTP and HTTP components.
-# There's nothing public-inbox-specific, here.
+# There's nothing which depends on public-inbox, here.
 # Each instance of this class represents a HTTP client socket
 
 package PublicInbox::HTTP;
 use strict;
 use warnings;
 use base qw(Danga::Socket);
-use fields qw(httpd env rbuf input_left remote_addr remote_port);
+use fields qw(httpd env rbuf input_left remote_addr remote_port forward pull);
 use Fcntl qw(:seek);
 use Plack::HTTPParser qw(parse_http_request); # XS or pure Perl
 use HTTP::Status qw(status_message);
 use HTTP::Date qw(time2str);
+use Scalar::Util qw(weaken);
 use IO::File;
 use constant {
        CHUNK_START => -1,   # [a-f0-9]+\r\n
@@ -24,6 +25,27 @@ use constant {
        CHUNK_MAX_HDR => 256,
 };
 
+# FIXME: duplicated code with NNTP.pm, layering violation
+my $WEAKEN = {}; # string(inbox) -> inbox
+my $weakt;
+sub weaken_task () {
+       $weakt = undef;
+       $_->weaken_all for values %$WEAKEN;
+       $WEAKEN = {};
+}
+
+my $pipelineq = [];
+my $pipet;
+sub process_pipelineq () {
+       my $q = $pipelineq;
+       $pipet = undef;
+       $pipelineq = [];
+       foreach (@$q) {
+               next if $_->{closed};
+               rbuf_process($_);
+       }
+}
+
 # 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...
@@ -118,11 +140,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 +153,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') {
@@ -151,7 +176,7 @@ sub response_header_write {
        my $proto = $env->{SERVER_PROTOCOL} or return; # HTTP/0.9 :P
        my $status = $res->[0];
        my $h = "$proto $status " . status_message($status) . "\r\n";
-       my $term;
+       my ($len, $chunked);
        my $headers = $res->[1];
 
        for (my $i = 0; $i < @$headers; $i += 2) {
@@ -159,22 +184,31 @@ sub response_header_write {
                my $v = $headers->[$i + 1];
                next if $k =~ /\A(?:Connection|Date)\z/i;
 
-               if ($k =~ /\AContent-Length\z/ ||
-                   ($k =~ /\ATransfer-Encoding\z/i && $v =~ /\bchunked\b/i)) {
-                       $term = 1;
+               $len = $v if $k =~ /\AContent-Length\z/i;
+               if ($k =~ /\ATransfer-Encoding\z/i && $v =~ /\bchunked\b/i) {
+                       $chunked = 1;
                }
                $h .= "$k: $v\r\n";
        }
 
        my $conn = $env->{HTTP_CONNECTION} || '';
-       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";
+       my $term = defined($len) || $chunked;
+       my $prot_persist = ($proto eq 'HTTP/1.1') && ($conn !~ /\bclose\b/i);
+       my $alive;
+       if (!$term && $prot_persist) { # auto-chunk
+               $chunked = $alive = 2;
+               $h .= "Transfer-Encoding: chunked\r\n";
+               # no need for "Connection: keep-alive" with HTTP/1.1
+       } elsif ($term && ($prot_persist || ($conn =~ /\bkeep-alive\b/i))) {
+               $alive = 1;
+               $h .= "Connection: keep-alive\r\n";
+       } else {
+               $alive = 0;
+               $h .= "Connection: close\r\n";
+       }
+       $h .= 'Date: ' . http_date() . "\r\n\r\n";
 
-       if ($term && $env->{REQUEST_METHOD} ne 'HEAD') {
+       if (($len || $chunked) && $env->{REQUEST_METHOD} ne 'HEAD') {
                more($self, $h);
        } else {
                $self->write($h);
@@ -182,22 +216,90 @@ sub response_header_write {
        $alive;
 }
 
+# middlewares such as Deflater may write empty strings
+sub chunked_wcb ($) {
+       my ($self) = @_;
+       sub {
+               return if $_[0] eq '';
+               more($self, sprintf("%x\r\n", bytes::length($_[0])));
+               more($self, $_[0]);
+
+               # use $self->write("\n\n") if you care about real-time
+               # streaming responses, public-inbox WWW does not.
+               more($self, "\r\n");
+       }
+}
+
+sub identity_wcb ($) {
+       my ($self) = @_;
+       sub { $self->write(\($_[0])) if $_[0] ne '' }
+}
+
+sub next_request ($) {
+       my ($self) = @_;
+       $self->watch_write(0);
+       if ($self->{rbuf} eq '') { # wait for next request
+               $self->watch_read(1);
+       } else { # avoid recursion for pipelined requests
+               push @$pipelineq, $self;
+               $pipet ||= PublicInbox::EvCleanup::asap(*process_pipelineq);
+       }
+}
+
+sub response_done ($$) {
+       my ($self, $alive) = @_;
+       my $env = $self->{env};
+       $self->{env} = undef;
+       $self->write("0\r\n\r\n") if $alive == 2;
+       $self->write(sub { $alive ? next_request($self) : $self->close });
+
+       # FIXME: layering violation
+       if (my $obj = $env->{'pi-httpd.inbox'}) {
+               # grace period for reaping resources
+               $WEAKEN->{"$obj"} = $obj;
+               PublicInbox::EvCleanup::later(*weaken_task);
+       }
+}
+
+sub getline_response {
+       my ($self, $body, $write, $close) = @_;
+       $self->{forward} = $body;
+       weaken($self);
+       my $pull = $self->{pull} = sub {
+               local $/ = \8192;
+               my $forward = $self->{forward};
+               # limit our own running time for fairness with other
+               # clients and to avoid buffering too much:
+               while ($forward && defined(my $buf = $forward->getline)) {
+                       $write->($buf);
+                       last if $self->{closed};
+                       if ($self->{write_buf_size}) {
+                               $self->write($self->{pull});
+                       } else {
+                               PublicInbox::EvCleanup::asap($self->{pull});
+                       }
+                       return;
+               }
+               $self->{forward} = $self->{pull} = undef;
+               $forward->close if $forward; # avoid recursion
+               $close->();
+       };
+       $pull->();
+}
+
 sub response_write {
        my ($self, $env, $res) = @_;
        my $alive = response_header_write($self, $env, $res);
-       my $write = sub { $self->write($_[0]) };
-       my $close = sub {
-               if ($alive) {
-                       $self->event_write; # watch for readability if done
+
+       my $write = $alive == 2 ? chunked_wcb($self) : identity_wcb($self);
+       my $close = sub { response_done($self, $alive) };
+       if (defined(my $body = $res->[2])) {
+               if (ref $body eq 'ARRAY') {
+                       $write->($_) foreach @$body;
+                       $close->();
                } else {
-                       $self->write(sub { $self->close });
+                       getline_response($self, $body, $write, $close);
                }
-               $self->{env} = undef;
-       };
-
-       if (defined $res->[2]) {
-               Plack::Util::foreach($res->[2], $write);
-               $close->();
        } else {
                # this is returned to the calling application:
                Plack::Util::inline_object(write => $write, close => $close);
@@ -207,6 +309,7 @@ sub response_write {
 use constant MSG_MORE => ($^O eq 'linux') ? 0x8000 : 0;
 sub more ($$) {
        my $self = $_[0];
+       return if $self->{closed};
        if (MSG_MORE && !$self->{write_buf_size}) {
                my $n = send($self->{sock}, $_[1], MSG_MORE);
                if (defined $n) {
@@ -219,19 +322,6 @@ sub more ($$) {
        $self->write($_[1]);
 }
 
-# overrides existing Danga::Socket method
-sub event_write {
-       my ($self) = @_;
-       # only continue watching for readability when we are done writing:
-       return if $self->write(undef) != 1;
-
-       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) });
-       }
-}
-
 sub input_prepare {
        my ($self, $env) = @_;
        my $input = $null_io;
@@ -250,7 +340,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;
@@ -302,7 +391,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) {
@@ -370,11 +460,14 @@ 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;
+       my $forward = $self->{forward};
+       my $env = $self->{env};
+       delete $env->{'psgix.io'} if $env; # prevent circular referernces
+       $self->{pull} = $self->{forward} = $self->{env} = undef;
+       $forward->close if $forward;
+       $self->SUPER::close(@_);
 }
 
 # for graceful shutdown in PublicInbox::Daemon: