FU::Util: Add brotli_compress() and use it for FU output compression
Seems to compresses and perform better than libdeflate at level 6, so certainly worth using.
This commit is contained in:
parent
bc33fe53f0
commit
6159b33950
5 changed files with 99 additions and 14 deletions
19
FU.pm
19
FU.pm
|
|
@ -400,7 +400,7 @@ sub _do_req($c) {
|
|||
($REQ->{trace_sqlexec}||0)*1000, ($REQ->{trace_sqlprep}||0)*1000,
|
||||
$REQ->{trace_nsqldirect}||0, $REQ->{trace_nsqlprep}||0, $REQ->{trace_nsql} : '',
|
||||
$REQ->{status}, ($REQ->{reshdr}{'content-type'}//'-') =~ s/;.+$//r,
|
||||
length($REQ->{resbody}), substr($REQ->{reshdr}{'content-encoding'}//'bytes', 0, 1)
|
||||
length($REQ->{resbody}), substr($REQ->{reshdr}{'content-encoding'}//'r', 0, 1)
|
||||
) if FU::debug || $proc_ms > (FU::log_slow_reqs||1e10);
|
||||
}
|
||||
|
||||
|
|
@ -850,6 +850,8 @@ sub _error_page($, $code, $title, $msg) {
|
|||
}
|
||||
|
||||
sub _finalize {
|
||||
state $hasgzip = FU::Util::gzip_lib();
|
||||
state $hasbrotli = eval { FU::Util::brotli_compress(6, ''); 1 };
|
||||
my $r = $FU::REQ;
|
||||
|
||||
fu->add_header('set-cookie', $_) for $r->{rescookie} ? sort values $r->{rescookie}->%* : ();
|
||||
|
|
@ -861,13 +863,19 @@ sub _finalize {
|
|||
$r->{resbody} = '';
|
||||
|
||||
} else {
|
||||
if (FU::Util::gzip_lib() && length($r->{resbody}) > 256
|
||||
&& !defined $r->{reshdr}{'content-encoding'} && FU::compress_mimes->{$r->{reshdr}{'content-type'}}) {
|
||||
if (($hasgzip || $hasbrotli) && length($r->{resbody}) > 256
|
||||
&& !defined $r->{reshdr}{'content-encoding'}
|
||||
&& FU::compress_mimes->{$r->{reshdr}{'content-type'}}
|
||||
) {
|
||||
|
||||
$r->{reshdr}{'vary'} = ($r->{reshdr}{'vary'} ? $r->{reshdr}{'vary'}.', ' : '').'accept-encoding'
|
||||
if ($r->{reshdr}{'vary'}||'') !~ /accept-encoding/i;
|
||||
|
||||
if (($r->{hdr}{'accept-encoding'}||'') =~ /gzip/) {
|
||||
if ($hasbrotli && ($r->{hdr}{'accept-encoding'}||'') =~ /\bbr\b/) {
|
||||
$r->{resbody} = FU::Util::brotli_compress(6, $r->{resbody});
|
||||
$r->{reshdr}{'content-encoding'} = 'br';
|
||||
|
||||
} elsif ($hasgzip && ($r->{hdr}{'accept-encoding'}||'') =~ /\bgzip\b/) {
|
||||
$r->{resbody} = FU::Util::gzip_compress(6, $r->{resbody});
|
||||
$r->{reshdr}{'content-encoding'} = 'gzip';
|
||||
}
|
||||
|
|
@ -991,6 +999,9 @@ C<dlopen()>.
|
|||
=item * C<libdeflate.so> or C<libz-ng.so> or C<libz.so> - required for
|
||||
C<gzip_compress()> in L<FU::Util> and used for HTTP output compression.
|
||||
|
||||
=item * C<libbrotlienc.so> - required for C<brotli_compress()> in L<FU::Util>
|
||||
and used for HTTP output compression.
|
||||
|
||||
=back
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue