blib/lib/MojoX/Encode/Gzip.pm

filter: Statementstmt 95.3% 41 / 43 Branchbran 66.6% 4 / 6 Conditioncond 68.4% 13 / 19 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 100% 7 / 7 Podpod 100% 1 / 1 totaltotal 80.9% 68 / 84 CC 11 SCAR 21.1
CC
11
Cov
85%
maybe_gzip
12.2
BEGIN
1.0
BEGIN
1.0
CRAP 8.3
↑ Summary
1 package MojoX::Encode::Gzip;
2  
3 # ABSTRACT: Gzip a Mojo::Message::Response
4  
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use base 'Mojo::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Mojo::Content::Single;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Mojo::Util qw(gzip);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 our $VERSION = '1.13';
15  
16 2 __PACKAGE__->attr( min_bytes => 500 );
17 2 __PACKAGE__->attr( max_bytes => 500000 );
18  
19 2 ▶sub maybe_gzip {
Subroutine maybe_gzip: called CC=11 SCAR=25.0
Pod documented
20 2 my $self = shift;
21 2 my $tx = shift;
22 #my $debug = shift;
23  
24 2 my $req = $tx->req;
25 2 my $res = $tx->res;
26  
27 2 ▶ my $accept = $req->headers->header('Accept-Encoding') || '';
Condition100%2/2
$req->headers->header('Accept-Encoding') || ''
l!l
11
MC/DC100%1/1
$req->headers->header('Accept-Encoding') || ''
$req->headers->header('Accept-Encoding')
Truth table100%2/2
A
A= $req->headers->header('Accept-Encoding')
Aresult
11
00
28 2 my $body = $res->body;
29 2 ▶ my $length = $res->body_size || 0;
Condition50%1/2
$res->body_size || 0
l!l
20
MC/DC0%0/1
$res->body_size || 0
$res->body_size
Truth table50%1/2
A
A= $res->body_size
Aresult
11
00
30  
31 # Don't both unless:
32 # - we have a success code
33 # - we have a content type that makes sense to gzip
34 # - a client is asking for giving
35 # - the content is not already encoded.
36 # - The body is not too small or too large to gzip
37 # XXX content-types should be configurable.
38 2 ▶ unless ( ( index( $accept, 'gzip' ) >= 0 )
Branch: 1 branch
BranchTrueFalseTotal
unless ((index($accept, "gzip") != -1) and $length > $self->min_bytes and $length < $self->max_bytes and $res->code == 200 and not $res->headers->header("Content-Encoding") and $res->headers->content_type =~ qr"^text|xml$|javascript$|^application/json$") 1 1 2
Condition66%10/15
(index($accept, "gzip") != -1) and $length > $self->min_bytes and $length < $self->max_bytes and $res->code == 200 and not $res->headers->header("Content-Encoding") and $res->headers->content_type =~ qr"^text|xml$|javascript$|^application/json$"
!ll&&!rl&&r
101
(index($accept, "gzip") != -1) and $length > $self->min_bytes and $length < $self->max_bytes and $res->code == 200 and not $res->headers->header("Content-Encoding")
!ll&&!rl&&r
101
(index($accept, "gzip") != -1) and $length > $self->min_bytes and $length < $self->max_bytes and $res->code == 200
!ll&&!rl&&r
101
(index($accept, "gzip") != -1) and $length > $self->min_bytes and $length < $self->max_bytes
!ll&&!rl&&r
101
(index($accept, "gzip") != -1) and $length > $self->min_bytes
!ll&&!rl&&r
101
MC/DC16%1/6
(index($accept, "gzip") != -1) and $length > $self->min_bytes and $length < $self->max_bytes and $res->code == 200 and not $res->headers->header("Content-Encoding") and $res->headers->content_type =~ qr"^text|xml$|javascript$|^application/json$"
(index($accept, "gzip") != -1) $length > $self->min_bytes $length < $self->max_bytes $res->code == 200 not $res->headers->header("Content-Encoding") $res->headers->content_type =~ qr"^text|xml$|javascript$|^application/json$"
Truth table28%2/7
A and B and C and D and not(E) and F
A= (index($accept, "gzip") != -1)B= $length > $self->min_bytesC= $length < $self->max_bytesD= $res->code == 200E= not $res->headers->header("Content-Encoding")F= $res->headers->content_type =~ qr"^text|xml$|javascript$|^application/json$"
ABCDEFresult
0XXXXX0
10XXXX0
110XXX0
1110XX0
11110X0
1111100
1111111
39 and ( $length > $self->min_bytes )
40 and ( $length < $self->max_bytes )
41 and ( $res->code == 200 )
42 and ( not $res->headers->header('Content-Encoding'))
43 and ( $res->headers->content_type =~ qr{^text|xml$|javascript$|^application/json$} )
44 ) {
45 1 return undef;
46 }
47  
48 0 ▶ eval { local $/; $body = <$body> } if ref $body;
Branch: 1 branch
BranchTrueFalseTotal
if ref $body 0 1 1
49 1 ▶ die "Response body is an unsupported kind of reference" if ref $body;
Branch: 1 branch
BranchTrueFalseTotal
if ref $body 0 1 1
50  
51 1 my $zipped = gzip $body;
52  
53 1 $res->content( Mojo::Content::Single->new );
54 1 $res->body( $zipped );
55 1 $res->fix_headers;
56 1 $res->headers->header( 'Content-Length' => length $zipped );
57 1 $res->headers->header( 'Content-Encoding' => 'gzip' );
58 1 $res->headers->add( 'Vary' => 'Accept-Encoding' );
59  
60 1 return 1;
61 }
62  
63 2 1;
64  
65 =pod
66  
67 =encoding UTF-8
68  
69 =head1 NAME
70  
71 MojoX::Encode::Gzip - Gzip a Mojo::Message::Response
72  
73 =head1 VERSION
74  
75 version 1.13
76  
77 =head1 SYNOPSIS
78  
79 use MojoX:Encode::Gzip;
80  
81 # Simple
82 MojoX::Encode::Gzip->new->maybe_gzip($tx);
83  
84 # With options
85 my $gzip = MojoX::Encode::Gzip->new(
86 min_bytes => 600,
87 max_bytes => 600000,
88 );
89 $success = $gzip->maybe_gzip($tx);
90  
91 =head1 DESCRIPTION
92  
93 Gzip compress a Mojo::Message::Response if client supports it.
94  
95 =head2 ATTRIBUTES
96  
97 =head2 C<min_bytes>
98  
99 The minumum number of bytes in the body before we would try to gzip it. Trying to gzip really
100 small messages can take extra CPU power on the server and client without saving any times. Defaults
101 to 500.
102  
103 =head2 C<max_bytes>
104  
105 The maximum number of bytes in the body before we give up on trying gzip it. Gzipping very large messages
106 can delay the response and possibly exhaust system resources. Defaults to 500000.
107  
108 =head1 METHODS
109  
110 =head2 C<maybe_gzip>
111  
112 my $success = $gzip->maybe_gzip($tx);
113  
114 Given a L<Mojo::Transaction> object, possibly gzips transforms the response by
115 gzipping it. Returns true if we gzip it, and undef otherwise. The behavior is
116 modified by the C<< min_bytes >> and C<< max_bytes >> attributes.
117  
118 Currently we only only try to gzip Content-types that start with "text/", or end in "xml" or "javascript",
119 along with "application/json". This may be configurable in the future.
120  
121 =head1 SUPPORT
122  
123 You can find documentation for this module with the perldoc command.
124  
125 perldoc MojoX::Encode::Gzip
126  
127 =head1 CODE REPOSITORY AND BUGTRACKER
128  
129 The code repository and a bugtracker are available at L<http://github.com/reneeb/MojoX-Encode-Gzip>.
130  
131 =head1 ACKNOWLEDGEMENTS
132  
133 Inspired by Catalyst::Plugin::Compress::Gzip
134  
135 =head1 PREVIOUS MAINTAINERS
136  
137 =over 4
138  
139 =item * 2008-2015 Mark Stosberg
140  
141 =back
142  
143 =head1 AUTHOR
144  
145 Renee Baecker <reneeb@cpan.org>
146  
147 =head1 COPYRIGHT AND LICENSE
148  
149 This software is copyright (c) 2018 by Renee Baecker.
150  
151 This is free software; you can redistribute it and/or modify it under
152 the same terms as the Perl 5 programming language system itself.
153  
154 =cut
155  
156 __END__
↑ Summary