]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/HTTP.pm
ds|http|nntp: simplify {wbuf} population
[public-inbox.git] / lib / PublicInbox / HTTP.pm
index e350daaff1c003bb3cd9ab43cb154b0d09e86d64..325371535995ce0a343277bd6fcc603f2cc9493b 100644 (file)
@@ -15,9 +15,10 @@ use fields qw(httpd env input_left remote_addr remote_port forward alive);
 use bytes (); # only for bytes::length
 use Fcntl qw(:seek);
 use Plack::HTTPParser qw(parse_http_request); # XS or pure Perl
+use Plack::Util;
 use HTTP::Status qw(status_message);
 use HTTP::Date qw(time2str);
-use IO::Handle;
+use IO::Handle; # ->write
 use PublicInbox::DS qw(msg_more);
 use PublicInbox::Syscall qw(EPOLLIN EPOLLONESHOT);
 use PublicInbox::Tmpfile;
@@ -154,7 +155,7 @@ sub app_dispatch {
        my $env = $self->{env};
        $env->{REMOTE_ADDR} = $self->{remote_addr};
        $env->{REMOTE_PORT} = $self->{remote_port};
-       if (my $host = $env->{HTTP_HOST}) {
+       if (defined(my $host = $env->{HTTP_HOST})) {
                $host =~ s/:([0-9]+)\z// and $env->{SERVER_PORT} = $1;
                $env->{SERVER_NAME} = $host;
        }
@@ -164,7 +165,7 @@ sub app_dispatch {
        }
        # note: NOT $self->{sock}, we want our close (+ PublicInbox::DS::close),
        # to do proper cleanup:
-       $env->{'psgix.io'} = $self; # only for ->close
+       $env->{'psgix.io'} = $self; # for ->close or async_pass
        my $res = Plack::Util::run_app($self->{httpd}->{app}, $env);
        eval {
                if (ref($res) eq 'CODE') {
@@ -173,7 +174,10 @@ sub app_dispatch {
                        response_write($self, $env, $res);
                }
        };
-       $self->close if $@;
+       if ($@) {
+               err($self, "response_write error: $@");
+               $self->close;
+       }
 }
 
 sub response_header_write {
@@ -276,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 ($@) {