From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-Status: No, score=-4.0 required=3.0 tests=ALL_TRUSTED,BAYES_00 shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id E0B841F8F2 for ; Sun, 5 Jul 2020 23:28:07 +0000 (UTC) From: Eric Wong To: meta@public-inbox.org Subject: [PATCH 11/43] wwwstatic: support gzipped directory listings Date: Sun, 5 Jul 2020 23:27:27 +0000 Message-Id: <20200705232759.3161-12-e@yhbt.net> In-Reply-To: <20200705232759.3161-1-e@yhbt.net> References: <20200705232759.3161-1-e@yhbt.net> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: This will allow others to mimic our award-winning homepage design without needing to rely on Plack::Middleware::Deflater or varnish to compress responses. --- lib/PublicInbox/WwwStatic.pm | 15 ++++++++++----- t/www_static.t | 11 ++++++++++- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/lib/PublicInbox/WwwStatic.pm b/lib/PublicInbox/WwwStatic.pm index 3c9331564..d0611949d 100644 --- a/lib/PublicInbox/WwwStatic.pm +++ b/lib/PublicInbox/WwwStatic.pm @@ -17,6 +17,8 @@ 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::NoopFilter; +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); @@ -310,12 +312,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) || PublicInbox::NoopFilter::new(); + $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] = bytes::length($out); + [ 200, $h, [ $out ] ] } sub call { # PSGI app endpoint diff --git a/t/www_static.t b/t/www_static.t index 10757cb7f..364b9447a 100644 --- a/t/www_static.t +++ b/t/www_static.t @@ -6,7 +6,7 @@ use Test::More; use PublicInbox::TestCommon; my ($tmpdir, $for_destroy) = tmpdir(); my @mods = qw(HTTP::Request::Common Plack::Test URI::Escape); -require_mods(@mods); +require_mods(@mods, 'IO::Uncompress::Gunzip'); use_ok $_ foreach @mods; use_ok 'PublicInbox::WwwStatic'; @@ -91,6 +91,15 @@ test_psgi($app->(autoindex => 1, index => []), sub { $get->header('Accept-Encoding' => 'gzip'); $res = $cb->($get); is($res->content, "hi", 'got compressed on mtime match'); + + $get = GET('/dir/'); + $get->header('Accept-Encoding' => 'gzip'); + $res = $cb->($get); + my $in = $res->content; + my $out = ''; + IO::Uncompress::Gunzip::gunzip(\$in => \$out); + like($out, qr/\A/, 'got HTML start after gunzip'); + like($out, qr{$}, 'got HTML end after gunzip'); }); done_testing();