File Coverage

blib/lib/MojoX/Encode/Gzip.pm
Criterion Covered Total %
statement 28 30 93.3
branch 3 6 50.0
condition 10 19 52.6
subroutine 5 5 100.0
pod 1 1 100.0
total 47 61 77.0


line stmt bran cond sub pod time code
1             package MojoX::Encode::Gzip;
2              
3             # ABSTRACT: Gzip a Mojo::Message::Response
4              
5 2     2   35351 use strict;
  2         4  
  2         68  
6 2     2   8 use warnings;
  2         2  
  2         52  
7              
8 2     2   8 use base 'Mojo::Base';
  2         11  
  2         913  
9              
10             our $VERSION = '1.11';
11              
12 2     2   19879 use Compress::Zlib ();
  2         143723  
  2         1237  
13              
14             __PACKAGE__->attr( min_bytes => 500 );
15             __PACKAGE__->attr( max_bytes => 500000 );
16              
17             sub maybe_gzip {
18 2     2 1 3531 my $self = shift;
19 2         5 my $tx = shift;
20 2         35 my $req = $tx->req;
21 2         34 my $res = $tx->res;
22              
23 2   100     16 my $accept = $req->headers->header('Accept-Encoding') || '';
24 2         108 my $body = $res->body;
25 2   50     86 my $length = $res->body_size || 0;
26              
27             # Don't both unless:
28             # - we have a success code
29             # - we have a content type that makes sense to gzip
30             # - a client is asking for giving
31             # - the content is not already encoded.
32             # - The body is not too small or too large to gzip
33             # XXX content-types should be configurable.
34 2 50 66     150 unless ( ( index( $accept, 'gzip' ) >= 0 )
      66        
      33        
      33        
      33        
35             and ( $length > $self->min_bytes )
36             and ( $length < $self->max_bytes )
37             and ( $res->code == 200 )
38             and ( not $res->headers->header('Content-Encoding'))
39             and ( $res->headers->content_type =~ qr{^text|xml$|javascript$|^application/json$} )
40             ) {
41 1         3 return undef;
42             }
43              
44 1 50       155 eval { local $/; $body = <$body> } if ref $body;
  0         0  
  0         0  
45 1 50       3 die "Response body is an unsupported kind of reference" if ref $body;
46              
47 1         6 $res->body( Compress::Zlib::memGzip( $body ) );
48 1         478 $res->headers->content_length( $length );
49 1         49 $res->headers->header('Content-Encoding' => 'gzip');
50 1         46 $res->headers->add( 'Vary' => 'Accept-Encoding' );
51              
52 1         58 return 1;
53             }
54              
55             1;
56              
57             =pod
58              
59             =encoding UTF-8
60              
61             =head1 NAME
62              
63             MojoX::Encode::Gzip - Gzip a Mojo::Message::Response
64              
65             =head1 VERSION
66              
67             version 1.11
68              
69             =head1 SYNOPSIS
70              
71             use MojoX:Encode::Gzip;
72              
73             # Simple
74             MojoX::Encode::Gzip->new->maybe_gzip($tx);
75              
76             # With options
77             my $gzip = MojoX::Encode::Gzip->new(
78             min_bytes => 600,
79             max_bytes => 600000,
80             );
81             $success = $gzip->maybe_gzip($tx);
82              
83             =head1 DESCRIPTION
84              
85             Gzip compress a Mojo::Message::Response if client supports it.
86              
87             =head2 ATTRIBUTES
88              
89             =head2 C
90              
91             The minumum number of bytes in the body before we would try to gzip it. Trying to gzip really
92             small messages can take extra CPU power on the server and client without saving any times. Defaults
93             to 500.
94              
95             =head2 C
96              
97             The maximum number of bytes in the body before we give up on trying gzip it. Gzipping very large messages
98             can delay the response and possibly exhaust system resources. Defaults to 500000.
99              
100             =head1 METHODS
101              
102             =head2 C
103              
104             my $success = $gzip->maybe_gzip($tx);
105              
106             Given a L object, possibly gzips transforms the response by
107             gzipping it. Returns true if we gzip it, and undef otherwise. The behavior is
108             modified by the C<< min_bytes >> and C<< max_bytes >> attributes.
109              
110             Currently we only only try to gzip Content-types that start with "text/", or end in "xml" or "javascript",
111             along with "application/json". This may be configurable in the future.
112              
113             =head1 AUTHOR
114              
115             Mark Stosberg, C<< >>
116              
117             =head1 BUGS
118              
119             Please report any bugs or feature requests to C, or through
120             the web interface at L. I will be notified, and then you'll
121             automatically be notified of progress on your bug as I make changes.
122              
123             =head1 SUPPORT
124              
125             You can find documentation for this module with the perldoc command.
126              
127             perldoc MojoX::Encode::Gzip
128              
129             You can also look for information at:
130              
131             =over 4
132              
133             =item * RT: CPAN's request tracker
134              
135             L
136              
137             =item * AnnoCPAN: Annotated CPAN documentation
138              
139             L
140              
141             =item * CPAN Ratings
142              
143             L
144              
145             =item * Search CPAN
146              
147             L
148              
149             =back
150              
151             =head1 ACKNOWLEDGEMENTS
152              
153             Inspired by Catalyst::Plugin::Compress::Gzip
154              
155             =head1 COPYRIGHT & LICENSE
156              
157             Copyright 2008 Mark Stosberg, all rights reserved.
158              
159             This program is free software; you can redistribute it and/or modify it
160             under the same terms as Perl itself.
161              
162             =head1 AUTHOR
163              
164             Renee Baecker
165              
166             =head1 COPYRIGHT AND LICENSE
167              
168             This software is copyright (c) 2015 by Renee Baecker.
169              
170             This is free software; you can redistribute it and/or modify it under
171             the same terms as the Perl 5 programming language system itself.
172              
173             =cut
174              
175             __END__