X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=lib%2FPublicInbox%2FHTTP.pm;h=19b57d59518d87ec79e0b564bf63a81ed5935c71;hb=9bd675d33ad1e49bd2ebe12a1d216216e61380de;hp=1153ef98779ff6390107afffd3174dacbe3da8f5;hpb=858ab5cfe5fffa6c5a4221a523db3682be8fae06;p=public-inbox.git diff --git a/lib/PublicInbox/HTTP.pm b/lib/PublicInbox/HTTP.pm index 1153ef98..19b57d59 100644 --- a/lib/PublicInbox/HTTP.pm +++ b/lib/PublicInbox/HTTP.pm @@ -1,4 +1,4 @@ -# Copyright (C) 2016-2018 all contributors +# Copyright (C) 2016-2019 all contributors # License: AGPL-3.0+ # # Generic PSGI server for convenience. It aims to provide @@ -11,7 +11,7 @@ package PublicInbox::HTTP; use strict; use warnings; use base qw(PublicInbox::DS); -use fields qw(httpd env input_left remote_addr remote_port forward pull); +use fields qw(httpd env input_left remote_addr remote_port forward); use bytes (); # only for bytes::length use Fcntl qw(:seek); use Plack::HTTPParser qw(parse_http_request); # XS or pure Perl @@ -19,7 +19,7 @@ use HTTP::Status qw(status_message); use HTTP::Date qw(time2str); use IO::Handle; require PublicInbox::EvCleanup; -PublicInbox::DS->import(qw(msg_more)); +use PublicInbox::DS qw(msg_more); use PublicInbox::Syscall qw(EPOLLIN EPOLLONESHOT); use constant { CHUNK_START => -1, # [a-f0-9]+\r\n @@ -30,10 +30,8 @@ use constant { use Errno qw(EAGAIN); my $pipelineq = []; -my $pipet; sub process_pipelineq () { my $q = $pipelineq; - $pipet = undef; $pipelineq = []; foreach (@$q) { next unless $_->{sock}; @@ -58,8 +56,16 @@ sub http_date () { sub new ($$$) { my ($class, $sock, $addr, $httpd) = @_; my $self = fields::new($class); - $self->SUPER::new($sock, EPOLLIN | EPOLLONESHOT); + my $ev = EPOLLIN; + my $wbuf; + if (ref($sock) eq 'IO::Socket::SSL' && !$sock->accept_SSL) { + return CORE::close($sock) if $! != EAGAIN; + $ev = PublicInbox::TLS::epollbit(); + $wbuf = [ \&PublicInbox::DS::accept_tls_step ]; + } + $self->SUPER::new($sock, $ev | EPOLLONESHOT); $self->{httpd} = $httpd; + $self->{wbuf} = $wbuf if $wbuf; ($self->{remote_addr}, $self->{remote_port}) = PublicInbox::Daemon::host_with_port($addr); $self; @@ -95,7 +101,7 @@ sub rbuf_process { } if ($r < 0) { # incomplete $self->rbuf_idle($rbuf); - return $self->watch_in1; + return $self->requeue; } $$rbuf = substr($$rbuf, $r); my $len = input_prepare($self, \%env); @@ -238,10 +244,10 @@ sub next_request ($) { my ($self) = @_; if ($self->{rbuf}) { # avoid recursion for pipelined requests + PublicInbox::DS::requeue(\&process_pipelineq) if !@$pipelineq; push @$pipelineq, $self; - $pipet ||= PublicInbox::EvCleanup::asap(*process_pipelineq); } else { # wait for next request - $self->watch_in1; + $self->requeue; } } @@ -254,48 +260,49 @@ sub response_done_cb ($$) { } } -sub getline_cb ($$$) { +sub getline_response ($$$) { 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 }; + my $pull; # DANGER: self-referential + $pull = sub { + my $forward = $self->{forward}; + # limit our own running time for fairness with other + # clients and to avoid buffering too much: + my $buf = eval { + local $/ = \8192; + $forward->getline; + } if $forward; + if (defined $buf) { $write->($buf); # may close in PublicInbox::DS::write + if ($self->{sock}) { - my $next = $self->{pull}; - if ($self->{wbuf}) { - $self->write($next); - } else { - PublicInbox::EvCleanup::asap($next); - } - return; + my $wbuf = $self->{wbuf} ||= []; + push @$wbuf, $pull; + + # wbuf may be populated by $write->($buf), + # no need to rearm if so: + $self->requeue if scalar(@$wbuf) == 1; + return; # likely } } elsif ($@) { err($self, "response ->getline error: $@"); - $forward = undef; $self->close; } - } - delete @$self{qw(forward pull)}; - # avoid recursion - if ($forward) { - eval { $forward->close }; - if ($@) { - err($self, "response ->close error: $@"); - $self->close; # idempotent + $pull = undef; # all done! + # avoid recursion + if (delete $self->{forward}) { + eval { $forward->close }; + if ($@) { + err($self, "response ->close error: $@"); + $self->close; # idempotent + } } - } - $close->(); -} + $forward = undef; + $close->(); # call response_done_cb + }; -sub getline_response ($$$) { - my ($self, $write, $close) = @_; - my $pull = $self->{pull} = sub { getline_cb($self, $write, $close) }; - $pull->(); + $pull->(); # kick-off! } sub response_write { @@ -447,7 +454,6 @@ sub close { if (my $env = delete $self->{env}) { delete $env->{'psgix.io'}; # prevent circular references } - delete $self->{pull}; if (my $forward = delete $self->{forward}) { eval { $forward->close }; err($self, "forward ->close error: $@") if $@; @@ -461,11 +467,4 @@ sub busy () { ($self->{rbuf} || $self->{env} || $self->{wbuf}); } -# fires after pending writes are complete: -sub restart_pass ($) { - $_[0]->{forward}->restart_read; # see PublicInbox::HTTPD::Async -} - -sub enqueue_restart_pass ($) { $_[0]->write(\&restart_pass) } - 1;