X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=lib%2FPublicInbox%2FHTTP.pm;h=3dd49be30ca7dc1330491b5b875e37384fdf7181;hb=3d41aa23f35501ca92aab8aa42980fa73f7fa74f;hp=00c9a0444bf75e63689d60893d6b3e236c1ca264;hpb=74bbc3da398d00ba12e9294e360ad177ab2061ed;p=public-inbox.git diff --git a/lib/PublicInbox/HTTP.pm b/lib/PublicInbox/HTTP.pm index 00c9a044..3dd49be3 100644 --- a/lib/PublicInbox/HTTP.pm +++ b/lib/PublicInbox/HTTP.pm @@ -1,22 +1,22 @@ -# Copyright (C) 2016 all contributors +# Copyright (C) 2016-2018 all contributors # License: AGPL-3.0+ # # 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 IO::File; +use IO::Handle; use constant { CHUNK_START => -1, # [a-f0-9]+\r\n CHUNK_END => -2, # \r\n @@ -24,22 +24,16 @@ use constant { CHUNK_MAX_HDR => 256, }; -# FIXME: duplicated code with NNTP.pm -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 = []; - rbuf_process($_) foreach @$q; + foreach (@$q) { + next if $_->{closed}; + rbuf_process($_); + } } # Use the same configuration parameter as git since this is primarily @@ -48,7 +42,7 @@ sub process_pipelineq () { our $MAX_REQUEST_BUFFER = $ENV{GIT_HTTP_MAX_REQUEST_BUFFER} || (10 * 1024 * 1024); -my $null_io = IO::File->new('/dev/null', '<'); +open(my $null_io, '<', '/dev/null') or die "failed to open /dev/null: $!"; my $http_date; my $prev = 0; sub http_date () { @@ -103,7 +97,7 @@ sub rbuf_process { $self->{rbuf} = substr($self->{rbuf}, $r); my $len = input_prepare($self, \%env); - defined $len or return write_err($self); # EMFILE/ENFILE + defined $len or return write_err($self, undef); # EMFILE/ENFILE $len ? event_read_input($self) : app_dispatch($self); } @@ -123,7 +117,7 @@ sub event_read_input ($) { while ($len > 0) { if ($$rbuf ne '') { my $w = write_in_full($input, $rbuf, $len); - return write_err($self) unless $w; + return write_err($self, $len) unless $w; $len -= $w; die "BUG: $len < 0 (w=$w)" if $len < 0; if ($len == 0) { # next request may be pipelined @@ -219,7 +213,10 @@ sub chunked_wcb ($) { return if $_[0] eq ''; more($self, sprintf("%x\r\n", bytes::length($_[0]))); more($self, $_[0]); - $self->write("\r\n"); + + # use $self->write("\n\n") if you care about real-time + # streaming responses, public-inbox WWW does not. + more($self, "\r\n"); } } @@ -228,46 +225,83 @@ sub identity_wcb ($) { sub { $self->write(\($_[0])) if $_[0] ne '' } } -sub response_write { - my ($self, $env, $res) = @_; - my $alive = response_header_write($self, $env, $res); +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); + } +} - my $write = $alive == 2 ? chunked_wcb($self) : identity_wcb($self); - my $close = sub { +sub response_done_cb ($$) { + my ($self, $alive) = @_; + sub { + my $env = $self->{env}; + $self->{env} = undef; $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 }); + $self->write(sub{$alive ? next_request($self) : $self->close}); + } +} + +sub getline_cb ($$$) { + my ($self, $write, $close) = @_; + local $/ = \8192; + my $forward = $self->{forward}; + # limit our own running time for fairness with other + # clients and to avoid buffering too much: + if ($forward) { + my $buf = eval { $forward->getline }; + if (defined $buf) { + $write->($buf); # may close in Danga::Socket::write + unless ($self->{closed}) { + my $next = $self->{pull}; + if ($self->{write_buf_size}) { + $self->write($next); + } else { + PublicInbox::EvCleanup::asap($next); + } + return; + } + } elsif ($@) { + err($self, "response ->getline error: $@"); + $forward = undef; + $self->close; } - if (my $obj = $env->{'pi-httpd.inbox'}) { - # grace period for reaping resources - $WEAKEN->{"$obj"} = $obj; - $weakt ||= PublicInbox::EvCleanup::later(*weaken_task); + } + + $self->{forward} = $self->{pull} = undef; + # avoid recursion + if ($forward) { + eval { $forward->close }; + if ($@) { + err($self, "response ->close error: $@"); + $self->close; # idempotent } - $self->{env} = undef; - }; + } + $close->(); +} +sub getline_response ($$$) { + my ($self, $write, $close) = @_; + my $pull = $self->{pull} = sub { getline_cb($self, $write, $close) }; + $pull->(); +} + +sub response_write { + my ($self, $env, $res) = @_; + my $alive = response_header_write($self, $env, $res); + my $close = response_done_cb($self, $alive); + my $write = $alive == 2 ? chunked_wcb($self) : identity_wcb($self); 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->(); + $self->{forward} = $body; + getline_response($self, $write, $close); } } else { # this is returned to the calling application: @@ -278,6 +312,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,33 +325,21 @@ 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; + my $input; my $len = $env->{CONTENT_LENGTH}; if ($len) { if ($len > $MAX_REQUEST_BUFFER) { quit($self, 413); return; } - $input = IO::File->new_tmpfile; + open($input, '+>', undef); } elsif (env_chunked($env)) { $len = CHUNK_START; - $input = IO::File->new_tmpfile; + open($input, '+>', undef); + } else { + $input = $null_io; } # TODO: expire idle clients on ENFILE / EMFILE @@ -329,11 +352,15 @@ sub input_prepare { sub env_chunked { ($_[0]->{HTTP_TRANSFER_ENCODING} || '') =~ /\bchunked\b/i } +sub err ($$) { + eval { $_[0]->{httpd}->{env}->{'psgi.errors'}->print($_[1]."\n") }; +} + sub write_err { - my ($self) = @_; - my $err = $self->{httpd}->{env}->{'psgi.errors'}; + my ($self, $len) = @_; my $msg = $! || '(zero write)'; - $err->print("error buffering to input: $msg\n"); + $msg .= " ($len bytes remaining)" if defined $len; + err($self, "error buffering to input: $msg"); quit($self, 500); } @@ -344,8 +371,7 @@ sub recv_err { $self->{input_left} = $len; return; } - my $err = $self->{httpd}->{env}->{'psgi.errors'}; - $err->print("error reading for input: $! ($len bytes remaining)\n"); + err($self, "error reading for input: $! ($len bytes remaining)"); quit($self, 500); } @@ -408,7 +434,7 @@ sub event_read_input_chunked { # unlikely... until ($len <= 0) { if ($$rbuf ne '') { my $w = write_in_full($input, $rbuf, $len); - return write_err($self) unless $w; + return write_err($self, "$len chunk") if !$w; $len -= $w; if ($len == 0) { # we may have leftover data to parse @@ -445,8 +471,13 @@ sub event_err { $_[0]->close } sub close { my $self = shift; my $forward = $self->{forward}; - $forward->close if $forward; - $self->{forward} = $self->{env} = undef; + my $env = $self->{env}; + delete $env->{'psgix.io'} if $env; # prevent circular references + $self->{pull} = $self->{forward} = $self->{env} = undef; + if ($forward) { + eval { $forward->close }; + err($self, "forward ->close error: $@") if $@; + } $self->SUPER::close(@_); }