]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/HTTP.pm
view: introduce WwwStream interface
[public-inbox.git] / lib / PublicInbox / HTTP.pm
index 00c9a0444bf75e63689d60893d6b3e236c1ca264..6df1c3fc566aebf1688fd3f1665542297279a684 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 forward);
+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,7 +25,7 @@ use constant {
        CHUNK_MAX_HDR => 256,
 };
 
-# FIXME: duplicated code with NNTP.pm
+# FIXME: duplicated code with NNTP.pm, layering violation
 my $WEAKEN = {}; # string(inbox) -> inbox
 my $weakt;
 sub weaken_task () {
@@ -39,7 +40,10 @@ sub process_pipelineq () {
        my $q = $pipelineq;
        $pipet = undef;
        $pipelineq = [];
-       rbuf_process($_) foreach @$q;
+       foreach (@$q) {
+               next if $_->{closed};
+               rbuf_process($_);
+       }
 }
 
 # Use the same configuration parameter as git since this is primarily
@@ -228,46 +232,69 @@ sub identity_wcb ($) {
        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:
+               my $n = 100;
+               while ($forward && defined(my $buf = $forward->getline)) {
+                       $write->($buf);
+                       last if $self->{closed};
+                       if ((--$n) <= 0 || $self->{write_buf_size}) {
+                               $self->write($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 = $alive == 2 ? chunked_wcb($self) : identity_wcb($self);
-       my $close = sub {
-               $self->write("0\r\n\r\n") if $alive == 2;
-               if ($alive) {
-                       $self->event_write; # watch for readability if done
-               } else {
-                       Danga::Socket::write($self, sub { $self->close });
-               }
-               if (my $obj = $env->{'pi-httpd.inbox'}) {
-                       # grace period for reaping resources
-                       $WEAKEN->{"$obj"} = $obj;
-                       $weakt ||= PublicInbox::EvCleanup::later(*weaken_task);
-               }
-               $self->{env} = undef;
-       };
-
+       my $close = sub { response_done($self, $alive) };
        if (defined(my $body = $res->[2])) {
                if (ref $body eq 'ARRAY') {
                        $write->($_) foreach @$body;
                        $close->();
                } else {
-                       my $pull;
-                       $pull = sub {
-                               local $/ = \8192;
-                               while (defined(my $buf = $body->getline)) {
-                                       $write->($buf);
-                                       if ($self->{write_buf_size}) {
-                                               $self->write($pull);
-                                               return;
-                                       }
-                               }
-                               $pull = undef;
-                               $body->close();
-                               $close->();
-                       };
-                       $pull->();
+                       getline_response($self, $body, $write, $close);
                }
        } else {
                # this is returned to the calling application:
@@ -278,6 +305,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) {
@@ -290,20 +318,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
-               push @$pipelineq, $self;
-               $pipet ||= PublicInbox::EvCleanup::asap(*process_pipelineq);
-       }
-}
-
 sub input_prepare {
        my ($self, $env) = @_;
        my $input = $null_io;
@@ -445,8 +459,10 @@ sub event_err { $_[0]->close }
 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->{forward} = $self->{env} = undef;
        $self->SUPER::close(@_);
 }