X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=lib%2FPublicInbox%2FHTTP.pm;h=6ccf20592405e2ee3732d79283b8ac262c26cd6f;hb=8fd41797b24736dfdccfacc5acc473234a29758a;hp=071251c632e394302097538bf86ed881473a6fe5;hpb=8f4720a57d9c5746dcf53fa6c612350c744c2cd1;p=public-inbox.git diff --git a/lib/PublicInbox/HTTP.pm b/lib/PublicInbox/HTTP.pm index 071251c6..6ccf2059 100644 --- a/lib/PublicInbox/HTTP.pm +++ b/lib/PublicInbox/HTTP.pm @@ -1,4 +1,4 @@ -# Copyright (C) 2016-2019 all contributors +# Copyright (C) 2016-2020 all contributors # License: AGPL-3.0+ # # Generic PSGI server for convenience. It aims to provide @@ -59,7 +59,7 @@ sub new ($$$) { my $self = fields::new($class); my $ev = EPOLLIN; my $wbuf; - if (ref($sock) eq 'IO::Socket::SSL' && !$sock->accept_SSL) { + if ($sock->can('accept_SSL') && !$sock->accept_SSL) { return CORE::close($sock) if $! != EAGAIN; $ev = PublicInbox::TLS::epollbit(); $wbuf = [ \&PublicInbox::DS::accept_tls_step ]; @@ -80,7 +80,7 @@ sub event_step { # called by PublicInbox::DS # only read more requests if we've drained the write buffer, # otherwise we can be buffering infinitely w/o backpressure - return read_input($self) if defined $self->{env}; + return read_input($self) if ref($self->{env}); my $rbuf = $self->{rbuf} // (\(my $x = '')); $self->do_read($rbuf, 8192, bytes::length($$rbuf)) or return; rbuf_process($self, $rbuf); @@ -124,7 +124,6 @@ sub read_input ($;$) { my ($self, $rbuf) = @_; $rbuf //= $self->{rbuf} // (\(my $x = '')); my $env = $self->{env}; - return if $env->{REMOTE_ADDR}; # in app dispatch return read_input_chunked($self, $rbuf) if env_chunked($env); # env->{CONTENT_LENGTH} (identity) @@ -153,6 +152,7 @@ sub app_dispatch { my ($self, $input, $rbuf) = @_; $self->rbuf_idle($rbuf); my $env = $self->{env}; + $self->{env} = undef; # for exists() check in ->busy $env->{REMOTE_ADDR} = $self->{remote_addr}; $env->{REMOTE_PORT} = $self->{remote_port}; if (defined(my $host = $env->{HTTP_HOST})) { @@ -280,12 +280,12 @@ sub getline_pull { } if ($self->{sock}) { - my $wbuf = $self->{wbuf} //= []; - push @$wbuf, \&getline_pull; + # autovivify wbuf + my $new_size = push(@{$self->{wbuf}}, \&getline_pull); # wbuf may be populated by {chunked,identity}_write() # above, no need to rearm if so: - $self->requeue if scalar(@$wbuf) == 1; + $self->requeue if $new_size == 1; return; # likely } } elsif ($@) { @@ -335,19 +335,31 @@ sub input_tmpfile ($) { sub input_prepare { my ($self, $env) = @_; - my $input; - my $len = $env->{CONTENT_LENGTH}; - if ($len) { - if ($len > $MAX_REQUEST_BUFFER) { - quit($self, 413); - return; - } - $input = input_tmpfile($self); - } elsif (env_chunked($env)) { + my ($input, $len); + + # rfc 7230 3.3.2, 3.3.3,: favor Transfer-Encoding over Content-Length + my $hte = $env->{HTTP_TRANSFER_ENCODING}; + if (defined $hte) { + # rfc7230 3.3.3, point 3 says only chunked is accepted + # as the final encoding. Since neither public-inbox-httpd, + # git-http-backend, or our WWW-related code uses "gzip", + # "deflate" or "compress" as the Transfer-Encoding, we'll + # reject them: + return quit($self, 400) if $hte !~ /\Achunked\z/i; + $len = CHUNK_START; $input = input_tmpfile($self); } else { - $input = $null_io; + $len = $env->{CONTENT_LENGTH}; + if (defined $len) { + # rfc7230 3.3.3.4 + return quit($self, 400) if $len !~ /\A[0-9]+\z/; + + return quit($self, 413) if $len > $MAX_REQUEST_BUFFER; + $input = $len ? input_tmpfile($self) : $null_io; + } else { + $input = $null_io; + } } # TODO: expire idle clients on ENFILE / EMFILE @@ -358,7 +370,7 @@ sub input_prepare { $self->{input_left} = $len || 0; } -sub env_chunked { ($_[0]->{HTTP_TRANSFER_ENCODING} || '') =~ /\bchunked\b/i } +sub env_chunked { ($_[0]->{HTTP_TRANSFER_ENCODING} // '') =~ /\Achunked\z/i } sub err ($$) { eval { $_[0]->{httpd}->{env}->{'psgi.errors'}->print($_[1]."\n") }; @@ -451,11 +463,11 @@ sub quit { my $h = "HTTP/1.1 $status " . status_message($status) . "\r\n\r\n"; $self->write(\$h); $self->close; + undef; # input_prepare expects this } sub close { my $self = $_[0]; - delete $self->{env}; # prevent circular references if (my $forward = delete $self->{forward}) { eval { $forward->close }; err($self, "forward ->close error: $@") if $@; @@ -466,7 +478,7 @@ sub close { # for graceful shutdown in PublicInbox::Daemon: sub busy () { my ($self) = @_; - ($self->{rbuf} || $self->{env} || $self->{wbuf}); + ($self->{rbuf} || exists($self->{env}) || $self->{wbuf}); } # Chunked and Identity packages are used for writing responses.