From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-4.2 required=3.0 tests=ALL_TRUSTED,AWL,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, T_SCC_BODY_TEXT_LINE shortcircuit=no autolearn=ham autolearn_force=no version=3.4.6 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id BA618D48BD for ; Wed, 19 Jun 2024 23:41:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=80x24.org; s=selector1; t=1718840485; bh=c1PEW0b0VVE11ZKMZZrvqmBvS/jMxXth8yjDTmLKhco=; h=From:To:Subject:Date:In-Reply-To:References:From; b=I5N381llI0TWPYpRN0Htnvz7CGSY2R85Rs5dXQeMMFdNcMK90G+DKiGpdDoJ0Xpuy bdREWAkhizNurroH0M5iS06pnH2D9N+5fxSbkl4/8nNs5bgj06tsx1UibpRiHgk49w 1EuZUyL1H6tpnwxjEh3XFFVrfoK7OSgAUU0nRMkY= From: Eric Wong To: meta@public-inbox.org Subject: [PATCH 4/5] http: set Content-Length for simple array responses Date: Wed, 19 Jun 2024 23:41:03 +0000 Message-ID: <20240619234104.80183-5-e@80x24.org> In-Reply-To: <20240619234104.80183-1-e@80x24.org> References: <20240619234104.80183-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: We'll be able to combine the header and body in a single writev(2) call in the next commit. --- lib/PublicInbox/Compat.pm | 4 +++- lib/PublicInbox/HTTP.pm | 13 ++++++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/lib/PublicInbox/Compat.pm b/lib/PublicInbox/Compat.pm index 78cba90e..8ed2d7a1 100644 --- a/lib/PublicInbox/Compat.pm +++ b/lib/PublicInbox/Compat.pm @@ -8,7 +8,7 @@ use v5.12; use parent qw(Exporter); require List::Util; -our @EXPORT_OK = qw(uniqstr); +our @EXPORT_OK = qw(uniqstr sum0); # uniqstr is in List::Util 1.45+, which means Perl 5.26+; # so maybe 2030 for us since we need to support enterprise distros. @@ -21,4 +21,6 @@ no warnings 'once'; grep { !$seen{$_}++ } @_; }; +*sum0 = List::Util->can('sum0') // sub (@) { List::Util::sum(@_) // 0 }; + 1; diff --git a/lib/PublicInbox/HTTP.pm b/lib/PublicInbox/HTTP.pm index 3ca0d18c..d0e5aa29 100644 --- a/lib/PublicInbox/HTTP.pm +++ b/lib/PublicInbox/HTTP.pm @@ -21,6 +21,7 @@ package PublicInbox::HTTP; use strict; use parent qw(PublicInbox::DS); +use bytes qw(length); use Fcntl qw(:seek); use Plack::HTTPParser qw(parse_http_request); # XS or pure Perl use Plack::Util; @@ -36,6 +37,7 @@ use constant { CHUNK_MAX_HDR => 256, }; use Errno qw(EAGAIN); +use PublicInbox::Compat qw(sum0); # Use the same configuration parameter as git since this is primarily # a slow-client sponge for git-http-backend @@ -165,7 +167,7 @@ sub app_dispatch { } } -sub response_header_write { +sub response_header_write ($$$) { my ($self, $env, $res) = @_; my $proto = $env->{SERVER_PROTOCOL} or return; # HTTP/0.9 :P my $status = $res->[0]; @@ -189,6 +191,11 @@ sub response_header_write { my $term = defined($len) || $chunked; my $prot_persist = ($proto eq 'HTTP/1.1') && ($conn !~ /\bclose\b/i); my $alive; + if (!$term && ref($res->[2]) eq 'ARRAY') { + $len = sum0(map length, @{$res->[2]}); + $h .= "Content-Length: $len\r\n"; + $term = 1; + } if (!$term && $prot_persist) { # auto-chunk $chunked = $alive = 2; $alive = 3 if $env->{REQUEST_METHOD} eq 'HEAD'; @@ -454,7 +461,7 @@ use v5.12; sub write { # ([$http], $buf) = @_; PublicInbox::HTTP::chunked_write($_[0]->[0], $_[1]); - $_[0]->[0]->{sock} ? length($_[1]) : undef; + $_[0]->[0]->{sock} ? bytes::length($_[1]) : undef; } sub close { @@ -469,7 +476,7 @@ our @ISA = qw(PublicInbox::HTTP::Chunked); sub write { # ([$http], $buf) = @_; PublicInbox::HTTP::identity_write($_[0]->[0], $_[1]); - $_[0]->[0]->{sock} ? length($_[1]) : undef; + $_[0]->[0]->{sock} ? bytes::length($_[1]) : undef; } 1;