| 1 | package PAGI::Middleware::GZip; | ||||||||||||||||||||||||||||
| 2 | 2 | $PAGI::Middleware::GZip::VERSION = '0.002002'; | |||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use parent 'PAGI::Middleware'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use Future::AsyncAwait; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use IO::Compress::Gzip qw(gzip $GzipError); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | =head1 NAME | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | PAGI::Middleware::GZip - Response compression middleware | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | use PAGI::Middleware::Builder; | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | my $app = builder { | ||||||||||||||||||||||||||||
| 18 | enable 'GZip', | ||||||||||||||||||||||||||||
| 19 | min_size => 1024, | ||||||||||||||||||||||||||||
| 20 | mime_types => ['text/*', 'application/json']; | ||||||||||||||||||||||||||||
| 21 | $my_app; | ||||||||||||||||||||||||||||
| 22 | }; | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | PAGI::Middleware::GZip compresses response bodies using gzip when the | ||||||||||||||||||||||||||||
| 27 | client supports it (Accept-Encoding: gzip). | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | =head1 CONFIGURATION | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | =over 4 | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | =item * min_size (default: 1024) | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | Minimum response size to compress (bytes). | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | =item * mime_types (default: text/*, application/json, application/javascript) | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | MIME types to compress. | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | =back | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | =cut | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 8 | ▶ | sub _init { | ||||||||||||||||||||||||||
|
Subroutine
_init: called CC=3 SCAR=11.7
| |||||||||||||||||||||||||||||
| 46 | 8 | my ($self, $config) = @_; | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 8 | ▶ | $self->{min_size} = $config->{min_size} // 1024; | ||||||||||||||||||||||||||
|
Condition50%1/2
$config->{"min_size"} // 1024
MC/DC0%0/1
$config->{"min_size"} // 1024
$config->{"min_size"}
Truth table50%1/2
A
A= $config->{"min_size"}
| |||||||||||||||||||||||||||||
| 49 | $self->{mime_types} = $config->{mime_types} // [ | ||||||||||||||||||||||||||||
| 50 | 8 | ▶ | 'text/html', 'text/plain', 'text/css', 'text/javascript', | ||||||||||||||||||||||||||
|
Condition50%1/2
$config->{"mime_types"} // ["text/html", "text/plain", "text/css", "text/javascript", "application/json", "application/javascript", "application/xml"]
MC/DC0%0/1
$config->{"mime_types"} // ["text/html", "text/plain", "text/css", "text/javascript", "application/json", "application/javascript", "application/xml"]
$config->{"mime_types"}
Truth table50%1/2
A
A= $config->{"mime_types"}
| |||||||||||||||||||||||||||||
| 51 | 'application/json', 'application/javascript', 'application/xml', | ||||||||||||||||||||||||||||
| 52 | ]; | ||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 9 | ▶ | sub wrap { | ||||||||||||||||||||||||||
|
Subroutine
wrap: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 56 | 9 | my ($self, $app) = @_; | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 13 | ▶ | return async sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=11 SCAR=24.3
| |||||||||||||||||||||||||||||
| 59 | 13 | my ($scope, $receive, $send) = @_; | |||||||||||||||||||||||||||
| 60 | 13 | ▶ | if ($scope->{type} ne 'http') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 0 | await $app->($scope, $receive, $send); | |||||||||||||||||||||||||||
| 62 | 0 | return; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | # Check if client accepts gzip | ||||||||||||||||||||||||||||
| 66 | 13 | ▶ | my $accept_encoding = $self->_get_header($scope, 'accept-encoding') // ''; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->_get_header($scope, "accept-encoding") // ""
MC/DC100%1/1
$self->_get_header($scope, "accept-encoding") // ""
$self->_get_header($scope, "accept-encoding")
Truth table100%2/2
A
A= $self->_get_header($scope, "accept-encoding")
| |||||||||||||||||||||||||||||
| 67 | 13 | my $accepts_gzip = $accept_encoding =~ /\bgzip\b/i; | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 13 | ▶ | unless ($accepts_gzip) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 2 | await $app->($scope, $receive, $send); | |||||||||||||||||||||||||||
| 71 | 2 | return; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | # Buffer response to compress | ||||||||||||||||||||||||||||
| 75 | # NOTE: All request-specific state MUST be lexical variables, not instance | ||||||||||||||||||||||||||||
| 76 | # state ($self->{}), because middleware instances are shared across | ||||||||||||||||||||||||||||
| 77 | # concurrent requests. Using $self->{} would cause race conditions. | ||||||||||||||||||||||||||||
| 78 | 11 | my @body_parts; | |||||||||||||||||||||||||||
| 79 | 11 | my $response_started = 0; | |||||||||||||||||||||||||||
| 80 | 11 | my $content_type = ''; | |||||||||||||||||||||||||||
| 81 | 11 | my $original_headers; | |||||||||||||||||||||||||||
| 82 | 11 | my $headers_sent = 0; # Request-local state (NOT on $self!) | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 25 | ▶ | my $wrapped_send = async sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=10 SCAR=23.5
| |||||||||||||||||||||||||||||
| 85 | 25 | my ($event) = @_; | |||||||||||||||||||||||||||
| 86 | 25 | ▶ | if ($event->{type} eq 'http.response.start') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 87 | 11 | $original_headers = $event->{headers}; | |||||||||||||||||||||||||||
| 88 | # Get content type | ||||||||||||||||||||||||||||
| 89 | 11 | ▶ | for my $h (@{$event->{headers} // []}) { | ||||||||||||||||||||||||||
|
Condition50%1/2
$event->{"headers"} // []
MC/DC0%0/1
$event->{"headers"} // []
$event->{"headers"}
Truth table50%1/2
A
A= $event->{"headers"}
| |||||||||||||||||||||||||||||
| 90 | 11 | ▶ | if (lc($h->[0]) eq 'content-type') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 11 | $content_type = $h->[1]; | |||||||||||||||||||||||||||
| 92 | 11 | last; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | 11 | $response_started = 1; | |||||||||||||||||||||||||||
| 96 | # Don't send yet - buffer to compress | ||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | elsif ($event->{type} eq 'http.response.body') { | ||||||||||||||||||||||||||||
| 99 | # If we're already in streaming mode, pass through all chunks | ||||||||||||||||||||||||||||
| 100 | 14 | ▶ | if ($headers_sent) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 3 | await $send->($event); | |||||||||||||||||||||||||||
| 102 | 3 | return; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 11 | ▶ | push @body_parts, $event->{body} // ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
$event->{"body"} // ""
MC/DC0%0/1
$event->{"body"} // ""
$event->{"body"}
Truth table50%1/2
A
A= $event->{"body"}
| |||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | # If streaming (more => 1), switch to pass-through mode | ||||||||||||||||||||||||||||
| 108 | 11 | ▶ | if ($event->{more}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 2 | ▶ | if (!$headers_sent) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | 2 | await $send->({ | |||||||||||||||||||||||||||
| 111 | type => 'http.response.start', | ||||||||||||||||||||||||||||
| 112 | status => 200, | ||||||||||||||||||||||||||||
| 113 | headers => $original_headers, | ||||||||||||||||||||||||||||
| 114 | }); | ||||||||||||||||||||||||||||
| 115 | 2 | $headers_sent = 1; | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | 2 | await $send->($event); | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | else { | ||||||||||||||||||||||||||||
| 121 | 0 | await $send->($event); | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | 11 | }; | |||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 11 | await $app->($scope, $receive, $wrapped_send); | |||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | # If headers already sent (streaming), we're done | ||||||||||||||||||||||||||||
| 128 | 11 | ▶ | return if $headers_sent; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | # Combine body | ||||||||||||||||||||||||||||
| 131 | 9 | my $body = join('', @body_parts); | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | # Decide whether to compress | ||||||||||||||||||||||||||||
| 134 | 9 | my $should_compress = $self->_should_compress($content_type, length($body)); | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 9 | ▶ | if ($should_compress && length($body) > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$should_compress and length $body > 0
MC/DC50%1/2
$should_compress and length $body > 0
$should_compress
length $body > 0
Truth table66%2/3
A and B
A= $should_compressB= length $body > 0
| |||||||||||||||||||||||||||||
| 137 | 8 | my $compressed; | |||||||||||||||||||||||||||
| 138 | 8 | ▶ | gzip(\$body, \$compressed) or die "gzip failed: $GzipError"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | # Update headers | ||||||||||||||||||||||||||||
| 141 | 8 | my @new_headers; | |||||||||||||||||||||||||||
| 142 | 8 | ▶ | for my $h (@{$original_headers // []}) { | ||||||||||||||||||||||||||
|
Condition50%1/2
$original_headers // []
MC/DC0%0/1
$original_headers // []
$original_headers
Truth table50%1/2
A
A= $original_headers
| |||||||||||||||||||||||||||||
| 143 | 8 | ▶ | next if lc($h->[0]) eq 'content-length'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | 8 | push @new_headers, $h; | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | 8 | push @new_headers, ['Content-Encoding', 'gzip']; | |||||||||||||||||||||||||||
| 147 | 8 | push @new_headers, ['Content-Length', length($compressed)]; | |||||||||||||||||||||||||||
| 148 | 8 | push @new_headers, ['Vary', 'Accept-Encoding']; | |||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 8 | await $send->({ | |||||||||||||||||||||||||||
| 151 | type => 'http.response.start', | ||||||||||||||||||||||||||||
| 152 | status => 200, | ||||||||||||||||||||||||||||
| 153 | headers => \@new_headers, | ||||||||||||||||||||||||||||
| 154 | }); | ||||||||||||||||||||||||||||
| 155 | 8 | await $send->({ | |||||||||||||||||||||||||||
| 156 | type => 'http.response.body', | ||||||||||||||||||||||||||||
| 157 | body => $compressed, | ||||||||||||||||||||||||||||
| 158 | more => 0, | ||||||||||||||||||||||||||||
| 159 | }); | ||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | else { | ||||||||||||||||||||||||||||
| 162 | 1 | await $send->({ | |||||||||||||||||||||||||||
| 163 | type => 'http.response.start', | ||||||||||||||||||||||||||||
| 164 | status => 200, | ||||||||||||||||||||||||||||
| 165 | headers => $original_headers, | ||||||||||||||||||||||||||||
| 166 | }); | ||||||||||||||||||||||||||||
| 167 | 1 | await $send->({ | |||||||||||||||||||||||||||
| 168 | type => 'http.response.body', | ||||||||||||||||||||||||||||
| 169 | body => $body, | ||||||||||||||||||||||||||||
| 170 | more => 0, | ||||||||||||||||||||||||||||
| 171 | }); | ||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | 9 | }; | |||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 9 | ▶ | sub _should_compress { | ||||||||||||||||||||||||||
|
Subroutine
_should_compress: called CC=6 SCAR=19.6
| |||||||||||||||||||||||||||||
| 177 | 9 | my ($self, $content_type, $size) = @_; | |||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 9 | ▶ | return 0 if $size < $self->{min_size}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 8 | $content_type =~ s/;.*//; # Remove charset etc. | |||||||||||||||||||||||||||
| 182 | 8 | $content_type = lc($content_type); | |||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 8 | for my $type (@{$self->{mime_types}}) { | |||||||||||||||||||||||||||
| 185 | 15 | ▶ | return 1 if $content_type eq lc($type); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 186 | 7 | ▶ | if ($type =~ /\*$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 187 | 0 | my $prefix = substr($type, 0, -1); | |||||||||||||||||||||||||||
| 188 | 0 | ▶ | return 1 if index($content_type, lc($prefix)) == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||
| 191 | 0 | return 0; | |||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | 13 | ▶ | sub _get_header { | ||||||||||||||||||||||||||
|
Subroutine
_get_header: called CC=4 SCAR=14.2
| |||||||||||||||||||||||||||||
| 195 | 13 | my ($self, $scope, $name) = @_; | |||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | 13 | $name = lc($name); | |||||||||||||||||||||||||||
| 198 | 13 | ▶ | for my $h (@{$scope->{headers} // []}) { | ||||||||||||||||||||||||||
|
Condition50%1/2
$scope->{"headers"} // []
MC/DC0%0/1
$scope->{"headers"} // []
$scope->{"headers"}
Truth table50%1/2
A
A= $scope->{"headers"}
| |||||||||||||||||||||||||||||
| 199 | 11 | ▶ | return $h->[1] if lc($h->[0]) eq $name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||
| 201 | 2 | return; | |||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 2 | 1; | |||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | __END__ | ||||||||||||||||||||||||||||