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:19 +0000
committerEric Wong <e@yhbt.net>2020-07-06 20:01:15 +0000
commitcde937e3bf4c0ad343479f9711a201494e4f36ee (patch)
tree6c6730fa2b3162888bbce45fec346b283693abe3 /lib/PublicInbox/GzipFilter.pm
parentb8b03f9c896432816019828b27708fa3b6903d83 (diff)
downloadpublic-inbox-cde937e3bf4c0ad343479f9711a201494e4f36ee.tar.gz
www*stream: gzip ->getline responses
Our most common endpoints deserve to be gzipped.
Diffstat (limited to 'lib/PublicInbox/GzipFilter.pm')
-rw-r--r--lib/PublicInbox/GzipFilter.pm21
1 files changed, 16 insertions, 5 deletions
diff --git a/lib/PublicInbox/GzipFilter.pm b/lib/PublicInbox/GzipFilter.pm
index 115660cb..95fced05 100644
--- a/lib/PublicInbox/GzipFilter.pm
+++ b/lib/PublicInbox/GzipFilter.pm
@@ -6,8 +6,9 @@ package PublicInbox::GzipFilter;
 use strict;
 use parent qw(Exporter);
 use Compress::Raw::Zlib qw(Z_FINISH Z_OK);
-our @EXPORT_OK = qw(gzip_maybe);
+our @EXPORT_OK = qw(gzip_maybe gzf_maybe);
 my %OPT = (-WindowBits => 15 + 16, -AppendOutput => 1);
+my @GZIP_HDRS = qw(Vary Accept-Encoding Content-Encoding gzip);
 
 sub new { bless {}, shift }
 
@@ -18,18 +19,28 @@ sub attach {
         $self
 }
 
-sub gzip_maybe ($) {
-        my ($env) = @_;
+sub gzip_maybe ($$) {
+        my ($res_hdr, $env) = @_;
         return if (($env->{HTTP_ACCEPT_ENCODING}) // '') !~ /\bgzip\b/;
 
+        my ($gz, $err) = Compress::Raw::Zlib::Deflate->new(%OPT);
+        return if $err != Z_OK;
+
         # in case Plack::Middleware::Deflater is loaded:
         $env->{'plack.skip-deflater'} = 1;
 
-        my ($gz, $err) = Compress::Raw::Zlib::Deflate->new(%OPT);
-        $err == Z_OK ? $gz : undef;
+        push @$res_hdr, @GZIP_HDRS;
+        $gz;
+}
+
+sub gzf_maybe ($$) {
+        my ($res_hdr, $env) = @_;
+        my $gz = gzip_maybe($res_hdr, $env) or return 0;
+        bless { gz => $gz }, __PACKAGE__;
 }
 
 # for GetlineBody (via Qspawn) when NOT using $env->{'pi-httpd.async'}
+# Also used for ->getline callbacks
 sub translate ($$) {
         my $self = $_[0];