| 1 | package Plack::Middleware::CSS::Compressor; | ||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||
| 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 | |||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use parent qw( Plack::Middleware ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use CSS::Compressor qw( css_compress ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use Plack::Util::Accessor qw( suffix ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use Plack::Util; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 2 | our $VERSION = '0.01'; | |||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||||
| 15 | sub prepare_app | ||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 1 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
prepare_app: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 18 | 1 | my $suffix = $self->suffix(); | |||||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 1 | ▶ | unless ( defined( $suffix ) ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 1 | $suffix = '-min'; | |||||||||||||||||||||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||||||
| 24 | # make the suffix usable in regular expressions | ||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 1 | ▶ | $self->suffix( ref( $suffix ) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 26 | ? $suffix | ||||||||||||||||||||||||||||||||||||||||||||||
| 27 | : quotemeta( $suffix ) | ||||||||||||||||||||||||||||||||||||||||||||||
| 28 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||||||
| 31 | sub call | ||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 3 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
call: called CC=7 SCAR=21.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 3 | my ( $self, $env ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 34 | 3 | my $suffix = $self->suffix(); | |||||||||||||||||||||||||||||||||||||||||||||
| 35 | 3 | my $path_info = $env->{PATH_INFO}; | |||||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||||||
| 37 | # match and adjust file name and extension | ||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 3 | ▶ | unless ( $env->{PATH_INFO} =~ s!\A (.*) $suffix ([.]css) \z!$1$2!x ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 39 | # otherwise pass through | ||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 2 | return $self->app->( $env ); | |||||||||||||||||||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||||||
| 43 | # call underlying handler with adjusted path | ||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 1 | my $res = $self->app->( $env ); | |||||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||||||
| 46 | # make sure the response is an array and not streamed | ||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 1 | ▶ | unless ( ref( $res ) eq 'ARRAY' ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | return $res; | |||||||||||||||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 1 | my $h = Plack::Util::headers( $res->[1] ); | |||||||||||||||||||||||||||||||||||||||||||||
| 52 | 1 | my $ct = $h->get( 'Content-Type' ); | |||||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||||||
| 54 | # got a stylesheet as response | ||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 1 | ▶ | if ( $res->[0] == 200 && $ct && $ct =~ m!\A text/css \b!x ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%2/6
$res->[0] == 200 and $ct and $ct =~ m[\A text/css \b]x
$res->[0] == 200 and $ct
MC/DC0%0/3
$res->[0] == 200 and $ct and $ct =~ m[\A text/css \b]x
$res->[0] == 200
$ct
$ct =~ m[\A text/css \b]x
Truth table25%1/4
A and B and C
A= $res->[0] == 200B= $ctC= $ct =~ m[\A text/css \b]x
| |||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 1 | my $css = ''; | |||||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||||||
| 58 | # collect all css chunks | ||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 1 | ▶ | Plack::Util::foreach( $res->[2], sub { $css .= $_[0] } ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||||||
| 61 | # store compressed css as new body | ||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 1 | $res->[2] = [ css_compress( $css ) ]; | |||||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||||||
| 64 | # adjust length | ||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 1 | $h->set( 'Content-Length' => length( $res->[2][0] ) ); | |||||||||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 67 | # nothing found with the adjusted path | ||||||||||||||||||||||||||||||||||||||||||||||
| 68 | elsif ( $res->[0] == 404 ) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 69 | # restore original path | ||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | $env->{PATH_INFO} = $path_info; | |||||||||||||||||||||||||||||||||||||||||||||
| 71 | # and try again | ||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | $res = $self->app->( $env ); | |||||||||||||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 1 | return $res; | |||||||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||||||
| 80 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||