Knork/Request.pm

filter: Statementstmt 55.1% 91 / 165 Branchbran 33.3% 16 / 48 Conditioncond 40.0% 14 / 35 MC/DCmcdc 5.2% 1 / 19 Subroutinesub 55.8% 19 / 34 Podpod 68.4% 13 / 19 totaltotal 48.1% 154 / 320 SCAR 57.2
CC
44
Cov
49%
_path
90.0
finalize
44.5
_form_data
12.0
CRAP 304.0
↑ Summary Knork.pm »
1 package Net::HTTP::Knork::Request;
2  
3 # ABSTRACT: HTTP request object from SPORE env hash
4  
5 6 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use HTTP::Headers;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use Net::HTTP::Knork::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 has env => (
15 is => 'rw',
16 required => 1,
17 0 ▶ default => sub { { } },
Subroutine __ANON__: not called CC=1 SCAR=6.9
18 6 );
19  
20 sub get_from_env {
21 16 ▶ return $_[0]->env->{$_[1]};
Subroutine get_from_env: called CC=1 SCAR=0.0
Pod undocumented
22 }
23  
24 sub set_to_env {
25 0 ▶ $_[0]->env->{$_[1]} = $_[2];
Subroutine set_to_env: not called CC=1 SCAR=6.9
Pod undocumented
26 }
27  
28 has path => (
29 is => 'rw',
30 lazy => 1,
31 0 ▶ default => sub { $_[0]->env->{PATH_INFO} }
Subroutine __ANON__: not called CC=1 SCAR=6.9
32 6 );
33  
34 has headers => (
35 is => 'rw',
36 lazy => 1,
37 handles => {
38 header => 'header',
39 },
40 default => sub {
41 8 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
42 8 my $env = $self->env;
43 my $h = HTTP::Headers->new(
44 map {
45 8 ( my $field = $_ ) =~ s/^HTTPS?_//;
46 8 ( $field => $env->{$_} );
47 8 } grep { /^(?:HTTP|CONTENT)/i } keys %$env
48 );
49 8 return $h;
50 },
51 6 );
52  
53 sub BUILDARGS {
54 8 ▶ my $class = shift;
Subroutine BUILDARGS: called CC=3 SCAR=13.3
Pod undocumented
55  
56 8 ▶ if (@_ == 1 && !exists $_[0]->{env}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 and not exists $_[0]{"env"}) 8 0 8
Condition33%1/3
@_ == 1 and not exists $_[0]{"env"}
!ll&&!rl&&r
008
MC/DC0%0/2
@_ == 1 and not exists $_[0]{"env"}
@_ == 1 not exists $_[0]{"env"}
Truth table33%1/3
A and not(B)
A= @_ == 1B= not exists $_[0]{"env"}
ABresult
0X0
100
111
57 8 return {env => $_[0]};
58 }
59 0 return @_;
60 }
61  
62 sub set_or_get_from_env {
63 16 ▶ my ( $self, $var, $value ) = @_;
Subroutine set_or_get_from_env: called CC=2 SCAR=8.4
Pod undocumented
64 16 ▶ if ($value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value) { } 0 16 16
65 0 $self->set_to_env( $var, $value );
66 0 return;
67 }
68 else {
69 16 return $self->get_from_env($var);
70 }
71 }
72  
73 sub method {
74 8 ▶ my ( $self, $value ) = @_;
Subroutine method: called CC=1 SCAR=0.0
Pod documented
75 8 return $self->set_or_get_from_env( 'REQUEST_METHOD', $value );
76 }
77  
78 sub host {
79 0 ▶ my ( $self, $value ) = @_;
Subroutine host: not called CC=1 SCAR=6.9
Pod undocumented
80 0 return $self->set_or_get_from_env( 'SERVER_NAME', $value );
81 }
82  
83 sub port {
84 0 ▶ my ( $self, $value ) = @_;
Subroutine port: not called CC=1 SCAR=6.9
Pod documented
85 0 return $self->set_or_get_from_env( 'SERVER_PORT', $value );
86 }
87  
88 sub script_name {
89 0 ▶ my ( $self, $value ) = @_;
Subroutine script_name: not called CC=1 SCAR=6.9
Pod documented
90 0 return $self->set_or_get_from_env( 'SCRIPT_NAME', $value );
91 }
92  
93 sub request_uri {
94 0 ▶ my ( $self, $value ) = @_;
Subroutine request_uri: not called CC=1 SCAR=6.9
Pod documented
95 0 return $self->set_or_get_from_env( 'REQUEST_URI', $value );
96 }
97  
98 sub scheme {
99 0 ▶ my ( $self, $value ) = @_;
Subroutine scheme: not called CC=1 SCAR=6.9
Pod documented
100 0 return $self->set_or_get_from_env( 'spore.url_scheme', $value );
101 }
102  
103 sub logger {
104 0 ▶ my ( $self, $value ) = @_;
Subroutine logger: not called CC=1 SCAR=6.9
Pod undocumented
105 0 return $self->set_or_get_from_env( 'sporex.logger', $value );
106 }
107  
108 sub body {
109 8 ▶ my ( $self, $value ) = @_;
Subroutine body: called CC=1 SCAR=0.0
Pod documented
110 8 return $self->set_or_get_from_env( 'spore.payload', $value );
111 }
112  
113 sub base {
114 0 ▶ my $self = shift;
Subroutine base: not called CC=1 SCAR=6.9
Pod documented
115 0 URI->new( $self->_uri_base )->canonical;
116 }
117  
118 0 ▶sub input { (shift)->body(@_) }
Subroutine input: not called CC=1 SCAR=6.9
Pod documented
119 8 ▶sub content { (shift)->body(@_) }
Subroutine content: called CC=1 SCAR=0.0
Pod documented
120 0 ▶sub secure { $_[0]->scheme eq 'https' }
Subroutine secure: not called CC=1 SCAR=6.9
Pod documented
121  
122 # TODO
123 # need to refactor this method, with path_info and query_string construction
124 sub uri {
125 8 ▶ my ($self, $path_info, $query_string) = @_;
Subroutine uri: called CC=7 SCAR=23.6
Pod documented
126  
127 8 ▶ if ( !defined $path_info || !defined $query_string ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $path_info and defined $query_string) 0 8 8
Condition33%1/3
defined $path_info and defined $query_string
!ll&&!rl&&r
008
MC/DC0%0/2
defined $path_info and defined $query_string
defined $path_info defined $query_string
Truth table33%1/3
A and B
A= defined $path_infoB= defined $query_string
ABresult
0X0
100
111
128 0 ($path_info,$query_string) = $self->_path;
129 }
130  
131 8 my $base = $self->_uri_base;
132  
133 8 my $path_escape_class = '^A-Za-z0-9\-\._~/';
134  
135 8 ▶ my $path = URI::Escape::uri_escape($path_info // '', $path_escape_class);
Condition50%1/2
$path_info // ""
l!l
80
MC/DC0%0/1
$path_info // ""
$path_info
Truth table50%1/2
A
A= $path_info
Aresult
11
00
136  
137 8 ▶ if (defined $query_string && length($query_string) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $query_string and length $query_string > 0) 0 8 8
Condition33%1/3
defined $query_string and length $query_string > 0
!ll&&!rl&&r
080
MC/DC0%0/2
defined $query_string and length $query_string > 0
defined $query_string length $query_string > 0
Truth table33%1/3
A and B
A= defined $query_stringB= length $query_string > 0
ABresult
0X0
100
111
138 0 $path .= '?' . $query_string;
139 }
140  
141 8 ▶ $base =~ s/\/$// if $path =~ m/^\//;
Branch: 1 branch
BranchTrueFalseTotal
if $path =~ m[^/] 8 0 8
142 8 return URI->new( $base . $path )->canonical;
143 }
144  
145 sub _path {
146 0 ▶ my $self = shift;
Subroutine _path: not called CC=9 SCAR=45.0
147  
148 0 my $query_string;
149 0 my $path = $self->env->{PATH_INFO};
150 0 ▶ my @params = @{ $self->env->{'spore.params'} || [] };
Branch: 1 branch
BranchTrueFalseTotal
$self->env->{"spore.params"} or [] 0 0 0
151  
152 0 my $j = 0;
153 0 ▶ for (my $i = 0; $i < scalar @params; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @params) 0 0 0
154 0 my $key = $params[$i];
155 0 my $value = $params[++$i];
156 0 ▶ if (!$value) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value) 0 0 0
157 0 $query_string .= $key;
158 0 last;
159 }
160 0 ▶ unless ( $path && $path =~ s/\:$key/$value/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($path and $path =~ s/\:$key/$value/) 0 0 0
Condition0%0/3
$path and $path =~ s/\:$key/$value/
!ll&&!rl&&r
000
MC/DC0%0/2
$path and $path =~ s/\:$key/$value/
$path $path =~ s/\:$key/$value/
Truth table0%0/3
A and B
A= $pathB= $path =~ s/\:$key/$value/
ABresult
0X0
100
111
161 0 $query_string .= $key . '=' . $value;
162 0 ▶ $query_string .= '&' if $query_string && scalar @params;
Branch: 1 branch
BranchTrueFalseTotal
if $query_string and scalar @params 0 0 0
Condition0%0/2
$query_string and scalar @params
!ll
00
MC/DC0%0/1
$query_string and scalar @params
$query_string
Truth table0%0/2
A
A= $query_string
Aresult
00
11
163 }
164 }
165  
166 0 ▶ $query_string =~ s/&$// if $query_string;
Branch: 1 branch
BranchTrueFalseTotal
if $query_string 0 0 0
167 0 return ( $path, $query_string );
168 }
169  
170 sub _uri_base {
171 8 ▶ my $self = shift;
Subroutine _uri_base: called CC=7 SCAR=22.6
172 8 my $env = $self->env;
173  
174 my $uri =
175 ( $env->{'spore.url_scheme'} || "http" ) . "://"
176 . (
177 defined $env->{'spore.userinfo'}
178 ? $env->{'spore.userinfo'} . '@'
179 : ''
180 )
181 . (
182 $env->{HTTP_HOST}
183 || (( $env->{SERVER_NAME} || "" ) . ":"
184 . ( $env->{SERVER_PORT} || 80 ) )
185 8 ▶ ) . ( $env->{SCRIPT_NAME} || '/' );
Branch: 1 branch
BranchTrueFalseTotal
defined $env->{'spore.userinfo'} ? : 0 8 8
Condition50%5/10
$env->{'spore.url_scheme'} || 'http'
l!l
80
$env->{'HTTP_HOST'} || ($env->{'SERVER_NAME'} || '') . ':' . ($env->{'SERVER_PORT'} || 80)
l!l
08
$env->{'SERVER_NAME'} || ''
l!l
80
$env->{'SERVER_PORT'} || 80
l!l
80
$env->{'SCRIPT_NAME'} || '/'
l!l
08
MC/DC0%0/1
$env->{'spore.url_scheme'} || 'http'
$env->{'spore.url_scheme'}
MC/DC0%0/1
$env->{'HTTP_HOST'} || ($env->{'SERVER_NAME'} || '') . ':' . ($env->{'SERVER_PORT'} || 80)
$env->{'HTTP_HOST'}
MC/DC0%0/1
$env->{'SERVER_PORT'} || 80
$env->{'SERVER_PORT'}
MC/DC0%0/1
$env->{'SCRIPT_NAME'} || '/'
$env->{'SCRIPT_NAME'}
Truth table50%1/2
A
A= $env->{'spore.url_scheme'}
Aresult
11
00
Truth table50%1/2
A
A= $env->{'HTTP_HOST'}
Aresult
11
00
Truth table50%1/2
A
A= $env->{'SERVER_PORT'}
Aresult
11
00
Truth table50%1/2
A
A= $env->{'SCRIPT_NAME'}
Aresult
11
00
186  
187 8 return $uri;
188 }
189  
190 # stolen from HTTP::Request::Common
191 sub _boundary {
192 0 ▶ my ( $self, $size ) = @_;
Subroutine _boundary: not called CC=2 SCAR=17.9
193  
194 0 ▶ return "xYzZy" unless $size;
Branch: 1 branch
BranchTrueFalseTotal
unless $size 0 0 0
195  
196 0 my $b =
197 MIME::Base64::encode( join( "", map chr( rand(256) ), 1 .. $size * 3 ),
198 "" );
199 0 $b =~ s/[\W]/X/g;
200 0 return $b;
201 }
202  
203 sub _form_data {
204 0 ▶ my ( $self, $data ) = @_;
Subroutine _form_data: not called CC=3 SCAR=24.8
205  
206 0 my $form_data;
207 0 foreach my $k ( keys %$data ) {
208 push @$form_data,
209 'Content-Disposition: form-data; name="'
210 . $k
211 . '"'."\r\n\r\n"
212 0 . $data->{$k};
213 }
214  
215 0 my $b = $self->_boundary(10);
216 0 my $t = [];
217 0 foreach (@$form_data) {
218 0 push @$t, '--', $b, "\r\n", $_, "\r\n";
219 }
220 0 push @$t, '--', $b, , '--', "\r\n";
221 0 my $content = join("", @$t);
222 0 return ($content, $b);
223 }
224  
225 sub new_response {
226 8 ▶ my $self = shift;
Subroutine new_response: called CC=1 SCAR=0.0
Pod documented
227 8 my $res = Net::HTTP::Knork::Response->new(@_);
228 8 $res->request($self);
229 8 $res;
230 }
231  
232 sub finalize {
233 8 ▶ my $self = shift;
Subroutine finalize: called CC=18 SCAR=38.0
Pod documented
234  
235 8 my $path_info = $self->env->{PATH_INFO};
236  
237 8 my $form_data = $self->env->{'spore.form_data'};
238 8 my $headers = $self->env->{'spore.headers'};
239 8 ▶ my $params = $self->env->{'spore.params'} || [];
Condition100%2/2
$self->env->{'spore.params'} || []
l!l
35
MC/DC100%1/1
$self->env->{'spore.params'} || []
$self->env->{'spore.params'}
Truth table100%2/2
A
A= $self->env->{'spore.params'}
Aresult
11
00
240  
241 8 my $query = [];
242 8 my $form = {};
243  
244 8 ▶ for ( my $i = 0 ; $i < scalar @$params ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @$params) 3 8 11
245 3 my $k = $params->[$i];
246 3 ▶ my $v = $params->[++$i] // '';
Condition50%1/2
$$params[++$i] // ""
l!l
30
MC/DC0%0/1
$$params[++$i] // ""
$$params[++$i]
Truth table50%1/2
A
A= $$params[++$i]
Aresult
11
00
247 3 my $modified = 0;
248  
249 3 ▶ if ($path_info && $path_info =~ s/\:$k/$v/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path_info and $path_info =~ s/\:$k/$v/) 3 0 3
Condition33%1/3
$path_info and $path_info =~ s/\:$k/$v/
!ll&&!rl&&r
003
MC/DC0%0/2
$path_info and $path_info =~ s/\:$k/$v/
$path_info $path_info =~ s/\:$k/$v/
Truth table33%1/3
A and B
A= $path_infoB= $path_info =~ s/\:$k/$v/
ABresult
0X0
100
111
250 3 $modified++;
251 }
252  
253 3 foreach my $f_k (keys %$form_data) {
254 0 my $f_v = $form_data->{$f_k};
255 0 ▶ if ($f_v =~ s/^\:$k/$v/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f_v =~ s/^\:$k/$v/) 0 0 0
256 0 $form->{$f_k} = $f_v;
257 0 $modified++;
258 }
259 }
260  
261 3 foreach my $h_k (keys %$headers) {
262 0 my $h_v = $headers->{$h_k};
263 0 ▶ if ($h_v =~ s/^\:$k/$v/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h_v =~ s/^\:$k/$v/) 0 0 0
264 0 $self->header($h_k => $h_v);
265 0 $modified++;
266 }
267 }
268  
269 3 ▶ if ($modified == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($modified == 0) 0 3 3
270 0 ▶ if (defined $v) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $v) { } 0 0 0
271 0 push @$query, $k.'='.$v;
272 }else{
273 0 push @$query, $k;
274 }
275 }
276 }
277  
278 # clean remaining :name in url
279 8 ▶ $path_info =~ s/:\w+//g if $path_info;
Branch: 1 branch
BranchTrueFalseTotal
if $path_info 8 0 8
280  
281 8 my $query_string;
282 8 ▶ if (scalar @$query) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$query) 0 8 8
283 0 $query_string = join('&', @$query);
284 }
285  
286 8 $self->env->{PATH_INFO} = $path_info;
287 8 $self->env->{QUERY_STRING} = $query_string;
288  
289 8 ▶ my $uri = $self->uri($path_info, $query_string || '');
Condition50%1/2
$query_string || ''
l!l
08
MC/DC0%0/1
$query_string || ''
$query_string
Truth table50%1/2
A
A= $query_string
Aresult
11
00
290  
291 8 my $request = HTTP::Request->new(
292 $self->method => $uri, $self->headers
293 );
294  
295 8 ▶ if ( keys %$form_data ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$form_data) 0 8 8
296 0 $self->env->{'spore.form_data'} = $form;
297 0 my ( $content, $b ) = $self->_form_data($form);
298 0 $request->content($content);
299 0 $request->header('Content-Length' => length($content));
300 0 $request->header(
301 'Content-Type' => 'multipart/form-data; boundary=' . $b );
302 }
303  
304 8 ▶ if ( my $payload = $self->content ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $payload = $self->content) 4 4 8
305 4 $request->content($payload);
306 4 ▶ $request->header(
Branch: 1 branch
BranchTrueFalseTotal
unless $request->header("Content-Type") 4 0 4
307 'Content-Type' => 'application/x-www-form-urlencoded' )
308 unless $request->header('Content-Type');
309 }
310  
311 8 return $request;
312 }
313  
314 6 1;
315  
316  
317  
318 =pod
319  
320 =head1 NAME
321  
322 Net::HTTP::Knork::Request - HTTP request object from SPORE env hash
323  
324 =head1 VERSION
325  
326 version 0.11
327  
328 =head1 SYNOPSIS
329  
330 use Net::HTTP::Knork::Request;
331  
332 my $request = Net::HTTP::Knork::Request->new($env);
333  
334 =head1 DESCRIPTION
335  
336 Net::HTTP::Knork::Request create a HTTP request
337 Based mostly on L<Net::HTTP::Spore::Request>, except that it uses Moo.
338  
339 =head1 METHODS
340  
341 =over 4
342  
343 =item new
344  
345 my $req = Net::HTTP::Knork::Request->new();
346  
347 Creates a new Net::HTTP::Knork::Request object.
348  
349 =item env
350  
351 my $env = $request->env;
352  
353 Get the environment for the given request
354  
355 =item method
356  
357 my $method = $request->method;
358  
359 Get the HTTP method for the given request
360  
361 =item port
362  
363 my $port = $request->port;
364  
365 Get the HTTP port from the URL
366  
367 =item script_name
368  
369 my $script_name = $request->script_name;
370  
371 Get the script name part from the URL
372  
373 =item path
374  
375 =item path_info
376  
377 my $path = $request->path_info;
378  
379 Get the path info part from the URL
380  
381 =item request_uri
382  
383 my $request_uri = $request->request_uri;
384  
385 Get the request uri from the URL
386  
387 =item scheme
388  
389 my $scheme = $request->scheme;
390  
391 Get the scheme from the URL
392  
393 =item secure
394  
395 my $secure = $request->secure;
396  
397 Return true if the URL is HTTPS
398  
399 =item content
400  
401 =item body
402  
403 =item input
404  
405 my $input = $request->input;
406  
407 Get the content that will be posted
408  
409 =item query_string
410  
411 =item headers
412  
413 =item header
414  
415 =item uri
416  
417 =item query_parameters
418  
419 =item base
420  
421 =item new_response
422  
423 =item finalize
424  
425 =back
426  
427 =head1 AUTHOR
428  
429 Emmanuel Peroumalnaïk
430  
431 =head1 COPYRIGHT AND LICENSE
432  
433 This software is copyright (c) 2014 by E. Peroumalnaik.
434  
435 This is free software; you can redistribute it and/or modify it under
436 the same terms as the Perl 5 programming language system itself.
437  
438 =cut
439  
440  
441 __END__
↑ Summary Knork.pm »