X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=lib%2FPublicInbox%2FWwwStatic.pm;h=b3476ab82e0922eb9c8f99ca5690016a30806571;hb=5c8909925072804901e9c3b45bbf25446d379e7b;hp=60a71d8d20f007eaa150e7f22866df782c8c7761;hpb=da0bddd3a5c573cd4623d1c95ebf4ee6dd87ed27;p=public-inbox.git diff --git a/lib/PublicInbox/WwwStatic.pm b/lib/PublicInbox/WwwStatic.pm index 60a71d8d..b3476ab8 100644 --- a/lib/PublicInbox/WwwStatic.pm +++ b/lib/PublicInbox/WwwStatic.pm @@ -1,4 +1,4 @@ -# Copyright (C) 2016-2019 all contributors +# Copyright (C) 2016-2021 all contributors # License: AGPL-3.0+ # This package can either be a PSGI response body for a static file @@ -9,14 +9,15 @@ # functionality of nginx. package PublicInbox::WwwStatic; use strict; +use v5.10.1; use parent qw(Exporter); -use bytes (); use Fcntl qw(SEEK_SET O_RDONLY O_NONBLOCK); use POSIX qw(strftime); use HTTP::Date qw(time2str); use HTTP::Status qw(status_message); use Errno qw(EACCES ENOTDIR ENOENT); use URI::Escape qw(uri_escape_utf8); +use PublicInbox::GzipFilter qw(gzf_maybe); use PublicInbox::Hval qw(ascii_html); use Plack::MIME; our @EXPORT_OK = qw(@NO_CACHE r path_info_raw); @@ -117,7 +118,9 @@ sub try_gzip_static ($$$$) { return unless -f $gz && (stat(_))[9] == $mtime; my $res = response($env, $h, $gz, $type); return if ($res->[0] > 300 || $res->[0] < 200); - push @{$res->[1]}, qw(Cache-Control no-transform Content-Encoding gzip); + push @{$res->[1]}, qw(Cache-Control no-transform + Content-Encoding gzip + Vary Accept-Encoding); $res; } @@ -176,9 +179,9 @@ sub getline { my $len = $self->{len} or return; # undef, tells server we're done my $n = 8192; $n = $len if $len < $n; - seek($self->{in}, $self->{off}, SEEK_SET) or - die "seek ($self->{path}): $!"; - my $r = read($self->{in}, my $buf, $n); + sysseek($self->{in}, $self->{off}, SEEK_SET) or + die "sysseek ($self->{path}): $!"; + my $r = sysread($self->{in}, my $buf, $n); if (defined $r && $r > 0) { # success! $self->{len} = $len - $r; $self->{off} += $r; @@ -308,12 +311,15 @@ sub dir_response ($$$) { (map { ${$other{$_}} } sort keys %other)); my $path_info_html = ascii_html($path_info); - my $body = "Index of $path_info_html" . + my $h = [qw(Content-Type text/html Content-Length), undef]; + my $gzf = gzf_maybe($h, $env); + $gzf->zmore("Index of $path_info_html" . ${$self->{style}} . - "
Index of $path_info_html

\n";
-	$body .= join("\n", @entries) . "

\n"; - [ 200, [ qw(Content-Type text/html - Content-Length), bytes::length($body) ], [ $body ] ] + "
Index of $path_info_html

\n");
+	$gzf->zmore(join("\n", @entries));
+	my $out = $gzf->zflush("

\n"); + $h->[3] = length($out); + [ 200, $h, [ $out ] ] } sub call { # PSGI app endpoint