blib/lib/Kelp/Request.pm

filter: Statementstmt 75.8% 94 / 124 Branchbran 66.6% 20 / 30 Conditioncond 68.7% 11 / 16 MC/DCmcdc 22.2% 2 / 9 Subroutinesub 70.7% 29 / 41 Podpod 100% 25 / 25 totaltotal 73.8% 181 / 245 CC 24 SCAR 17.9
CC
24
Cov
74%
session
42.0
json_param
5.1
charset_decode
4.0
CRAP 6.0
« blib/lib/Kelp/Util.pm ↑ Summary blib/lib/Kelp/Routes/Pattern.pm »
1 package Kelp::Request;
2  
3 29 ▶use Kelp::Base 'Plack::Request';
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 29 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 29 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 29 ▶use Hash::MultiValue;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 29 ▶use Kelp::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 29 our @CARP_NOT = qw(Kelp);
11  
12 0 ▶attr -app => sub { croak "app is required" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
13  
14 # The stash is used to pass values from one route to another
15 8 ▶attr stash => sub { {} };
Subroutine __ANON__: called CC=1 SCAR=0.0
16  
17 # The named hash contains the values of the named placeholders
18 0 ▶attr named => sub { {} };
Subroutine __ANON__: not called CC=1 SCAR=1.2
19  
20 # charset which came with the request
21 42 ▶attr -charset => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
22 42 my $self = shift;
23  
24 42 ▶ return undef unless $self->content_type;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->content_type 0 42 42
25 42 ▶ return undef unless $self->content_type =~ m{
Branch: 1 branch
BranchTrueFalseTotal
unless $self->content_type =~ m[ ^(?: # only on some content-types text/ | application/ ) .+ ;\s*charset=([^;\$]+) # get the charset ]xi 35 7 42
26 ^(?: # only on some content-types
27 text/ | application/
28 )
29 .+
30 ;\s*charset=([^;\$]+) # get the charset
31 }xi;
32 7 return $1;
33 29 };
34  
35 # The name of the matched route for this request
36 0 ▶attr route_name => sub { undef };
Subroutine __ANON__: not called CC=1 SCAR=1.2
37  
38 14 ▶attr query_parameters => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
39 14 my $self = shift;
40  
41 14 $self->SUPER::query_parameters;
42 14 my $decoded = $self->_charset_decode_array($self->env->{'plack.request.query_parameters'}, 1);
43 14 return Hash::MultiValue->new(@{$decoded});
44 29 };
45  
46 2 ▶attr body_parameters => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
47 2 my $self = shift;
48  
49 2 $self->SUPER::body_parameters;
50 2 my $decoded = $self->_charset_decode_array($self->env->{'plack.request.body_parameters'});
51 2 return Hash::MultiValue->new(@{$decoded});
52 29 };
53  
54 45 ▶attr parameters => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
55 45 my $self = shift;
56  
57 45 $self->SUPER::parameters;
58 45 my $decoded_query = $self->_charset_decode_array($self->env->{'plack.request.query_parameters'}, 1);
59 45 my $decoded_body = $self->_charset_decode_array($self->env->{'plack.request.body_parameters'});
60 45 return Hash::MultiValue->new(@{$decoded_query}, @{$decoded_body});
61 29 };
62  
63 # Raw methods - methods in Plack::Request (without decoding)
64 # in Kelp::Request, they are replaced with decoding versions
65  
66 sub raw_path
67 0 ▶{
Subroutine raw_path: not called CC=1 SCAR=6.9
Pod documented
68 0 my $self = shift;
69 0 return $self->SUPER::path(@_);
70 }
71  
72 sub raw_body
73 0 ▶{
Subroutine raw_body: not called CC=1 SCAR=6.9
Pod documented
74 0 my $self = shift;
75 0 return $self->SUPER::content(@_);
76 }
77  
78 sub raw_body_parameters
79 0 ▶{
Subroutine raw_body_parameters: not called CC=1 SCAR=6.9
Pod documented
80 0 my $self = shift;
81 0 return $self->SUPER::body_parameters(@_);
82 }
83  
84 sub raw_query_parameters
85 0 ▶{
Subroutine raw_query_parameters: not called CC=1 SCAR=6.9
Pod documented
86 0 my $self = shift;
87 0 return $self->SUPER::query_parameters(@_);
88 }
89  
90 sub raw_parameters
91 0 ▶{
Subroutine raw_parameters: not called CC=1 SCAR=6.9
Pod documented
92 0 my $self = shift;
93 0 return $self->SUPER::parameters(@_);
94 }
95  
96 # If you're running the web app as a proxy, use Plack::Middleware::ReverseProxy
97 1 ▶sub address { $_[0]->env->{REMOTE_ADDR} }
Subroutine address: called CC=1 SCAR=0.0
Pod documented
98 0 ▶sub remote_host { $_[0]->env->{REMOTE_HOST} }
Subroutine remote_host: not called CC=1 SCAR=6.9
Pod documented
99 0 ▶sub user { $_[0]->env->{REMOTE_USER} }
Subroutine user: not called CC=1 SCAR=6.9
Pod documented
100  
101 # Interface
102  
103 sub new
104 261 ▶{
Subroutine new: called CC=2 SCAR=6.9
Pod documented
105 261 my ($class, %args) = @_;
106 261 my $self = $class->SUPER::new(delete $args{env});
107 261 $self->{$_} = $args{$_} for keys %args;
108 261 return $self;
109 }
110  
111 sub is_ajax
112 2 ▶{
Subroutine is_ajax: called CC=2 SCAR=6.9
Pod documented
113 2 my $self = shift;
114 2 ▶ return 0 unless my $with = $self->headers->header('X-Requested-With');
Branch: 1 branch
BranchTrueFalseTotal
unless my $with = $self->headers->header("X-Requested-With") 1 1 2
115 1 return $with =~ m{XMLHttpRequest}i;
116 }
117  
118 sub content_type_is
119 107 ▶{
Subroutine content_type_is: called CC=2 SCAR=6.9
Pod documented
120 107 my ($self, $type) = @_;
121 107 ▶ return 0 unless $self->content_type;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->content_type 29 78 107
122 78 return $self->content_type =~ m{^\Q$type\E}i;
123 }
124  
125 sub is_text
126 2 ▶{
Subroutine is_text: called CC=1 SCAR=0.0
Pod documented
127 2 return $_[0]->content_type_is('text/plain');
128 }
129  
130 sub is_html
131 2 ▶{
Subroutine is_html: called CC=1 SCAR=0.0
Pod documented
132 2 return $_[0]->content_type_is('text/html');
133 }
134  
135 sub is_json
136 101 ▶{
Subroutine is_json: called CC=1 SCAR=0.0
Pod documented
137 101 return $_[0]->content_type_is('application/json');
138 }
139  
140 sub is_xml
141 2 ▶{
Subroutine is_xml: called CC=1 SCAR=0.0
Pod documented
142 2 return $_[0]->content_type_is('application/xml');
143 }
144  
145 sub charset_decode
146 147 ▶{
Subroutine charset_decode: called CC=4 SCAR=13.9
Pod documented
147 147 my ($self, $string, $configured_only) = @_;
148 147 my $req_charset = $self->app->request_charset;
149  
150 # do not decode at all if the application is set no not decode
151 147 ▶ return $string unless $req_charset;
Branch: 1 branch
BranchTrueFalseTotal
unless $req_charset 2 145 147
152 145 ▶ return Kelp::Util::charset_decode(
Condition83%5/6
!$configured_only && Kelp::Util::effective_charset($self->charset) || $req_charset
l!l&&r!l&&!r
71380
not $configured_only and Kelp::Util::effective_charset($self->charset)
!ll&&!rl&&r
72667
MC/DC0%0/3
!$configured_only && Kelp::Util::effective_charset($self->charset) || $req_charset
not $configured_only Kelp::Util::effective_charset($self->charset) $req_charset
Truth table60%3/5
not(A) and B || C
A= not $configured_onlyB= Kelp::Util::effective_charset($self->charset)C= $req_charset
ABCresult
11X1
0X11
1011
0X00
1000
153 (!$configured_only && Kelp::Util::effective_charset($self->charset)) || $req_charset,
154 $string,
155 );
156 }
157  
158 sub _charset_decode_array
159 106 ▶{
Subroutine _charset_decode_array: called CC=1 SCAR=0.0
160 106 my ($self, $arr, $configured_only) = @_;
161  
162 106 return [map { $self->charset_decode($_, $configured_only) } @$arr];
163 }
164  
165 sub path
166 261 ▶{
Subroutine path: called CC=1 SCAR=0.0
Pod documented
167 261 my $self = shift;
168 261 return Kelp::Util::charset_decode($self->app->request_charset, $self->SUPER::path(@_));
169 }
170  
171 sub content
172 19 ▶{
Subroutine content: called CC=1 SCAR=0.0
Pod documented
173 19 my $self = shift;
174 19 return $self->charset_decode($self->SUPER::content(@_));
175 }
176  
177 sub json_content
178 20 ▶{
Subroutine json_content: called CC=2 SCAR=6.9
Pod documented
179 20 my $self = shift;
180 20 ▶ return undef unless $self->is_json;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_json 1 19 20
181  
182 19 ▶ return try {
Subroutine __ANON__: called CC=1 SCAR=0.0
183 19 $self->app->get_encoder(json => 'internal')->decode($self->content);
184 }
185 1 ▶ catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
186 1 undef;
187 19 };
188 }
189  
190 sub param
191 77 ▶{
Subroutine param: called CC=4 SCAR=13.9
Pod documented
192 77 my $self = shift;
193  
194 77 ▶ if ($self->is_json && $self->app->can('json')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_json and $self->app->can("json")) 20 57 77
Condition66%2/3
$self->is_json and $self->app->can("json")
!ll&&!rl&&r
57020
MC/DC50%1/2
$self->is_json and $self->app->can("json")
$self->is_json $self->app->can("json")
Truth table66%2/3
A and B
A= $self->is_jsonB= $self->app->can("json")
ABresult
0X0
100
111
195 20 return $self->json_param(@_);
196 }
197  
198 # safe method without calling Plack::Request::param
199 57 ▶ return $self->parameters->get($_[0]) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 50 7 57
200 7 return keys %{$self->parameters};
201 }
202  
203 sub cgi_param
204 0 ▶{
Subroutine cgi_param: not called CC=1 SCAR=6.9
Pod documented
205 0 shift->SUPER::param(@_);
206 }
207  
208 sub query_param
209 14 ▶{
Subroutine query_param: called CC=2 SCAR=9.2
Pod documented
210 14 my $self = shift;
211  
212 14 ▶ return $self->query_parameters->get($_[0]) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 14 0 14
213 0 return keys %{$self->query_parameters};
214 }
215  
216 sub body_param
217 2 ▶{
Subroutine body_param: called CC=2 SCAR=9.2
Pod documented
218 2 my $self = shift;
219  
220 2 ▶ return $self->body_parameters->get($_[0]) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 2 0 2
221 0 return keys %{$self->body_parameters};
222 }
223  
224 sub json_param
225 22 ▶{
Subroutine json_param: called CC=5 SCAR=16.2
Pod documented
226 22 my $self = shift;
227  
228 22 ▶ my $hash = $self->{_param_json_content} //= do {
Condition66%2/3
$self->{"_param_json_content"} //= do { my $hash = $self->json_content // {}; ref $hash eq "HASH" ? $hash : {ref $hash, $hash} }
l!l&&r!l&&!r
5170
MC/DC0%0/2
$self->{"_param_json_content"} //= do { my $hash = $self->json_content // {}; ref $hash eq "HASH" ? $hash : {ref $hash, $hash} }
$self->{"_param_json_content"} do { my $hash = $self->json_content // {}; ref $hash eq "HASH" ? $hash : {ref $hash, $hash} }
Truth table66%2/3
A //= B
A= $self->{"_param_json_content"}B= do { my $hash = $self->json_content // {}; ref $hash eq "HASH" ? $hash : {ref $hash, $hash} }
ABresult
1X1
011
000
229 17 ▶ my $hash = $self->json_content // {};
Condition100%2/2
$self->json_content // {}
l!l
161
MC/DC100%1/1
$self->json_content // {}
$self->json_content
Truth table100%2/2
A
A= $self->json_content
Aresult
11
00
230 17 ▶ ref $hash eq 'HASH' ? $hash : {ref $hash, $hash};
Branch: 1 branch
BranchTrueFalseTotal
ref $hash eq 'HASH' ? : 17 0 17
231 };
232  
233 22 ▶ return $hash->{$_[0]} if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 18 4 22
234 4 return keys %$hash;
235 }
236  
237 sub session
238 0 ▶{
Subroutine session: not called CC=6 SCAR=37.4
Pod documented
239 0 my $self = shift;
240 0 ▶ my $session = $self->env->{'psgix.session'}
Condition0%0/2
$self->env->{"psgix.session"} // croak("No Session middleware wrapped")
l!l
00
MC/DC0%0/1
$self->env->{"psgix.session"} // croak("No Session middleware wrapped")
$self->env->{"psgix.session"}
Truth table0%0/2
A
A= $self->env->{"psgix.session"}
Aresult
11
00
241 // croak "No Session middleware wrapped";
242  
243 0 ▶ return $session if !@_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
244  
245 0 ▶ if (@_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) 0 0 0
246 0 my $value = shift;
247 0 ▶ return $session->{$value} unless ref $value;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value 0 0 0
248 0 return $self->env->{'psgix.session'} = $value;
249 }
250  
251 0 my %hash = @_;
252 0 $session->{$_} = $hash{$_} for keys %hash;
253 0 return $session;
254 }
255  
256 29 1;
257  
258 __END__
« blib/lib/Kelp/Util.pm ↑ Summary blib/lib/Kelp/Routes/Pattern.pm »