line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Plack::Middleware::FileWrap; |
2
|
|
|
|
|
|
|
{ |
3
|
|
|
|
|
|
|
$Plack::Middleware::FileWrap::VERSION = '0.01'; |
4
|
|
|
|
|
|
|
} |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
# ABSTRACT: Wrap file with headers/footers in Plack |
7
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
91069
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
38
|
|
9
|
1
|
|
|
1
|
|
7
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
29
|
|
10
|
1
|
|
|
1
|
|
6
|
use parent qw( Plack::Middleware ); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
6
|
|
11
|
|
|
|
|
|
|
|
12
|
1
|
|
|
1
|
|
55
|
use Plack::Util; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
22
|
|
13
|
1
|
|
|
1
|
|
6
|
use Plack::Util::Accessor qw( headers footers ); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
7
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub call { |
16
|
2
|
|
|
2
|
1
|
222604
|
my ( $self, $env ) = @_; |
17
|
|
|
|
|
|
|
|
18
|
2
|
|
|
|
|
12
|
my $headers = _wrap_files( $self->headers ); |
19
|
2
|
|
|
|
|
15
|
my $footers = _wrap_files( $self->footers ); |
20
|
2
|
50
|
33
|
|
|
11
|
return $self->app->($env) unless defined($headers) or defined($footers); |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
$self->response_cb( |
23
|
|
|
|
|
|
|
$self->app->($env), |
24
|
|
|
|
|
|
|
sub { |
25
|
2
|
|
|
2
|
|
95
|
my $res = shift; |
26
|
|
|
|
|
|
|
|
27
|
2
|
50
|
|
|
|
10
|
if ( defined $res->[2] ) { # do we need it? |
28
|
2
|
|
|
|
|
3
|
my $body; |
29
|
2
|
|
|
|
|
12
|
Plack::Util::foreach( $res->[2], sub { $body .= $_[0] } ); |
|
2
|
|
|
|
|
20
|
|
30
|
2
|
|
|
|
|
14
|
my $new_body = $headers . $body . $footers; |
31
|
2
|
|
|
|
|
5
|
$res->[2] = [$new_body]; |
32
|
2
|
|
|
|
|
10
|
my $h = Plack::Util::headers( $res->[1] ); |
33
|
2
|
|
|
|
|
81
|
$h->set( 'Content-Length', length $new_body ); |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
} |
36
|
2
|
|
|
|
|
19
|
); |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub _wrap_files { |
40
|
4
|
|
|
4
|
|
119
|
my ($files) = @_; |
41
|
|
|
|
|
|
|
|
42
|
4
|
50
|
|
|
|
17
|
return unless defined $files; |
43
|
4
|
50
|
|
|
|
13
|
return $$files if ref $files eq 'SCALAR'; # \'' |
44
|
4
|
50
|
|
|
|
15
|
unless ( ref $files ) { # '/path/to/file' |
45
|
0
|
|
|
|
|
0
|
$files = [$files]; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
4
|
|
|
|
|
6
|
my $body; |
49
|
4
|
|
|
|
|
8
|
foreach my $file (@$files) { |
50
|
4
|
100
|
|
|
|
13
|
if ( ref $file eq 'SCALAR' ) { |
51
|
2
|
|
|
|
|
5
|
$body .= $$file; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
else { |
54
|
2
|
50
|
|
|
|
187
|
if ( open( my $fh, '<', $file ) ) { |
55
|
2
|
|
|
|
|
9
|
local $/; |
56
|
2
|
|
|
|
|
123298
|
$body .= <$fh>; |
57
|
2
|
|
|
|
|
78
|
close($fh); |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
else { |
60
|
0
|
|
|
|
|
0
|
warn "[FileWrap] Can't open $file: $!\n"; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
4
|
|
|
|
|
18
|
return $body; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
1; |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
__END__ |