line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Plack::Middleware::EasyHooks; |
2
|
|
|
|
|
|
|
|
3
|
5
|
|
|
5
|
|
106407
|
use 5.006; |
|
5
|
|
|
|
|
19
|
|
|
5
|
|
|
|
|
196
|
|
4
|
5
|
|
|
5
|
|
29
|
use strict; |
|
5
|
|
|
|
|
7
|
|
|
5
|
|
|
|
|
156
|
|
5
|
5
|
|
|
5
|
|
24
|
use warnings; |
|
5
|
|
|
|
|
19
|
|
|
5
|
|
|
|
|
173
|
|
6
|
|
|
|
|
|
|
|
7
|
5
|
|
|
5
|
|
836
|
use parent qw(Plack::Middleware); |
|
5
|
|
|
|
|
291
|
|
|
5
|
|
|
|
|
46
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
our $VERSION = '0.02'; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub before { |
12
|
20
|
|
|
20
|
1
|
31
|
my ($self, $env) = @_; |
13
|
|
|
|
|
|
|
|
14
|
20
|
100
|
|
|
|
388
|
$self->{before}->($env) if defined $self->{before}; |
15
|
|
|
|
|
|
|
} |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
sub after { |
18
|
20
|
|
|
20
|
1
|
32
|
my ($self, $env, $res) = @_; |
19
|
|
|
|
|
|
|
|
20
|
20
|
100
|
|
|
|
230
|
$self->{after}->($env, $res) if defined $self->{after}; |
21
|
|
|
|
|
|
|
} |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
sub filter { |
24
|
36
|
|
|
36
|
1
|
58
|
my ($self, $env, $chunk) = @_; |
25
|
|
|
|
|
|
|
|
26
|
36
|
100
|
|
|
|
104
|
$chunk = $self->{filter}->($env, $chunk) if defined $self->{filter}; |
27
|
|
|
|
|
|
|
|
28
|
36
|
|
|
|
|
182
|
return $chunk; |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub tail { |
32
|
20
|
|
|
20
|
1
|
32
|
my ($self, $env) = @_; |
33
|
|
|
|
|
|
|
|
34
|
20
|
100
|
|
|
|
128
|
$self->{tail}->($env) if defined $self->{tail}; |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
sub finalize { |
38
|
20
|
|
|
20
|
1
|
29
|
my ($self, $env) = @_; |
39
|
|
|
|
|
|
|
|
40
|
20
|
100
|
|
|
|
82
|
$self->{finalize}->($env) if defined $self->{finalize}; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub call { |
44
|
25
|
|
|
25
|
1
|
161460
|
my ($self, $env) = @_; |
45
|
|
|
|
|
|
|
|
46
|
25
|
|
|
|
|
95
|
$self->before($env); |
47
|
25
|
|
|
|
|
151
|
my $res = $self->app->($env); |
48
|
|
|
|
|
|
|
|
49
|
25
|
50
|
|
|
|
609
|
if ( $env->{"psgix.cleanup"} ) { |
50
|
0
|
|
|
0
|
|
0
|
push @{ $env->{"psgix.cleanup.handlers"} }, sub { $self->finalize($_[0]) }; |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
25
|
100
|
|
|
|
86
|
if ( ref $res eq "ARRAY" ) { |
54
|
15
|
|
|
|
|
58
|
return $self->handle_full_response($env, $res); |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
10
|
50
|
|
|
|
39
|
if ( ref $res eq "CODE" ) { |
58
|
10
|
|
|
|
|
42
|
return $self->handle_delayed_response($env, $res); |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
# Unknown response type... |
62
|
0
|
|
|
|
|
0
|
return $res; |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub handle_full_response { |
66
|
20
|
|
|
20
|
0
|
40
|
my ($self, $env, $res) = @_; |
67
|
|
|
|
|
|
|
|
68
|
20
|
|
|
|
|
58
|
my $override = $self->after($env, $res); |
69
|
20
|
100
|
66
|
|
|
528
|
if (defined($override) && ref $override eq "ARRAY") { |
70
|
4
|
|
|
|
|
10
|
$res->[0] = $override->[0]; |
71
|
4
|
|
|
|
|
6
|
$res->[1] = $override->[1]; |
72
|
4
|
|
|
|
|
8
|
$res->[2] = $override->[2]; |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
20
|
|
|
|
|
43
|
my ($status, $header, $body) = @$res; |
76
|
|
|
|
|
|
|
|
77
|
20
|
100
|
|
|
|
64
|
if (ref $body eq "ARRAY") { |
78
|
10
|
|
|
|
|
16
|
my $tail; |
79
|
20
|
|
|
|
|
84
|
$body = [ |
80
|
10
|
50
|
|
|
|
22
|
(map { $self->filter($env, $_) } @$body), |
81
|
|
|
|
|
|
|
(defined($tail = $self->tail($env)) ? $tail : ()) |
82
|
|
|
|
|
|
|
]; |
83
|
|
|
|
|
|
|
|
84
|
10
|
50
|
|
|
|
94
|
$self->finalize($env) unless $env->{'psgix.cleanup'}; |
85
|
10
|
|
|
|
|
91
|
return [ $status, $header, $body ]; |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
10
|
|
|
|
|
17
|
my $done; |
89
|
|
|
|
|
|
|
my $wrapped = Plack::Util::inline_object( |
90
|
|
|
|
|
|
|
getline => sub { |
91
|
35
|
100
|
|
35
|
|
2278
|
return if $done; |
92
|
|
|
|
|
|
|
|
93
|
25
|
|
|
|
|
362
|
my $chunk = $body->getline(); |
94
|
|
|
|
|
|
|
|
95
|
25
|
100
|
|
|
|
562
|
if (defined $chunk) { |
96
|
15
|
|
|
|
|
49
|
return $self->filter($env, $chunk); |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
10
|
|
|
|
|
14
|
$done = 1; |
100
|
10
|
|
|
|
|
37
|
return $self->tail($env); |
101
|
|
|
|
|
|
|
}, |
102
|
|
|
|
|
|
|
close => sub { |
103
|
10
|
|
|
10
|
|
181
|
$body->close; |
104
|
|
|
|
|
|
|
|
105
|
10
|
50
|
|
|
|
125
|
$self->finalize($env) unless $env->{"psgix.cleanup"}; |
106
|
|
|
|
|
|
|
}, |
107
|
10
|
|
|
|
|
111
|
); |
108
|
|
|
|
|
|
|
|
109
|
10
|
|
|
|
|
154
|
return [ $status, $header, $wrapped ]; |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
sub handle_delayed_response { |
113
|
10
|
|
|
10
|
0
|
23
|
my ($self, $env, $res) = @_; |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
return sub { |
116
|
10
|
|
|
10
|
|
136
|
my $responder = shift; |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
$res->( |
119
|
|
|
|
|
|
|
sub { |
120
|
10
|
|
|
|
|
123
|
my ($res) = @_; |
121
|
|
|
|
|
|
|
|
122
|
10
|
100
|
|
|
|
39
|
if (exists( $res->[2] )) { |
123
|
5
|
|
|
|
|
20
|
return $responder->( $self->handle_full_response($env, $res) ); |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
|
126
|
5
|
|
|
|
|
31
|
my $override = $self->after($env, $res); |
127
|
5
|
100
|
66
|
|
|
179
|
if (defined($override) && ref $override eq "ARRAY") { |
128
|
1
|
|
|
|
|
1
|
$res->[0] = $override->[0]; |
129
|
1
|
|
|
|
|
2
|
$res->[1] = $override->[1]; |
130
|
1
|
50
|
|
|
|
5
|
$res->[2] = $override->[2] if exists $override->[2]; |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
|
133
|
5
|
|
|
|
|
21
|
my $writer = $responder->( $res ); |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
return Plack::Util::inline_object( |
136
|
|
|
|
|
|
|
write => sub { |
137
|
10
|
|
|
|
|
363
|
$writer->write( $self->filter($env, @_) ); |
138
|
|
|
|
|
|
|
}, |
139
|
|
|
|
|
|
|
close => sub { |
140
|
5
|
|
|
|
|
162
|
my $tail = $self->tail(); |
141
|
5
|
50
|
|
|
|
112
|
$writer->write($tail) if defined($tail); |
142
|
|
|
|
|
|
|
|
143
|
5
|
50
|
|
|
|
140
|
$self->finalize($env) unless $env->{'psgix.cleanup'}; |
144
|
5
|
|
|
|
|
110
|
$writer->close; |
145
|
|
|
|
|
|
|
}, |
146
|
5
|
|
|
|
|
197
|
); |
147
|
|
|
|
|
|
|
} |
148
|
10
|
|
|
|
|
65
|
); |
149
|
10
|
|
|
|
|
118
|
}; |
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
1; |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
__END__ |