blib/lib/Dancer2/Core/Request.pm

filter: Statementstmt 93.0% 346 / 372 Branchbran 84.5% 115 / 136 Conditioncond 75.6% 56 / 74 MC/DCmcdc 51.1% 23 / 45 Subroutinesub 90.4% 85 / 94 Podpod 87.2% 48 / 55 totaltotal 86.7% 673 / 776 CC 99 SCAR 15.5
CC
99
Cov
89%
is_ajax
20.0
params
15.2
_parse_get_params
11.0
CRAP 4.7
« blib/lib/Dancer2/Core/Role/HasLocation.pm ↑ Summary blib/lib/Dancer2/Core/Response.pm »
1 package Dancer2::Core::Request;
2 # ABSTRACT: Interface for accessing incoming requests
3 187 $Dancer2::Core::Request::VERSION = '2.2.2';
4 187 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 187 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 187 ▶use parent 'Plack::Request';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 187 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 187 ▶use Encode qw(decode FB_CROAK LEAVE_SRC);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 187 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 187 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 187 ▶use Safe::Isa;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 187 ▶use Hash::MultiValue;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 187 ▶use Ref::Util qw< is_ref is_arrayref is_hashref is_coderef >;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 187 ▶use Dancer2::Core::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 187 ▶use Dancer2::Core::Request::Upload;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 187 ▶use Dancer2::Core::Cookie;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 # add an attribute for each HTTP_* variables
21 # (HOST is managed manually)
22 187 my @http_env_keys = (qw/
23 accept_charset
24 accept_encoding
25 accept_language
26 connection
27 keep_alive
28 x_requested_with
29 /);
30  
31 # apparently you can't eval core functions
32 1 ▶sub accept { $_[0]->env->{'HTTP_ACCEPT'} }
Subroutine accept: called CC=1 SCAR=0.0
Pod documented
33  
34 1 ▶eval << "_EVAL" or die $@ for @http_env_keys; ## no critic
Subroutine accept_charset: called CC=1 SCAR=0.0
Subroutine accept_encoding: called CC=1 SCAR=0.0
Subroutine accept_language: called CC=1 SCAR=0.0
Subroutine connection: called CC=1 SCAR=0.0
Subroutine keep_alive: called CC=1 SCAR=0.0
Subroutine x_requested_with: called CC=1 SCAR=0.0
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
eval qq[sub $_ { \$_[0]->env->{ 'HTTP_' . ( uc "$_" ) } }\n1;\n] or die $@ 1122 0 1122
35 sub $_ { \$_[0]->env->{ 'HTTP_' . ( uc "$_" ) } }
36 1;
37 _EVAL
38  
39 eval {
40 187 require Unicode::UTF8;
41 187 ▶ no warnings qw<redefine once>;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 34 ▶ *__decode = sub { Unicode::UTF8::decode_utf8($_[0]) };
Subroutine __ANON__: called CC=1 SCAR=0.0
43 39 ▶ *__valid = sub { Unicode::UTF8::valid_utf8($_[0]) };
Subroutine __ANON__: called CC=1 SCAR=0.0
44 187 1;
45 187 ▶} or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require Unicode::UTF8; BEGIN {${^WARNING_BITS} = "\x55\x55\x55\x45\x15\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55"} *__decode = sub { Unicode::UTF8::decode_utf8($_[0]); } ; *__valid = sub { Unicode::UTF8::valid_utf8($_[0]); } ; 1 } } or do { BEGIN {${^WARNING_BITS} = "\x55\x55\x55\x45\x15\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55"} *__decode = sub { &decode("UTF-8", $_[0]); } ; *__valid = sub { eval { do { &decode("UTF-8", $_[0], 9); 1 } }; } } 187 0 187
46 187 ▶ no warnings qw<redefine once>;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 0 ▶ *__decode = sub { decode( 'UTF-8', $_[0] ) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
48 0 ▶ *__valid = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
49 0 eval { decode( 'UTF-8', $_[0], FB_CROAK | LEAVE_SRC ); 1 };
50 0 };
51 };
52  
53 # check presence of XS module to speedup request
54 187 our $XS_URL_DECODE = eval { require URL::Encode::XS; 1; };
55 187 our $XS_PARSE_QUERY_STRING = eval { require CGI::Deurl::XS; 1; };
56 187 our $XS_HTTP_COOKIES = eval { require HTTP::XSCookies; 1; };
57  
58 187 our $_id = 0;
59  
60 # self->new( env => {}, serializer => $s, is_behind_proxy => 0|1 )
61 978 ▶sub new {
Subroutine new: called CC=5 SCAR=16.1
Pod documented
62 978 my ( $class, @args ) = @_;
63  
64 # even sized list
65 978 ▶ @args % 2 == 0
Branch: 1 branch
BranchTrueFalseTotal
@args % 2 == 0 or croak("Must provide even sized list") 978 0 978
66 or croak 'Must provide even sized list';
67  
68 978 my %opts = @args;
69 978 my $env = $opts{'env'};
70  
71 978 my $self = $class->SUPER::new($env);
72  
73 978 ▶ if ( my $s = $opts{'serializer'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $s = $opts{"serializer"}) 116 862 978
74 116 ▶ $s->$_does('Dancer2::Core::Role::Serializer')
Branch: 1 branch
BranchTrueFalseTotal
$s->$Dancer2::Core::Request::_does("Dancer2::Core::Role::Serializer") or croak("Serializer provided not a Serializer object") 115 1 116
75 or croak 'Serializer provided not a Serializer object';
76  
77 115 $self->{'serializer'} = $s;
78 }
79  
80 # additionally supported attributes
81 977 $self->{'id'} = ++$_id;
82 977 $self->{'vars'} = {};
83 977 $self->{'is_behind_proxy'} = !!$opts{'is_behind_proxy'};
84 977 $self->{'uri_for_route'} = $opts{'uri_for_route'};
85  
86 $opts{'body_params'}
87 977 ▶ and $self->{'_body_params'} = $opts{'body_params'};
Branch: 1 branch
BranchTrueFalseTotal
$opts{"body_params"} and $self->{"_body_params"} = $opts{"body_params"} 60 917 977
88 977 $self->{'_strict_utf8'} = !!$opts{'strict_utf8'};
89  
90 # Deserialize/parse body for HMV
91 977 $self->data;
92 973 $self->_build_uploads();
93  
94 972 return $self;
95 }
96  
97 # a buffer for per-request variables
98 130 ▶sub vars { $_[0]->{'vars'} }
Subroutine vars: called CC=1 SCAR=0.0
Pod documented
99  
100 13 ▶sub var {
Subroutine var: called CC=2 SCAR=6.9
Pod documented
101 13 my $self = shift;
102 @_ == 2
103 ? $self->vars->{ $_[0] } = $_[1]
104 13 ▶ : $self->vars->{ $_[0] };
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 ? : 9 4 13
105 }
106  
107 # I don't like this. I know send_file uses this and I wonder
108 # if we can remove it.
109 # -- Sawyer
110 0 ▶sub set_path_info { $_[0]->env->{'PATH_INFO'} = $_[1] }
Subroutine set_path_info: not called CC=1 SCAR=6.9
Pod undocumented
111  
112 # XXX: incompatible with Plack::Request
113 47 ▶sub body { $_[0]->raw_body }
Subroutine body: called CC=1 SCAR=0.0
Pod documented
114  
115 20 ▶sub id { $_id }
Subroutine id: called CC=1 SCAR=0.0
Pod documented
116  
117 # Private 'read-only' attributes for request params. See the params()
118 # method for the public interface.
119 #
120 # _body_params, _query_params and _route_params have setter methods that
121 # decode byte string to characters before setting; If you know you have
122 # decoded (character) params, such as output from a deserializer, you can
123 # set these directly in the request object hash to avoid the decode op.
124 364 ▶sub _params { $_[0]->{'_params'} ||= $_[0]->_build_params }
Subroutine _params: called CC=2 SCAR=7.2
Condition66%2/3
$_[0]{"_params"} ||= $_[0]->_build_params
l!l&&r!l&&!r
327370
MC/DC0%0/2
$_[0]{"_params"} ||= $_[0]->_build_params
$_[0]{"_params"} $_[0]->_build_params
Truth table66%2/3
A ||= B
A= $_[0]{"_params"}B= $_[0]->_build_params
ABresult
1X1
011
000
125  
126 781 ▶sub _has_params { defined $_[0]->{'_params'} }
Subroutine _has_params: called CC=1 SCAR=0.0
127  
128 1770 ▶sub _body_params { $_[0]->{'_body_params'} ||= $_[0]->body_parameters->as_hashref_mixed }
Subroutine _body_params: called CC=2 SCAR=7.2
Condition66%2/3
$_[0]{"_body_params"} ||= $_[0]->body_parameters->as_hashref_mixed
l!l&&r!l&&!r
8968740
MC/DC0%0/2
$_[0]{"_body_params"} ||= $_[0]->body_parameters->as_hashref_mixed
$_[0]{"_body_params"} $_[0]->body_parameters->as_hashref_mixed
Truth table66%2/3
A ||= B
A= $_[0]{"_body_params"}B= $_[0]->body_parameters->as_hashref_mixed
ABresult
1X1
011
000
129  
130 101 ▶sub _query_params { $_[0]->{'_query_params'} }
Subroutine _query_params: called CC=1 SCAR=0.0
131  
132 46 ▶sub _set_query_params {
Subroutine _set_query_params: called CC=1 SCAR=0.0
133 46 my ( $self, $params ) = @_;
134 46 $self->{_query_params} = $self->_decode( $params, 'query parameters' );
135 }
136  
137 782 ▶sub _route_params { $_[0]->{'_route_params'} ||= {} }
Subroutine _route_params: called CC=2 SCAR=6.9
Condition100%2/2
$_[0]{"_route_params"} ||= {}
l!l
74636
MC/DC100%1/1
$_[0]{"_route_params"} ||= {}
$_[0]{"_route_params"}
Truth table100%2/2
A
A= $_[0]{"_route_params"}
Aresult
11
00
138  
139 744 ▶sub _set_route_params {
Subroutine _set_route_params: called CC=1 SCAR=0.0
140 744 my ( $self, $params ) = @_;
141 744 $self->{_route_params} = $self->_decode( $params, 'route parameters' );
142 744 $self->_build_params();
143 }
144  
145 # XXX: incompatible with Plack::Request
146 6 ▶sub uploads { $_[0]->{'uploads'} }
Subroutine uploads: called CC=1 SCAR=0.0
Pod documented
147  
148 415 ▶sub is_behind_proxy { $_[0]->{'is_behind_proxy'} || 0 }
Subroutine is_behind_proxy: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"is_behind_proxy"} or 0 23 392 415
149  
150 201 ▶sub host {
Subroutine host: called CC=3 SCAR=11.0
Pod documented
151 201 my ($self) = @_;
152  
153 201 ▶ if ( $self->is_behind_proxy and exists $self->env->{'HTTP_X_FORWARDED_HOST'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_behind_proxy and exists $self->env->{'HTTP_X_FORWARDED_HOST'}) { } 3 198 201
Condition100%3/3
$self->is_behind_proxy and exists $self->env->{"HTTP_X_FORWARDED_HOST"}
!ll&&!rl&&r
19353
MC/DC100%2/2
$self->is_behind_proxy and exists $self->env->{"HTTP_X_FORWARDED_HOST"}
$self->is_behind_proxy exists $self->env->{"HTTP_X_FORWARDED_HOST"}
Truth table100%3/3
A and B
A= $self->is_behind_proxyB= exists $self->env->{"HTTP_X_FORWARDED_HOST"}
ABresult
0X0
100
111
154 3 my @hosts = split /\s*,\s*/, $self->env->{'HTTP_X_FORWARDED_HOST'}, 2;
155 3 return $hosts[0];
156 } else {
157 198 return $self->env->{'HTTP_HOST'};
158 }
159 }
160  
161 # aliases, kept for backward compat
162 13 ▶sub agent { shift->user_agent }
Subroutine agent: called CC=1 SCAR=0.0
Pod documented
163 4 ▶sub remote_address { shift->address }
Subroutine remote_address: called CC=1 SCAR=0.0
Pod documented
164 3 ▶sub forwarded_for_address { shift->env->{'HTTP_X_FORWARDED_FOR'} }
Subroutine forwarded_for_address: called CC=1 SCAR=0.0
Pod documented
165 0 ▶sub forwarded_host { shift->env->{'HTTP_X_FORWARDED_HOST'} }
Subroutine forwarded_host: not called CC=1 SCAR=6.9
Pod documented
166  
167 # there are two options
168 14 ▶sub forwarded_protocol {
Subroutine forwarded_protocol: called CC=3 SCAR=11.0
Pod documented
169 $_[0]->env->{'HTTP_X_FORWARDED_PROTO'} ||
170 $_[0]->env->{'HTTP_X_FORWARDED_PROTOCOL'} ||
171 14 ▶ $_[0]->env->{'HTTP_FORWARDED_PROTO'}
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->env->{"HTTP_X_FORWARDED_PROTO"} or $_[0]->env->{"HTTP_X_FORWARDED_PROTOCOL"} or $_[0]->env->{"HTTP_FORWARDED_PROTO"} 7 7 14
Condition100%3/3
$_[0]->env->{"HTTP_X_FORWARDED_PROTO"} or $_[0]->env->{"HTTP_X_FORWARDED_PROTOCOL"}
l!l&&r!l&&!r
167
MC/DC100%2/2
$_[0]->env->{"HTTP_X_FORWARDED_PROTO"} or $_[0]->env->{"HTTP_X_FORWARDED_PROTOCOL"}
$_[0]->env->{"HTTP_X_FORWARDED_PROTO"} $_[0]->env->{"HTTP_X_FORWARDED_PROTOCOL"}
Truth table100%3/3
A or B
A= $_[0]->env->{"HTTP_X_FORWARDED_PROTO"}B= $_[0]->env->{"HTTP_X_FORWARDED_PROTOCOL"}
ABresult
1X1
011
000
172 }
173  
174 213 ▶sub scheme {
Subroutine scheme: called CC=3 SCAR=11.0
Pod documented
175 213 my ($self) = @_;
176 213 ▶ my $scheme = $self->is_behind_proxy
Branch: 1 branch
BranchTrueFalseTotal
$self->is_behind_proxy ? : 14 199 213
177 ? $self->forwarded_protocol
178 : '';
179  
180 213 ▶ return $scheme || $self->env->{'psgi.url_scheme'};
Condition100%3/3
$scheme || $self->env->{'psgi.url_scheme'}
l!l&&r!l&&!r
71997
MC/DC100%2/2
$scheme || $self->env->{'psgi.url_scheme'}
$scheme $self->env->{'psgi.url_scheme'}
Truth table100%3/3
A || B
A= $schemeB= $self->env->{'psgi.url_scheme'}
ABresult
1X1
011
000
181 }
182  
183 1061 ▶sub serializer { $_[0]->{'serializer'} }
Subroutine serializer: called CC=1 SCAR=0.0
Pod documented
184  
185 1043 ▶sub data { $_[0]->{'data'} ||= $_[0]->deserialize() }
Subroutine data: called CC=2 SCAR=6.9
Pod documented
Condition100%3/3
$_[0]{"data"} ||= $_[0]->deserialize
l!l&&r!l&&!r
6241940
MC/DC100%2/2
$_[0]{"data"} ||= $_[0]->deserialize
$_[0]{"data"} $_[0]->deserialize
Truth table100%3/3
A ||= B
A= $_[0]{"data"}B= $_[0]->deserialize
ABresult
1X1
011
000
186  
187 981 ▶sub deserialize {
Subroutine deserialize: called CC=10 SCAR=23.0
Pod undocumented
188 981 my $self = shift;
189  
190 # don't attempt to deserialize if the form is 'multipart/form-data'
191 981 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($self->content_type and $self->content_type =~ m[^multipart/form-data]i) 9 972 981
Condition100%3/3
$self->content_type and $self->content_type =~ m[^multipart/form-data]i
!ll&&!rl&&r
8531199
MC/DC100%2/2
$self->content_type and $self->content_type =~ m[^multipart/form-data]i
$self->content_type $self->content_type =~ m[^multipart/form-data]i
Truth table100%3/3
A and B
A= $self->content_typeB= $self->content_type =~ m[^multipart/form-data]i
ABresult
0X0
100
111
192 $self->content_type
193 && $self->content_type =~ /^multipart\/form-data/i
194 ) {
195 9 return;
196 }
197  
198  
199 972 ▶ my $serializer = $self->serializer
Branch: 1 branch
BranchTrueFalseTotal
my $serializer = $self->serializer or return 116 856 972
200 or return;
201  
202 # The latest draft of the RFC does not forbid DELETE to have content,
203 # rather the behaviour is undefined. Take the most lenient route and
204 # deserialize any content on delete as well.
205 return
206 116 ▶ unless grep { $self->method eq $_ } qw/ PUT POST PATCH DELETE /;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$self->method eq $_;} "PUT", "POST", "PATCH", "DELETE" 70 46 116
207  
208 # try to deserialize
209 46 my $body = $self->body;
210  
211 46 ▶ $body && length $body > 0
Branch: 1 branch
BranchTrueFalseTotal
$body and length $body > 0 or return 43 3 46
Condition66%2/3
$body and length $body > 0
!ll&&!rl&&r
3043
MC/DC50%1/2
$body and length $body > 0
$body length $body > 0
Truth table66%2/3
A and B
A= $bodyB= length $body > 0
ABresult
0X0
100
111
212 or return;
213  
214 # Catch serializer fails - which is tricky as Role::Serializer
215 # wraps the deserializaion in an eval and returns undef.
216 # We want to generate a 500 error on serialization fail (Ref #794)
217 # to achieve that, override the log callback so we can catch a signal
218 # that it failed. This is messy (messes with serializer internals), but
219 # "works".
220 43 my $serializer_fail;
221 43 my $serializer_log_cb = $serializer->log_cb;
222 4 ▶ local $serializer->{log_cb} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
223 4 $serializer_fail = $_[1];
224 4 $serializer_log_cb->(@_);
225 43 };
226 # work-around to resolve a chicken-and-egg issue when instantiating a
227 # request object; the serializer needs that request object to deserialize
228 # the body params.
229 43 Scalar::Util::weaken( my $request = $self );
230 43 ▶ $self->serializer->has_request || $self->serializer->set_request($request);
Branch: 1 branch
BranchTrueFalseTotal
$self->serializer->has_request or $self->serializer->set_request($request) 2 41 43
231 43 my $data = $serializer->deserialize($body);
232 43 ▶ die $serializer_fail if $serializer_fail;
Branch: 1 branch
BranchTrueFalseTotal
if $serializer_fail 4 39 43
233  
234 # Set _body_params directly rather than using the setter. Deserializiation
235 # returns characters and skipping the decode op in the setter ensures
236 # that numerical data "stays" numerical; decoding an SV that is an IV
237 # converts that to a PVIV. Some serializers are picky (JSON)..
238 39 $self->{_body_params} = $data;
239  
240 # Set body parameters (decoded HMV)
241 39 ▶ $self->{'body_parameters'} =
Branch: 1 branch
BranchTrueFalseTotal
is_hashref($data) ? : 37 2 39
242 Hash::MultiValue->from_mixed( is_hashref($data) ? %$data : () );
243  
244 39 return $data;
245 }
246  
247 3 ▶sub uri { $_[0]->request_uri }
Subroutine uri: called CC=1 SCAR=0.0
Pod documented
248  
249 4 ▶sub is_head { $_[0]->method eq 'HEAD' }
Subroutine is_head: called CC=1 SCAR=0.0
Pod documented
250 7 ▶sub is_post { $_[0]->method eq 'POST' }
Subroutine is_post: called CC=1 SCAR=0.0
Pod documented
251 7 ▶sub is_get { $_[0]->method eq 'GET' }
Subroutine is_get: called CC=1 SCAR=0.0
Pod documented
252 4 ▶sub is_put { $_[0]->method eq 'PUT' }
Subroutine is_put: called CC=1 SCAR=0.0
Pod documented
253 4 ▶sub is_delete { $_[0]->method eq 'DELETE' }
Subroutine is_delete: called CC=1 SCAR=0.0
Pod documented
254 4 ▶sub is_patch { $_[0]->method eq 'PATCH' }
Subroutine is_patch: called CC=1 SCAR=0.0
Pod undocumented
255 0 ▶sub is_options { $_[0]->method eq 'OPTIONS' }
Subroutine is_options: not called CC=1 SCAR=6.9
Pod documented
256  
257 # public interface compat with CGI.pm objects
258 3 ▶sub request_method { $_[0]->method }
Subroutine request_method: called CC=1 SCAR=0.0
Pod documented
259 0 ▶sub input_handle { $_[0]->env->{'psgi.input'} }
Subroutine input_handle: not called CC=1 SCAR=6.9
Pod documented
260  
261 7 ▶sub to_string {
Subroutine to_string: called CC=1 SCAR=0.0
Pod documented
262 7 my ($self) = @_;
263 7 return "[#" . $self->id . "] " . $self->method . " " . $self->path;
264 }
265  
266 41 ▶sub base {
Subroutine base: called CC=1 SCAR=0.0
Pod documented
267 41 my $self = shift;
268 41 my $uri = $self->_common_uri;
269  
270 41 return $uri->canonical;
271 }
272  
273 195 ▶sub _common_uri {
Subroutine _common_uri: called CC=3 SCAR=11.0
274 195 my $self = shift;
275  
276 195 my $path = $self->env->{SCRIPT_NAME};
277 195 my $port = $self->env->{SERVER_PORT};
278 195 my $server = $self->env->{SERVER_NAME};
279 195 my $host = $self->host;
280 195 my $scheme = $self->scheme;
281  
282 195 my $uri = URI->new;
283 195 $uri->scheme($scheme);
284 195 ▶ $uri->authority( $host || "$server:$port" );
Condition100%2/2
$host || "${server}:$port"
l!l
15738
MC/DC100%1/1
$host || "${server}:$port"
$host
Truth table100%2/2
A
A= $host
Aresult
11
00
285 195 ▶ $uri->path( $path || '/' );
Condition100%2/2
$path || '/'
l!l
39156
MC/DC100%1/1
$path || '/'
$path
Truth table100%2/2
A
A= $path
Aresult
11
00
286  
287 195 return $uri;
288 }
289  
290 154 ▶sub uri_base {
Subroutine uri_base: called CC=2 SCAR=6.9
Pod documented
291 154 my $self = shift;
292 154 my $uri = $self->_common_uri;
293 154 my $canon = $uri->canonical;
294  
295 154 ▶ if ( $uri->path eq '/' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri->path eq "/") 142 12 154
296 142 $canon =~ s{/$}{};
297 }
298  
299 154 return $canon;
300 }
301  
302 2166 ▶sub path {
Subroutine path: called CC=2 SCAR=6.9
Pod documented
303 2166 my $self = shift;
304 2166 ▶ my $path = $self->env->{PATH_INFO} || '/';
Condition100%2/2
$self->env->{'PATH_INFO'} || '/'
l!l
215610
MC/DC100%1/1
$self->env->{'PATH_INFO'} || '/'
$self->env->{'PATH_INFO'}
Truth table100%2/2
A
A= $self->env->{'PATH_INFO'}
Aresult
11
00
305 2166 return $self->_decode_bytes( $path, 'PATH_INFO' );
306 }
307  
308 0 ▶sub dispatch_path {
Subroutine dispatch_path: not called CC=1 SCAR=6.9
Pod documented
309 0 Carp::croak q{DEPRECATED: request->dispatch_path. Please use request->path instead};
310 }
311  
312 32 ▶sub uri_for {
Subroutine uri_for: called CC=4 SCAR=13.9
Pod documented
313 32 my ( $self, $part, $params, $dont_escape ) = @_;
314  
315 32 ▶ $part ||= '';
Condition50%1/2
$part ||= ""
l!l
320
MC/DC0%0/1
$part ||= ""
$part
Truth table50%1/2
A
A= $part
Aresult
11
00
316 32 my $uri = $self->base;
317  
318 # Make sure there's exactly one slash between the base and the new part
319 32 my $base = $uri->path;
320 32 $base =~ s|/$||;
321 32 $part =~ s|^/||;
322 32 $uri->path("$base/$part");
323  
324 32 ▶ $uri->query_form($params) if $params;
Branch: 1 branch
BranchTrueFalseTotal
if $params 15 17 32
325  
326 return $dont_escape
327 3 ? uri_unescape( ${ $uri->canonical } )
328 32 ▶ : ${ $uri->canonical };
Branch: 1 branch
BranchTrueFalseTotal
$dont_escape ? : 3 29 32
329 }
330  
331 0 ▶sub uri_for_route {
Subroutine uri_for_route: not called CC=2 SCAR=17.9
Pod documented
332 0 my ( $self, @args ) = @_;
333  
334 0 ▶ is_coderef( $self->{'uri_for_route'} )
Branch: 1 branch
BranchTrueFalseTotal
is_coderef($self->{"uri_for_route"}) or die "uri_for_route called on a request instance without it" 0 0 0
335 or die 'uri_for_route called on a request instance without it';
336  
337 0 return $self->{'uri_for_route'}->(@_);
338 }
339  
340 333 ▶sub params {
Subroutine params: called CC=12 SCAR=27.2
Pod documented
341 333 my ( $self, $source ) = @_;
342  
343 333 ▶ return %{ $self->_params } if wantarray && @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray and @_ == 1 30 303 333
Condition66%2/3
wantarray and @_ == 1
!ll&&!rl&&r
303030
MC/DC50%1/2
wantarray and @_ == 1
wantarray @_ == 1
Truth table66%2/3
A and B
A= wantarrayB= @_ == 1
ABresult
0X0
100
111
344 303 ▶ return $self->_params if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 278 25 303
345  
346 25 ▶ if ( $source eq 'query' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($source eq 'query') { } 6 19 25
elsif ($source eq 'body') { } 16 3 19
347 6 ▶ return %{ $self->_query_params || {} } if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 6 6
Condition0%0/2
$self->_query_params || {}
l!l
00
MC/DC0%0/1
$self->_query_params || {}
$self->_query_params
Truth table0%0/2
A
A= $self->_query_params
Aresult
11
00
348 6 return $self->_query_params;
349 }
350 elsif ( $source eq 'body' ) {
351 16 ▶ return %{ $self->_body_params || {} } if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 16 16
Condition0%0/2
$self->_body_params || {}
l!l
00
MC/DC0%0/1
$self->_body_params || {}
$self->_body_params
Truth table0%0/2
A
A= $self->_body_params
Aresult
11
00
352 16 return $self->_body_params;
353 }
354 3 ▶ if ( $source eq 'route' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($source eq 'route') { } 2 1 3
355 2 ▶ return %{ $self->_route_params } if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 2 2
356 2 return $self->_route_params;
357 }
358 else {
359 1 croak "Unknown source params \"$source\".";
360 }
361 }
362  
363 75 ▶sub query_parameters {
Subroutine query_parameters: called CC=4 SCAR=13.9
Pod documented
364 75 my $self = shift;
365 75 ▶ $self->{'query_parameters'} ||= do {
Condition66%2/3
$self->{"query_parameters"} ||= do { if ($Dancer2::Core::Request::XS_PARSE_QUERY_STRING) { my $query = $self->_decode(CGI::Deurl::XS::parse_query_string($self->env->{"QUERY_STRING"}), "query parameters"); "Hash::MultiValue"->new(map({my $key = $_; is_arrayref($query->{$key}) ? map(($key, $_), @{$$query{$key};}) : ($key, $query->{$key});} keys %{$query;})); } else { $self->_decode($self->SUPER::query_parameters, "query parameters"); } }
l!l&&r!l&&!r
14610
MC/DC0%0/2
$self->{"query_parameters"} ||= do { if ($Dancer2::Core::Request::XS_PARSE_QUERY_STRING) { my $query = $self->_decode(CGI::Deurl::XS::parse_query_string($self->env->{"QUERY_STRING"}), "query parameters"); "Hash::MultiValue"->new(map({my $key = $_; is_arrayref($query->{$key}) ? map(($key, $_), @{$$query{$key};}) : ($key, $query->{$key});} keys %{$query;})); } else { $self->_decode($self->SUPER::query_parameters, "query parameters"); } }
$self->{"query_parameters"} do { if ($Dancer2::Core::Request::XS_PARSE_QUERY_STRING) { my $query = $self->_decode(CGI::Deurl::XS::parse_query_string($self->env->{"QUERY_STRING"}), "query parameters"); "Hash::MultiValue"->new(map({my $key = $_; is_arrayref($query->{$key}) ? map(($key, $_), @{$$query{$key};}) : ($key, $query->{$key});} keys %{$query;})); } else { $self->_decode($self->SUPER::query_parameters, "query parameters"); } }
Truth table66%2/3
A ||= B
A= $self->{"query_parameters"}B= do { if ($Dancer2::Core::Request::XS_PARSE_QUERY_STRING) { my $query = $self->_decode(CGI::Deurl::XS::parse_query_string($self->env->{"QUERY_STRING"}), "query parameters"); "Hash::MultiValue"->new(map({my $key = $_; is_arrayref($query->{$key}) ? map(($key, $_), @{$$query{$key};}) : ($key, $query->{$key});} keys %{$query;})); } else { $self->_decode($self->SUPER::query_parameters, "query parameters"); } }
ABresult
1X1
011
000
366 61 ▶ if ($XS_PARSE_QUERY_STRING) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Dancer2::Core::Request::XS_PARSE_QUERY_STRING) { } 57 4 61
367 my $query = $self->_decode(CGI::Deurl::XS::parse_query_string(
368 57 $self->env->{'QUERY_STRING'}
369 ), 'query parameters');
370  
371 Hash::MultiValue->new(
372 map {;
373 20 my $key = $_;
374 is_arrayref( $query->{$key} )
375 3 ? ( map +( $key => $_ ), @{ $query->{$key} } )
376 20 ▶ : ( $key => $query->{$key} )
Branch: 1 branch
BranchTrueFalseTotal
is_arrayref($query->{$key}) ? : 3 17 20
377 57 } keys %{$query}
378 );
379 } else {
380 # defer to Plack::Request
381 4 $self->_decode($self->SUPER::query_parameters, 'query parameters');
382 }
383 };
384 }
385  
386 # this will be filled once the route is matched
387 21 ▶sub route_parameters { $_[0]->{'route_parameters'} ||= Hash::MultiValue->new }
Subroutine route_parameters: called CC=2 SCAR=9.2
Pod undocumented
Condition33%1/3
$_[0]{"route_parameters"} ||= "Hash::MultiValue"->new
l!l&&r!l&&!r
2100
MC/DC0%0/2
$_[0]{"route_parameters"} ||= "Hash::MultiValue"->new
$_[0]{"route_parameters"} "Hash::MultiValue"->new
Truth table33%1/3
A ||= B
A= $_[0]{"route_parameters"}B= "Hash::MultiValue"->new
ABresult
1X1
011
000
388  
389 744 ▶sub _set_route_parameters {
Subroutine _set_route_parameters: called CC=1 SCAR=0.0
390 744 my ( $self, $params ) = @_;
391 # remove reserved splat parameter name
392 # you should access splat parameters using splat() keyword
393 744 delete @{$params}{qw<splat captures>};
394 $self->{'route_parameters'} = Hash::MultiValue->from_mixed(
395 744 %{ $self->_decode( $params, 'route parameters' ) }
396 );
397 }
398  
399 960 ▶sub body_parameters {
Subroutine body_parameters: called CC=2 SCAR=7.1
Pod documented
400 960 my $self = shift;
401 # defer to (the overridden) Plack::Request->body_parameters
402 960 ▶ $self->{'body_parameters'} ||= $self->_decode(
Condition66%2/3
$self->{"body_parameters"} ||= $self->_decode($self->SUPER::body_parameters, "body parameters")
l!l&&r!l&&!r
868740
MC/DC0%0/2
$self->{"body_parameters"} ||= $self->_decode($self->SUPER::body_parameters, "body parameters")
$self->{"body_parameters"} $self->_decode($self->SUPER::body_parameters, "body parameters")
Truth table66%2/3
A ||= B
A= $self->{"body_parameters"}B= $self->_decode($self->SUPER::body_parameters, "body parameters")
ABresult
1X1
011
000
403 $self->SUPER::body_parameters(),
404 'body parameters',
405 );
406 }
407  
408 6 ▶sub parameters {
Subroutine parameters: called CC=3 SCAR=11.7
Pod documented
409 6 my ( $self, $type ) = @_;
410  
411 # handle a specific case
412 6 ▶ if ($type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type) 0 6 6
413 0 my $attr = "${type}_parameters";
414 0 return $self->$attr;
415 }
416  
417 # merge together the *decoded* parameters
418 6 ▶ $self->{'merged_parameters'} ||= do {
Condition66%2/3
$self->{"merged_parameters"} ||= do { my $query = $self->query_parameters; my $body = $self->body_parameters; my $route = $self->route_parameters; "Hash::MultiValue"->new(map($_->flatten, $query, $body, $route)) }
l!l&&r!l&&!r
150
MC/DC0%0/2
$self->{"merged_parameters"} ||= do { my $query = $self->query_parameters; my $body = $self->body_parameters; my $route = $self->route_parameters; "Hash::MultiValue"->new(map($_->flatten, $query, $body, $route)) }
$self->{"merged_parameters"} do { my $query = $self->query_parameters; my $body = $self->body_parameters; my $route = $self->route_parameters; "Hash::MultiValue"->new(map($_->flatten, $query, $body, $route)) }
Truth table66%2/3
A ||= B
A= $self->{"merged_parameters"}B= do { my $query = $self->query_parameters; my $body = $self->body_parameters; my $route = $self->route_parameters; "Hash::MultiValue"->new(map($_->flatten, $query, $body, $route)) }
ABresult
1X1
011
000
419 5 my $query = $self->query_parameters;
420 5 my $body = $self->body_parameters;
421 5 my $route = $self->route_parameters; # not in Plack::Request
422 5 Hash::MultiValue->new( map $_->flatten, $query, $body, $route );
423 };
424 }
425  
426 3 ▶sub captures { shift->params->{captures} || {} }
Subroutine captures: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
+(shift())->params->{"captures"} or {} 2 1 3
427  
428 33 ▶sub splat { @{ shift->params->{splat} || [] } }
Subroutine splat: called CC=2 SCAR=6.9
Pod undocumented
Condition100%2/2
+(shift())->params->{'splat'} || []
l!l
321
MC/DC100%1/1
+(shift())->params->{'splat'} || []
+(shift())->params->{'splat'}
Truth table100%2/2
A
A= +(shift())->params->{'splat'}
Aresult
11
00
429  
430 # XXX: incompatible with Plack::Request
431 10 ▶sub param { shift->params->{ $_[0] } }
Subroutine param: called CC=1 SCAR=0.0
Pod documented
432  
433 25261 ▶sub _decode {
Subroutine _decode: called CC=7 SCAR=19.5
434 25261 my ( $self, $h, $context ) = @_;
435 25261 ▶ return if not defined $h;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $h 49 25212 25261
436  
437 25212 ▶ if ( !is_ref($h) && !utf8::is_utf8($h) ) {
Branch: 4 branches
BranchTrueFalseTotal
if (not is_ref($h) || utf8::is_utf8($h)) { } 21774 3438 25212
elsif (ref $h eq 'Hash::MultiValue') { } 877 2561 3438
elsif (is_hashref($h)) { } 2429 132 2561
elsif (is_arrayref($h)) { } 122 10 132
Condition100%3/3
is_ref($h) || utf8::is_utf8($h)
l!l&&r!l&&!r
34281021774
MC/DC100%2/2
is_ref($h) || utf8::is_utf8($h)
is_ref($h) utf8::is_utf8($h)
Truth table100%3/3
A || B
A= is_ref($h)B= utf8::is_utf8($h)
ABresult
1X1
011
000
438 21774 return $self->_decode_bytes( $h, $context );
439 }
440 elsif ( ref($h) eq 'Hash::MultiValue' ) {
441 877 return Hash::MultiValue->from_mixed(
442 $self->_decode( $h->as_hashref_mixed, $context )
443 );
444 }
445 elsif ( is_hashref($h) ) {
446 2429 return { map scalar $self->_decode( $_, $context ), %{$h} };
447 }
448 elsif ( is_arrayref($h) ) {
449 122 return [ map $self->_decode( $_, $context ), @{$h} ];
450 }
451  
452 10 return $h;
453 }
454  
455 23940 ▶sub _decode_bytes {
Subroutine _decode_bytes: called CC=4 SCAR=13.9
456 23940 my ( $self, $bytes, $context ) = @_;
457  
458 # If PSGI already gave us characters, avoid re-decoding.
459 23940 ▶ return $bytes if utf8::is_utf8($bytes);
Branch: 1 branch
BranchTrueFalseTotal
if utf8::is_utf8($bytes) 0 23940 23940
460 23940 ▶ return $bytes if $bytes !~ /[\x80-\xFF]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $bytes =~ /[\x80-\xFF]/ 23901 39 23940
461  
462 39 ▶ return __decode($bytes) if __valid($bytes);
Branch: 1 branch
BranchTrueFalseTotal
if __valid($bytes) 34 5 39
463 5 return $self->_invalid_utf8( $bytes, $context );
464 }
465  
466 5 ▶sub _invalid_utf8 {
Subroutine _invalid_utf8: called CC=4 SCAR=13.9
467 5 my ( $self, $bytes, $context ) = @_;
468 5 my $strict = $self->{_strict_utf8};
469 5 ▶ my $where = $context || 'input';
Condition50%1/2
$context || 'input'
l!l
50
MC/DC0%0/1
$context || 'input'
$context
Truth table50%1/2
A
A= $context
Aresult
11
00
470 5 my $msg = "Invalid UTF-8 in $where";
471  
472 5 ▶ $strict
Branch: 1 branch
BranchTrueFalseTotal
$strict and Carp::croak($msg) 2 3 5
473 and Carp::croak($msg);
474  
475 3 ▶ if ( my $logger = $self->env->{'psgix.logger'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $logger = $self->env->{'psgix.logger'}) { } 1 2 3
476 1 $logger->({
477 level => 'warning',
478 message => "$msg; leaving bytes unchanged",
479 });
480 } else {
481 2 Carp::carp("$msg; leaving bytes unchanged");
482 }
483  
484 3 return $bytes;
485 }
486  
487 0 ▶sub is_ajax {
Subroutine is_ajax: not called CC=4 SCAR=30.0
Pod documented
488 0 my $self = shift;
489  
490 0 ▶ return 0 unless defined $self->headers;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->headers 0 0 0
491 0 ▶ return 0 unless defined $self->header('X-Requested-With');
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->header("X-Requested-With") 0 0 0
492 0 ▶ return 0 if $self->header('X-Requested-With') ne 'XMLHttpRequest';
Branch: 1 branch
BranchTrueFalseTotal
if $self->header("X-Requested-With") ne "XMLHttpRequest" 0 0 0
493 0 return 1;
494 }
495  
496 # XXX incompatible with Plack::Request
497 # context-aware accessor for uploads
498 21 ▶sub upload {
Subroutine upload: called CC=4 SCAR=13.9
Pod documented
499 21 my ( $self, $name ) = @_;
500 21 my $res = $self->{uploads}{$name};
501  
502 21 ▶ return $res unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 12 9 21
503 9 ▶ return () unless defined $res;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $res 3 6 9
504 6 ▶ return ( is_arrayref($res) ) ? @$res : $res;
Branch: 1 branch
BranchTrueFalseTotal
is_arrayref($res) ? : 3 3 6
505 }
506  
507 781 ▶sub _build_params {
Subroutine _build_params: called CC=3 SCAR=11.0
508 781 my ($self) = @_;
509  
510 # params may have been populated by before filters
511 # _before_ we get there, so we have to save it first
512 781 ▶ my $previous = $self->_has_params ? $self->_params : {};
Branch: 1 branch
BranchTrueFalseTotal
$self->_has_params ? : 56 725 781
513  
514 # now parse environment params...
515 781 my $get_params = $self->_parse_get_params();
516  
517 # and merge everything
518 $self->{_params} = {
519 780 ▶ map +( is_hashref($_) ? %{$_} : () ),
Branch: 1 branch
BranchTrueFalseTotal
is_hashref($_) ? : 2433 687 3120
520 $previous,
521 $get_params,
522 $self->_body_params,
523 $self->_route_params,
524 };
525  
526 }
527  
528 76 ▶sub _url_decode {
Subroutine _url_decode: called CC=2 SCAR=6.9
529 76 my ( $self, $encoded ) = @_;
530 76 ▶ return URL::Encode::XS::url_decode($encoded) if $XS_URL_DECODE;
Branch: 1 branch
BranchTrueFalseTotal
if $Dancer2::Core::Request::XS_URL_DECODE 32 44 76
531 44 my $clean = $encoded;
532 44 $clean =~ tr/\+/ /;
533 44 $clean =~ s/%([a-fA-F0-9]{2})/pack "H2", $1/eg;
534 44 return $clean;
535 }
536  
537 781 ▶sub _parse_get_params {
Subroutine _parse_get_params: called CC=11 SCAR=24.0
538 781 my ($self) = @_;
539 781 ▶ return $self->_query_params if defined $self->{_query_params};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"_query_params"} 50 731 781
540  
541 731 my $query_params = {};
542  
543 731 my $source = $self->env->{QUERY_STRING};
544 731 ▶ return if !defined $source || $source eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $source or $source eq "" 685 46 731
Condition100%3/3
not defined $source or $source eq ""
l!l&&r!l&&!r
2266346
MC/DC100%2/2
not defined $source or $source eq ""
not defined $source $source eq ""
Truth table100%3/3
not(A) or B
A= not defined $sourceB= $source eq ""
ABresult
1X1
011
000
545  
546 46 ▶ if ($XS_PARSE_QUERY_STRING) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Dancer2::Core::Request::XS_PARSE_QUERY_STRING) 35 11 46
547 35 ▶ $self->_set_query_params(
Condition50%1/2
CGI::Deurl::XS::parse_query_string($source) || {}
l!l
350
MC/DC0%0/1
CGI::Deurl::XS::parse_query_string($source) || {}
CGI::Deurl::XS::parse_query_string($source)
Truth table50%1/2
A
A= CGI::Deurl::XS::parse_query_string($source)
Aresult
11
00
548 CGI::Deurl::XS::parse_query_string($source) || {}
549 );
550 34 return $self->_query_params;
551 }
552  
553 11 foreach my $token ( split /[&;]/, $source ) {
554 38 my ( $key, $val ) = split( /=/, $token );
555 38 ▶ next unless defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 0 38 38
556 38 ▶ $val = ( defined $val ) ? $val : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $val ? : 38 0 38
557 38 $key = $self->_url_decode($key);
558 38 $val = $self->_url_decode($val);
559  
560 # looking for multi-value params
561 38 ▶ if ( exists $query_params->{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $query_params->{$key}) { } 7 31 38
562 7 my $prev_val = $query_params->{$key};
563 7 ▶ if ( is_arrayref($prev_val) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_arrayref($prev_val)) { } 2 5 7
564 2 push @{ $query_params->{$key} }, $val;
565 }
566 else {
567 5 $query_params->{$key} = [ $prev_val, $val ];
568 }
569 }
570  
571 # simple value param (first time we see it)
572 else {
573 31 $query_params->{$key} = $val;
574 }
575 }
576 11 $self->_set_query_params( $query_params );
577 11 return $self->_query_params;
578 }
579  
580 973 ▶sub _build_uploads {
Subroutine _build_uploads: called CC=4 SCAR=13.9
581 973 my ($self) = @_;
582  
583 # parse body and build body params
584 973 my $body_params = $self->_body_params;
585  
586 972 my $uploads = $self->SUPER::uploads;
587 972 my %uploads;
588  
589 972 for my $name ( keys %$uploads ) {
590 my @uploads = map Dancer2::Core::Request::Upload->new(
591 # For back-compatibility, we use a HashRef of headers
592 22 headers => {@{$_->{headers}->psgi_flatten_without_sort}},
593 tempname => $_->{tempname},
594 size => $_->{size},
595 16 filename => $self->_decode( $_->{filename}, 'upload filename' ),
596 ), $uploads->get_all($name);
597  
598 16 ▶ $uploads{$name} = @uploads > 1 ? \@uploads : $uploads[0];
Branch: 1 branch
BranchTrueFalseTotal
@uploads > 1 ? : 6 10 16
599  
600 # support access to the filename as a normal param
601 16 my @filenames = map $_->{'filename'}, @uploads;
602 16 ▶ $self->{_body_params}{$name} =
Branch: 1 branch
BranchTrueFalseTotal
@filenames > 1 ? : 6 10 16
603 @filenames > 1 ? \@filenames : $filenames[0];
604 }
605  
606 972 $self->{uploads} = \%uploads;
607 }
608  
609 # XXX: incompatible with Plack::Request
610 757 ▶sub cookies { $_[0]->{'cookies'} ||= $_[0]->_build_cookies }
Subroutine cookies: called CC=2 SCAR=7.2
Pod documented
Condition66%2/3
$_[0]{"cookies"} ||= $_[0]->_build_cookies
l!l&&r!l&&!r
1176400
MC/DC0%0/2
$_[0]{"cookies"} ||= $_[0]->_build_cookies
$_[0]{"cookies"} $_[0]->_build_cookies
Truth table66%2/3
A ||= B
A= $_[0]{"cookies"}B= $_[0]->_build_cookies
ABresult
1X1
011
000
611  
612 640 ▶sub _build_cookies {
Subroutine _build_cookies: called CC=5 SCAR=16.3
613 640 my $self = shift;
614 640 my $cookies = {};
615  
616 640 my $http_cookie = $self->header('Cookie');
617 640 ▶ return $cookies unless defined $http_cookie; # nothing to do
Branch: 1 branch
BranchTrueFalseTotal
unless defined $http_cookie 570 70 640
618  
619 70 ▶ if ( $XS_HTTP_COOKIES ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Dancer2::Core::Request::XS_HTTP_COOKIES) { } 0 70 70
620 0 $cookies = HTTP::XSCookies::crush_cookie($http_cookie);
621 }
622 else {
623 # handle via Plack::Request
624 70 $cookies = $self->SUPER::cookies();
625 }
626  
627 # convert to objects
628 70 ▶ while (my ($name, $value) = each %{$cookies}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$cookies;}) 73 70 143
629 73 ▶ $cookies->{$name} = Dancer2::Core::Cookie->new(
Branch: 1 branch
BranchTrueFalseTotal
is_arrayref($value) ? : 0 73 73
630 name => $name,
631 # HTTP::XSCookies v0.17+ will do the split and return an arrayref
632 value => is_arrayref($value) ? $value : [split '&', $value ]
633 );
634 }
635 70 return $cookies;
636 }
637  
638 # poor man's clone
639 60 ▶sub _shallow_clone {
Subroutine _shallow_clone: called CC=5 SCAR=16.1
640 60 my ($self, $params, $options) = @_;
641  
642 # shallow clone $env; we don't want to alter the existing one
643 # in $self, then merge any overridden values
644 60 ▶ my $env = { %{ $self->env }, %{ $options || {} } };
Condition50%1/2
$options || {}
l!l
600
MC/DC0%0/1
$options || {}
$options
Truth table50%1/2
A
A= $options
Aresult
11
00
645  
646 60 my $new_request = __PACKAGE__->new(
647 env => $env,
648 body_params => {},
649 );
650  
651 # Clone and merge query params
652 60 my $new_params = $self->params;
653 60 ▶ $new_request->{_query_params} = { %{ $self->{_query_params} || {} } };
Condition100%2/2
$self->{'_query_params'} || {}
l!l
1644
MC/DC100%1/1
$self->{'_query_params'} || {}
$self->{'_query_params'}
Truth table100%2/2
A
A= $self->{'_query_params'}
Aresult
11
00
654 60 $new_request->{_strict_utf8} = $self->{_strict_utf8};
655 60 $new_request->{query_parameters} = $self->query_parameters->clone;
656 60 ▶ for my $key ( keys %{ $params || {} } ) {
Condition100%2/2
$params || {}
l!l
1248
MC/DC100%1/1
$params || {}
$params
Truth table100%2/2
A
A= $params
Aresult
11
00
657 12 my $value = $params->{$key};
658 12 $new_params->{$key} = $value;
659 12 $new_request->{_query_params}->{$key} = $value;
660 12 $new_request->{query_parameters}->add( $key => $value );
661 }
662  
663 # Copy params (these are already decoded)
664 60 $new_request->{_params} = $new_params;
665 60 $new_request->{_body_params} = $self->{_body_params};
666 60 $new_request->{_route_params} = $self->{_route_params};
667 60 $new_request->{headers} = $self->headers;
668  
669 # Copy remaining settings
670 60 $new_request->{is_behind_proxy} = $self->{is_behind_proxy};
671 60 $new_request->{vars} = $self->{vars};
672  
673 # Clone any existing decoded & cached body params. (GH#1116 GH#1269)
674 60 $new_request->{'body_parameters'} = $self->body_parameters->clone;
675  
676 # Delete merged HMV parameters, allowing them to be reconstructed on first use.
677 60 delete $new_request->{'merged_parameters'};
678  
679 60 return $new_request;
680 }
681  
682  
683 742 ▶sub _set_route {
Subroutine _set_route: called CC=1 SCAR=0.0
684 742 my ( $self, $route ) = @_;
685 742 $self->{'route'} = $route;
686 }
687  
688 6 ▶sub route { $_[0]->{'route'} }
Subroutine route: called CC=1 SCAR=0.0
Pod documented
689  
690 2 ▶sub body_data {
Subroutine body_data: called CC=3 SCAR=11.0
Pod documented
691 2 my $self = shift;
692 2 ▶ return $self->data if $self->serializer;
Branch: 1 branch
BranchTrueFalseTotal
if $self->serializer 1 1 2
693 1 $self->_body_params;
694 1 ▶ return $self->{_body_params} if keys %{ $self->{_body_params} };
Branch: 1 branch
BranchTrueFalseTotal
if keys %{$$self{"_body_params"};} 0 1 1
695 1 return $self->body;
696 }
697  
698 187 1;
699  
700 __END__
« blib/lib/Dancer2/Core/Role/HasLocation.pm ↑ Summary blib/lib/Dancer2/Core/Response.pm »