]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/GzipFilter.pm
www: update internal docs
[public-inbox.git] / lib / PublicInbox / GzipFilter.pm
1 # Copyright (C) 2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 #
4 # In public-inbox <=1.5.0, public-inbox-httpd favored "getline"
5 # response bodies to take a "pull"-based approach to feeding
6 # slow clients (as opposed to a more common "push" model).
7 #
8 # In newer versions, public-inbox-httpd supports a backpressure-aware
9 # pull/push model which also accounts for slow git blob storage.
10 # {async_next} callbacks only run when the DS {wbuf} is drained
11 # {async_eml} callbacks only run when a blob arrives from git.
12 #
13 # We continue to support getline+close for generic PSGI servers.
14 package PublicInbox::GzipFilter;
15 use strict;
16 use parent qw(Exporter);
17 use Compress::Raw::Zlib qw(Z_OK);
18 use PublicInbox::CompressNoop;
19 use PublicInbox::Eml;
20 use PublicInbox::GitAsyncCat;
21
22 our @EXPORT_OK = qw(gzf_maybe);
23 my %OPT = (-WindowBits => 15 + 16, -AppendOutput => 1);
24 my @GZIP_HDRS = qw(Vary Accept-Encoding Content-Encoding gzip);
25
26 sub new { bless {}, shift } # qspawn filter
27
28 # for Qspawn if using $env->{'pi-httpd.async'}
29 sub attach {
30         my ($self, $http_out) = @_;
31         $self->{http_out} = $http_out; # PublicInbox::HTTP::{Chunked,Identity}
32         $self
33 }
34
35 sub gz_or_noop {
36         my ($res_hdr, $env) = @_;
37         if (($env->{HTTP_ACCEPT_ENCODING} // '') =~ /\bgzip\b/) {
38                 $env->{'plack.skip-deflater'} = 1;
39                 push @$res_hdr, @GZIP_HDRS;
40                 gzip_or_die();
41         } else {
42                 PublicInbox::CompressNoop::new();
43         }
44 }
45
46 sub gzf_maybe ($$) { bless { gz => gz_or_noop(@_) }, __PACKAGE__ }
47
48 sub psgi_response {
49         my ($self, $code, $res_hdr, $next_cb, $eml_cb) = @_;
50         my $env = $self->{env};
51         $self->{gz} //= gz_or_noop($res_hdr, $env);
52         if ($env->{'pi-httpd.async'}) {
53                 $self->{async_next} = $next_cb;
54                 $self->{async_eml} = $eml_cb;
55                 my $http = $env->{'psgix.io'}; # PublicInbox::HTTP
56                 $http->{forward} = $self;
57                 sub {
58                         my ($wcb) = @_; # -httpd provided write callback
59                         $self->{http_out} = $wcb->([$code, $res_hdr]);
60                         $next_cb->($http); # start stepping
61                 };
62         } else { # generic PSGI code path
63                 [ $code, $res_hdr, $self ];
64         }
65 }
66
67 sub qsp_maybe ($$) {
68         my ($res_hdr, $env) = @_;
69         return if ($env->{HTTP_ACCEPT_ENCODING} // '') !~ /\bgzip\b/;
70         my $hdr = join("\n", @$res_hdr);
71         return if $hdr !~ m!^Content-Type\n
72                                 (?:(?:text/(?:html|plain))|
73                                 application/atom\+xml)\b!ixsm;
74         return if $hdr =~ m!^Content-Encoding\ngzip\n!smi;
75         return if $hdr =~ m!^Content-Length\n[0-9]+\n!smi;
76         return if $hdr =~ m!^Transfer-Encoding\n!smi;
77         # in case Plack::Middleware::Deflater is loaded:
78         return if $env->{'plack.skip-deflater'}++;
79         push @$res_hdr, @GZIP_HDRS;
80         bless {}, __PACKAGE__;
81 }
82
83 sub gzip_or_die () {
84         my ($gz, $err) = Compress::Raw::Zlib::Deflate->new(%OPT);
85         $err == Z_OK or die "Deflate->new failed: $err";
86         $gz;
87 }
88
89 # for GetlineBody (via Qspawn) when NOT using $env->{'pi-httpd.async'}
90 # Also used for ->getline callbacks
91 sub translate ($$) {
92         my $self = $_[0]; # $_[1] => input
93
94         # allocate the zlib context lazily here, instead of in ->new.
95         # Deflate contexts are memory-intensive and this object may
96         # be sitting in the Qspawn limiter queue for a while.
97         my $gz = $self->{gz} //= gzip_or_die();
98         my $zbuf = delete($self->{zbuf});
99         if (defined $_[1]) { # my $buf = $_[1];
100                 my $err = $gz->deflate($_[1], $zbuf);
101                 die "gzip->deflate: $err" if $err != Z_OK;
102                 return $zbuf if length($zbuf) >= 8192;
103
104                 $self->{zbuf} = $zbuf;
105                 '';
106         } else { # undef == EOF
107                 my $err = $gz->flush($zbuf);
108                 die "gzip->flush: $err" if $err != Z_OK;
109                 $zbuf;
110         }
111 }
112
113 sub write {
114         # my $ret = bytes::length($_[1]); # XXX does anybody care?
115         $_[0]->{http_out}->write(translate($_[0], $_[1]));
116 }
117
118 # similar to ->translate; use this when we're sure we know we have
119 # more data to buffer after this
120 sub zmore {
121         my $self = $_[0]; # $_[1] => input
122         my $err = $self->{gz}->deflate($_[1], $self->{zbuf});
123         die "gzip->deflate: $err" if $err != Z_OK;
124         undef;
125 }
126
127 # flushes and returns the final bit of gzipped data
128 sub zflush ($;$) {
129         my $self = $_[0]; # $_[1] => final input (optional)
130         my $zbuf = delete $self->{zbuf};
131         my $gz = delete $self->{gz};
132         my $err;
133         if (defined $_[1]) {
134                 $err = $gz->deflate($_[1], $zbuf);
135                 die "gzip->deflate: $err" if $err != Z_OK;
136         }
137         $err = $gz->flush($zbuf);
138         die "gzip->flush: $err" if $err != Z_OK;
139         $zbuf;
140 }
141
142 sub close {
143         my ($self) = @_;
144         if (my $http_out = delete $self->{http_out}) {
145                 $http_out->write(zflush($self));
146                 $http_out->close;
147         }
148 }
149
150 # this is public-inbox-httpd-specific
151 sub async_blob_cb { # git->cat_async callback
152         my ($bref, $oid, $type, $size, $self) = @_;
153         my $http = $self->{env}->{'psgix.io'} or return; # client abort
154         my $smsg = $self->{smsg} or die 'BUG: no smsg';
155         if (!defined($oid)) {
156                 # it's possible to have TOCTOU if an admin runs
157                 # public-inbox-(edit|purge), just move onto the next message
158                 return $http->next_step($self->{async_next});
159         }
160         $smsg->{blob} eq $oid or die "BUG: $smsg->{blob} != $oid";
161         $self->{async_eml}->($self, PublicInbox::Eml->new($bref));
162         $http->next_step($self->{async_next});
163 }
164
165 sub smsg_blob {
166         my ($self, $smsg) = @_;
167         git_async_cat($self->{-inbox}->git, $smsg->{blob},
168                         \&async_blob_cb, $self);
169 }
170
171 1;