blib/lib/Dancer/Handler.pm

filter: Statementstmt 87.9% 109 / 124 Branchbran 61.7% 21 / 34 Conditioncond 73.9% 17 / 23 MC/DCmcdc 38.4% 5 / 13 Subroutinesub 92.8% 26 / 28 Podpod 0.0% 0 / 8 totaltotal 77.3% 178 / 230 SCAR 34.2
CC
26
Cov
81%
render_response
19.9
print_banner
6.0
handle_request
4.0
CRAP 30.5
« blib/lib/Dancer/Logger/File.pm ↑ Summary lib/Dancer/Route/Cache.pm »
1 package Dancer::Handler;
2 175 our $AUTHORITY = 'cpan:SUKRIA';
3 # ABSTRACT: Dancer request handler
4 175 $Dancer::Handler::VERSION = '1.3520';
5 175 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 175 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 175 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 175 ▶use File::stat;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 175 ▶use HTTP::Headers;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 175 ▶use Dancer::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 175 ▶use Dancer::GetOpt;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 175 ▶use Dancer::SharedData;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 175 ▶use Dancer::Renderer;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 175 ▶use Dancer::Config 'setting';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 175 ▶use Dancer::ModuleLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 175 ▶use Dancer::Exception qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 175 ▶use Dancer::Factory::Hook;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 175 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 175 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 519 ▶ my $request = shift;
Subroutine render_request: called CC=1 SCAR=0.0
Pod undocumented
82 519 my $action;
83 $action = try {
84 519 ▶ 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 462 474
Condition100%6/6
"Dancer::Renderer"->render_file or "Dancer::Renderer"->render_action or "Dancer::Renderer"->render_autopage
l!l&&r!l&&!r
455712
"Dancer::Renderer"->render_file or "Dancer::Renderer"->render_action
l!l&&r!l&&!r
545361
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 13 ▶ my ($continuation) = @_;
Subroutine __ANON__: called CC=3 SCAR=11.4
91 13 ▶ $continuation->isa('Dancer::Continuation::Halted')
Branch: 1 branch
BranchTrueFalseTotal
$continuation->isa("Dancer::Continuation::Halted") or $continuation->isa("Dancer::Continuation::Route") or $continuation->rethrow 0 13 13
Condition66%2/3
$continuation->isa("Dancer::Continuation::Halted") or $continuation->isa("Dancer::Continuation::Route")
l!l&&r!l&&!r
1120
MC/DC0%0/2
$continuation->isa("Dancer::Continuation::Halted") or $continuation->isa("Dancer::Continuation::Route")
$continuation->isa("Dancer::Continuation::Halted") $continuation->isa("Dancer::Continuation::Route")
Truth table66%2/3
A or B
A= $continuation->isa("Dancer::Continuation::Halted")B= $continuation->isa("Dancer::Continuation::Route")
ABresult
1X1
011
000
92 || $continuation->isa('Dancer::Continuation::Route')
93 or $continuation->rethrow();
94 # special case for halted workflow continuation: still render the response
95 13 Dancer::Serializer->process_response(Dancer::SharedData->response);
96 } catch {
97 32 ▶ my ($exception) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
98 32 Dancer::Factory::Hook->execute_hooks('on_handler_exception', $exception);
99 32 Dancer::Logger::error(
100 sprintf(
101 'request to %s %s crashed: %s',
102 $request->method, $request->path_info, $exception
103 )
104 );
105  
106 # use stringification, to get exception message in case of a
107 # Dancer::Exception
108 32 Dancer::Error->new(
109 code => 500,
110 title => "Runtime Error",
111 message => "$exception",
112 exception => $exception,
113 )->render();
114 519 };
115 519 return $action;
116 }
117  
118 sub psgi_app {
119 2 ▶ my $self = shift;
Subroutine psgi_app: called CC=1 SCAR=0.0
Pod undocumented
120 sub {
121 1 ▶ my $env = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
122 1 $self->init_request_headers($env);
123 1 my $request = Dancer::Request->new(env => $env);
124 1 $self->handle_request($request);
125 2 };
126 }
127  
128 sub init_request_headers {
129 0 ▶ my ($self, $env) = @_;
Subroutine init_request_headers: not called CC=1 SCAR=6.9
Pod undocumented
130  
131 my $psgi_headers = HTTP::Headers->new(
132 map {
133 0 (my $field = $_) =~ s/^HTTPS?_//;
134 0 ($field => $env->{$_});
135 }
136 0 grep {/^(?:HTTP|CONTENT|COOKIE)/i} keys %$env
137 );
138 0 Dancer::SharedData->headers($psgi_headers);
139 }
140  
141 # render a PSGI-formatted response from a response built by
142 # handle_request()
143 sub render_response {
144 27 ▶ my $self = shift;
Subroutine render_response: called CC=14 SCAR=29.9
Pod undocumented
145 27 my $response = Dancer::SharedData->response();
146  
147 27 my $content = $response->content;
148  
149 27 ▶ unless ( ref($content) eq 'GLOB' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $content eq 'GLOB') { } 0 27 27
150 27 my $charset = setting('charset');
151 27 my $ctype = $response->header('Content-Type');
152  
153 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
154 3 ▶ $content = Encode::encode( $charset, $content ) unless $response->_already_encoded;
Branch: 1 branch
BranchTrueFalseTotal
unless $response->_already_encoded 3 0 3
155 3 ▶ $response->header( 'Content-Type' => "$ctype; charset=$charset" )
Branch: 1 branch
BranchTrueFalseTotal
unless $ctype =~ /$charset/ 2 1 3
156 if $ctype !~ /$charset/;
157 }
158 27 ▶ if (!defined $response->header('Content-Length')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $response->header("Content-Length")) 22 5 27
159 175 ▶ use bytes; # turn off character semantics
Subroutine BEGIN: called CC=1 SCAR=0.0
160 22 $response->header( 'Content-Length' => length($content) );
161 }
162 27 $content = [$content];
163 }
164 else {
165 0 ▶ if ( !defined $response->header('Content-Length') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $response->header("Content-Length")) 0 0 0
166 0 my $stat = stat $content;
167 0 $response->header( 'Content-Length' => $stat->size );
168 }
169 }
170  
171 # drop content if request is HEAD
172 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
173 if ( defined Dancer::SharedData->request
174 && Dancer::SharedData->request->is_head() );
175  
176 # drop content AND content_length if response is 1xx or (2|3)04
177 27 ▶ if ($response->status =~ (/^[23]04$/ )) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->status =~ /^[23]04$/) 0 27 27
178 0 $content = [''];
179 0 $response->header('Content-Length' => 0);
180 }
181  
182 27 Dancer::Logger::core("response: " . $response->status);
183  
184 27 my $status = $response->status();
185 27 my $headers = $response->headers_to_array();
186  
187 # reverse streaming
188 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
189 0 return $response->streamed->(
190 $status, $headers
191 );
192 }
193  
194 27 return [ $status, $headers, $content ];
195 }
196  
197 sub _is_text {
198 8 ▶ my ($content_type) = @_;
Subroutine _is_text: called CC=1 SCAR=0.0
199 8 return $content_type =~ /(\bx(?:ht)?ml\b|text|json|javascript)/;
200 }
201  
202 # Fancy banner to print on startup
203 sub print_banner {
204 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
205 0 my $env = setting('environment');
206 0 print "== Entering the $env dance floor ...\n";
207 }
208 }
209  
210 1 ▶sub dance { (shift)->start(@_) }
Subroutine dance: called CC=1 SCAR=0.0
Pod undocumented
211  
212 175 1;
213  
214 __END__
« blib/lib/Dancer/Logger/File.pm ↑ Summary lib/Dancer/Route/Cache.pm »