From: Eric Wong Date: Sun, 7 Jul 2024 05:57:26 +0000 (+0000) Subject: www: manifest.js.gz handles If-Modified-Since X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=35433a5f4a50ae855c4dc807eda708e15ef1812a;p=thirdparty%2Fpublic-inbox.git www: manifest.js.gz handles If-Modified-Since While we can't avoid the expensive manifest.js.gz generation, non-Varnish users now get the bandwidth savings from seeing a 304 response. This has no effect on Varnish users since Varnish will forward the request to us without If-Modified-Since if it gets a cache miss, and handle 304 for us on cache hits. --- diff --git a/lib/PublicInbox/ManifestJsGz.pm b/lib/PublicInbox/ManifestJsGz.pm index be5d5f2af..f912f8123 100644 --- a/lib/PublicInbox/ManifestJsGz.pm +++ b/lib/PublicInbox/ManifestJsGz.pm @@ -107,8 +107,12 @@ sub psgi_triple { } $manifest = $json->encode($manifest); gzip(\$manifest => \(my $out)); + my $mtime = time2str($ctx->{-mtime}); + if (my $ims = $ctx->{env}->{HTTP_IF_MODIFIED_SINCE}) { + return [ 304, [], [] ] if $mtime eq $ims; + } [ 200, [ qw(Content-Type application/gzip), - 'Last-Modified', time2str($ctx->{-mtime}), + 'Last-Modified', $mtime, 'Content-Length', length($out) ], [ $out ] ] } diff --git a/t/www_listing.t b/t/www_listing.t index 0a4c79e8f..5bca45706 100644 --- a/t/www_listing.t +++ b/t/www_listing.t @@ -6,7 +6,7 @@ use v5.12; use PublicInbox::TestCommon; use PublicInbox::Import; use IO::Uncompress::Gunzip qw(gunzip); require_mods(qw(json URI::Escape Plack::Builder HTTP::Tiny)); -require_cmd 'curl'; +my $curl = require_cmd 'curl'; require PublicInbox::WwwListing; require PublicInbox::ManifestJsGz; use PublicInbox::Config; @@ -177,6 +177,15 @@ EOM $td = start_script($cmd, $env, { 3 => $sock }); + my $local_mfest = "$tmpdir/local.manifest.js.gz"; + xsys_e [$curl, '-gsSfR', '-o', $local_mfest, + "http://$host:$port/manifest.js.gz" ]; + xsys_e [$curl, '-vgsSfR', '-o', "$tmpdir/again.js.gz", + '-z', $local_mfest, "http://$host:$port/manifest.js.gz" ], + undef, { 2 => \(my $curl_err) }; + like $curl_err, qr! HTTP/1\.[012] 304 !sm, + 'got 304 response w/ If-Modified-Since'; + # default publicinboxGrokManifest match=domain default tiny_test($json, $host, $port);