From cde937e3bf4c0ad343479f9711a201494e4f36ee Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Sun, 5 Jul 2020 23:27:19 +0000 Subject: www*stream: gzip ->getline responses Our most common endpoints deserve to be gzipped. --- lib/PublicInbox/GzipFilter.pm | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) (limited to 'lib/PublicInbox/GzipFilter.pm') 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]; -- cgit v1.2.3-24-ge0c7