Plack:...eflater(3pm)
π Plack::Middleware::Deflater π
okay bestie, gather round, because this is Plack::Middleware::Deflater from the Perl world (User Contributed Perl Documentation, we love her) and she does exactly one thing extremely well: squishing your response body down with Gzip or Deflate. πΈ
β¨ NAME β¨
Plack::Middleware::Deflater β compress that response body with Gzip or Deflate. that's the whole assignment. π
π¦ SYNOPSIS π¦
here's the vibe, verbatim, no notes:
use Plack::Builder;
builder {
enable sub {
my $app = shift;
sub {
my $env = shift;
my $ua = $env->{HTTP_USER_AGENT} || '';
# Netscape has some problem
$env->{"psgix.compress-only-text/html"} = 1 if $ua =~ m!^Mozilla/4!;
# Netscape 4.06-4.08 have some more problems
$env->{"psgix.no-compress"} = 1 if $ua =~ m!^Mozilla/4\.0[678]!;
# MSIE (7|8) masquerades as Netscape, but it is fine
if ( $ua =~ m!\bMSIE (?:7|8)! ) {
$env->{"psgix.no-compress"} = 0;
$env->{"psgix.compress-only-text/html"} = 0;
}
$app->($env);
}
};
enable "Deflater",
content_type => ['text/css','text/html','text/javascript','application/javascript'],
vary_user_agent => 1;
sub { [200,['Content-Type','text/html'],["OK"]] }
};
like. look at that. it's giving user-agent archaeology and honestly the Netscape lore is sending me. π
π DESCRIPTION π
Plack::Middleware::Deflater is a middleware that encodes your response body in gzip or deflate, and it decides to do that based on the Accept-Encoding HTTP request header. she reads the room before she acts. queen behavior. π
it would save the bandwidth a little bit but it should increase the Plack server load, so ideally you should handle this on the frontend reverse proxy servers. like, let the bouncer do it, not the person actually running the party. π«Ά
also, important: this middleware removes Content-Length and streams encoded content. which means the server should support HTTP/1.1 chunked response, or downgrade to HTTP/1.0 and close the connection. no cap, that's just how it is.
π CONFIGURATIONS π
π content_type
content_type => 'text/html',
content_type => [ 'text/html', 'text/css', 'text/javascript', 'application/javascript', 'application/x-javascript' ]
this is the Content-Type header to apply deflater on. and if content-type is not defined, Deflater will try to deflate all contents. she's an overachiever, we respect it. β¨
π§ vary_user_agent
vary_user_agent => 1
adds User-Agent to the Vary header. that's it, that's the whole option. π
β ENVIRONMENT VALUE β
psgix.no-compress
do not apply deflater. π
psgix.compress-only-text/html
apply deflater only if content_type is text/html. picky, but make it fashion. π
plack.skip-deflater
skip all Deflater features. the full ghost. π
πΈ Compare psgix.no-compress with plack.skip-deflater
okay this distinction is lowkey the most important part of the whole page, so pay attention bestie:
- if
no-compressis true, PM::Deflater skips gzip or deflate. but it still addsVary: Accept-EncodingandVary: User-Agentheaders. skip-deflaterforces it to skip all PM::Deflater features β it doesn't allow theVaryheader to be added.
so one of them is "don't do the thing but still sign the guestbook" and the other is "leave the building entirely." different energies fr fr. π₯
π LICENSE π
this software is licensed under the same terms as Perl itself. periodt. π
π AUTHOR π
Tatsuhiko Miyagawa β icon, legend, the moment. π«Ά
π₯ SEE ALSO π₯
cute sign-off time!! this page came to you via perl v5.38.2, dated 2024-05-15. mwah, go compress something responsibly bestie ππ¦πΈ
π¦ where this page comes from
- debian trixie: libplack-middleware-deflater-perl 0.14-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libplack-middleware-deflater-perl 0.14-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libplack-middleware-deflater-perl 0.14-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms