line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Catalyst::Plugin::Static; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
918
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
48
|
|
4
|
1
|
|
|
1
|
|
7
|
use base 'Class::Data::Inheritable'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
9338
|
|
5
|
1
|
|
|
1
|
|
5483
|
use File::MimeInfo::Magic; |
|
1
|
|
|
|
|
14809
|
|
|
1
|
|
|
|
|
67
|
|
6
|
1
|
|
|
1
|
|
1079
|
use File::stat; |
|
1
|
|
|
|
|
19170
|
|
|
1
|
|
|
|
|
9
|
|
7
|
1
|
|
|
1
|
|
4178
|
use File::Slurp; |
|
1
|
|
|
|
|
40453
|
|
|
1
|
|
|
|
|
320
|
|
8
|
1
|
|
|
1
|
|
1365
|
use File::Spec::Functions qw/catdir no_upwards splitdir/; |
|
1
|
|
|
|
|
1519
|
|
|
1
|
|
|
|
|
91
|
|
9
|
1
|
|
|
1
|
|
2999
|
use MRO::Compat;; |
|
1
|
|
|
|
|
13148
|
|
|
1
|
|
|
|
|
688
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our $VERSION = '0.11'; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=head1 NAME |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
Catalyst::Plugin::Static - DEPRECATED - Serve static files with Catalyst |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
=head1 SYNOPSIS |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
use Catalyst 'Static'; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
# let File::MMagic determine the content type |
23
|
|
|
|
|
|
|
$c->serve_static; |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
# or specify explicitly if you know better |
26
|
|
|
|
|
|
|
$c->serve_static('text/css'); |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
=head1 DESCRIPTION |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
Serve static files from config->{root}. You probably want to use |
31
|
|
|
|
|
|
|
use L<Catalyst::Plugin::Static::Simple> rather than this module. |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
=head2 METHODS |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
=over 4 |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
=item finalize |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
This plugin overrides finalize to make sure content is removed on |
40
|
|
|
|
|
|
|
redirect. |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
=cut |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub finalize { |
45
|
0
|
|
|
0
|
1
|
|
my $c = shift; |
46
|
0
|
0
|
|
|
|
|
if ( $c->res->status =~ /^(1\d\d|[23]04)$/ ) { |
47
|
0
|
|
|
|
|
|
$c->res->headers->remove_content_headers; |
48
|
0
|
|
|
|
|
|
$c->finalize_headers; |
49
|
|
|
|
|
|
|
} |
50
|
0
|
|
|
|
|
|
return $c->next::method(@_); |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
=item serve_static |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
Call this method from your action to serve requested path |
57
|
|
|
|
|
|
|
as a static file from your root. takes an optional content_type |
58
|
|
|
|
|
|
|
parameter |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=cut |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub serve_static { |
63
|
0
|
|
|
0
|
1
|
|
my $c = shift; |
64
|
0
|
|
|
|
|
|
my $r = eval { |
65
|
0
|
|
|
|
|
|
my $path = $c->config->{root} . '/' . $c->req->path; |
66
|
0
|
|
|
|
|
|
$c->serve_static_file( $path, @_ ); |
67
|
|
|
|
|
|
|
}; |
68
|
0
|
0
|
|
|
|
|
warn("serve_static puked $@") if $@; |
69
|
0
|
|
|
|
|
|
$r; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=item serve_static_file <file> |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
Serve a specified static file. |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=cut |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub serve_static_file { |
79
|
0
|
|
|
0
|
1
|
|
my $c = shift; |
80
|
0
|
|
|
|
|
|
my $path = catdir(no_upwards(splitdir( shift ))); |
81
|
|
|
|
|
|
|
|
82
|
0
|
0
|
|
|
|
|
if ( -f $path ) { |
83
|
|
|
|
|
|
|
|
84
|
0
|
|
|
|
|
|
my $stat = stat($path); |
85
|
|
|
|
|
|
|
|
86
|
0
|
0
|
|
|
|
|
if ( $c->req->headers->header('If-Modified-Since') ) { |
87
|
|
|
|
|
|
|
|
88
|
0
|
0
|
|
|
|
|
if ( $c->req->headers->if_modified_since == $stat->mtime ) { |
89
|
0
|
|
|
|
|
|
$c->res->status(304); # Not Modified |
90
|
0
|
|
|
|
|
|
$c->res->headers->remove_content_headers; |
91
|
0
|
|
|
|
|
|
return 1; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
0
|
|
0
|
|
|
|
my $type = shift || mimetype($path); |
96
|
0
|
|
|
|
|
|
my $content = read_file($path); |
97
|
0
|
|
|
|
|
|
$c->res->headers->content_type($type); |
98
|
0
|
|
|
|
|
|
$c->res->headers->content_length( $stat->size ); |
99
|
0
|
|
|
|
|
|
$c->res->headers->last_modified( $stat->mtime ); |
100
|
0
|
|
|
|
|
|
$c->res->output($content); |
101
|
0
|
0
|
0
|
|
|
|
if ( $c->config->{static}->{no_logs} && $c->log->can('abort') ) { |
102
|
0
|
|
|
|
|
|
$c->log->abort( 1 ); |
103
|
|
|
|
|
|
|
} |
104
|
0
|
0
|
|
|
|
|
$c->log->debug(qq/Serving file "$path" as "$type"/) if $c->debug; |
105
|
0
|
|
|
|
|
|
return 1; |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
0
|
0
|
|
|
|
|
$c->log->debug(qq/Failed to serve file "$path"/) if $c->debug; |
109
|
0
|
|
|
|
|
|
$c->res->status(404); |
110
|
|
|
|
|
|
|
|
111
|
0
|
|
|
|
|
|
return 0; |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=back |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=head1 SEE ALSO |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
L<Catalyst>. |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=head1 CAVEATS |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
This module is not as optimized for static files as a normal web |
123
|
|
|
|
|
|
|
server, and is most useful for stand alone operation and development. |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
=head1 AUTHOR |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
Sebastian Riedel, C<sri@cpan.org> |
128
|
|
|
|
|
|
|
Christian Hansen <ch@ngmedia.com> |
129
|
|
|
|
|
|
|
Marcus Ramberg <mramberg@cpan.org> |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=head1 THANK YOU |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
Torsten Seemann and all the others who've helped. |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
=head1 COPYRIGHT |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
This program is free software, you can redistribute it and/or modify it under |
138
|
|
|
|
|
|
|
the same terms as Perl itself. |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=cut |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
1; |