about summary refs log tree commit homepage
path: root/lib/PublicInbox/GzipFilter.pm
diff options
context:
space:
mode:
authorEric Wong <e@yhbt.net>2020-07-05 23:27:28 +0000
committerEric Wong <e@yhbt.net>2020-07-06 20:01:15 +0000
commitf982abbaa39e07090d3fb641fd9de25434c0d74c (patch)
treee5c5d0a4cd6312045ea79dd8706ac390624ed734 /lib/PublicInbox/GzipFilter.pm
parent2bb3b3491416232a44ad8b318537c856cff727f4 (diff)
downloadpublic-inbox-f982abbaa39e07090d3fb641fd9de25434c0d74c.tar.gz
This will allow us to gzip responses generated by cgit
and any other CGI programs or long-lived streaming
responses we may spawn.
Diffstat (limited to 'lib/PublicInbox/GzipFilter.pm')
-rw-r--r--lib/PublicInbox/GzipFilter.pm16
1 files changed, 16 insertions, 0 deletions
diff --git a/lib/PublicInbox/GzipFilter.pm b/lib/PublicInbox/GzipFilter.pm
index 0fbb4476..0a6c56a5 100644
--- a/lib/PublicInbox/GzipFilter.pm
+++ b/lib/PublicInbox/GzipFilter.pm
@@ -32,6 +32,22 @@ sub gzf_maybe ($$) {
         bless { gz => $gz }, __PACKAGE__;
 }
 
+sub qsp_maybe ($$) {
+        my ($res_hdr, $env) = @_;
+        return if ($env->{HTTP_ACCEPT_ENCODING} // '') !~ /\bgzip\b/;
+        my $hdr = join("\n", @$res_hdr);
+        return if $hdr !~ m!^Content-Type\n
+                                (?:(?:text/(?:html|plain))|
+                                application/atom\+xml)\b!ixsm;
+        return if $hdr =~ m!^Content-Encoding\ngzip\n!smi;
+        return if $hdr =~ m!^Content-Length\n[0-9]+\n!smi;
+        return if $hdr =~ m!^Transfer-Encoding\n!smi;
+        # in case Plack::Middleware::Deflater is loaded:
+        return if $env->{'plack.skip-deflater'}++;
+        push @$res_hdr, @GZIP_HDRS;
+        bless {}, __PACKAGE__;
+}
+
 sub gzip_or_die () {
         my ($gz, $err) = Compress::Raw::Zlib::Deflate->new(%OPT);
         $err == Z_OK or die "Deflate->new failed: $err";