Engine/Request.pm

filter: Statementstmt 100% 154 / 154 Branchbran 100% 68 / 68 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 100% 30 / 30 Podpod 70.0% 7 / 10 totaltotal 98.8% 262 / 265 CC 43 SCAR 16.6
CC
43
Cov
100%
upload
12.0
param
8.0
_build_parameters
7.0
CRAP 5.3
« Engine/Interface/POE.pm ↑ Summary Engine/Role/ResponseWriter/OutputBody.pm »
1 package HTTP::Engine::Request;
2 62 ▶use Any::Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 62 ▶use HTTP::Headers::Fast;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 62 ▶use HTTP::Engine::Types::Core qw( Uri Header );
Subroutine BEGIN: called CC=1 SCAR=0.0
5 62 ▶use URI::QueryParam;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 62 require Carp; # Carp->import is too heavy =(
7  
8 # Mouse, Moose role merging is borked with attributes
9 #with qw(HTTP::Engine::Request);
10  
11 # this object constructs all our lazy fields for us
12 62 has request_builder => (
13 does => "HTTP::Engine::Role::RequestBuilder",
14 is => "rw",
15 required => 1,
16 );
17  
18 116 ▶sub BUILD {
Subroutine BUILD: called CC=3 SCAR=11.0
Pod documented
19 116 my ( $self, $param ) = @_;
20  
21 116 foreach my $field (qw/base path/) {
22 232 ▶ if ( my $val = $param->{$field} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $val = $param->{$field}) 48 184 232
23 48 $self->$field($val);
24 }
25 }
26 }
27  
28 62 has _connection => (
29 is => "ro",
30 isa => 'HashRef',
31 required => 1,
32 );
33  
34 62 has "_read_state" => (
35 is => "rw",
36 lazy_build => 1,
37 );
38  
39 19 ▶sub _build__read_state {
Subroutine _build__read_state: called CC=1 SCAR=0.0
40 19 my $self = shift;
41 19 $self->request_builder->_build_read_state($self);
42 }
43  
44 62 has connection_info => (
45 is => "rw",
46 isa => "HashRef",
47 lazy_build => 1,
48 );
49  
50 35 ▶sub _build_connection_info {
Subroutine _build_connection_info: called CC=1 SCAR=0.0
51 35 my $self = shift;
52 35 $self->request_builder->_build_connection_info($self);
53 }
54  
55 62 has cookies => (
56 is => 'rw',
57 isa => 'HashRef',
58 lazy_build => 1,
59 );
60  
61 2 ▶sub _build_cookies {
Subroutine _build_cookies: called CC=1 SCAR=0.0
62 2 my $self = shift;
63 2 $self->request_builder->_build_cookies($self);
64 }
65  
66 62 foreach my $attr (qw/address method protocol user port _https_info request_uri/) {
67 has $attr => (
68 is => 'rw',
69 # isa => "Str",
70 lazy => 1,
71 90 ▶ default => sub { shift->connection_info->{$attr} },
Subroutine __ANON__: called CC=1 SCAR=0.0
72 434 );
73 }
74 62 has query_parameters => (
75 is => 'rw',
76 isa => 'HashRef',
77 lazy_build => 1,
78 );
79  
80 31 ▶sub _build_query_parameters {
Subroutine _build_query_parameters: called CC=1 SCAR=0.0
81 31 my $self = shift;
82 31 $self->uri->query_form_hash;
83 }
84  
85 # https or not?
86 62 has secure => (
87 is => 'rw',
88 isa => 'Bool',
89 lazy_build => 1,
90 );
91  
92 27 ▶sub _build_secure {
Subroutine _build_secure: called CC=5 SCAR=16.1
93 27 my $self = shift;
94  
95 27 ▶ if ( my $https = $self->_https_info ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $https = $self->_https_info) 3 24 27
96 3 ▶ return 1 if uc($https) eq 'ON';
Branch: 1 branch
BranchTrueFalseTotal
if uc $https eq "ON" 2 1 3
97 }
98  
99 25 ▶ if ( my $port = $self->port ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $port = $self->port) 3 22 25
100 3 ▶ return 1 if $port == 443;
Branch: 1 branch
BranchTrueFalseTotal
if $port == 443 1 2 3
101 }
102  
103 24 return 0;
104 }
105  
106 # proxy request?
107 62 has proxy_request => (
108 is => 'rw',
109 isa => 'Str', # TODO: union(Uri, Undef) type
110 # coerce => 1,
111 lazy_build => 1,
112 );
113  
114 27 ▶sub _build_proxy_request {
Subroutine _build_proxy_request: called CC=3 SCAR=11.0
115 27 my $self = shift;
116 27 ▶ return '' unless $self->request_uri; # TODO: return undef
Branch: 1 branch
BranchTrueFalseTotal
unless $self->request_uri 24 3 27
117 3 ▶ return '' unless $self->request_uri =~ m!^https?://!i; # TODO: return undef
Branch: 1 branch
BranchTrueFalseTotal
unless $self->request_uri =~ m[^https?://]i 1 2 3
118 2 return $self->request_uri; # TODO: return URI->new($self->request_uri);
119 }
120  
121 62 has uri => (
122 is => 'rw',
123 isa => Uri,
124 coerce => 1,
125 lazy_build => 1,
126 handles => [qw(base path)],
127 );
128  
129 28 ▶sub _build_uri {
Subroutine _build_uri: called CC=1 SCAR=0.0
130 28 my $self = shift;
131 28 $self->request_builder->_build_uri($self);
132 }
133  
134 has builder_options => (
135 is => 'rw',
136 isa => 'HashRef',
137 116 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
138 +{
139 116 disable_raw_body => 0,
140 upload_tmp => undef,
141 },
142 },
143 62 );
144  
145 62 has raw_body => (
146 is => 'rw',
147 isa => 'Str',
148 lazy_build => 1,
149 );
150  
151 5 ▶sub _build_raw_body {
Subroutine _build_raw_body: called CC=1 SCAR=0.0
152 5 my $self = shift;
153 5 $self->request_builder->_build_raw_body($self);
154 }
155  
156 62 has headers => (
157 is => 'rw',
158 isa => Header,
159 coerce => 1,
160 lazy_build => 1,
161 handles => [ qw(content_encoding content_length content_type header referer user_agent) ],
162 );
163  
164 10 ▶sub _build_headers {
Subroutine _build_headers: called CC=1 SCAR=0.0
165 10 my $self = shift;
166 10 $self->request_builder->_build_headers($self);
167 }
168  
169 # Contains the URI base. This will always have a trailing slash.
170 # If your application was queried with the URI C<http://localhost:3000/some/path> then C<base> is C<http://localhost:3000/>.
171  
172 62 has hostname => (
173 is => 'rw',
174 isa => 'Str',
175 lazy_build => 1,
176 );
177  
178 2 ▶sub _build_hostname {
Subroutine _build_hostname: called CC=1 SCAR=0.0
179 2 my $self = shift;
180 2 $self->request_builder->_build_hostname($self);
181 }
182  
183 62 has http_body => (
184 is => 'rw',
185 isa => 'HTTP::Body',
186 lazy_build => 1,
187 handles => {
188 body_parameters => 'param',
189 body => 'body',
190 },
191 );
192  
193 14 ▶sub _build_http_body {
Subroutine _build_http_body: called CC=1 SCAR=0.0
194 14 my $self = shift;
195 14 $self->request_builder->_build_http_body($self);
196 }
197  
198 # contains body_params and query_params
199 62 has parameters => (
200 is => 'rw',
201 isa => 'HashRef',
202 lazy_build => 1,
203 );
204  
205 11 ▶sub _build_parameters {
Subroutine _build_parameters: called CC=7 SCAR=19.5
206 11 my $self = shift;
207  
208 11 my $query = $self->query_parameters;
209 11 my $body = $self->body_parameters;
210  
211 11 my %merged;
212  
213 11 foreach my $hash ( $query, $body ) {
214 22 foreach my $name ( keys %$hash ) {
215 12 my $param = $hash->{$name};
216 12 ▶ push( @{ $merged{$name} ||= [] }, ( ref $param ? @$param : $param ) );
Branch: 1 branch
BranchTrueFalseTotal
ref $param ? : 1 11 12
Condition100%2/2
$merged{$name} ||= []
l!l
210
MC/DC100%1/1
$merged{$name} ||= []
$merged{$name}
Truth table100%2/2
A
A= $merged{$name}
Aresult
11
00
217 }
218 }
219  
220 11 foreach my $param ( values %merged ) {
221 10 ▶ $param = $param->[0] if @$param == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @$param == 1 7 3 10
222 }
223  
224 11 return \%merged;
225 }
226  
227 62 has uploads => (
228 is => 'rw',
229 isa => 'HashRef',
230 lazy_build => 1,
231 );
232  
233 8 ▶sub _build_uploads {
Subroutine _build_uploads: called CC=1 SCAR=0.0
234 8 my $self = shift;
235 8 $self->request_builder->_prepare_uploads($self);
236 }
237  
238 # aliases
239 62 *body_params = \&body_parameters;
240 62 *input = \&body;
241 62 *params = \&parameters;
242 62 *query_params = \&query_parameters;
243 62 *path_info = \&path;
244  
245 5 ▶sub cookie {
Subroutine cookie: called CC=4 SCAR=13.9
Pod documented
246 5 my $self = shift;
247  
248 5 ▶ return keys %{ $self->cookies } if @_ == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 0 1 4 5
249  
250 4 ▶ if (@_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) 3 1 4
251 3 my $name = shift;
252 3 ▶ return undef unless exists $self->cookies->{$name}; ## no critic.
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->cookies->{$name} 1 2 3
253 2 return $self->cookies->{$name};
254 }
255 1 return;
256 }
257  
258 13 ▶sub param {
Subroutine param: called CC=8 SCAR=20.8
Pod documented
259 13 my $self = shift;
260  
261 13 ▶ return keys %{ $self->parameters } if @_ == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 0 5 8 13
262  
263 8 ▶ if (@_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 6 2 8
264 6 my $param = shift;
265 6 ▶ return wantarray ? () : undef unless exists $self->parameters->{$param};
Branch: 2 branches
BranchTrueFalseTotal
unless exists $self->parameters->{$param} 2 4 6
wantarray ? : 1 1 2
266  
267 4 ▶ if ( ref $self->parameters->{$param} eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->parameters->{$param} eq 'ARRAY') { } 2 2 4
268 return (wantarray)
269 1 ? @{ $self->parameters->{$param} }
270 2 ▶ : $self->parameters->{$param}->[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 1 2
271 } else {
272 return (wantarray)
273 ? ( $self->parameters->{$param} )
274 2 ▶ : $self->parameters->{$param};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 1 2
275 }
276 } else {
277 2 my $field = shift;
278 2 $self->parameters->{$field} = [@_];
279 }
280 }
281  
282 27 ▶sub upload {
Subroutine upload: called CC=12 SCAR=24.8
Pod documented
283 27 my $self = shift;
284  
285 27 ▶ return keys %{ $self->uploads } if @_ == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 0 2 25 27
286  
287 25 ▶ if (@_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 21 4 25
288 21 my $upload = shift;
289 21 ▶ return wantarray ? () : undef unless exists $self->uploads->{$upload};
Branch: 2 branches
BranchTrueFalseTotal
unless exists $self->uploads->{$upload} 2 19 21
wantarray ? : 1 1 2
290  
291 19 ▶ if (ref $self->uploads->{$upload} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->uploads->{$upload} eq 'ARRAY') { } 9 10 19
292 return (wantarray)
293 3 ? @{ $self->uploads->{$upload} }
294 9 ▶ : $self->uploads->{$upload}->[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 3 6 9
295 } else {
296 return (wantarray)
297 ? ( $self->uploads->{$upload} )
298 10 ▶ : $self->uploads->{$upload};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 9 10
299 }
300 } else {
301 4 ▶ while ( my($field, $upload) = splice(@_, 0, 2) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@_, 0, 2)) 4 4 8
302 4 ▶ if ( exists $self->uploads->{$field} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->uploads->{$field}) { } 2 2 4
303 2 for ( $self->uploads->{$field} ) {
304 2 ▶ $_ = [$_] unless ref($_) eq "ARRAY";
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_ eq "ARRAY" 1 1 2
305 2 push(@{ $_ }, $upload);
306 }
307 } else {
308 2 $self->uploads->{$field} = $upload;
309 }
310 }
311 }
312 }
313  
314 44 ▶sub uri_with {
Subroutine uri_with: called CC=6 SCAR=17.9
Pod documented
315 44 my($self, $args) = @_;
316
317 44 ▶ Carp::carp( 'No arguments passed to uri_with()' ) unless $args;
Branch: 1 branch
BranchTrueFalseTotal
unless $args 22 22 44
318  
319 44 for my $value (values %{ $args }) {
320 9 ▶ next unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 8 9
321 8 ▶ for ( ref $value eq 'ARRAY' ? @{ $value } : $value ) {
Branch: 1 branch
BranchTrueFalseTotal
ref $value eq 'ARRAY' ? : 4 4 8
322 12 $_ = "$_";
323 12 utf8::encode( $_ );
324 }
325 };
326
327 44 my $uri = $self->uri->clone;
328
329 $uri->query_form( {
330 44 %{ $uri->query_form_hash },
331 44 %{ $args },
332 } );
333 44 return $uri;
334 }
335  
336 2 ▶sub as_http_request {
Subroutine as_http_request: called CC=1 SCAR=0.0
Pod documented
337 2 my $self = shift;
338 2 require 'HTTP/Request.pm'; ## no critic
339 2 HTTP::Request->new( $self->method, $self->uri, $self->headers, $self->raw_body );
340 }
341  
342 5 ▶sub absolute_url {
Subroutine absolute_url: called CC=2 SCAR=6.9
Pod documented
343 5 my ($self, $location) = @_;
344  
345 5 ▶ unless ($location =~ m!^https?://!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($location =~ m[^https?://]) { } 1 4 5
346 4 return URI->new( $location )->abs( $self->base );
347 } else {
348 1 return $location;
349 }
350 }
351  
352 2 ▶sub content {
Subroutine content: called CC=2 SCAR=6.9
Pod undocumented
353 2 my ( $self, @args ) = @_;
354  
355 2 ▶ if ( @args ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) { } 1 1 2
356 1 Carp::croak "The HTTP::Request method 'content' is unsupported when used as a writer, use HTTP::Engine::RequestBuilder";
357 } else {
358 1 return $self->raw_body;
359 }
360 }
361  
362 1 ▶sub as_string {
Subroutine as_string: called CC=1 SCAR=0.0
Pod undocumented
363 1 my $self = shift;
364 1 $self->as_http_request->as_string; # FIXME not efficient
365 }
366  
367 1 ▶sub parse {
Subroutine parse: called CC=1 SCAR=0.0
Pod undocumented
368 1 Carp::croak "The HTTP::Request method 'parse' is unsupported, use HTTP::Engine::RequestBuilder";
369 }
370  
371 62 ▶no Any::Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
372 62 __PACKAGE__->meta->make_immutable(inline_destructor => 1);
373 62 1;
374 __END__
« Engine/Interface/POE.pm ↑ Summary Engine/Role/ResponseWriter/OutputBody.pm »