blib/lib/Dancer/Handler.pm

filter: Statementstmt 87.9% 109 / 124 Branchbran 61.7% 21 / 34 Conditioncond 75.0% 15 / 20 MC/DCmcdc 45.4% 5 / 11 Subroutinesub 92.8% 26 / 28 Podpod 0.0% 0 / 8 totaltotal 78.2% 176 / 225 CC 25 SCAR 19.5
CC
25
Cov
81%
render_response
19.9
print_banner
6.0
handle_request
4.0
CRAP 7.0
« blib/lib/Dancer/Route.pm ↑ Summary blib/lib/Dancer/Template/Simple.pm »
1 package Dancer::Handler;
2 173 our $AUTHORITY = 'cpan:SUKRIA';
3 # ABSTRACT: Dancer request handler
4 173 $Dancer::Handler::VERSION = '1.3513';
5 173 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 173 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 173 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 173 ▶use File::stat;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 173 ▶use HTTP::Headers;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 173 ▶use Dancer::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 173 ▶use Dancer::GetOpt;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 173 ▶use Dancer::SharedData;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 173 ▶use Dancer::Renderer;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 173 ▶use Dancer::Config 'setting';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 173 ▶use Dancer::ModuleLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 173 ▶use Dancer::Exception qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 173 ▶use Dancer::Factory::Hook;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 173 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 173 Dancer::Factory::Hook->instance->install_hooks(
24 qw/on_handler_exception/
25 );
26  
27 # This is where we choose which application handler to return
28 sub get_handler {
29 7 ▶ my $handler = 'Dancer::Handler::Standalone';
Subroutine get_handler: called CC=4 SCAR=13.9
Pod undocumented
30  
31 # force PSGI is PLACK_ENV is set
32 7 ▶ if ($ENV{'PLACK_ENV'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"PLACK_ENV"}) 1 6 7
33 1 Dancer::Logger::core("PLACK_ENV is set (".$ENV{'PLACK_ENV'}.") forcing PSGI handler");
34 1 setting('apphandler' => 'PSGI');
35 }
36  
37 # if Plack is detected or set by conf, use the PSGI handler
38 7 ▶ if ( defined setting('apphandler') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined setting("apphandler")) 7 0 7
39 7 $handler = 'Dancer::Handler::' . setting('apphandler');
40 }
41  
42 # load the app handler
43 7 my ($loaded, $error) = Dancer::ModuleLoader->load($handler);
44 7 ▶ raise core_handler => "Unable to load app handler `$handler': $error" if $error;
Branch: 1 branch
BranchTrueFalseTotal
if $error 1 6 7
45  
46 # OK, everything's fine, load the handler
47 6 Dancer::Logger::core('loading ' . $handler . ' handler');
48 6 return $handler->new;
49 }
50  
51 # handle an incoming request, process it and return a response
52 sub handle_request {
53 20 ▶ my ($self, $request) = @_;
Subroutine handle_request: called CC=4 SCAR=13.9
Pod undocumented
54 20 ▶ my $ip_addr = $request->remote_address || '-';
Condition100%2/2
$request->remote_address || '-'
l!l
218
MC/DC100%1/1
$request->remote_address || '-'
$request->remote_address
Truth table100%2/2
A
A= $request->remote_address
Aresult
11
00
55  
56 20 Dancer::SharedData->reset_all( reset_vars => !$request->is_forward);
57  
58 20 Dancer::Logger::core("request: "
59 . $request->method . " "
60 . $request->path_info
61 . " from $ip_addr");
62  
63 # save the request object
64 20 Dancer::SharedData->request($request);
65  
66 # deserialize the request body if possible
67 20 ▶ $request = Dancer::Serializer->process_request($request)
Branch: 1 branch
BranchTrueFalseTotal
if "Dancer::App"->current->setting("serializer") 0 20 20
68 if Dancer::App->current->setting('serializer');
69  
70 # read cookies from client
71  
72 20 Dancer::Cookies->init;
73  
74 20 ▶ Dancer::App->reload_apps if Dancer::Config::setting('auto_reload');
Branch: 1 branch
BranchTrueFalseTotal
if Dancer::Config::setting("auto_reload") 0 20 20
75  
76 20 render_request($request);
77 20 return $self->render_response();
78 }
79  
80 sub render_request {
81 516 ▶ my $request = shift;
Subroutine render_request: called CC=1 SCAR=0.0
Pod undocumented
82 516 my $action;
83 $action = try {
84 516 ▶ Dancer::Renderer->render_file
Subroutine __ANON__: called CC=4 SCAR=13.9
Branch: 1 branch
BranchTrueFalseTotal
"Dancer::Renderer"->render_file or "Dancer::Renderer"->render_action or "Dancer::Renderer"->render_autopage or "Dancer::Renderer"->render_error(404) 12 461 473
Condition100%6/6
"Dancer::Renderer"->render_file or "Dancer::Renderer"->render_action or "Dancer::Renderer"->render_autopage
l!l&&r!l&&!r
454712
"Dancer::Renderer"->render_file or "Dancer::Renderer"->render_action
l!l&&r!l&&!r
545259
MC/DC100%3/3
"Dancer::Renderer"->render_file or "Dancer::Renderer"->render_action or "Dancer::Renderer"->render_autopage
"Dancer::Renderer"->render_file "Dancer::Renderer"->render_action "Dancer::Renderer"->render_autopage
Truth table100%4/4
A or B or C
A= "Dancer::Renderer"->render_fileB= "Dancer::Renderer"->render_actionC= "Dancer::Renderer"->render_autopage
ABCresult
1XX1
01X1
0011
0000
85 || Dancer::Renderer->render_action
86 || Dancer::Renderer->render_autopage
87 || Dancer::Renderer->render_error(404);
88 } continuation {
89 # workflow exception (continuation)
90 11 ▶ my ($continuation) = @_;
Subroutine __ANON__: called CC=2 SCAR=7.1
91 11 ▶ $continuation->isa('Dancer::Continuation::Halted')
Branch: 1 branch
BranchTrueFalseTotal
$continuation->isa("Dancer::Continuation::Halted") or $continuation->rethrow 0 11 11
92 or $continuation->rethrow();
93 # special case for halted workflow continuation: still render the response
94 11 Dancer::Serializer->process_response(Dancer::SharedData->response);
95 } catch {
96 32 ▶ my ($exception) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
97 32 Dancer::Factory::Hook->execute_hooks('on_handler_exception', $exception);
98 32 Dancer::Logger::error(
99 sprintf(
100 'request to %s %s crashed: %s',
101 $request->method, $request->path_info, $exception
102 )
103 );
104  
105 # use stringification, to get exception message in case of a
106 # Dancer::Exception
107 32 Dancer::Error->new(
108 code => 500,
109 title => "Runtime Error",
110 message => "$exception",
111 exception => $exception,
112 )->render();
113 516 };
114 516 return $action;
115 }
116  
117 sub psgi_app {
118 2 ▶ my $self = shift;
Subroutine psgi_app: called CC=1 SCAR=0.0
Pod undocumented
119 sub {
120 1 ▶ my $env = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
121 1 $self->init_request_headers($env);
122 1 my $request = Dancer::Request->new(env => $env);
123 1 $self->handle_request($request);
124 2 };
125 }
126  
127 sub init_request_headers {
128 0 ▶ my ($self, $env) = @_;
Subroutine init_request_headers: not called CC=1 SCAR=6.9
Pod undocumented
129  
130 my $psgi_headers = HTTP::Headers->new(
131 map {
132 0 (my $field = $_) =~ s/^HTTPS?_//;
133 0 ($field => $env->{$_});
134 }
135 0 grep {/^(?:HTTP|CONTENT|COOKIE)/i} keys %$env
136 );
137 0 Dancer::SharedData->headers($psgi_headers);
138 }
139  
140 # render a PSGI-formatted response from a response built by
141 # handle_request()
142 sub render_response {
143 27 ▶ my $self = shift;
Subroutine render_response: called CC=14 SCAR=29.9
Pod undocumented
144 27 my $response = Dancer::SharedData->response();
145  
146 27 my $content = $response->content;
147  
148 27 ▶ unless ( ref($content) eq 'GLOB' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $content eq 'GLOB') { } 0 27 27
149 27 my $charset = setting('charset');
150 27 my $ctype = $response->header('Content-Type');
151  
152 27 ▶ if ( $charset && $ctype && _is_text($ctype) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($charset and $ctype and _is_text($ctype)) 3 24 27
Condition66%4/6
$charset and $ctype and _is_text($ctype)
!ll&&!rl&&r
2403
$charset and $ctype
!ll&&!rl&&r
2403
MC/DC33%1/3
$charset and $ctype and _is_text($ctype)
$charset $ctype _is_text($ctype)
Truth table50%2/4
A and B and C
A= $charsetB= $ctypeC= _is_text($ctype)
ABCresult
0XX0
10X0
1100
1111
153 3 ▶ $content = Encode::encode( $charset, $content ) unless $response->_already_encoded;
Branch: 1 branch
BranchTrueFalseTotal
unless $response->_already_encoded 3 0 3
154 3 ▶ $response->header( 'Content-Type' => "$ctype; charset=$charset" )
Branch: 1 branch
BranchTrueFalseTotal
unless $ctype =~ /$charset/ 2 1 3
155 if $ctype !~ /$charset/;
156 }
157 27 ▶ if (!defined $response->header('Content-Length')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $response->header("Content-Length")) 22 5 27
158 173 ▶ use bytes; # turn off character semantics
Subroutine BEGIN: called CC=1 SCAR=0.0
159 22 $response->header( 'Content-Length' => length($content) );
160 }
161 27 $content = [$content];
162 }
163 else {
164 0 ▶ if ( !defined $response->header('Content-Length') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $response->header("Content-Length")) 0 0 0
165 0 my $stat = stat $content;
166 0 $response->header( 'Content-Length' => $stat->size );
167 }
168 }
169  
170 # drop content if request is HEAD
171 27 ▶ $content = ['']
Branch: 1 branch
BranchTrueFalseTotal
if defined "Dancer::SharedData"->request and "Dancer::SharedData"->request->is_head 0 27 27
Condition66%2/3
defined "Dancer::SharedData"->request and "Dancer::SharedData"->request->is_head
!ll&&!rl&&r
7200
MC/DC0%0/2
defined "Dancer::SharedData"->request and "Dancer::SharedData"->request->is_head
defined "Dancer::SharedData"->request "Dancer::SharedData"->request->is_head
Truth table66%2/3
A and B
A= defined "Dancer::SharedData"->requestB= "Dancer::SharedData"->request->is_head
ABresult
0X0
100
111
172 if ( defined Dancer::SharedData->request
173 && Dancer::SharedData->request->is_head() );
174  
175 # drop content AND content_length if response is 1xx or (2|3)04
176 27 ▶ if ($response->status =~ (/^[23]04$/ )) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->status =~ /^[23]04$/) 0 27 27
177 0 $content = [''];
178 0 $response->header('Content-Length' => 0);
179 }
180  
181 27 Dancer::Logger::core("response: " . $response->status);
182  
183 27 my $status = $response->status();
184 27 my $headers = $response->headers_to_array();
185  
186 # reverse streaming
187 27 ▶ if ( ref $response->streamed and ref $response->streamed eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $response->streamed and ref $response->streamed eq "CODE") 0 27 27
Condition33%1/3
ref $response->streamed and ref $response->streamed eq "CODE"
!ll&&!rl&&r
2700
MC/DC0%0/2
ref $response->streamed and ref $response->streamed eq "CODE"
ref $response->streamed ref $response->streamed eq "CODE"
Truth table33%1/3
A and B
A= ref $response->streamedB= ref $response->streamed eq "CODE"
ABresult
0X0
100
111
188 0 return $response->streamed->(
189 $status, $headers
190 );
191 }
192  
193 27 return [ $status, $headers, $content ];
194 }
195  
196 sub _is_text {
197 8 ▶ my ($content_type) = @_;
Subroutine _is_text: called CC=1 SCAR=0.0
198 8 return $content_type =~ /(\bx(?:ht)?ml\b|text|json|javascript)/;
199 }
200  
201 # Fancy banner to print on startup
202 sub print_banner {
203 0 ▶ if (setting('startup_info')) {
Subroutine print_banner: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if (setting("startup_info")) 0 0 0
204 0 my $env = setting('environment');
205 0 print "== Entering the $env dance floor ...\n";
206 }
207 }
208  
209 1 ▶sub dance { (shift)->start(@_) }
Subroutine dance: called CC=1 SCAR=0.0
Pod undocumented
210  
211 173 1;
212  
213 __END__
« blib/lib/Dancer/Route.pm ↑ Summary blib/lib/Dancer/Template/Simple.pm »