blib/lib/Dancer/Renderer.pm

filter: Statementstmt 96.7% 177 / 183 Branchbran 87.5% 35 / 40 Conditioncond 61.2% 38 / 62 MC/DCmcdc 29.7% 11 / 37 Subroutinesub 97.3% 37 / 38 Podpod 0.0% 0 / 11 totaltotal 80.3% 298 / 371 SCAR 38.6
CC
44
Cov
88%
get_action_response
14.6
get_file_response_for_path
10.3
render_autopage
8.0
CRAP 47.6
« /home/cpancover/.cpan/build/Dancer-1.3520-0/bin/dancer ↑ Summary blib/lib/Dancer/App.pm »
1 package Dancer::Renderer;
2 180 our $AUTHORITY = 'cpan:SUKRIA';
3 # ABSTRACT: Rendering class for Dancer
4 180 $Dancer::Renderer::VERSION = '1.3520';
5 180 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 180 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 180 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 180 ▶use HTTP::Headers;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 180 ▶use HTTP::Date qw( str2time time2str );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 180 ▶use Dancer::Route;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 180 ▶use Dancer::HTTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 180 ▶use Dancer::Cookie;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 180 ▶use Dancer::Factory::Hook;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 180 ▶use Dancer::Cookies;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 180 ▶use Dancer::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 180 ▶use Dancer::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 180 ▶use Dancer::Serializer;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 180 ▶use Dancer::Config 'setting';
Subroutine BEGIN: called CC=1 SCAR=0.0
19 180 ▶use Dancer::FileUtils qw(path path_or_empty dirname read_file_content open_file);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 180 ▶use Dancer::SharedData;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 180 ▶use Dancer::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 180 ▶use Dancer::MIME;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 180 ▶use Dancer::Exception qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 180 Dancer::Factory::Hook->instance->install_hooks(
26 qw/before after before_serializer after_serializer before_file_render after_file_render/
27 );
28  
29 519 ▶sub render_file { get_file_response() }
Subroutine render_file: called CC=1 SCAR=0.0
Pod undocumented
30  
31 sub render_action {
32 514 ▶ my $class = shift;
Subroutine render_action: called CC=2 SCAR=6.9
Pod undocumented
33 514 my $resp = $class->get_action_response();
34 469 ▶ return (defined $resp)
Branch: 1 branch
BranchTrueFalseTotal
defined $resp ? : 450 19 469
35 ? response_with_headers()
36 : undef;
37 }
38  
39 sub render_error {
40 32 ▶ my ($class, $error_code) = @_;
Subroutine render_error: called CC=2 SCAR=6.9
Pod undocumented
41  
42 32 my $app = Dancer::App->current;
43 32 my $static_file = path($app->setting('public'), "$error_code.html");
44 32 my $response = Dancer::Renderer->get_file_response_for_path(
45 $static_file => $error_code);
46 32 ▶ return $response if $response;
Branch: 1 branch
BranchTrueFalseTotal
if $response 2 30 32
47  
48 30 return Dancer::Response->new(
49 status => $error_code,
50 headers => ['Content-Type' => 'text/html'],
51 content => Dancer::Renderer->html_page(
52 "Error $error_code" => "<h2>Unable to process your query</h2>"
53 . "The page you requested is not available"
54 )
55 );
56 }
57  
58 # Takes a response object and add default headers
59 sub response_with_headers {
60 450 ▶ my $response = Dancer::SharedData->response();
Subroutine response_with_headers: called CC=4 SCAR=14.8
Pod undocumented
61  
62 450 ▶ if (Dancer::Config::setting('server_tokens')) {
Branch: 1 branch
BranchTrueFalseTotal
if (Dancer::Config::setting("server_tokens")) 450 0 450
63 450 ▶ $response->{headers} ||= HTTP::Headers->new;
Condition33%1/3
$response->{"headers"} ||= "HTTP::Headers"->new
l!l&&r!l&&!r
45000
MC/DC0%0/2
$response->{"headers"} ||= "HTTP::Headers"->new
$response->{"headers"} "HTTP::Headers"->new
Truth table33%1/3
A ||= B
A= $response->{"headers"}B= "HTTP::Headers"->new
ABresult
1X1
011
000
64 450 ▶ my $powered_by = "Perl Dancer " . ( Dancer->VERSION || '' );
Condition50%1/2
'Dancer'->VERSION || ''
l!l
4500
MC/DC0%0/1
'Dancer'->VERSION || ''
'Dancer'->VERSION
Truth table50%1/2
A
A= 'Dancer'->VERSION
Aresult
11
00
65 450 $response->header('X-Powered-By' => $powered_by);
66 450 $response->header('Server' => $powered_by);
67 }
68  
69 450 return $response;
70 }
71  
72 sub html_page {
73 39 ▶ my ($class, $title, $content, $style) = @_;
Subroutine html_page: called CC=2 SCAR=6.9
Pod undocumented
74 39 ▶ $style ||= 'style';
Condition100%2/2
$style ||= "style"
l!l
930
MC/DC100%1/1
$style ||= "style"
$style
Truth table100%2/2
A
A= $style
Aresult
11
00
75  
76 39 my $template = $class->templates->{'default'};
77 39 my $ts = Dancer::Template::Simple->new;
78  
79 39 return $ts->render(
80 \$template,
81 { title => $title,
82 style => $style,
83 version => $Dancer::VERSION,
84 content => $content
85 }
86 );
87 }
88  
89 sub get_action_response {
90 538 ▶ my $class = shift;
Subroutine get_action_response: called CC=14 SCAR=26.8
Pod undocumented
91 538 ▶ my $depth = shift || 1;
Condition100%2/2
shift() || 1
l!l
24514
MC/DC100%1/1
shift() || 1
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
92  
93 # save the request before the filters are ran
94 538 my $request = Dancer::SharedData->request;
95 538 my ($method, $path) = ($request->method, $request->path_info);
96  
97 # look for a matching route handler, for the given request
98 538 my $handler =
99 Dancer::App->find_route_through_apps(Dancer::SharedData->request);
100  
101 538 ▶ my $app = ($handler && $handler->app) ? $handler->app : Dancer::App->current();
Branch: 1 branch
BranchTrueFalseTotal
$handler && $handler->app ? : 515 23 538
Condition66%2/3
$handler and $handler->app
!ll&&!rl&&r
230515
MC/DC50%1/2
$handler and $handler->app
$handler $handler->app
Truth table66%2/3
A and B
A= $handlerB= $handler->app
ABresult
0X0
100
111
102  
103 # run the before filters, before "running" the route handler
104 538 Dancer::Factory::Hook->instance->execute_hooks('before', $handler);
105  
106 # recurse if something has changed
107 532 my $MAX_RECURSIVE_LOOP = 10;
108 532 ▶ if ( ($path ne Dancer::SharedData->request->path_info)
Branch: 1 branch
BranchTrueFalseTotal
if ($path ne "Dancer::SharedData"->request->path_info or $method ne "Dancer::SharedData"->request->method) 26 506 532
Condition66%2/3
$path ne "Dancer::SharedData"->request->path_info or $method ne "Dancer::SharedData"->request->method
l!l&&r!l&&!r
260506
MC/DC50%1/2
$path ne "Dancer::SharedData"->request->path_info or $method ne "Dancer::SharedData"->request->method
$path ne "Dancer::SharedData"->request->path_info $method ne "Dancer::SharedData"->request->method
Truth table66%2/3
A or B
A= $path ne "Dancer::SharedData"->request->path_infoB= $method ne "Dancer::SharedData"->request->method
ABresult
1X1
011
000
109 || ($method ne Dancer::SharedData->request->method))
110 {
111 26 ▶ if ($depth > $MAX_RECURSIVE_LOOP) {
Branch: 1 branch
BranchTrueFalseTotal
if ($depth > $MAX_RECURSIVE_LOOP) 2 24 26
112 2 raise core_renderer => "infinite loop detected, "
113 . "check your route/filters for "
114 . $method . ' '
115 . $path;
116 }
117 24 return $class->get_action_response($depth + 1);
118 }
119  
120 # redirect immediately - skip route execution
121 506 my $response = Dancer::SharedData->response();
122 506 ▶ if (defined $response && (my $status = $response->status)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $response and my $status = $response->status) 506 0 506
Condition33%1/3
defined $response and my $status = $response->status
!ll&&!rl&&r
00506
MC/DC0%0/2
defined $response and my $status = $response->status
defined $response my $status = $response->status
Truth table33%1/3
A and B
A= defined $responseB= my $status = $response->status
ABresult
0X0
100
111
123 506 ▶ if ($status == 302 || $status == 301) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status == 302 or $status == 301) 6 500 506
Condition66%2/3
$status == 302 or $status == 301
l!l&&r!l&&!r
60500
MC/DC50%1/2
$status == 302 or $status == 301
$status == 302 $status == 301
Truth table66%2/3
A or B
A= $status == 302B= $status == 301
ABresult
1X1
011
000
124 6 $class->serialize_response_if_needed();
125 6 Dancer::Factory::Hook->instance->execute_hooks('after', $response);
126 6 return $response;
127 }
128 }
129  
130 # execute the action
131 500 ▶ if ($handler) {
Branch: 1 branch
BranchTrueFalseTotal
if ($handler) { } 481 19 500
132 # a response may exist, produced by a before filter
133 481 ▶ return $class->serialize_response_if_needed() if defined $response && $response->exists;
Branch: 1 branch
BranchTrueFalseTotal
if defined $response and $response->exists 0 481 481
Condition33%1/3
defined $response and $response->exists
!ll&&!rl&&r
04810
MC/DC0%0/2
defined $response and $response->exists
defined $response $response->exists
Truth table33%1/3
A and B
A= defined $responseB= $response->exists
ABresult
0X0
100
111
134 # else, get the route handler's response
135 481 Dancer::App->current($handler->{app});
136 try {
137 481 ▶ $handler->run($request);
Subroutine __ANON__: called CC=1 SCAR=0.0
138 444 $class->serialize_response_if_needed();
139 } continuation {
140 9 ▶ my ($continuation) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
141 # If we have a Route continuation, run the after hook, then
142 # propagate the continuation
143 9 my $resp = Dancer::SharedData->response();
144 9 Dancer::Factory::Hook->instance->execute_hooks('after', $resp);
145 9 $continuation->rethrow();
146 481 };
147 444 my $resp = Dancer::SharedData->response();
148 444 Dancer::Factory::Hook->instance->execute_hooks('after', $resp);
149 444 return $resp;
150 }
151 else {
152 19 return undef; # 404
153 }
154 }
155  
156 sub render_autopage {
157 19 ▶ return unless Dancer::setting('auto_page');
Subroutine render_autopage: called CC=8 SCAR=20.8
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
unless Dancer::setting("auto_page") 11 8 19
158  
159 8 my $request = Dancer::SharedData->request;
160 8 my $path = $request->path_info;
161  
162 # See if we find a matching view for this request, if so, render it
163 8 my $viewpath = $path;
164 8 $viewpath =~ s{^/}{};
165 8 ▶ my $view = Dancer::engine('template')->view($viewpath) || '';
Condition100%2/2
Dancer::engine('template')->view($viewpath) || ''
l!l
53
MC/DC100%1/1
Dancer::engine('template')->view($viewpath) || ''
Dancer::engine('template')->view($viewpath)
Truth table100%2/2
A
A= Dancer::engine('template')->view($viewpath)
Aresult
11
00
166  
167 8 ▶ if ($view && -f $view) {
Branch: 1 branch
BranchTrueFalseTotal
if ($view and -f $view) 5 3 8
Condition66%2/3
$view and -f $view
!ll&&!rl&&r
305
MC/DC50%1/2
$view and -f $view
$view -f $view
Truth table66%2/3
A and B
A= $viewB= -f $view
ABresult
0X0
100
111
168 # A view exists for the path requested, go ahead and render it:
169 5 return _autopage_response($viewpath);
170 }
171  
172 # Try appending "index" and looking again
173 3 ▶ $view = Dancer::engine('template')->view(
Condition100%2/2
Dancer::engine('template')->view(Dancer::FileUtils::path($viewpath, 'index')) || ''
l!l
21
MC/DC100%1/1
Dancer::engine('template')->view(Dancer::FileUtils::path($viewpath, 'index')) || ''
Dancer::engine('template')->view(Dancer::FileUtils::path($viewpath, 'index'))
Truth table100%2/2
A
A= Dancer::engine('template')->view(Dancer::FileUtils::path($viewpath, 'index'))
Aresult
11
00
174 Dancer::FileUtils::path($viewpath, 'index')
175 )|| '';
176 3 Dancer::error("Looking for $viewpath/index - got $view");
177 3 ▶ if ($view && -f $view) {
Branch: 1 branch
BranchTrueFalseTotal
if ($view and -f $view) 2 1 3
Condition66%2/3
$view and -f $view
!ll&&!rl&&r
102
MC/DC50%1/2
$view and -f $view
$view -f $view
Truth table66%2/3
A and B
A= $viewB= -f $view
ABresult
0X0
100
111
178 2 return _autopage_response(
179 Dancer::FileUtils::path($viewpath, 'index')
180 );
181 }
182  
183 1 return;
184 }
185 sub _autopage_response {
186 7 ▶ my $viewpath = shift;
Subroutine _autopage_response: called CC=1 SCAR=0.0
187 7 my $response = Dancer::Response->new;
188 7 $response->status(200);
189 7 $response->content(
190 Dancer::template($viewpath)
191 );
192 7 $response->header( 'Content-Type' => 'text/html' );
193 7 return $response;
194 }
195  
196 sub serialize_response_if_needed {
197 450 ▶ my $response = Dancer::SharedData->response();
Subroutine serialize_response_if_needed: called CC=3 SCAR=11.5
Pod undocumented
198  
199 450 ▶ if (Dancer::App->current->setting('serializer') && $response->content()){
Branch: 1 branch
BranchTrueFalseTotal
if ("Dancer::App"->current->setting("serializer") and $response->content) 30 420 450
Condition66%2/3
"Dancer::App"->current->setting("serializer") and $response->content
!ll&&!rl&&r
420030
MC/DC50%1/2
"Dancer::App"->current->setting("serializer") and $response->content
"Dancer::App"->current->setting("serializer") $response->content
Truth table66%2/3
A and B
A= "Dancer::App"->current->setting("serializer")B= $response->content
ABresult
0X0
100
111
200 30 Dancer::Factory::Hook->execute_hooks('before_serializer', $response);
201 try {
202 30 ▶ Dancer::Serializer->process_response($response);
Subroutine __ANON__: called CC=1 SCAR=0.0
203 } continuation {
204 0 ▶ my ($continuation) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
205 # If we have a Route continuation, run the after hook, then
206 # propagate the continuation
207 0 Dancer::Factory::Hook->execute_hooks('after_serializer', $response);
208 0 $continuation->rethrow();
209 30 };
210 30 Dancer::Factory::Hook->execute_hooks('after_serializer', $response);
211 }
212 450 return $response;
213 }
214  
215 sub get_file_response {
216 532 ▶ my $request = Dancer::SharedData->request;
Subroutine get_file_response: called CC=6 SCAR=18.1
Pod undocumented
217 532 my $path_info = $request->path_info;
218  
219 # requests that have \0 in path are forbidden
220 532 ▶ if ( $path_info =~ /\0/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path_info =~ /\0/) 1 531 532
221 1 _bad_request();
222 1 return 1;
223 }
224  
225 531 my $app = Dancer::App->current;
226 # TODO: this should be later removed with a check whether the file exists
227 # and then returning a 404
228 531 ▶ my $public = defined $app->setting('public') ?
Branch: 1 branch
BranchTrueFalseTotal
defined $app->setting('public') ? : 531 0 531
229 $app->setting('public') :
230 '';
231  
232 531 my $static_file = path( $public, $path_info );
233  
234 531 ▶ return if ( !$static_file
Branch: 1 branch
BranchTrueFalseTotal
if not $static_file or index($static_file, path($public) || "") != 0 2 529 531
Condition60%3/5
not $static_file or index($static_file, path($public) || "") != 0
l!l&&r!l&&!r
02529
path($public) || ''
l!l
5310
MC/DC50%1/2
not $static_file or index($static_file, path($public) || "") != 0
not $static_file index($static_file, path($public) || "") != 0
MC/DC0%0/1
path($public) || ''
path($public)
Truth table66%2/3
not(A) or B
A= not $static_fileB= index($static_file, path($public) || "") != 0
ABresult
1X1
011
000
Truth table50%1/2
A
A= path($public)
Aresult
11
00
235 || index( $static_file, ( path($public) || '' ) ) != 0 );
236  
237 529 return Dancer::Renderer->get_file_response_for_path( $static_file, undef,
238 $request->content_type );
239 }
240  
241 sub get_file_response_for_path {
242 563 ▶ my ($class, $static_file, $status, $mime) = @_;
Subroutine get_file_response_for_path: called CC=9 SCAR=23.3
Pod undocumented
243  
244 563 ▶ if ( -f $static_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $static_file) 16 547 563
245 16 Dancer::Factory::Hook->execute_hooks( 'before_file_render',
246 $static_file );
247  
248 16 ▶ my $response = Dancer::SharedData->response() || Dancer::Response->new();
Condition33%1/3
'Dancer::SharedData'->response || 'Dancer::Response'->new
l!l&&r!l&&!r
1600
MC/DC0%0/2
'Dancer::SharedData'->response || 'Dancer::Response'->new
'Dancer::SharedData'->response 'Dancer::Response'->new
Truth table33%1/3
A || B
A= 'Dancer::SharedData'->responseB= 'Dancer::Response'->new
ABresult
1X1
011
000
249  
250 # handle If-Modified-Since
251 16 my $last_modified = (stat $static_file)[9];
252 16 my $since = str2time(Dancer::SharedData->request->env->{HTTP_IF_MODIFIED_SINCE});
253 16 ▶ if( defined $since && $since >= $last_modified ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $since and $since >= $last_modified) 0 16 16
Condition33%1/3
defined $since and $since >= $last_modified
!ll&&!rl&&r
1600
MC/DC0%0/2
defined $since and $since >= $last_modified
defined $since $since >= $last_modified
Truth table33%1/3
A and B
A= defined $sinceB= $since >= $last_modified
ABresult
0X0
100
111
254 0 $response->status( 304 );
255 0 $response->content( '' );
256 0 return $response;
257 }
258  
259 16 my $fh = open_file( '<', $static_file );
260 16 binmode $fh;
261 16 ▶ $response->status($status) if ($status);
Branch: 1 branch
BranchTrueFalseTotal
if $status 2 14 16
262 16 $response->header( 'Last-Modified' => time2str( $last_modified ) );
263 16 ▶ $response->header('Content-Type' => (($mime && _get_full_mime_type($mime)) ||
Condition77%7/9
$mime && _get_full_mime_type($mime) || 'Dancer::SharedData'->request->content_type || _get_mime_type($static_file)
l!l&&r!l&&!r
2140
$mime and _get_full_mime_type($mime) or "Dancer::SharedData"->request->content_type
l!l&&r!l&&!r
1114
$mime and _get_full_mime_type($mime)
!ll&&!rl&&r
1501
MC/DC0%0/4
$mime && _get_full_mime_type($mime) || 'Dancer::SharedData'->request->content_type || _get_mime_type($static_file)
$mime _get_full_mime_type($mime) "Dancer::SharedData"->request->content_type _get_mime_type($static_file)
Truth table42%3/7
A and B or C || D
A= $mimeB= _get_full_mime_type($mime)C= "Dancer::SharedData"->request->content_typeD= _get_mime_type($static_file)
ABCDresult
11XX1
0X1X1
101X1
0X011
10011
0X000
10000
264 Dancer::SharedData->request->content_type ||
265 _get_mime_type($static_file)));
266 16 $response->content($fh);
267  
268 16 Dancer::Factory::Hook->execute_hooks( 'after_file_render', $response );
269  
270 16 return $response;
271 }
272 547 return;
273 }
274  
275 # private
276 sub _get_full_mime_type {
277 1 ▶ my $mime = Dancer::MIME->instance();
Subroutine _get_full_mime_type: called CC=1 SCAR=0.0
278 1 return $mime->name_or_type(shift @_);
279 }
280  
281 sub _get_mime_type {
282 14 ▶ my $file = shift;
Subroutine _get_mime_type: called CC=1 SCAR=0.0
283 14 my $mime = Dancer::MIME->instance();
284 14 return $mime->for_file($file);
285 }
286  
287 sub _bad_request{
288 1 ▶ my $response = Dancer::SharedData->response() || Dancer::Response->new();
Subroutine _bad_request: called CC=2 SCAR=7.6
Condition33%1/3
'Dancer::SharedData'->response || 'Dancer::Response'->new
l!l&&r!l&&!r
100
MC/DC0%0/2
'Dancer::SharedData'->response || 'Dancer::Response'->new
'Dancer::SharedData'->response 'Dancer::Response'->new
Truth table33%1/3
A || B
A= 'Dancer::SharedData'->responseB= 'Dancer::Response'->new
ABresult
1X1
011
000
289 1 $response->status(400);
290 1 $response->content('Bad Request');
291 }
292  
293 # set of builtin templates needed by Dancer when rendering HTML pages
294 sub templates {
295 39 ▶ my $charset = setting('charset') || 'UTF-8';
Subroutine templates: called CC=2 SCAR=7.2
Pod undocumented
Condition50%1/2
setting('charset') || 'UTF-8'
l!l
039
MC/DC0%0/1
setting('charset') || 'UTF-8'
setting('charset')
Truth table50%1/2
A
A= setting('charset')
Aresult
11
00
296 39 { default =>
297 '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
298 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
299 <html>
300 <head>
301 <title><% title %></title>
302 <link rel="stylesheet" href="/css/<% style %>.css" />
303 <meta http-equiv="Content-type" content="text/html; charset=' . $charset
304 . '" />
305 </head>
306 <body>
307 <h1><% title %></h1>
308 <div id="content">
309 <% content %>
310 </div>
311 <div id="footer">
312 Powered by <a href="http://perldancer.org/">Dancer</a> <% version %>
313 </div>
314 </body>
315 </html>',
316 };
317 }
318  
319  
320 180 1;
321  
322 __END__
« /home/cpancover/.cpan/build/Dancer-1.3520-0/bin/dancer ↑ Summary blib/lib/Dancer/App.pm »