]> git.ipfire.org Git - thirdparty/public-inbox.git/commitdiff
www: manifest.js.gz handles If-Modified-Since
authorEric Wong <e@80x24.org>
Sun, 7 Jul 2024 05:57:26 +0000 (05:57 +0000)
committerEric Wong <e@80x24.org>
Mon, 8 Jul 2024 01:43:33 +0000 (01:43 +0000)
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.

lib/PublicInbox/ManifestJsGz.pm
t/www_listing.t

index be5d5f2af42fa53401c36aff64d15e7bd95ff543..f912f81234b60c29988079f58d50d1746bf61a8b 100644 (file)
@@ -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 ] ]
 }
 
index 0a4c79e8f58acfbaf009de3c4e7015dc2a36ece1..5bca45706d64250d6c5295d3667b428e18aa2036 100644 (file)
@@ -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);