| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | |||
| 4 | package Jifty::Plugin::Deflater; | ||
| 5 | use base 'Jifty::Plugin'; | ||
| 6 | use Plack::Builder; | ||
| 7 | use Plack::Util; | ||
| 8 | |||
| 9 | =head1 NAME | ||
| 10 | |||
| 11 | Jifty::Plugin::Deflater - Handles Accept-Encoding and compression | ||
| 12 | |||
| 13 | =head1 SYNOPSIS | ||
| 14 | |||
| 15 | # In your jifty config.yml under the framework section: | ||
| 16 | |||
| 17 | Plugins: | ||
| 18 | - Deflater: {} | ||
| 19 | |||
| 20 | # You should put deflater at the end of the plugins list | ||
| 21 | |||
| 22 | =head1 DESCRIPTION | ||
| 23 | |||
| 24 | This plugin provides Accept-Encoding handling. | ||
| 25 | |||
| 26 | =head1 METHODS | ||
| 27 | |||
| 28 | =head2 wrap APP | ||
| 29 | |||
| 30 | Wraps the provided Plack app with the deflation middleware. You'll never need | ||
| 31 | to call this yourself. | ||
| 32 | |||
| 33 | =cut | ||
| 34 | |||
| 35 | sub wrap { | ||
| 36 | my ($self, $app) = @_; | ||
| 37 | |||
| 38 | builder { | ||
| 39 | enable 'Deflater'; | ||
| 40 | enable sub { my $app = shift; | ||
| 41 | sub { my $env = shift; | ||
| 42 | my $res = $app->($env); | ||
| 43 | # skip streamy response | ||
| 44 | return $res unless ref($res) eq 'ARRAY' && $res->[2]; | ||
| 45 | my $h = Plack::Util::headers($res->[1]); | ||
| 46 | my $type = $h->get('Content-Type') | ||
| 47 | or return $res; | ||
| 48 | delete $env->{HTTP_ACCEPT_ENCODING} | ||
| 49 | unless $type =~ m|application/x-javascript| || $type =~ m|^text/|; | ||
| 50 | $res } | ||
| 51 | }; | ||
| 52 | $app; | ||
| 53 | }; | ||
| 54 | } | ||
| 55 | |||
| 56 | 1; |