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