line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Plack::Middleware::DevFilter; |
2
|
2
|
|
|
2
|
|
69190
|
use strict; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
70
|
|
3
|
2
|
|
|
2
|
|
10
|
use warnings; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
57
|
|
4
|
2
|
|
|
2
|
|
10
|
use Carp qw/croak/; |
|
2
|
|
|
|
|
8134
|
|
|
2
|
|
|
|
|
190
|
|
5
|
2
|
|
|
2
|
|
3712
|
use parent 'Plack::Middleware'; |
|
2
|
|
|
|
|
633
|
|
|
2
|
|
|
|
|
10
|
|
6
|
2
|
|
|
2
|
|
80420
|
use Plack::Util::Accessor qw/force_enable filters image_type/; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
12
|
|
7
|
2
|
|
|
2
|
|
100
|
use Plack::Util; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
1820
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub prepare_app { |
12
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
13
|
|
|
|
|
|
|
|
14
|
0
|
0
|
|
|
|
|
croak "'filters' option must be ARRAY." unless ref $self->filters eq 'ARRAY'; |
15
|
|
|
|
|
|
|
|
16
|
0
|
0
|
|
|
|
|
unless ($self->image_type) { |
17
|
|
|
|
|
|
|
$self->image_type( |
18
|
|
|
|
|
|
|
sub { |
19
|
0
|
|
|
0
|
|
|
my ($env, $res) = @_; |
20
|
|
|
|
|
|
|
|
21
|
0
|
|
|
|
|
|
my $ct = Plack::Util::header_get($res->[1], 'content-type'); |
22
|
0
|
0
|
|
|
|
|
my $image_type = ($ct =~ m!(png|gif|jpeg|ico)!) ? $1 : undef; |
23
|
0
|
|
|
|
|
|
return $image_type; |
24
|
|
|
|
|
|
|
}, |
25
|
0
|
|
|
|
|
|
); |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub call { |
30
|
0
|
|
|
0
|
1
|
|
my($self, $env) = @_; |
31
|
|
|
|
|
|
|
|
32
|
0
|
|
|
|
|
|
my $res = $self->app->($env); |
33
|
|
|
|
|
|
|
|
34
|
0
|
0
|
|
|
|
|
return $res if $self->_no_filter; |
35
|
|
|
|
|
|
|
|
36
|
0
|
|
|
|
|
|
my $body = ''; |
37
|
|
|
|
|
|
|
Plack::Util::foreach($res->[2], sub { |
38
|
0
|
|
|
0
|
|
|
my($buf) = @_; |
39
|
0
|
|
|
|
|
|
$body .= $buf; |
40
|
0
|
|
|
|
|
|
}); |
41
|
|
|
|
|
|
|
|
42
|
0
|
|
|
|
|
|
my ($imager, $image_type); |
43
|
0
|
0
|
|
|
|
|
if ( $image_type = $self->image_type->($env, $res) ) { |
44
|
0
|
|
|
|
|
|
$imager = $self->_imager_obj(\$body, $image_type); |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
0
|
|
|
|
|
|
my $filtered = 0; |
48
|
0
|
|
|
|
|
|
for my $filter (@{$self->filters}) { |
|
0
|
|
|
|
|
|
|
49
|
0
|
0
|
|
|
|
|
if ( $filter->{match}->($self, $env) ) { |
50
|
0
|
|
|
|
|
|
$filter->{proc}->($self, $env, $res, |
51
|
|
|
|
|
|
|
\$body, $imager, $image_type); |
52
|
0
|
|
|
|
|
|
$filtered = 1; |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
0
|
0
|
|
|
|
|
return $filtered ? $res : [ $res->[0], $res->[1], [$body] ]; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub _no_filter { |
60
|
0
|
|
|
0
|
|
|
my $self = shift; |
61
|
|
|
|
|
|
|
|
62
|
0
|
|
0
|
|
|
|
return !$self->force_enable |
63
|
|
|
|
|
|
|
&& ( ! $ENV{PLACK_ENV} |
64
|
|
|
|
|
|
|
|| $ENV{PLACK_ENV} !~ m!^(?:development|test)$! ) |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub _imager_obj { |
68
|
0
|
|
|
0
|
|
|
my ($self, $body_ref, $image_type) = @_; |
69
|
|
|
|
|
|
|
|
70
|
0
|
|
|
|
|
|
require Imager; |
71
|
0
|
0
|
|
|
|
|
return Imager->new( |
72
|
|
|
|
|
|
|
data => $$body_ref, |
73
|
|
|
|
|
|
|
type => $image_type, |
74
|
|
|
|
|
|
|
) or croak Imager->errstr; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
1; |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
__END__ |