Spore/Request.pm

filter: Statementstmt 89.6% 174 / 194 Branchbran 73.6% 56 / 76 Conditioncond 78.3% 29 / 37 MC/DCmcdc 60.0% 12 / 20 Subroutinesub 84.3% 27 / 32 Podpod 87.5% 14 / 16 totaltotal 83.2% 312 / 375 CC 59 SCAR 21.3
CC
59
Cov
84%
finalize
20.0
_path
10.2
uri
10.2
CRAP 8.4
« Spore/Middleware/Format/Auto.pm ↑ Summary Spore/Role/Debug.pm »
1 package Net::HTTP::Spore::Request;
2 34 $Net::HTTP::Spore::Request::VERSION = '0.09';
3 # ABSTRACT: Net::HTTP::Spore::Request - Portable HTTP request object from SPORE env hash
4  
5 34 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 34 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 34 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 34 ▶use HTTP::Headers;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 34 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 34 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 34 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 34 ▶use Net::HTTP::Spore::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 34 ▶use Encode qw{is_utf8};
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 34 has env => (
17 is => 'rw',
18 isa => 'HashRef',
19 required => 1,
20 traits => ['Hash'],
21 handles => {
22 set_to_env => 'set',
23 get_from_env => 'get',
24 }
25 );
26  
27 has path => (
28 is => 'rw',
29 isa => 'Str',
30 lazy => 1,
31 0 ▶ default => sub { $_[0]->env->{PATH_INFO} }
Subroutine __ANON__: not called CC=1 SCAR=6.9
32 34 );
33  
34 has headers => (
35 is => 'rw',
36 isa => 'HTTP::Headers',
37 lazy => 1,
38 handles => {
39 header => 'header',
40 },
41 38 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
42 38 my $self = shift;
43 38 my $env = $self->env;
44 my $h = HTTP::Headers->new(
45 map {
46 35 ( my $field = $_ ) =~ s/^HTTPS?_//;
47 35 ( $field => $env->{$_} );
48 38 } grep { /^(?:HTTP|CONTENT)/i } keys %$env
49 );
50 38 return $h;
51 },
52 34 );
53  
54 51 ▶sub BUILDARGS {
Subroutine BUILDARGS: called CC=3 SCAR=13.3
Pod documented
55 51 my $class = shift;
56  
57 51 ▶ if ( @_ == 1 && !exists $_[0]->{env} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 and not exists $_[0]{"env"}) 51 0 51
Condition33%1/3
@_ == 1 and not exists $_[0]{"env"}
!ll&&!rl&&r
0051
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
58 51 return { env => $_[0] };
59 }
60 0 return @_;
61 }
62  
63 17 ▶sub _safe_uri_escape {
Subroutine _safe_uri_escape: called CC=3 SCAR=12.0
64 17 my ( $self, $str, $unsafe ) = @_;
65 17 ▶ return unless defined $str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str 0 17 17
66 17 ▶ if ( is_utf8($str) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&is_utf8($str)) 0 17 17
67 0 utf8::encode($str);
68 }
69 17 return uri_escape( $str, $unsafe );
70 }
71  
72 48 ▶sub method {
Subroutine method: called CC=2 SCAR=7.6
Pod documented
73 48 my ( $self, $value ) = @_;
74 48 ▶ if ($value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value) { } 0 48 48
75 0 $self->set_to_env( 'REQUEST_METHOD', $value );
76 }
77 else {
78 48 return $self->get_from_env('REQUEST_METHOD');
79 }
80 }
81  
82 0 ▶sub host {
Subroutine host: not called CC=2 SCAR=17.9
Pod undocumented
83 0 my ($self, $value) = @_;
84 0 ▶ if ($value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value) { } 0 0 0
85 0 $self->set_to_env('SERVER_NAME', $value);
86 }else{
87 0 return $self->get_from_env('SERVER_NAME');
88 }
89 }
90  
91 4 ▶sub port {
Subroutine port: called CC=2 SCAR=7.6
Pod documented
92 4 my ( $self, $value ) = @_;
93 4 ▶ if ($value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value) { } 0 4 4
94 0 $self->set_to_env( 'SERVER_PORT', $value );
95 }
96 else {
97 4 return $self->get_from_env('SERVER_PORT');
98 }
99 }
100  
101 2 ▶sub script_name {
Subroutine script_name: called CC=2 SCAR=7.6
Pod documented
102 2 my ( $self, $value ) = @_;
103 2 ▶ if ($value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value) { } 0 2 2
104 0 $self->set_to_env( 'SCRIPT_NAME', $value );
105 }
106 else {
107 2 return $self->get_from_env('SCRIPT_NAME');
108 }
109 }
110  
111 2 ▶sub request_uri {
Subroutine request_uri: called CC=2 SCAR=7.6
Pod documented
112 2 my ($self, $value) = @_;
113 2 ▶ if ($value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value) { } 0 2 2
114 0 $self->set_to_env( 'REQUEST_URI', $value );
115 }
116 else {
117 2 return $self->get_from_env('REQUEST_URI');
118 }
119 }
120  
121 4 ▶sub scheme {
Subroutine scheme: called CC=2 SCAR=7.6
Pod documented
122 4 my ($self, $value) = @_;
123 4 ▶ if ($value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value) { } 0 4 4
124 0 $self->set_to_env( 'spore.url_scheme', $value );
125 }
126 else {
127 4 return $self->get_from_env('spore.url_scheme');
128 }
129 }
130  
131 0 ▶sub logger {
Subroutine logger: not called CC=2 SCAR=17.9
Pod undocumented
132 0 my ($self, $value) = @_;
133 0 ▶ if ($value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value) { } 0 0 0
134 0 $self->set_to_env( 'sporex.logger', $value );
135 }
136 else {
137 0 return $self->get_from_env('sporex.logger');
138 }
139 }
140  
141 47 ▶sub body {
Subroutine body: called CC=2 SCAR=7.6
Pod documented
142 47 my ($self, $value) = @_;
143 47 ▶ if ($value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value) { } 0 47 47
144 0 $self->set_to_env( 'spore.payload', $value );
145 }
146 else {
147 47 return $self->get_from_env('spore.payload');
148 }
149 }
150  
151 9 ▶sub base {
Subroutine base: called CC=1 SCAR=0.0
Pod documented
152 9 my $self = shift;
153 9 URI->new( $self->_uri_base )->canonical;
154 }
155  
156 0 ▶sub input { (shift)->body(@_) }
Subroutine input: not called CC=1 SCAR=6.9
Pod documented
157 42 ▶sub content { (shift)->body(@_) }
Subroutine content: called CC=1 SCAR=0.0
Pod documented
158 0 ▶sub secure { $_[0]->scheme eq 'https' }
Subroutine secure: not called CC=1 SCAR=6.9
Pod documented
159  
160 # TODO
161 # need to refactor this method, with path_info and query_string construction
162 43 ▶sub uri {
Subroutine uri: called CC=10 SCAR=23.2
Pod documented
163 43 my ($self, $path_info, $query_string) = @_;
164  
165 43 ▶ if ( !defined $path_info || !defined $query_string ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $path_info and defined $query_string) 9 34 43
Condition66%2/3
defined $path_info and defined $query_string
!ll&&!rl&&r
9034
MC/DC50%1/2
defined $path_info and defined $query_string
defined $path_info defined $query_string
Truth table66%2/3
A and B
A= defined $path_infoB= defined $query_string
ABresult
0X0
100
111
166 9 my @path_info = $self->_path;
167 9 ▶ $path_info = $path_info[0] if !$path_info;
Branch: 1 branch
BranchTrueFalseTotal
unless $path_info 9 0 9
168 9 ▶ $query_string = $path_info[1] if !$query_string;
Branch: 1 branch
BranchTrueFalseTotal
unless $query_string 7 2 9
169 }
170  
171 43 ▶ $path_info = $path_info // '';
Condition100%2/2
$path_info // ""
l!l
349
MC/DC100%1/1
$path_info // ""
$path_info
Truth table100%2/2
A
A= $path_info
Aresult
11
00
172 43 my $base = $self->_uri_base;
173  
174 43 ▶ if ( defined $query_string && length($query_string) > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $query_string and length $query_string > 0) 9 34 43
Condition100%3/3
defined $query_string and length $query_string > 0
!ll&&!rl&&r
6289
MC/DC100%2/2
defined $query_string and length $query_string > 0
defined $query_string length $query_string > 0
Truth table100%3/3
A and B
A= defined $query_stringB= length $query_string > 0
ABresult
0X0
100
111
175 9 my $is_interrogation = index( $path_info, '?' );
176 9 ▶ if ( $is_interrogation >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_interrogation >= 0) { } 0 9 9
177 0 $path_info .= '&' . $query_string;
178 }
179 else {
180 9 $path_info .= '?' . $query_string;
181 }
182 }
183  
184 43 ▶ $base =~ s!/$!! if $path_info =~ m!^/!;
Branch: 1 branch
BranchTrueFalseTotal
if $path_info =~ m[^/] 34 9 43
185 43 return URI->new( $base . $path_info )->canonical;
186 }
187  
188 9 ▶sub _path {
Subroutine _path: called CC=10 SCAR=23.3
189 9 my $self = shift;
190  
191 9 my $query_string;
192 9 my $path = $self->env->{PATH_INFO};
193 9 ▶ my @params = @{ $self->env->{'spore.params'} || [] };
Condition100%2/2
$self->env->{'spore.params'} || []
l!l
36
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
194  
195 9 my $j = 0;
196 9 ▶ for ( my $i = 0; $i < scalar @params; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @params) 3 8 11
197 3 my $key = $params[$i];
198 3 my $value = $params[++$i];
199  
200 3 ▶ $value = (defined $value) ? $value : '' ;
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 2 1 3
201 3 ▶ if (! length($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $value) 1 2 3
202 1 $query_string .= $key;
203 1 last;
204 }
205  
206 # add params as string vide to query_string even it's undefined
207 2 ▶ unless ( $path && $path =~ s/\:$key/$value/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($path and $path =~ s/\:$key/$value/) 2 0 2
Condition33%1/3
$path and $path =~ s/\:$key/$value/
!ll&&!rl&&r
200
MC/DC0%0/2
$path and $path =~ s/\:$key/$value/
$path $path =~ s/\:$key/$value/
Truth table33%1/3
A and B
A= $pathB= $path =~ s/\:$key/$value/
ABresult
0X0
100
111
208 2 $query_string .= $key . '=' . $self->_safe_uri_escape($value);
209 2 ▶ $query_string .= '&' if $query_string && scalar @params;
Branch: 1 branch
BranchTrueFalseTotal
if $query_string and scalar @params 2 0 2
Condition50%1/2
$query_string and scalar @params
!ll
02
MC/DC0%0/1
$query_string and scalar @params
$query_string
Truth table50%1/2
A
A= $query_string
Aresult
00
11
210 }
211 }
212  
213 9 ▶ $query_string =~ s/&$// if $query_string;
Branch: 1 branch
BranchTrueFalseTotal
if $query_string 2 7 9
214 9 $self->env->{QUERY_STRING} = $query_string;
215  
216 9 return ( $path, $query_string );
217 }
218  
219 52 ▶sub _uri_base {
Subroutine _uri_base: called CC=7 SCAR=19.6
220 52 my $self = shift;
221 52 my $env = $self->env;
222  
223 my $uri =
224 ( $env->{'spore.url_scheme'} || "http" ) . "://"
225 . (
226 defined $env->{'spore.userinfo'}
227 ? $env->{'spore.userinfo'} . '@'
228 : ''
229 )
230 . (
231 $env->{HTTP_HOST}
232 || (( $env->{SERVER_NAME} || "" ) . ":"
233 . ( $env->{SERVER_PORT} || 80 ) )
234 52 ▶ ) . ( $env->{SCRIPT_NAME} || '/' );
Branch: 1 branch
BranchTrueFalseTotal
defined $env->{'spore.userinfo'} ? : 0 52 52
Condition90%9/10
$env->{'spore.url_scheme'} || 'http'
l!l
3913
$env->{'HTTP_HOST'} || ($env->{'SERVER_NAME'} || '') . ':' . ($env->{'SERVER_PORT'} || 80)
l!l
1735
$env->{'SERVER_NAME'} || ''
l!l
341
$env->{'SERVER_PORT'} || 80
l!l
350
$env->{'SCRIPT_NAME'} || '/'
l!l
943
MC/DC100%1/1
$env->{'spore.url_scheme'} || 'http'
$env->{'spore.url_scheme'}
MC/DC100%1/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/DC100%1/1
$env->{'SCRIPT_NAME'} || '/'
$env->{'SCRIPT_NAME'}
Truth table100%2/2
A
A= $env->{'spore.url_scheme'}
Aresult
11
00
Truth table100%2/2
A
A= $env->{'HTTP_HOST'}
Aresult
11
00
Truth table50%1/2
A
A= $env->{'SERVER_PORT'}
Aresult
11
00
Truth table100%2/2
A
A= $env->{'SCRIPT_NAME'}
Aresult
11
00
235  
236 52 return $uri;
237 }
238  
239 # stolen from HTTP::Request::Common
240 2 ▶sub _boundary {
Subroutine _boundary: called CC=2 SCAR=7.0
241 2 my ( $self, $size ) = @_;
242  
243 2 ▶ return "xYzZy" unless $size;
Branch: 1 branch
BranchTrueFalseTotal
unless $size 0 2 2
244  
245 2 my $b =
246 MIME::Base64::encode( join( "", map chr( rand(256) ), 1 .. $size * 3 ),
247 "" );
248 2 $b =~ s/[\W]/X/g;
249 2 return $b;
250 }
251  
252 2 ▶sub _form_data {
Subroutine _form_data: called CC=3 SCAR=11.0
253 2 my ( $self, $data ) = @_;
254  
255 2 my $form_data;
256 2 foreach my $k ( keys %$data ) {
257 push @$form_data,
258 'Content-Disposition: form-data; name="'
259 . $k
260 . '"'."\r\n\r\n"
261 1 . $data->{$k};
262 }
263  
264 2 my $b = $self->_boundary(10);
265 2 my $t = [];
266 2 foreach (@$form_data) {
267 1 push @$t, '--', $b, "\r\n", $_, "\r\n";
268 }
269 2 push @$t, '--', $b, , '--', "\r\n";
270 2 my $content = join("", @$t);
271 2 return ($content, $b);
272 }
273  
274 24 ▶sub new_response {
Subroutine new_response: called CC=1 SCAR=0.0
Pod documented
275 24 my $self = shift;
276 24 my $res = Net::HTTP::Spore::Response->new(@_);
277 24 $res->request($self);
278 24 $res;
279 }
280  
281 42 ▶sub finalize {
Subroutine finalize: called CC=20 SCAR=30.0
Pod documented
282 42 my $self = shift;
283  
284 42 my $path_info = $self->env->{PATH_INFO};
285  
286 42 my $form_data = $self->env->{'spore.form_data'};
287 42 my $headers = $self->env->{'spore.headers'};
288 42 ▶ my $params = $self->env->{'spore.params'} || [];
Condition100%2/2
$self->env->{'spore.params'} || []
l!l
1131
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
289  
290 42 my $query = [];
291 42 my $form = {};
292  
293 42 my $path_escape_class = '^A-Za-z0-9\-\._~/@\:';
294  
295 42 ▶ for ( my $i = 0 ; $i < scalar @$params ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @$params) 16 42 58
296 16 my $k = $params->[$i];
297 16 my $v = $params->[++$i];
298 16 ▶ $v = $self->_safe_uri_escape($v || '', $path_escape_class) if defined $v;
Branch: 1 branch
BranchTrueFalseTotal
if defined $v 15 1 16
Condition50%1/2
$v || ''
l!l
150
MC/DC0%0/1
$v || ''
$v
Truth table50%1/2
A
A= $v
Aresult
11
00
299 16 my $modified = 0;
300  
301 16 ▶ if ($path_info && $path_info =~ s/\:$k/=$k/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path_info and $path_info =~ s/\:$k/=$k/) 3 13 16
Condition100%3/3
$path_info and $path_info =~ s/\:$k/=$k/
!ll&&!rl&&r
3103
MC/DC100%2/2
$path_info and $path_info =~ s/\:$k/=$k/
$path_info $path_info =~ s/\:$k/=$k/
Truth table100%3/3
A and B
A= $path_infoB= $path_info =~ s/\:$k/=$k/
ABresult
0X0
100
111
302 3 $modified++;
303 }
304  
305 16 foreach my $f_k (keys %$form_data) {
306 2 my $f_v = $form_data->{$f_k};
307 2 ▶ if ($f_v =~ s/^\:$k/$v/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f_v =~ s/^\:$k/$v/) 1 1 2
308 1 $form->{$f_k} = $f_v;
309 1 $modified++;
310 }
311 }
312  
313 16 foreach my $h_k (keys %$headers) {
314 4 my $h_v = $headers->{$h_k};
315 4 ▶ if ($h_v =~ s/^\:$k/$v/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h_v =~ s/^\:$k/$v/) 2 2 4
316 2 $self->header($h_k => $h_v);
317 2 $modified++;
318 }
319 }
320  
321 16 ▶ if ($modified == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($modified == 0) 10 6 16
322 10 ▶ if (defined $v) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $v) { } 9 1 10
323 9 push @$query, $k.'='.$v;
324 }else{
325 1 push @$query, $k;
326 }
327 }
328 }
329  
330 # clean remaining :name in url
331 42 ▶ if ($path_info) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path_info) 34 8 42
332 34 $path_info =~ s/:\w+//g;
333  
334 34 ▶ for (my $i = 0; $i < @$params; $i+=2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$params) 13 34 47
335 13 my ($k, $v) = @$params[$i,$i+1];
336  
337 13 $path_info =~ s/=$k/$v/;
338 }
339 }
340  
341  
342  
343 42 my $query_string;
344 42 ▶ if (scalar @$query) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$query) 9 33 42
345 9 $query_string = join('&', @$query);
346 }
347  
348 42 $self->env->{PATH_INFO} = $path_info;
349 42 $self->env->{QUERY_STRING} = $query_string;
350  
351 42 ▶ my $uri = $self->uri($path_info, $query_string || '');
Condition100%2/2
$query_string || ''
l!l
834
MC/DC100%1/1
$query_string || ''
$query_string
Truth table100%2/2
A
A= $query_string
Aresult
11
00
352  
353 42 my $request = HTTP::Request->new(
354 $self->method => $uri, $self->headers
355 );
356  
357 42 ▶ if ( keys %$form_data ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$form_data) 2 40 42
358 2 $self->env->{'spore.form_data'} = $form;
359 2 my ( $content, $b ) = $self->_form_data($form);
360 2 $request->content($content);
361 2 $request->header('Content-Length' => length($content));
362 2 $request->header(
363 'Content-Type' => 'multipart/form-data; boundary=' . $b );
364 }
365  
366 42 ▶ if ( my $payload = $self->content ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $payload = $self->content) 2 40 42
367 2 $request->content($payload);
368 2 ▶ $request->header(
Branch: 1 branch
BranchTrueFalseTotal
unless $request->header("Content-Type") 0 2 2
369 'Content-Type' => 'application/x-www-form-urlencoded' )
370 unless $request->header('Content-Type');
371 }
372  
373 42 return $request;
374 }
375  
376 34 1;
377  
378 __END__
« Spore/Middleware/Format/Auto.pm ↑ Summary Spore/Role/Debug.pm »