Server/Request.pm

filter: Statementstmt 97.3% 108 / 111 Branchbran 77.9% 67 / 86 Conditioncond 42.4% 14 / 33 MC/DCmcdc 15.0% 3 / 20 Subroutinesub 100% 17 / 17 Podpod 100% 7 / 7 totaltotal 78.8% 216 / 274 CC 59 SCAR 22.0
CC
59
Cov
82%
BUILDARGS
16.3
has_body
15.2
_normalize_query
10.3
CRAP 9.1
« Server/MirrorFetcher/HTTP.pm ↑ Summary Server/AuthorizationResult.pm »
1 package Net::Blossom::Server::Request;
2  
3 2 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 ▶use Net::Blossom::_ConstructorArgs ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Class::Tiny qw(method path body remote_addr content_length content_type _headers _query);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 my $TOKEN = qr/[!#$%&'*+\-.^_`|~0-9A-Za-z]+/;
12  
13 17 ▶sub BUILDARGS {
Subroutine BUILDARGS: called CC=16 SCAR=27.9
Pod documented
14 17 my $class = shift;
15 17 my %args = Net::Blossom::_ConstructorArgs::normalize(@_);
16 17 my %known = map { $_ => 1 } qw(method path query headers body remote_addr content_length content_type);
17 17 my @unknown = grep { !exists $known{$_} } keys %args;
18 17 ▶ croak "unknown argument(s): " . join(', ', sort @unknown) if @unknown;
Branch: 1 branch
BranchTrueFalseTotal
if @unknown 1 16 17
19  
20 16 _validate_required_scalar(\%args, 'method');
21 14 ▶ croak "method must be an HTTP token" unless $args{method} =~ /\A$TOKEN\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"method"} =~ /\A$TOKEN\z/ 1 13 14
22 13 $args{method} = uc $args{method};
23  
24 13 _validate_required_scalar(\%args, 'path');
25 12 ▶ croak "path must start with /" unless $args{path} =~ m{\A/};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"path"} =~ m[\A/] 1 11 12
26 11 ▶ croak "path must not contain a query string" if $args{path} =~ /\?/;
Branch: 1 branch
BranchTrueFalseTotal
if $args{"path"} =~ /\?/ 1 10 11
27  
28 10 ▶ $args{headers} = {} unless defined $args{headers};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"headers"} 5 5 10
29 10 my $headers = _normalize_headers($args{headers});
30  
31 7 ▶ $args{query} = {} unless defined $args{query};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"query"} 3 4 7
32 7 my $query = _normalize_query($args{query});
33  
34 5 ▶ if (!defined $args{content_type}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $args{'content_type'}) { } 5 0 5
elsif (ref $args{'content_type'}) { } 0 0 0
35 $args{content_type} = $headers->{'content-type'}{value}
36 5 ▶ if exists $headers->{'content-type'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $headers->{"content-type"} 1 4 5
37 }
38 elsif (ref($args{content_type})) {
39 0 croak "content_type must be a scalar";
40 }
41  
42 5 ▶ if (!defined $args{content_length}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"content_length"}) 4 1 5
43 $args{content_length} = $headers->{'content-length'}{value}
44 4 ▶ if exists $headers->{'content-length'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $headers->{"content-length"} 1 3 4
45 }
46 5 ▶ _validate_content_length($args{content_length}) if defined $args{content_length};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"content_length"} 2 3 5
47  
48 4 ▶ croak "remote_addr must be a scalar" if defined $args{remote_addr} && ref($args{remote_addr});
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"remote_addr"} and ref $args{"remote_addr"} 0 4 4
Condition66%2/3
defined $args{"remote_addr"} and ref $args{"remote_addr"}
!ll&&!rl&&r
310
MC/DC0%0/2
defined $args{"remote_addr"} and ref $args{"remote_addr"}
defined $args{"remote_addr"} ref $args{"remote_addr"}
Truth table66%2/3
A and B
A= defined $args{"remote_addr"}B= ref $args{"remote_addr"}
ABresult
0X0
100
111
49 4 ▶ _validate_body($args{body}) if defined $args{body};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"body"} 3 1 4
50  
51 3 $args{_headers} = $headers;
52 3 $args{_query} = $query;
53 3 delete @args{qw(headers query)};
54 3 return \%args;
55 }
56  
57 1 ▶sub headers {
Subroutine headers: called CC=1 SCAR=0.0
Pod documented
58 1 my ($self) = @_;
59 1 return { map { $self->_headers->{$_}{name} => $self->_headers->{$_}{value} } keys %{$self->_headers} };
60 }
61  
62 4 ▶sub header {
Subroutine header: called CC=3 SCAR=11.0
Pod documented
63 4 my ($self, $name) = @_;
64 4 ▶ return undef unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 4 4
65 4 my $header = $self->_headers->{lc $name};
66 4 ▶ return defined $header ? $header->{value} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $header ? : 3 1 4
67 }
68  
69 1 ▶sub query {
Subroutine query: called CC=3 SCAR=11.0
Pod documented
70 1 my ($self) = @_;
71 1 my %query;
72 1 for my $name (keys %{$self->_query}) {
73 2 my $value = $self->_query->{$name};
74 2 ▶ $query{$name} = ref($value) eq 'ARRAY' ? [@$value] : $value;
Branch: 1 branch
BranchTrueFalseTotal
ref $value eq 'ARRAY' ? : 1 1 2
75 }
76 1 return \%query;
77 }
78  
79 3 ▶sub query_param {
Subroutine query_param: called CC=4 SCAR=14.6
Pod documented
80 3 my ($self, $name) = @_;
81 3 ▶ return undef unless defined $name && exists $self->_query->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and exists $self->_query->{$name} 0 3 3
Condition33%1/3
defined $name and exists $self->_query->{$name}
!ll&&!rl&&r
003
MC/DC0%0/2
defined $name and exists $self->_query->{$name}
defined $name exists $self->_query->{$name}
Truth table33%1/3
A and B
A= defined $nameB= exists $self->_query->{$name}
ABresult
0X0
100
111
82 3 my $value = $self->_query->{$name};
83 3 ▶ return ref($value) eq 'ARRAY' ? $value->[0] : $value;
Branch: 1 branch
BranchTrueFalseTotal
ref $value eq 'ARRAY' ? : 1 2 3
84 }
85  
86 2 ▶sub query_params {
Subroutine query_params: called CC=4 SCAR=15.6
Pod documented
87 2 my ($self, $name) = @_;
88 2 ▶ return () unless defined $name && exists $self->_query->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and exists $self->_query->{$name} 0 2 2
Condition33%1/3
defined $name and exists $self->_query->{$name}
!ll&&!rl&&r
002
MC/DC0%0/2
defined $name and exists $self->_query->{$name}
defined $name exists $self->_query->{$name}
Truth table33%1/3
A and B
A= defined $nameB= exists $self->_query->{$name}
ABresult
0X0
100
111
89 2 my $value = $self->_query->{$name};
90 2 ▶ return ref($value) eq 'ARRAY' ? @$value : ($value);
Branch: 1 branch
BranchTrueFalseTotal
ref $value eq 'ARRAY' ? : 2 0 2
91 }
92  
93 2 ▶sub has_body {
Subroutine has_body: called CC=7 SCAR=27.2
Pod documented
94 2 my ($self) = @_;
95 2 ▶ return 1 if defined $self->body && ref($self->body);
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->body and ref $self->body 1 1 2
Condition66%2/3
defined $self->body and ref $self->body
!ll&&!rl&&r
011
MC/DC50%1/2
defined $self->body and ref $self->body
defined $self->body ref $self->body
Truth table66%2/3
A and B
A= defined $self->bodyB= ref $self->body
ABresult
0X0
100
111
96 1 ▶ return 1 if defined $self->body && length $self->body;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->body and length $self->body 1 0 1
Condition33%1/3
defined $self->body and length $self->body
!ll&&!rl&&r
001
MC/DC0%0/2
defined $self->body and length $self->body
defined $self->body length $self->body
Truth table33%1/3
A and B
A= defined $self->bodyB= length $self->body
ABresult
0X0
100
111
97 0 ▶ return 1 if defined $self->content_length && $self->content_length > 0;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->content_length and $self->content_length > 0 0 0 0
Condition0%0/3
defined $self->content_length and $self->content_length > 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->content_length and $self->content_length > 0
defined $self->content_length $self->content_length > 0
Truth table0%0/3
A and B
A= defined $self->content_lengthB= $self->content_length > 0
ABresult
0X0
100
111
98 0 return 0;
99 }
100  
101 29 ▶sub _validate_required_scalar {
Subroutine _validate_required_scalar: called CC=5 SCAR=16.3
102 29 my ($args, $field) = @_;
103 29 ▶ croak "$field is required" unless exists $args->{$field} && defined $args->{$field};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args->{$field} and defined $args->{$field} 2 27 29
Condition66%2/3
exists $args->{$field} and defined $args->{$field}
!ll&&!rl&&r
2027
MC/DC50%1/2
exists $args->{$field} and defined $args->{$field}
exists $args->{$field} defined $args->{$field}
Truth table66%2/3
A and B
A= exists $args->{$field}B= defined $args->{$field}
ABresult
0X0
100
111
104 27 ▶ croak "$field must be a scalar" if ref($args->{$field});
Branch: 1 branch
BranchTrueFalseTotal
if ref $args->{$field} 1 26 27
105 26 ▶ croak "$field is required" unless length $args->{$field};
Branch: 1 branch
BranchTrueFalseTotal
unless length $args->{$field} 0 26 26
106 }
107  
108 10 ▶sub _normalize_headers {
Subroutine _normalize_headers: called CC=9 SCAR=22.9
109 10 my ($headers) = @_;
110 10 ▶ croak "headers must be a hash reference" unless ref($headers) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $headers eq "HASH" 1 9 10
111  
112 9 my %normalized;
113 9 for my $name (keys %$headers) {
114 7 ▶ croak "header names must be HTTP tokens"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and not ref $name and $name =~ /\A$TOKEN\z/ 0 7 7
Condition33%2/6
defined $name and not ref $name and $name =~ /\A$TOKEN\z/
!ll&&!rl&&r
007
defined $name and not ref $name
!ll&&!rl&&r
007
MC/DC0%0/3
defined $name and not ref $name and $name =~ /\A$TOKEN\z/
defined $name not ref $name $name =~ /\A$TOKEN\z/
Truth table25%1/4
A and not(B) and C
A= defined $nameB= not ref $nameC= $name =~ /\A$TOKEN\z/
ABCresult
0XX0
10X0
1100
1111
115 unless defined $name && !ref($name) && $name =~ /\A$TOKEN\z/;
116 7 ▶ croak "header values must be defined" unless defined $headers->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $headers->{$name} 0 7 7
117 7 ▶ croak "header values must be scalars" if ref($headers->{$name});
Branch: 1 branch
BranchTrueFalseTotal
if ref $headers->{$name} 1 6 7
118  
119 6 my $lower = lc $name;
120 6 ▶ croak "duplicate header: $name" if exists $normalized{$lower};
Branch: 1 branch
BranchTrueFalseTotal
if exists $normalized{$lower} 1 5 6
121 $normalized{$lower} = {
122 name => $name,
123 5 value => $headers->{$name},
124 };
125 }
126  
127 7 return \%normalized;
128 }
129  
130 7 ▶sub _normalize_query {
Subroutine _normalize_query: called CC=10 SCAR=23.3
131 7 my ($query) = @_;
132 7 ▶ croak "query must be a hash reference" unless ref($query) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $query eq "HASH" 1 6 7
133  
134 6 my %normalized;
135 6 for my $name (keys %$query) {
136 5 ▶ croak "query parameter names must be defined" unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 5 5
137 5 my $value = $query->{$name};
138 5 ▶ if (ref($value) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq 'ARRAY') { } 3 2 5
139 3 for my $item (@$value) {
140 4 ▶ croak "query values must be defined" unless defined $item;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $item 0 4 4
141 4 ▶ croak "query values must be scalars" if ref($item);
Branch: 1 branch
BranchTrueFalseTotal
if ref $item 1 3 4
142 }
143 2 $normalized{$name} = [@$value];
144 }
145 else {
146 2 ▶ croak "query values must be defined" unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 2 2
147 2 ▶ croak "query values must be scalars" if ref($value);
Branch: 1 branch
BranchTrueFalseTotal
if ref $value 0 2 2
148 2 $normalized{$name} = $value;
149 }
150 }
151  
152 5 return \%normalized;
153 }
154  
155 2 ▶sub _validate_content_length {
Subroutine _validate_content_length: called CC=3 SCAR=11.1
156 2 my ($content_length) = @_;
157 2 ▶ croak "content_length must be a scalar" if ref($content_length);
Branch: 1 branch
BranchTrueFalseTotal
if ref $content_length 0 2 2
158 2 ▶ croak "content_length must be a non-negative integer"
Branch: 1 branch
BranchTrueFalseTotal
unless $content_length =~ /\A\d+\z/ 1 1 2
159 unless $content_length =~ /\A\d+\z/;
160 }
161  
162 3 ▶sub _validate_body {
Subroutine _validate_body: called CC=5 SCAR=16.6
163 3 my ($body) = @_;
164 3 ▶ return unless ref($body);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $body 1 2 3
165 2 ▶ return if blessed($body) && ($body->can('read') || $body->can('getline'));
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($body) and $body->can("read") || $body->can("getline") 1 1 2
Condition50%3/6
builtin::blessed($body) and $body->can("read") || $body->can("getline")
!ll&&!rl&&r
101
$body->can("read") or $body->can("getline")
l!l&&r!l&&!r
100
MC/DC33%1/3
builtin::blessed($body) and $body->can("read") || $body->can("getline")
builtin::blessed($body) $body->can("read") $body->can("getline")
Truth table50%2/4
A and B or C
A= builtin::blessed($body)B= $body->can("read")C= $body->can("getline")
ABCresult
0XX0
1000
11X1
1011
166 1 croak "body must be a scalar or stream object";
167 }
168  
169 2 1;
170  
171 =pod
172  
173 =head1 NAME
174  
175 Net::Blossom::Server::Request - Framework-neutral Blossom server request
176  
177 =head1 SYNOPSIS
178  
179 use Net::Blossom::Server::Request;
180  
181 my $request = Net::Blossom::Server::Request->new(
182 method => 'PUT',
183 path => '/upload',
184 headers => {
185 'Content-Type' => 'image/png',
186 'Content-Length' => 1024,
187 },
188 body => $stream,
189 );
190  
191 my $type = $request->content_type;
192  
193 =head1 DESCRIPTION
194  
195 C<Net::Blossom::Server::Request> is the normalized request object used by
196 C<Net::Blossom::Server>. It is intentionally independent of PSGI, PAGI, Plack,
197 and any daemon framework.
198  
199 Adapters should translate their native request representation into this object
200 before calling the Blossom server core.
201  
202 =head1 CONSTRUCTOR
203  
204 =head2 new
205  
206 my $request = Net::Blossom::Server::Request->new(%args);
207  
208 Required arguments:
209  
210 =over 4
211  
212 =item * C<method>
213  
214 HTTP method. It must be an HTTP token and is normalized to uppercase.
215  
216 =item * C<path>
217  
218 Request path. It must start with C</> and must not contain a query string.
219  
220 =back
221  
222 Optional arguments:
223  
224 =over 4
225  
226 =item * C<headers>
227  
228 Hash reference of HTTP request headers. Header names are case-insensitive and
229 duplicate case-insensitive names croak.
230  
231 =item * C<query>
232  
233 Hash reference of query parameters. Values may be scalars or array references of
234 scalars.
235  
236 =item * C<body>
237  
238 Request body. It may be a scalar byte string or a stream object that provides
239 C<read> or C<getline>.
240  
241 =item * C<remote_addr>
242  
243 Client address as provided by the adapter.
244  
245 =item * C<content_length>
246  
247 Content length. Defaults from the C<Content-Length> header when present. It must
248 be a non-negative integer.
249  
250 =item * C<content_type>
251  
252 Content type. Defaults from the C<Content-Type> header when present.
253  
254 =back
255  
256 Unknown arguments or invalid values croak.
257  
258 =head1 ACCESSORS
259  
260 =head2 method
261  
262 Returns the uppercase HTTP method.
263  
264 =head2 path
265  
266 Returns the request path.
267  
268 =head2 body
269  
270 Returns the scalar body or stream object.
271  
272 =head2 remote_addr
273  
274 Returns the adapter-provided client address.
275  
276 =head2 content_length
277  
278 Returns the content length, when known.
279  
280 =head2 content_type
281  
282 Returns the content type, when known.
283  
284 =head1 METHODS
285  
286 =head2 headers
287  
288 my $headers = $request->headers;
289  
290 Returns a copy hash reference of request headers.
291  
292 =head2 header
293  
294 my $value = $request->header($name);
295  
296 Returns a header value using case-insensitive lookup. Returns C<undef> when the
297 header is absent.
298  
299 =head2 query
300  
301 my $query = $request->query;
302  
303 Returns a copy hash reference of query parameters. Repeated values are returned
304 as copied array references.
305  
306 =head2 query_param
307  
308 my $value = $request->query_param($name);
309  
310 Returns the first query value for C<$name>, or C<undef> when absent.
311  
312 =head2 query_params
313  
314 my @values = $request->query_params($name);
315  
316 Returns all query values for C<$name>.
317  
318 =head2 has_body
319  
320 my $has_body = $request->has_body;
321  
322 Returns true when the request has a scalar body, a stream body, or a positive
323 C<content_length>.
324  
325 =head1 INTERNAL METHODS
326  
327 =head2 BUILDARGS
328  
329 Normalizes constructor arguments for Class::Tiny.
330  
331 =cut
« Server/MirrorFetcher/HTTP.pm ↑ Summary Server/AuthorizationResult.pm »