Jifty/View/Static/Handler.pm

filter: Statementstmt 21.5% 20 / 93 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 23 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 37.5% 6 / 16 Podpod 100% 10 / 10 totaltotal 19.6% 36 / 183 CC 27 SCAR 35.1
CC
27
Cov
14%
new
90.0
mime_type
42.0
file_path
30.0
CRAP 33.3
« Jifty/Continuation.pm ↑ Summary Jifty/Plugin/ActorMetadata/Mixin/Model/ActorMetadata.pm »
1 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use File::MMagic ();
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use MIME::Types ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use HTTP::Date ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 package Jifty::View::Static::Handler;
8  
9 1 ▶use base qw/Jifty::View/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 our ($MIME,$MAGIC);
12  
13 =head1 NAME
14  
15 Jifty::View::Static::Handler - Jifty view handler for static files
16  
17 =head1 DESCRIPTION
18  
19 This is deprecated and is being port to a stack of plack middleware and plack::app::file
20  
21 This class takes care of serving out static files for a Jifty application.
22  
23 When fully operational, it will use an algorithm along the lines of the following:
24  
25 * Static files are served out of a separate root
26 * If static files go through apache:
27 * How do we merge together the N static roots?
28 * If static files go through Jifty::Handler
29 * We need a flag to allow them to go through the dispatcher, too
30 * return "True" (304) for if-modified-since
31 * if the browser accepts gzipped data,
32 see if we have a cached gzipped copy
33 if so, send it
34 see if we have a marker indicating that gzip is a lose
35 if so, send uncompressed
36  
37 gzip the content
38 send the gzipped content
39 * if the browser doesn't accept gzipped content
40 send the content uncompressed
41  
42 =head2 new
43  
44 Create a new static file handler. Likely, only the C<Jifty::Handler> needs to do this.
45  
46 =cut
47  
48 0 ▶sub new {
Subroutine new: not called CC=9 SCAR=45.0
Pod documented
49 0 my $class = shift;
50 0 my @roots = (Jifty->config->framework('Web')->{StaticRoot});
51 0 my %seen; $seen{$_} = 1 for map Jifty->config->framework('Web')->{$_}, qw/StaticRoot DefaultStaticRoot/;
52 0 for my $plugin ( Jifty->plugins ) {
53 0 for my $root ($plugin->static_root) {
54 0 ▶ next unless ( defined $root and -d $root and -r $root and not $seen{$root}++);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $root and -d $root and -r $root and not $seen{$root}++ 0 0 0
Condition0%0/9
defined $root and -d $root and -r $root and not $seen{$root}++
!ll&&!rl&&r
000
defined $root and -d $root and -r $root
!ll&&!rl&&r
000
defined $root and -d $root
!ll&&!rl&&r
000
MC/DC0%0/4
defined $root and -d $root and -r $root and not $seen{$root}++
defined $root -d $root -r $root not $seen{$root}++
Truth table0%0/5
A and B and C and not(D)
A= defined $rootB= -d $rootC= -r $rootD= not $seen{$root}++
ABCDresult
0XXX0
10XX0
110X0
11100
11111
55 0 push @roots, $root;
56 0 ▶ $plugin->log->debug( "Plugin @{[ref($plugin)]} static root added: (@{[$root ||'']})");
Condition0%0/2
$root || ''
l!l
00
MC/DC0%0/1
$root || ''
$root
Truth table0%0/2
A
A= $root
Aresult
11
00
57 }
58 }
59 0 push @roots, (Jifty->config->framework('Web')->{DefaultStaticRoot});
60  
61 0 return bless { roots => \@roots }, $class;
62 }
63  
64 =head2 roots
65  
66 Returns all the static roots the handler will search
67  
68 =cut
69  
70 0 ▶sub roots {
Subroutine roots: not called CC=2 SCAR=17.9
Pod documented
71 0 my $self = shift;
72 0 ▶ return wantarray ? @{$self->{roots}} : $self->{roots};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
73 }
74  
75 =head2 show $path
76  
77 Handle a request for C<$path>. If we can't find a static file of that name, return undef.
78  
79 =head2 handle_request $path
80  
81 An alias for L</show>
82  
83 =cut
84  
85  
86 0 ▶sub show {
Subroutine show: not called CC=1 SCAR=6.9
Pod documented
87 0 shift->handle_request(@_);
88 }
89  
90 0 ▶sub handle_request {
Subroutine handle_request: not called CC=4 SCAR=30.0
Pod documented
91 0 my $self = shift;
92 0 my $path = shift;
93  
94 0 ▶ my $local_path = $self->file_path($path) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $local_path = $self->file_path($path) or return undef 0 0 0
95  
96 0 ▶ if ( my $since = Jifty->web->request->header('If-Modified-Since') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $since = "Jifty"->web->request->header("If-Modified-Since")) 0 0 0
97 0 my @file_info = stat($local_path);
98  
99 # IE appends "; length=N" to If-Modified-Since headers and we need
100 # to get rid of it so str2time doesn't choke below
101 0 $since =~ s/;.+$//;
102  
103 0 ▶ return $self->send_not_modified
Branch: 1 branch
BranchTrueFalseTotal
unless $file_info[9] > &HTTP::Date::str2time($since) 0 0 0
104 unless $file_info[9] > HTTP::Date::str2time($since);
105 }
106  
107 0 return $self->send_file($local_path, $self->mime_type($local_path));
108  
109 }
110  
111  
112 =head2 file_path $path
113  
114 Returns the system path for C<$path>, searching inside the
115 application's static root, loaded plugins' static roots, and finally
116 Jifty's static root. Returns undef if it can't find the file in any
117 path.
118  
119 =cut
120  
121 =head2 template_exists $path
122  
123 An alias for L</file_path>.
124  
125 =cut
126  
127 0 ▶sub template_exists {
Subroutine template_exists: not called CC=2 SCAR=17.9
Pod documented
128 0 my $class = shift;
129 0 my $template = shift;
130 0 ▶ return $template if $class->file_path($template);
Branch: 1 branch
BranchTrueFalseTotal
if $class->file_path($template) 0 0 0
131 0 return undef;
132 }
133  
134 0 ▶sub file_path {
Subroutine file_path: not called CC=5 SCAR=34.0
Pod documented
135 0 my $self = shift;
136 0 my $file = shift;
137  
138 # Chomp a leading "/static" - should this be configurable?
139 0 $file =~ s/^\/*?static//;
140  
141 0 foreach my $path ( $self->roots ) {
142 0 my $abspath = Jifty::Util->absolute_path( File::Spec->catdir($path,$file ));
143 # If the user is trying to request something outside our static root,
144 # decline the request
145 0 my $abs_base_path = Jifty::Util->absolute_path( $path );
146 0 ▶ unless ($abspath =~ /^\Q$abs_base_path\E/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($abspath =~ /^\Q$abs_base_path\E/) 0 0 0
147 0 return undef;
148 }
149 0 ▶ return $abspath if ( -f $abspath && -r $abspath );
Branch: 1 branch
BranchTrueFalseTotal
if -f $abspath and -r $abspath 0 0 0
Condition0%0/3
-f $abspath and -r $abspath
!ll&&!rl&&r
000
MC/DC0%0/2
-f $abspath and -r $abspath
-f $abspath -r $abspath
Truth table0%0/3
A and B
A= -f $abspathB= -r $abspath
ABresult
0X0
100
111
150 }
151 0 return undef;
152  
153 }
154  
155 =head2 mime_type $path
156  
157 Returns the mime type of the file whose path on disk is C<$path>. Tries to use
158 L<MIME::Types> to guess first. If that fails, it falls back to C<File::MMagic>.
159  
160 =cut
161  
162 0 ▶sub mime_type {
Subroutine mime_type: not called CC=6 SCAR=37.4
Pod documented
163 0 my $self = shift;
164 0 my $local_path = shift;
165  
166 # The key is the file extension, the value is the MIME type to send.
167 0 my %type_override = (
168 'json' => 'application/json; charset=UTF-8',
169 'htc' => 'text/x-component',
170 );
171  
172 return ($type_override{$1})
173 0 ▶ if $local_path =~ /^.*\.(.+?)$/ and defined $type_override{$1};
Branch: 1 branch
BranchTrueFalseTotal
if $local_path =~ /^.*\.(.+?)$/ and defined $type_override{$1} 0 0 0
Condition0%0/3
$local_path =~ /^.*\.(.+?)$/ and defined $type_override{$1}
!ll&&!rl&&r
000
MC/DC0%0/2
$local_path =~ /^.*\.(.+?)$/ and defined $type_override{$1}
$local_path =~ /^.*\.(.+?)$/ defined $type_override{$1}
Truth table0%0/3
A and B
A= $local_path =~ /^.*\.(.+?)$/B= defined $type_override{$1}
ABresult
0X0
100
111
174  
175 # Defer initialization to first use. (It's not actually cheap)
176 0 ▶ $MIME ||= MIME::Types->new();
Condition0%0/3
$Jifty::View::Static::Handler::MIME ||= "MIME::Types"->new
l!l&&r!l&&!r
000
MC/DC0%0/2
$Jifty::View::Static::Handler::MIME ||= "MIME::Types"->new
$Jifty::View::Static::Handler::MIME "MIME::Types"->new
Truth table0%0/3
A ||= B
A= $Jifty::View::Static::Handler::MIMEB= "MIME::Types"->new
ABresult
1X1
011
000
177 0 ▶ $MAGIC ||= File::MMagic->new();
Condition0%0/3
$Jifty::View::Static::Handler::MAGIC ||= "File::MMagic"->new
l!l&&r!l&&!r
000
MC/DC0%0/2
$Jifty::View::Static::Handler::MAGIC ||= "File::MMagic"->new
$Jifty::View::Static::Handler::MAGIC "File::MMagic"->new
Truth table0%0/3
A ||= B
A= $Jifty::View::Static::Handler::MAGICB= "File::MMagic"->new
ABresult
1X1
011
000
178 0 my $mimeobj = $MIME->mimeTypeOf($local_path);
179 0 ▶ my $mime_type = ( $mimeobj ? $mimeobj->type : $MAGIC->checktype_filename($local_path));
Branch: 1 branch
BranchTrueFalseTotal
$mimeobj ? : 0 0 0
180  
181 0 return ($mime_type);
182 }
183  
184  
185 =head2 send_file $path $mimetype
186  
187 Print C<$path> to STDOUT (the client), identified with a mimetype of C<$mimetype>.
188  
189 =cut
190  
191  
192 0 ▶sub send_file {
Subroutine send_file: not called CC=3 SCAR=24.8
Pod documented
193 0 my $self = shift;
194 0 my $local_path = shift;
195 0 my $mime_type = shift;
196  
197 0 my $fh = IO::File->new( $local_path, 'r' );
198 0 ▶ if ( defined $fh ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fh) { } 0 0 0
199 0 binmode $fh;
200  
201 # This is designed to work under CGI or FastCGI; will need an
202 # abstraction for mod_perl
203  
204 # Clear out the mason output, if any
205 0 ▶ Jifty->web->mason->clear_buffer if Jifty->web->mason;
Branch: 1 branch
BranchTrueFalseTotal
if "Jifty"->web->mason 0 0 0
206  
207 0 my @file_info = stat($local_path);
208 0 Jifty->web->response->content_type($mime_type);
209 0 $self->send_http_header( '', $file_info[7], $file_info[9] );
210  
211 0 Jifty->web->response->content($fh);
212  
213 0 return 1;
214 }
215 else {
216 0 return undef;
217 }
218 }
219  
220 =head2 send_http_header [COMPRESSION, LENGTH, LAST_MODIFIED]
221  
222 Sends appropriate cache control and expiration headers such that the
223 client will cache the content. COMPRESSION is deprecated
224  
225 =cut
226  
227 0 ▶sub send_http_header {
Subroutine send_http_header: not called CC=3 SCAR=24.8
Pod documented
228 0 my $self = shift;
229 0 my (undef, $length, $modified) = @_;
230 0 my $now = time();
231 0 my $response = Jifty->web->response;
232  
233 # Expire in a year
234 0 $response->header( 'Cache-Control' => 'max-age=31536000, public' );
235 0 $response->header( 'Expires' => HTTP::Date::time2str( $now + 31536000 ) );
236 0 ▶ $response->header( 'Content-Length' => $length ) if $length;
Branch: 1 branch
BranchTrueFalseTotal
if $length 0 0 0
237  
238 0 ▶ $response->header(
Branch: 1 branch
BranchTrueFalseTotal
if $modified 0 0 0
239 'Last-Modified' => HTTP::Date::time2str( $modified ) ) if $modified;
240 }
241  
242  
243 =head2 send_not_modified
244  
245 Sends a "304 Not modified" response to the browser, telling it to use a cached copy.
246  
247 =cut
248  
249 0 ▶sub send_not_modified {
Subroutine send_not_modified: not called CC=1 SCAR=6.9
Pod documented
250 0 my $self = shift;
251 0 my $response = Jifty->web->response;
252 0 $response->status( 304 );
253 0 return 1;
254 }
255  
256 1 1;
« Jifty/Continuation.pm ↑ Summary Jifty/Plugin/ActorMetadata/Mixin/Model/ActorMetadata.pm »