lib/HTTP/Request/CurlParameters.pm

filter: Statementstmt 65.7% 282 / 429 Branchbran 53.3% 111 / 208 Conditioncond 39.2% 33 / 84 MC/DCmcdc 25.5% 11 / 43 Subroutinesub 83.3% 30 / 36 Podpod 55.5% 5 / 9 totaltotal 58.3% 472 / 809 CC 147 SCAR 45.7
CC
147
Cov
59%
as_wget
552.0
as_rest_client_snippet
156.0
_build_rest_client_headers
72.0
CRAP 96.2
« lib/HTTP/Request/FromWget.pm ↑ Summary lib/HTTP/Request/FromCurl.pm »
1 package HTTP::Request::CurlParameters 0.57;
2 19 ▶use 5.020;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 19 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 19 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 19 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 19 ▶use File::Spec::Unix;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 19 ▶use List::Util 'pairmap';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 19 ▶use PerlX::Maybe;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 19 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 19 ▶use Moo 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 19 ▶use feature 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 19 ▶no warnings 'experimental::signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 =head1 NAME
16  
17 HTTP::Request::CurlParameters - container for a Curl-like HTTP request
18  
19 =head1 SYNOPSIS
20  
21 my $ua = LWP::UserAgent->new;
22 my $params = HTTP::Request::CurlParameters->new(argv => \@ARGV);
23 my $response = $ua->request($params->as_request);
24  
25 =head1 DESCRIPTION
26  
27 Objects of this class are mostly created from L<HTTP::Request::FromCurl>. Most
28 likely you want to use that module instead:
29  
30 my $ua = LWP::UserAgent->new;
31 my $params = HTTP::Request::FromCurl->new(command_curl => $cmd);
32 my $response = $ua->request($params->as_request);
33  
34 =head1 METHODS
35  
36 =head2 C<< ->new >>
37  
38 Options:
39  
40 =over 4
41  
42 =item *
43  
44 C<method>
45  
46 method => 'GET'
47  
48 The HTTP method to use.
49  
50 =cut
51  
52 19 has method => (
53 is => 'ro',
54 default => 'GET',
55 );
56  
57 =item *
58  
59 C<uri>
60  
61 uri => 'https://example.com'
62  
63 The URI of the request.
64  
65 =cut
66  
67 19 has uri => (
68 is => 'ro',
69 default => 'https://example.com',
70 );
71  
72 =item *
73  
74 C<headers>
75  
76 headers => {
77 'Content-Type' => 'text/json',
78 'X-Secret' => ['value-1', 'value-2'],
79 }
80  
81 The headers of the request. Multiple headers with the same
82 name can be passed as an arrayref to the header key.
83  
84 =cut
85  
86 has headers => (
87 is => 'ro',
88 0 ▶ default => sub { {} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
89 19 );
90  
91 =item *
92  
93 C<cookie_jar>
94  
95 The cookie jar to use.
96  
97 =cut
98  
99 19 has cookie_jar => (
100 is => 'ro',
101 );
102  
103 =item *
104  
105 C<cookie_jar_options>
106  
107 Options for the constructor of the cookie jar.
108  
109 =cut
110  
111 has cookie_jar_options => (
112 is => 'ro',
113 167 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
114 19 );
115  
116 =item *
117  
118 C<credentials>
119  
120 credentials => 'hunter2:secret'
121  
122 The credentials to use for basic authentication.
123  
124 =cut
125  
126 19 has credentials => (
127 is => 'ro',
128 );
129  
130 =item *
131  
132 C<auth>
133  
134 auth => 'basic'
135  
136 The authentication method to use.
137  
138 =cut
139  
140 19 has auth => (
141 is => 'ro',
142 );
143  
144 =item *
145  
146 C<post_data>
147  
148 post_data => ['A string','across multiple','scalars']
149  
150 The POST body to use.
151  
152 =cut
153  
154 has post_data => (
155 is => 'ro',
156 169 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
157 19 );
158  
159 =item *
160  
161 C<body>
162  
163 body => '{"greeting":"Hello"}'
164  
165 The body of the request.
166  
167 =cut
168  
169 19 has body => (
170 is => 'ro',
171 );
172  
173 =item *
174  
175 C<timeout>
176  
177 timeout => 50
178  
179 The timeout for the request
180  
181 =cut
182  
183 19 has timeout => (
184 is => 'ro',
185 );
186  
187 =item *
188  
189 C<unix_socket>
190  
191 unix_socket => '/var/run/docker/docker.sock'
192  
193 The timeout for the request
194  
195 =cut
196  
197 19 has unix_socket => (
198 is => 'ro',
199 );
200  
201 =item *
202  
203 C<local_address>
204  
205 local_address => '192.0.2.116'
206  
207 The local network address to bind to when making the request
208  
209 =cut
210  
211 19 has local_address => (
212 is => 'ro',
213 );
214  
215 =item *
216  
217 C<form_args>
218  
219 The HTML form parameters. These get converted into
220 a body.
221  
222 =cut
223  
224 has form_args => (
225 is => 'ro',
226 165 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
227 19 );
228  
229 =item *
230  
231 C<insecure>
232  
233 insecure => 1
234  
235 Disable SSL certificate verification
236  
237 =cut
238  
239 19 has insecure => (
240 is => 'ro',
241 );
242  
243 =item *
244  
245 C<cert>
246  
247 cert => '/path/to/certificate',
248  
249 Use the certificate file for SSL
250  
251 =cut
252  
253 19 has cert => (
254 is => 'ro',
255 );
256  
257 =item *
258  
259 C<capath>
260  
261 capath => '/path/to/cadir/',
262  
263 Use the certificate directory for SSL
264  
265 =cut
266  
267 19 has capath => (
268 is => 'ro',
269 );
270  
271 =item *
272  
273 C<output>
274  
275 Name of the output file
276  
277 =cut
278  
279 19 has output => (
280 is => 'ro',
281 );
282  
283 =item *
284  
285 C<max_filesize>
286  
287 Maximum size (in bytes) of a file to download
288  
289 =cut
290  
291 19 has max_filesize => (
292 is => 'ro',
293 );
294  
295 =item *
296  
297 C<show_error>
298  
299 show_error => 0
300  
301 Show error message on HTTP errors
302  
303 =cut
304  
305 19 has show_error => (
306 is => 'ro',
307 );
308  
309 =item *
310  
311 C<fail>
312  
313 fail => 1
314  
315 Let the Perl code C<die> on error
316  
317 =back
318  
319 =cut
320  
321 19 has fail => (
322 is => 'ro',
323 );
324  
325 179 ▶sub _build_quoted_body( $self, $body=$self->body ) {
Subroutine _build_quoted_body: called CC=5 SCAR=20.4
326 179 ▶ if( defined $body ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $body) { } 26 153 179
327 26 $body =~ s!([\x00-\x1f'"\$\@\%\\])!sprintf '\\x%02x', ord $1!ge;
328 26 return sprintf qq{"%s"}, $body
329  
330 } else {
331 # Sluuuurp
332 my @post_data = map {
333 0 ▶ /^\@(.*)/ ? do {
Branch: 1 branch
BranchTrueFalseTotal
/^\@(.*)/u ? : 0 0 0
334 0 ▶ open my $fh, '<', $1
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $1 or die "$1: $!" 0 0 0
335 or die "$1: $!";
336 0 local $/; # / for Filter::Simple
337 0 binmode $fh;
338 <$fh>
339 0 }
340 : $_
341 153 } @{ $self->post_data };
342 153 return join "", @post_data;
343 }
344 };
345  
346 =head2 C<< ->as_request >>
347  
348 $ua->request( $r->as_request );
349  
350 Returns an equivalent L<HTTP::Request> object
351  
352 =cut
353  
354 261 ▶sub _explode_headers( $self ) {
Subroutine _explode_headers: called CC=2 SCAR=6.9
355 my @res =
356 858 map { my $h = $_;
357 858 my $v = $self->headers->{$h};
358 858 ▶ ref $v ? (map { $h => $_ } @$v)
Branch: 1 branch
BranchTrueFalseTotal
ref $v ? : 4 854 858
359 : ($h => $v)
360 261 } keys %{ $self->headers };
361 }
362  
363 =head2 C<< $r->as_request >>
364  
365 my $r = $curl->as_request;
366  
367 Returns a L<HTTP::Request> object that represents
368 the Curl options.
369  
370 =cut
371  
372 81 ▶sub as_request( $self ) {
Subroutine as_request: called CC=1 SCAR=0.0
Pod documented
373 81 HTTP::Request->new(
374 $self->method => $self->uri,
375 [ $self->_explode_headers() ],
376 $self->body(),
377 )
378 };
379  
380 0 ▶sub _fill_snippet( $self, $snippet ) {
Subroutine _fill_snippet: not called CC=1 SCAR=6.9
381 # Doesn't parse parameters, yet
382 0 $snippet =~ s!\$self->(\w+)!$self->$1!ge;
383 0 $snippet
384 }
385  
386 88 ▶sub _init_cookie_jar_lwp( $self ) {
Subroutine _init_cookie_jar_lwp: called CC=3 SCAR=11.0
387 88 ▶ if( my $fn = $self->cookie_jar ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $fn = $self->cookie_jar) 2 86 88
388 2 ▶ my $save = $self->cookie_jar_options->{'write'} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->cookie_jar_options->{'write'} ? : 1 1 2
389 return {
390 2 preamble => [
391 "use Path::Tiny;",
392 "use HTTP::Cookies;",
393 ],
394 code => \"HTTP::Cookies->new(\n file => path('$fn'),\n autosave => $save,\n)",
395 postamble => [
396 #"path('$fn')->spew(\$ua->cookie_jar->dump_cookies())",
397 ],
398 };
399 }
400 }
401  
402 86 ▶sub _init_cookie_jar_tiny( $self ) {
Subroutine _init_cookie_jar_tiny: called CC=3 SCAR=11.0
403 86 ▶ if( my $fn = $self->cookie_jar ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $fn = $self->cookie_jar) 2 84 86
404 2 my $save = $self->cookie_jar_options->{'write'};
405 return {
406 2 ▶ preamble => [
Branch: 1 branch
BranchTrueFalseTotal
$save ? : 1 1 2
407 "use Path::Tiny;",
408 "use HTTP::CookieJar;",
409 ],
410 code => \"HTTP::CookieJar->new->load_cookies(path('$fn')->lines),",
411 postamble => [
412 $save ?
413 ("path('$fn')->spew(\$ua->cookie_jar->dump_cookies())")
414 : (),
415 ],
416 };
417 }
418 }
419  
420 5 ▶sub _init_cookie_jar_mojolicious( $self ) {
Subroutine _init_cookie_jar_mojolicious: called CC=2 SCAR=10.5
421 5 ▶ if( my $fn = $self->cookie_jar ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $fn = $self->cookie_jar) 0 5 5
422 0 my $save = $self->cookie_jar_options->{'write'};
423 return {
424 0 preamble => [
425 # "use Path::Tiny;",
426 "use Mojo::UserAgent::CookieJar;",
427 ],
428 code => \"Mojo::UserAgent::CookieJar->new,",
429 postamble => [
430 #$save ?
431 # ("path('$fn')->spew(\$ua->cookie_jar->dump_cookies())")
432 # : (),
433 ],
434 };
435 }
436 }
437  
438 540 ▶sub _pairlist( $self, $l, $prefix = " " ) {
Subroutine _pairlist: called CC=2 SCAR=6.9
439 return join ",\n",
440 611 ▶ pairmap { my $v = ! ref $b ? qq{'$b'}
Subroutine __ANON__: called CC=5 SCAR=16.1
441 : ref $b eq 'SCALAR' ? $$b
442 6 ▶ : ref $b eq 'ARRAY' ? '[' . join( ", ", map {qq{'$_'}} @$b ) . ']'
Branch: 1 branch
BranchTrueFalseTotal
ref $b eq 'HASH' ? : 1 0 1
443 611 ▶ : ref $b eq 'HASH' ? '{' . $self->_pairlist([ map { $_ => $b->{$_} } sort keys %$b ]) . '}'
Branch: 3 branches
BranchTrueFalseTotal
!ref($b) ? : 603 8 611
ref $b eq 'SCALAR' ? : 5 3 8
ref $b eq 'ARRAY' ? : 2 1 3
444 : die "Unknown type of $b";
445 611 qq{$prefix'$a' => $v}
446 540 } @$l
447 }
448  
449 88 ▶sub _build_lwp_headers( $self, $prefix = " ", %options ) {
Subroutine _build_lwp_headers: called CC=5 SCAR=16.1
450 # This is so we create the standard header order in our output
451 88 my @h = $self->_explode_headers;
452 88 my $h = HTTP::Headers->new( @h );
453 88 $h->remove_header( @{$options{implicit_headers}} );
454  
455 # also skip the Host: header if it derives from $uri
456 88 my $val = $h->header('Host');
457 88 ▶ if( $val and ($val eq $self->uri->host_port
Branch: 1 branch
BranchTrueFalseTotal
if ($val and $val eq $self->uri->host_port || $val eq $self->uri->host) 81 7 88
Condition83%5/6
$val and $val eq $self->uri->host_port || $val eq $self->uri->host
!ll&&!rl&&r
5281
$val eq $self->uri->host_port or $val eq $self->uri->host
l!l&&r!l&&!r
8102
MC/DC66%2/3
$val and $val eq $self->uri->host_port || $val eq $self->uri->host
$val $val eq $self->uri->host_port $val eq $self->uri->host
Truth table75%3/4
A and B or C
A= $valB= $val eq $self->uri->host_portC= $val eq $self->uri->host
ABCresult
0XX0
1000
11X1
1011
458 or $val eq $self->uri->host )) {
459 # trivial host header
460 81 $h->remove_header('Host');
461 };
462  
463 88 $self->_pairlist([ $h->flatten ], $prefix);
464 }
465  
466 86 ▶sub _build_tiny_headers( $self, $prefix = " ", %options ) {
Subroutine _build_tiny_headers: called CC=5 SCAR=16.2
467 86 my @h = $self->_explode_headers;
468 86 my $h = HTTP::Headers->new( @h );
469 86 $h->remove_header( @{$options{implicit_headers}} );
470  
471 # HTTP::Tiny does not like overriding the Host: header :-/
472 86 $h->remove_header('Host');
473  
474 86 @h = $h->flatten;
475 86 my %h;
476 my @order;
477 86 ▶ while( @h ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@h) 203 86 289
478 203 my ($k,$v) = splice(@h,0,2);
479 203 ▶ if( ! exists $h{ $k }) {
Branch: 2 branches
BranchTrueFalseTotal
if (not exists $h{$k}) { } 202 1 203
elsif (not ref $h{$k}) { } 1 0 1
480 # Fresh value
481 202 $h{ $k } = $v;
482 202 push @order, $k;
483 } elsif( ! ref $h{$k}) {
484 # Second value
485 1 $h{ $k } = [$h{$k}, $v];
486 } else {
487 # Multiple values
488 0 push @{$h{ $k }}, $v;
489 }
490 };
491  
492 86 $self->_pairlist([ map { $_ => $h{ $_ } } @order ], $prefix);
493 }
494  
495 5 ▶sub _build_mojolicious_headers( $self, $prefix = " ", %options ) {
Subroutine _build_mojolicious_headers: called CC=8 SCAR=24.5
496 # This is so we create the standard header order in our output
497 5 my @h = $self->_explode_headers;
498 5 my $h = HTTP::Headers->new( @h );
499 5 $h->remove_header( @{$options{implicit_headers}} );
500  
501 # also skip the Host: header if it derives from $uri
502 5 my $val = $h->header('Host');
503 5 ▶ if( $val and ($val eq $self->uri->host_port
Branch: 1 branch
BranchTrueFalseTotal
if ($val and $val eq $self->uri->host_port || $val eq $self->uri->host) 0 5 5
Condition16%1/6
$val and $val eq $self->uri->host_port || $val eq $self->uri->host
!ll&&!rl&&r
500
$val eq $self->uri->host_port or $val eq $self->uri->host
l!l&&r!l&&!r
000
MC/DC0%0/3
$val and $val eq $self->uri->host_port || $val eq $self->uri->host
$val $val eq $self->uri->host_port $val eq $self->uri->host
Truth table25%1/4
A and B or C
A= $valB= $val eq $self->uri->host_portC= $val eq $self->uri->host
ABCresult
0XX0
1000
11X1
1011
504 or $val eq $self->uri->host )) {
505 # trivial host header
506 0 $h->remove_header('Host');
507 };
508  
509 5 @h = $h->flatten;
510 5 my %h;
511 my @order;
512 5 ▶ while( @h ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@h) 4 5 9
513 4 my ($k,$v) = splice(@h,0,2);
514 4 ▶ if( ! exists $h{ $k }) {
Branch: 2 branches
BranchTrueFalseTotal
if (not exists $h{$k}) { } 4 0 4
elsif (not ref $h{$k}) { } 0 0 0
515 # Fresh value
516 4 $h{ $k } = $v;
517 4 push @order, $k;
518 } elsif( ! ref $h{$k}) {
519 # Second value
520 0 $h{ $k } = [$h{$k}, $v];
521 } else {
522 # Multiple values
523 0 push @{$h{ $k }}, $v;
524 }
525 };
526  
527 5 $self->_pairlist([ map { $_ => $h{ $_ } } @order ], $prefix);
528 }
529  
530 0 ▶sub _build_rest_client_headers( $self, $prefix = " ", %options ) {
Subroutine _build_rest_client_headers: not called CC=8 SCAR=42.8
531 # This is so we create the standard header order in our output
532 0 my @h = $self->_explode_headers;
533 0 my $h = HTTP::Headers->new( @h );
534 0 $h->remove_header( @{$options{implicit_headers}} );
535  
536 # also skip the Host: header if it derives from $uri
537 0 my $val = $h->header('Host');
538 0 ▶ if( $val and ($val eq $self->uri->host_port
Branch: 1 branch
BranchTrueFalseTotal
if ($val and $val eq $self->uri->host_port || $val eq $self->uri->host) 0 0 0
Condition0%0/6
$val and $val eq $self->uri->host_port || $val eq $self->uri->host
!ll&&!rl&&r
000
$val eq $self->uri->host_port or $val eq $self->uri->host
l!l&&r!l&&!r
000
MC/DC0%0/3
$val and $val eq $self->uri->host_port || $val eq $self->uri->host
$val $val eq $self->uri->host_port $val eq $self->uri->host
Truth table0%0/4
A and B or C
A= $valB= $val eq $self->uri->host_portC= $val eq $self->uri->host
ABCresult
0XX0
1000
11X1
1011
539 or $val eq $self->uri->host )) {
540 # trivial host header
541 0 $h->remove_header('Host');
542 };
543  
544 0 @h = $h->flatten;
545 0 my %h;
546 my @order;
547 0 ▶ while( @h ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@h) 0 0 0
548 0 my ($k,$v) = splice(@h,0,2);
549 0 ▶ if( ! exists $h{ $k }) {
Branch: 2 branches
BranchTrueFalseTotal
if (not exists $h{$k}) { } 0 0 0
elsif (not ref $h{$k}) { } 0 0 0
550 # Fresh value
551 0 $h{ $k } = $v;
552 0 push @order, $k;
553 } elsif( ! ref $h{$k}) {
554 # Second value
555 0 $h{ $k } = [$h{$k}, $v];
556 } else {
557 # Multiple values
558 0 push @{$h{ $k }}, $v;
559 }
560 };
561  
562 0 $self->_pairlist([ map { $_ => $h{ $_ } } @order ], $prefix);
563 }
564  
565 =head2 C<< $r->as_snippet( %options ) >>
566  
567 print $r->as_snippet( type => 'LWP' );
568  
569 Returns a code snippet that returns code to create an equivalent
570 L<HTTP::Request> object and to perform the request using L<WWW::Mechanize>.
571  
572 This is mostly intended as a convenience function for creating Perl demo
573 snippets from C<curl> examples.
574  
575 =head3 Options
576  
577 =over 4
578  
579 =item B<implicit_headers>
580  
581 Arrayref of headers that will not be output.
582  
583 Convenient values are ['Content-Length']
584  
585 =item B<type>
586  
587 type => 'Tiny',
588  
589 Type of snippet. Valid values are C<LWP> for L<LWP::UserAgent>,
590 C<Mojolicious> for L<Mojolicious::UserAgent>
591 and C<Tiny> for L<HTTP::Tiny>.
592  
593 =back
594  
595 =cut
596  
597 179 ▶sub as_snippet( $self, %options ) {
Subroutine as_snippet: called CC=5 SCAR=16.2
Pod documented
598 179 ▶ my $type = delete $options{ type } || 'LWP';
Condition100%2/2
delete $options{'type'} || 'LWP'
l!l
1772
MC/DC100%1/1
delete $options{'type'} || 'LWP'
delete $options{'type'}
Truth table100%2/2
A
A= delete $options{'type'}
Aresult
11
00
599 179 ▶ if( 'LWP' eq $type ) {
Branch: 3 branches
BranchTrueFalseTotal
if ('LWP' eq $type) { } 88 91 179
elsif ('Tiny' eq $type) { } 86 5 91
elsif ('Mojolicious' eq $type) { } 5 0 5
600 88 $self->as_lwp_snippet( %options )
601 } elsif( 'Tiny' eq $type ) {
602 86 $self->as_http_tiny_snippet( %options )
603 } elsif( 'Mojolicious' eq $type ) {
604 5 $self->as_mojolicious_snippet( %options )
605 } else {
606 0 croak "Unknown type '$type'.";
607 }
608 }
609  
610 88 ▶sub as_lwp_snippet( $self, %options ) {
Subroutine as_lwp_snippet: called CC=16 SCAR=28.8
Pod undocumented
611 88 ▶ $options{ prefix } ||= '';
Condition50%1/2
$options{"prefix"} ||= ""
l!l
088
MC/DC0%0/1
$options{"prefix"} ||= ""
$options{"prefix"}
Truth table50%1/2
A
A= $options{"prefix"}
Aresult
11
00
612 88 ▶ $options{ implicit_headers } ||= [];
Condition100%2/2
$options{"implicit_headers"} ||= []
l!l
286
MC/DC100%1/1
$options{"implicit_headers"} ||= []
$options{"implicit_headers"}
Truth table100%2/2
A
A= $options{"implicit_headers"}
Aresult
11
00
613  
614 88 my @preamble;
615 my @postamble;
616 88 my %ssl_options;
617 88 ▶ push @preamble, @{ $options{ preamble } } if $options{ preamble };
Branch: 1 branch
BranchTrueFalseTotal
if $options{"preamble"} 86 2 88
618 0 ▶ push @postamble, @{ $options{ postamble } } if $options{ postamble };
Branch: 1 branch
BranchTrueFalseTotal
if $options{"postamble"} 0 88 88
619 88 my @setup_ua = ('');
620  
621 88 my $request_args = join ", ",
622 '$r',
623 $self->_pairlist([
624 maybe ':content_file', $self->output
625 ], '')
626 ;
627 88 my $init_cookie_jar = $self->_init_cookie_jar_lwp();
628 88 ▶ if( my $p = $init_cookie_jar->{preamble}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $p = $init_cookie_jar->{"preamble"}) 2 86 88
629 2 push @preamble, @{$p}
630 };
631  
632 88 ▶ if( $self->insecure ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->insecure) 1 87 88
633 1 push @preamble, 'use IO::Socket::SSL;';
634 1 $ssl_options{ SSL_verify_mode } = \'IO::Socket::SSL::SSL_VERIFY_NONE';
635 1 $ssl_options{ SSL_hostname } = '';
636 1 $ssl_options{ verify_hostname } = '';
637 };
638  
639 88 ▶ if( $self->cert ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->cert) 0 88 88
640 0 push @preamble, 'use IO::Socket::SSL;';
641 0 $ssl_options{ SSL_ca_file } = $self->cert;
642 };
643 88 ▶ if( $self->capath ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->capath) 0 88 88
644 0 push @preamble, 'use IO::Socket::SSL;';
645 0 $ssl_options{ SSL_ca_path } = $self->capath;
646 };
647 my $constructor_args = join ",",
648 $self->_pairlist([
649 send_te => 0,
650 maybe local_address => $self->local_address,
651 maybe max_size => $self->max_filesize,
652 maybe timeout => $self->timeout,
653 maybe cookie_jar => $init_cookie_jar->{code},
654 88 ▶ maybe SSL_opts => keys %ssl_options ? \%ssl_options : undef,
Branch: 1 branch
BranchTrueFalseTotal
keys %ssl_options ? : 1 87 88
655 ], '')
656 ;
657 88 ▶ if( defined( my $credentials = $self->credentials )) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $credentials = $self->credentials)) 0 88 88
658 0 my( $user, $pass ) = split /:/, $credentials, 2;
659 0 my $setup_credentials = sprintf qq{\$ua->credentials("%s","%s");},
660 quotemeta $user,
661 quotemeta $pass;
662 0 push @setup_ua, $setup_credentials;
663 };
664 88 ▶ if( $self->show_error ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->show_error) { } 9 79 88
elsif ($self->fail) { } 0 79 79
665 9 push @postamble,
666 ' die $res->message if $res->is_error;',
667 } elsif( $self->fail ) {
668 0 push @postamble,
669 ' exit 1 if !$res->{success};',
670 };
671  
672 88 ▶ @setup_ua = ()
Branch: 1 branch
BranchTrueFalseTotal
if @setup_ua == 1 88 0 88
673 if @setup_ua == 1;
674  
675 88 my $request_constructor;
676  
677 88 ▶ if( $self->method ne 'GET' and @{ $self->form_args }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->method ne 'GET' and @{$self->form_args;}) { } 2 86 88
Condition100%3/3
$self->method ne "GET" and @{$self->form_args;}
!ll&&!rl&&r
69172
MC/DC100%2/2
$self->method ne "GET" and @{$self->form_args;}
$self->method ne "GET" @{$self->form_args;}
Truth table100%3/3
A and B
A= $self->method ne "GET"B= @{$self->form_args;}
ABresult
0X0
100
111
678 2 push @preamble, 'use HTTP::Request::Common;';
679 2 push @{$options{ implicit_headers }}, 'Content-Type';
680 2 $request_constructor = <<SNIPPET;
681 2 my \$r = HTTP::Request::Common::@{[$self->method]}(
682 2 '@{[$self->uri]}',
683 Content_Type => 'form-data',
684 Content => [
685 2 @{[$self->_pairlist($self->form_args, ' ')]}
686 ],
687 2 @{[$self->_build_lwp_headers(' ', %options)]}
688 );
689 SNIPPET
690 } else {
691 86 $request_constructor = <<SNIPPET;
692 my \$r = HTTP::Request->new(
693 86 '@{[$self->method]}' => '@{[$self->uri]}',
694 [
695 86 @{[$self->_build_lwp_headers(' ', %options)]}
696 ],
697 86 @{[$self->_build_quoted_body()]}
698 );
699 SNIPPET
700 }
701  
702 88 @preamble = map { "$options{prefix} $_\n" } @preamble;
703 88 @postamble = map { "$options{prefix} $_\n" } @postamble;
704 0 @setup_ua = map { "$options{prefix} $_\n" } @setup_ua;
705  
706 88 return <<SNIPPET;
707 @preamble
708 my \$ua = LWP::UserAgent->new($constructor_args);@setup_ua
709 $request_constructor
710 my \$res = \$ua->request( $request_args );
711 @postamble
712 SNIPPET
713 };
714  
715 86 ▶sub as_http_tiny_snippet( $self, %options ) {
Subroutine as_http_tiny_snippet: called CC=15 SCAR=28.6
Pod undocumented
716 86 ▶ $options{ prefix } ||= '';
Condition50%1/2
$options{"prefix"} ||= ""
l!l
086
MC/DC0%0/1
$options{"prefix"} ||= ""
$options{"prefix"}
Truth table50%1/2
A
A= $options{"prefix"}
Aresult
11
00
717 86 ▶ $options{ implicit_headers } ||= [];
Condition50%1/2
$options{"implicit_headers"} ||= []
l!l
086
MC/DC0%0/1
$options{"implicit_headers"} ||= []
$options{"implicit_headers"}
Truth table50%1/2
A
A= $options{"implicit_headers"}
Aresult
11
00
718  
719 86 push @{ $options{ implicit_headers }}, 'Host'; # HTTP::Tiny dislikes that header
720  
721 86 my @preamble;
722 my @postamble;
723 86 my %ssl_options;
724 86 ▶ push @preamble, @{ $options{ preamble } } if $options{ preamble };
Branch: 1 branch
BranchTrueFalseTotal
if $options{"preamble"} 86 0 86
725 0 ▶ push @postamble, @{ $options{ postamble } } if $options{ postamble };
Branch: 1 branch
BranchTrueFalseTotal
if $options{"postamble"} 0 86 86
726 86 my @setup_ua = ('');
727  
728 86 my $request_args = join ", ",
729 '$r',
730 $self->_pairlist([
731 maybe ':content_file', $self->output
732 ], '')
733 ;
734 86 my $init_cookie_jar = $self->_init_cookie_jar_tiny();
735 86 ▶ if( my $p = $init_cookie_jar->{preamble}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $p = $init_cookie_jar->{"preamble"}) 2 84 86
736 2 push @preamble, @{$p}
737 };
738  
739 86 my @ssl;
740 86 ▶ if( $self->insecure ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->insecure) { } 1 85 86
741 } else {
742 85 push @ssl, verify_SSL => 1;
743 };
744 86 ▶ if( $self->cert ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->cert) 0 86 86
745 0 push @preamble, 'use IO::Socket::SSL;';
746 0 $ssl_options{ SSL_cert_file } = $self->cert;
747 };
748 86 ▶ if( $self->show_error ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->show_error) { } 9 77 86
elsif ($self->fail) { } 0 77 77
749 9 push @postamble,
750 ' die $res->{reason} if !$res->{success};',
751 } elsif( $self->fail ) {
752 0 push @postamble,
753 ' exit 1 if !$res->{success};',
754 };
755 my $constructor_args = join ",",
756 $self->_pairlist([
757 @ssl,
758 maybe timeout => $self->timeout,
759 maybe local_address => $self->local_address,
760 maybe max_size => $self->max_filesize,
761 maybe cookie_jar => $init_cookie_jar->{code},
762 86 ▶ maybe SSL_options => keys %ssl_options ? \%ssl_options : undef,
Branch: 1 branch
BranchTrueFalseTotal
keys %ssl_options ? : 0 86 86
763 ], '')
764 ;
765 86 ▶ if( defined( my $credentials = $self->credentials )) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $credentials = $self->credentials)) 0 86 86
766 0 my( $user, $pass ) = split /:/, $credentials, 2;
767 0 my $setup_credentials = sprintf qq{\$ua->credentials("%s","%s");},
768 quotemeta $user,
769 quotemeta $pass;
770 0 push @setup_ua, $setup_credentials;
771 };
772  
773 86 ▶ @setup_ua = ()
Branch: 1 branch
BranchTrueFalseTotal
if @setup_ua == 1 86 0 86
774 if @setup_ua == 1;
775  
776 86 @preamble = map { "$options{prefix} $_\n" } @preamble;
777 86 @postamble = map { "$options{prefix} $_\n" } @postamble;
778 0 @setup_ua = map { "$options{prefix} $_\n" } @setup_ua;
779  
780 86 my @content = $self->_build_quoted_body();
781 86 ▶ if( grep {/\S/} @content ) {
Branch: 2 branches
BranchTrueFalseTotal
if (grep {/\S/u;} @content) { } 11 75 86
elsif (@{$self->form_args;}) { } 2 73 75
782 11 unshift @content, 'content => ',
783 75 } elsif( @{ $self->form_args }) {
784 2 my $req = HTTP::Request::Common::POST(
785 'https://example.com',
786 Content_Type => 'form-data',
787 Content => $self->form_args,
788 );
789 2 @content = ('content => ', $self->_build_quoted_body( $req->content ));
790 2 $self->headers->{ 'Content-Type' } = join "; ", $req->headers->content_type;
791 }
792  
793 86 return <<SNIPPET;
794 @preamble
795 my \$ua = HTTP::Tiny->new($constructor_args);@setup_ua
796 my \$res = \$ua->request(
797 86 '@{[$self->method]}' => '@{[$self->uri]}',
798 {
799 headers => {
800 86 @{[$self->_build_tiny_headers(' ', %options)]}
801 },
802 @content
803 },
804 );
805 @postamble
806 SNIPPET
807 };
808  
809 5 ▶sub as_mojolicious_snippet( $self, %options ) {
Subroutine as_mojolicious_snippet: called CC=17 SCAR=36.7
Pod undocumented
810 5 ▶ $options{ prefix } ||= '';
Condition50%1/2
$options{"prefix"} ||= ""
l!l
05
MC/DC0%0/1
$options{"prefix"} ||= ""
$options{"prefix"}
Truth table50%1/2
A
A= $options{"prefix"}
Aresult
11
00
811 5 ▶ $options{ implicit_headers } ||= [];
Condition50%1/2
$options{"implicit_headers"} ||= []
l!l
05
MC/DC0%0/1
$options{"implicit_headers"} ||= []
$options{"implicit_headers"}
Truth table50%1/2
A
A= $options{"implicit_headers"}
Aresult
11
00
812  
813 5 my @preamble;
814 my @postamble;
815 5 my %ssl_options;
816 5 ▶ push @preamble, @{ $options{ preamble } } if $options{ preamble };
Branch: 1 branch
BranchTrueFalseTotal
if $options{"preamble"} 5 0 5
817 0 ▶ push @postamble, @{ $options{ postamble } } if $options{ postamble };
Branch: 1 branch
BranchTrueFalseTotal
if $options{"postamble"} 0 5 5
818 5 my @setup_ua = ('');
819  
820 5 my $request_args = join ", ",
821 '$r',
822 $self->_pairlist([
823 maybe ':content_file', $self->output
824 ], '')
825 ;
826 5 my $init_cookie_jar = $self->_init_cookie_jar_mojolicious();
827 5 ▶ if( my $p = $init_cookie_jar->{preamble}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $p = $init_cookie_jar->{"preamble"}) 0 5 5
828 0 push @preamble, @{$p}
829 };
830  
831 5 my @ssl;
832 5 ▶ if( $self->insecure ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->insecure) 0 5 5
833 0 push @ssl, insecure => 1,
834 };
835 5 ▶ if( $self->cert ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->cert) 0 5 5
836 0 push @ssl, cert => $self->cert,
837 };
838 5 ▶ if( $self->show_error ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->show_error) { } 0 5 5
elsif ($self->fail) { } 0 5 5
839 0 push @postamble,
840 ' die $res->message if $res->is_error;',
841 } elsif( $self->fail ) {
842 0 push @postamble,
843 ' exit 1 if !$res->is_error;',
844 };
845 5 my $socket_options = {};
846 5 ▶ if( my $host = $self->local_address ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $host = $self->local_address) 0 5 5
847 0 $socket_options->{ LocalAddr } = $host;
848 }
849 my $constructor_args = join ",",
850 $self->_pairlist([
851 @ssl,
852 keys %$socket_options ? ( socket_options => $socket_options ) : (),
853 maybe request_timeout => $self->timeout,
854 maybe max_response_size => $self->max_filesize,
855 maybe cookie_jar => $init_cookie_jar->{code},
856 5 ▶ maybe SSL_options => keys %ssl_options ? \%ssl_options : undef,
Branch: 2 branches
BranchTrueFalseTotal
keys %$socket_options ? : 0 5 5
keys %ssl_options ? : 0 5 5
857 ], '')
858 ;
859 5 ▶ if( defined( my $credentials = $self->credentials )) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $credentials = $self->credentials)) 0 5 5
860 0 my( $user, $pass ) = split /:/, $credentials, 2;
861 0 my $setup_credentials = sprintf qq{\$ua->userinfo("%s","%s");},
862 quotemeta $user,
863 quotemeta $pass;
864 0 push @setup_ua, $setup_credentials;
865 };
866  
867 5 ▶ @setup_ua = ()
Branch: 1 branch
BranchTrueFalseTotal
if @setup_ua == 1 5 0 5
868 if @setup_ua == 1;
869  
870 5 @preamble = map { "$options{prefix} $_\n" } @preamble;
871 0 @postamble = map { "$options{prefix} $_\n" } @postamble;
872 0 @setup_ua = map { "$options{prefix} $_\n" } @setup_ua;
873  
874 5 my $content = $self->_build_quoted_body();
875 5 ▶ if( @{ $self->form_args }) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->form_args;}) 0 5 5
876 0 my $req = HTTP::Request::Common::POST(
877 'https://example.com',
878 Content_Type => 'form-data',
879 Content => $self->form_args,
880 );
881 0 ▶ $content ||= $self->_build_quoted_body( $req->content );
Condition0%0/3
$content ||= $self->_build_quoted_body($req->content)
l!l&&r!l&&!r
000
MC/DC0%0/2
$content ||= $self->_build_quoted_body($req->content)
$content $self->_build_quoted_body($req->content)
Truth table0%0/3
A ||= B
A= $contentB= $self->_build_quoted_body($req->content)
ABresult
1X1
011
000
882 0 $self->headers->{ 'Content-Type' } = join "; ", $req->headers->content_type;
883 }
884  
885 5 return <<SNIPPET;
886 @preamble
887 my \$ua = Mojo::UserAgent->new($constructor_args);@setup_ua
888 my \$tx = \$ua->build_tx(
889 5 '@{[$self->method]}' => '@{[$self->uri]}',
890 {
891 5 @{[$self->_build_mojolicious_headers(' ', %options)]}
892 },
893 $content
894 );
895 my \$res = \$ua->start(\$tx)->result;
896 @postamble
897 SNIPPET
898 };
899  
900 0 ▶sub as_rest_client_snippet( $self, %options ) {
Subroutine as_rest_client_snippet: not called CC=12 SCAR=50.5
Pod undocumented
901 0 ▶ $options{ prefix } ||= '';
Condition0%0/2
$options{"prefix"} ||= ""
l!l
00
MC/DC0%0/1
$options{"prefix"} ||= ""
$options{"prefix"}
Truth table0%0/2
A
A= $options{"prefix"}
Aresult
11
00
902 0 ▶ $options{ implicit_headers } ||= [];
Condition0%0/2
$options{"implicit_headers"} ||= []
l!l
00
MC/DC0%0/1
$options{"implicit_headers"} ||= []
$options{"implicit_headers"}
Truth table0%0/2
A
A= $options{"implicit_headers"}
Aresult
11
00
903  
904 0 my @preamble;
905 my @postamble;
906 0 my %ssl_options;
907 0 ▶ push @preamble, @{ $options{ preamble } } if $options{ preamble };
Branch: 1 branch
BranchTrueFalseTotal
if $options{"preamble"} 0 0 0
908 0 ▶ push @postamble, @{ $options{ postamble } } if $options{ postamble };
Branch: 1 branch
BranchTrueFalseTotal
if $options{"postamble"} 0 0 0
909 0 my @setup_ua = ('');
910  
911 0 my $request_args = join ", ",
912 '$r',
913 $self->_pairlist([
914 maybe ':content_file', $self->output
915 ], '')
916 ;
917 0 my $init_cookie_jar = $self->_init_cookie_jar_tiny();
918 0 ▶ if( my $p = $init_cookie_jar->{preamble}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $p = $init_cookie_jar->{"preamble"}) 0 0 0
919 0 push @preamble, @{$p}
920 };
921  
922 0 ▶ if( $self->show_error ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->show_error) { } 0 0 0
elsif ($self->fail) { } 0 0 0
923 0 push @postamble,
924 ' die $res->{reason} if !$res->{success};',
925 } elsif( $self->fail ) {
926 0 push @postamble,
927 ' exit 1 if !$res->{success};',
928 };
929 0 my $constructor_args = join ",",
930 $self->_pairlist([
931 maybe timeout => $self->timeout,
932 maybe host => $self->host,
933 maybe cert => $self->cert,
934 maybe timeout => $self->timeout,
935 ], '')
936 ;
937 0 ▶ if( defined( my $credentials = $self->credentials )) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $credentials = $self->credentials)) 0 0 0
938 0 my( $user, $pass ) = split /:/, $credentials, 2;
939 0 my $setup_credentials = sprintf qq{\$ua->credentials("%s","%s");},
940 quotemeta $user,
941 quotemeta $pass;
942 0 push @setup_ua, $setup_credentials;
943 };
944  
945 0 ▶ @setup_ua = ()
Branch: 1 branch
BranchTrueFalseTotal
if @setup_ua == 1 0 0 0
946 if @setup_ua == 1;
947  
948 0 @preamble = map { "$options{prefix} $_\n" } @preamble;
949 0 @postamble = map { "$options{prefix} $_\n" } @postamble;
950 0 @setup_ua = map { "$options{prefix} $_\n" } @setup_ua;
951  
952 0 my @content = $self->_build_quoted_body();
953 0 ▶ if( grep {/\S/} @content ) {
Branch: 2 branches
BranchTrueFalseTotal
if (grep {/\S/u;} @content) { } 0 0 0
elsif (@{$self->form_args;}) { } 0 0 0
954 0 unshift @content, 'content => ',
955 0 } elsif( @{ $self->form_args }) {
956 0 my $req = HTTP::Request::Common::POST(
957 'https://example.com',
958 Content_Type => 'form-data',
959 Content => $self->form_args,
960 );
961 0 @content = ('content => ', $self->_build_quoted_body( $req->content ));
962 0 $self->headers->{ 'Content-Type' } = join "; ", $req->headers->content_type;
963 }
964  
965 0 return <<SNIPPET;
966 @preamble
967 my \$ua = REST::Client->new($constructor_args);@setup_ua
968 my \$res = \$ua->request(
969 0 '@{[$self->method]}' => '@{[$self->uri]}',
970 {
971 headers => {
972 0 @{[$self->_build_rest_client_headers(' ', %options)]}
973 },
974 @content
975 },
976 );
977 @postamble
978 SNIPPET
979 };
980  
981 =head2 C<< $r->as_curl >>
982  
983 print $r->as_curl;
984  
985 Returns a curl command line representing the request
986  
987 This is convenient if you started out from something else or want a canonical
988 representation of a curl command line.
989  
990 =over 4
991  
992 =item B<curl>
993  
994 The curl command to be used. Default is C<curl>.
995  
996 =back
997  
998 =cut
999  
1000 # These are what curl uses as defaults, not what Perl should use as default!
1001 19 our %curl_header_defaults = (
1002 'Accept' => '*/*',
1003 #'Accept-Encoding' => 'deflate, gzip',
1004 # For Perl, use HTTP::Message::decodable() instead of the above list
1005 );
1006  
1007 73 ▶sub as_curl($self,%options) {
Subroutine as_curl: called CC=28 SCAR=34.2
Pod documented
1008 $options{ curl } = 'curl'
1009 73 ▶ if ! exists $options{ curl };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $options{"curl"} 0 73 73
1010 $options{ long_options } = 1
1011 73 ▶ if ! exists $options{ long_options };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $options{"long_options"} 73 0 73
1012  
1013 73 my @request_commands;
1014  
1015 73 ▶ if( $self->method eq 'HEAD' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->method eq 'HEAD') { } 1 72 73
elsif ($self->method ne 'GET') { } 16 56 72
1016 push @request_commands,
1017 1 ▶ $options{ long_options } ? '--head' : '-I';
Branch: 1 branch
BranchTrueFalseTotal
$options{'long_options'} ? : 1 0 1
1018  
1019 } elsif( $self->method ne 'GET' ) {
1020 push @request_commands,
1021 16 ▶ $options{ long_options } ? '--request' : '-X',
Branch: 1 branch
BranchTrueFalseTotal
$options{'long_options'} ? : 16 0 16
1022 $self->method;
1023 };
1024  
1025 73 ▶ if( scalar keys %{ $self->headers }) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %{$self->headers;}) 73 0 73
1026 73 for my $h (sort keys %{$self->headers}) {
1027 253 my $v = $self->headers->{$h};
1028  
1029 253 my $default;
1030 253 ▶ if( exists $curl_header_defaults{ $h }) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $HTTP::Request::CurlParameters::curl_header_defaults{$h}) 72 181 253
1031 72 $default = $curl_header_defaults{ $h };
1032 };
1033  
1034 253 ▶ if( ! ref $v ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $v) 252 1 253
1035 252 $v = [$v];
1036 };
1037 253 for my $val (@$v) {
1038 254 ▶ if( !defined $default or $val ne $default ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $default or $val ne $default) 183 71 254
Condition100%3/3
not defined $default or $val ne $default
l!l&&r!l&&!r
182171
MC/DC100%2/2
not defined $default or $val ne $default
not defined $default $val ne $default
Truth table100%3/3
not(A) or B
A= not defined $defaultB= $val ne $default
ABresult
1X1
011
000
1039 # also skip the Host: header if it derives from $uri
1040 183 ▶ if( $h eq 'Host' and ($val eq $self->uri->host_port
Branch: 3 branches
BranchTrueFalseTotal
if ($h eq 'Host' and $val eq $self->uri->host_port || $val eq $self->uri->host) { } 71 112 183
elsif ($h eq 'Content-Length' and $self->post_data && $val == length $self->post_data || $self->body && $val == length $self->body) { } 11 101 112
elsif ($h eq 'User-Agent') { } 73 28 101
Condition83%5/6
$h eq "Host" and $val eq $self->uri->host_port || $val eq $self->uri->host
!ll&&!rl&&r
110271
$val eq $self->uri->host_port or $val eq $self->uri->host
l!l&&r!l&&!r
7102
MC/DC66%2/3
$h eq "Host" and $val eq $self->uri->host_port || $val eq $self->uri->host
$h eq "Host" $val eq $self->uri->host_port $val eq $self->uri->host
Truth table75%3/4
A and B or C
A= $h eq "Host"B= $val eq $self->uri->host_portC= $val eq $self->uri->host
ABCresult
0XX0
1000
11X1
1011
1041 or $val eq $self->uri->host )) {
1042 # trivial host header
1043  
1044 # also skip the Content-Length header if it derives from the body
1045 ▶ } elsif( $h eq 'Content-Length' and
Condition58%7/12
$h eq "Content-Length" and $self->post_data && $val == length $self->post_data || $self->body && $val == length $self->body
!ll&&!rl&&r
101011
$self->post_data and $val == length $self->post_data or $self->body and $val == length $self->body
l!l&&r!l&&!r
1100
$self->post_data and $val == length $self->post_data
!ll&&!rl&&r
0101
$self->body and $val == length $self->body
!ll&&!rl&&r
0010
MC/DC20%1/5
$h eq "Content-Length" and $self->post_data && $val == length $self->post_data || $self->body && $val == length $self->body
$h eq "Content-Length" $self->post_data $val == length $self->post_data $self->body $val == length $self->body
Truth table37%3/8
A and B and C or D and E
A= $h eq "Content-Length"B= $self->post_dataC= $val == length $self->post_dataD= $self->bodyE= $val == length $self->body
ABCDEresult
0XXXX0
10X0X0
10X100
1100X0
110100
111XX1
10X111
110111
1046 (
1047 ($self->post_data and $val == length $self->post_data)
1048 or ($self->body and $val == length $self->body)
1049 )) {
1050 # trivial content-length header
1051  
1052 } elsif( $h eq 'User-Agent' ) {
1053 push @request_commands,
1054 73 ▶ $options{ long_options } ? '--user-agent' : '-A',
Branch: 1 branch
BranchTrueFalseTotal
$options{'long_options'} ? : 73 0 73
1055 $val;
1056 } else {
1057 push @request_commands,
1058 28 ▶ $options{ long_options } ? '--header' : '-h',
Branch: 1 branch
BranchTrueFalseTotal
$options{'long_options'} ? : 28 0 28
1059 "$h: $val";
1060 };
1061 };
1062 };
1063 };
1064 };
1065  
1066 73 ▶ if( @{$self->form_args} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@{$self->form_args;}) { } 2 71 73
elsif (defined(my $body = $self->body)) { } 11 60 71
1067 2 my $form_args = $self->form_args;
1068 2 for (0..(@{$form_args}/2-1)) {
1069 4 my( $name, $val) = ($form_args->[$_*2], $form_args->[$_*2+1]);
1070 4 push @request_commands,
1071 '--form-string',
1072 "$name=$val";
1073 }
1074  
1075 } elsif( defined( my $body = $self->body )) {
1076 # Can we collapse stuff into --json or other nicer representations
1077 # here ?!
1078 11 push @request_commands,
1079 '--data-raw',
1080 $body;
1081 };
1082  
1083 73 push @request_commands, $self->uri;
1084  
1085 return
1086 #(defined $options{ curl } ? $options{curl} : () ),
1087 73 @request_commands;
1088 }
1089  
1090 =head2 C<< $r->as_wget >>
1091  
1092 print $r->as_wget;
1093  
1094 Returns a curl command line representing the request
1095  
1096 This is convenient if you started out from something else or want a canonical
1097 representation of a curl command line.
1098  
1099 =over 4
1100  
1101 =item B<wget>
1102  
1103 The curl command to be used. Default is C<wget>.
1104  
1105 =back
1106  
1107 =cut
1108  
1109 # These are what wget uses as defaults, not what Perl should use as default!
1110 19 our %wget_header_defaults = (
1111 'Accept' => '*/*',
1112 'Accept-Encoding' => 'identity',
1113 'User-Agent' => 'Wget/1.21',
1114 'Connection' => 'Keep-Alive',
1115 );
1116  
1117 0 ▶sub as_wget($self,%options) {
Subroutine as_wget: not called CC=23 SCAR=63.1
Pod documented
1118 $options{ wget } = 'wget'
1119 0 ▶ if ! exists $options{ wget };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $options{"wget"} 0 0 0
1120 $options{ long_options } = 1
1121 0 ▶ if ! exists $options{ long_options };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $options{"long_options"} 0 0 0
1122  
1123 0 my @request_commands;
1124  
1125 0 ▶ if( $self->method ne 'GET' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->method ne "GET") 0 0 0
1126 0 ▶ if( $self->method eq 'POST' and $self->body ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->method eq 'POST' and $self->body) { } 0 0 0
Condition0%0/3
$self->method eq "POST" and $self->body
!ll&&!rl&&r
000
MC/DC0%0/2
$self->method eq "POST" and $self->body
$self->method eq "POST" $self->body
Truth table0%0/3
A and B
A= $self->method eq "POST"B= $self->body
ABresult
0X0
100
111
1127 # This is implied by '--post-data', below
1128 } else {
1129 0 push @request_commands,
1130 '--method' => $self->method;
1131 };
1132 };
1133  
1134 0 ▶ if( scalar keys %{ $self->headers }) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %{$self->headers;}) 0 0 0
1135 0 my %h = %{ $self->headers };
1136  
1137 # "--no-cache" implies two headers, Cache-Control and Pragma
1138 my $is_cache = exists $h{ 'Pragma' }
1139 && exists $h{ 'Cache-Control' }
1140 && $h{ 'Cache-Control' } =~ /^no-cache\b/
1141 0 ▶ && $h{ 'Pragma' } eq 'no-cache'
Condition0%0/9
exists $h{'Pragma'} && exists $h{'Cache-Control'} && $h{'Cache-Control'} =~ /^no-cache\b/u && $h{'Pragma'} eq 'no-cache'
!ll&&!rl&&r
000
exists $h{"Pragma"} and exists $h{"Cache-Control"} and $h{"Cache-Control"} =~ /^no-cache\b/u
!ll&&!rl&&r
000
exists $h{"Pragma"} and exists $h{"Cache-Control"}
!ll&&!rl&&r
000
MC/DC0%0/4
exists $h{'Pragma'} && exists $h{'Cache-Control'} && $h{'Cache-Control'} =~ /^no-cache\b/u && $h{'Pragma'} eq 'no-cache'
exists $h{"Pragma"} exists $h{"Cache-Control"} $h{"Cache-Control"} =~ /^no-cache\b/u $h{'Pragma'} eq 'no-cache'
Truth table0%0/5
A and B and C && D
A= exists $h{"Pragma"}B= exists $h{"Cache-Control"}C= $h{"Cache-Control"} =~ /^no-cache\b/uD= $h{'Pragma'} eq 'no-cache'
ABCDresult
0XXX0
10XX0
110X0
11100
11111
1142 ;
1143 0 ▶ if( $is_cache ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_cache) 0 0 0
1144 0 delete $h{ 'Pragma' };
1145 0 delete $h{ 'Cache-Control' };
1146 0 push @request_commands, '--no-cache';
1147 };
1148  
1149 0 for my $name (sort keys %h) {
1150 0 my $v = $h{ $name };
1151  
1152 0 my $default;
1153 0 ▶ if( exists $wget_header_defaults{ $name }) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $HTTP::Request::CurlParameters::wget_header_defaults{$name}) 0 0 0
1154 0 $default = $wget_header_defaults{ $name };
1155 };
1156  
1157 0 ▶ if( ! ref $v ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $v) 0 0 0
1158 0 $v = [$v];
1159 };
1160 0 for my $val (@$v) {
1161 0 ▶ if( !defined $default or $val ne $default ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $default or $val ne $default) 0 0 0
Condition0%0/3
not defined $default or $val ne $default
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $default or $val ne $default
not defined $default $val ne $default
Truth table0%0/3
not(A) or B
A= not defined $defaultB= $val ne $default
ABresult
1X1
011
000
1162 # also skip the Host: header if it derives from $uri
1163 0 ▶ if( $name eq 'Host' and ($val eq $self->uri->host_port
Branch: 2 branches
BranchTrueFalseTotal
if ($name eq 'Host' and $val eq $self->uri->host_port || $val eq $self->uri->host) { } 0 0 0
elsif ($name eq 'User-Agent') { } 0 0 0
Condition0%0/6
$name eq "Host" and $val eq $self->uri->host_port || $val eq $self->uri->host
!ll&&!rl&&r
000
$val eq $self->uri->host_port or $val eq $self->uri->host
l!l&&r!l&&!r
000
MC/DC0%0/3
$name eq "Host" and $val eq $self->uri->host_port || $val eq $self->uri->host
$name eq "Host" $val eq $self->uri->host_port $val eq $self->uri->host
Truth table0%0/4
A and B or C
A= $name eq "Host"B= $val eq $self->uri->host_portC= $val eq $self->uri->host
ABCresult
0XX0
1000
11X1
1011
1164 or $val eq $self->uri->host )) {
1165 # trivial host header, ignore
1166 } elsif( $name eq 'User-Agent' ) {
1167 0 push @request_commands,
1168 '--user-agent',
1169 $val;
1170 } else {
1171 0 push @request_commands,
1172 '--header',
1173 "$name: $val";
1174 };
1175 };
1176 };
1177 };
1178 };
1179  
1180 0 ▶ if( my $body = $self->body ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $body = $self->body) 0 0 0
1181 0 ▶ if( $self->method eq 'POST' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->method eq 'POST') { } 0 0 0
1182 0 push @request_commands,
1183 '--post-data',
1184 $body;
1185 } else {
1186 0 push @request_commands,
1187 '--body-data',
1188 $body;
1189 };
1190 };
1191  
1192 0 push @request_commands, $self->uri;
1193  
1194 return
1195 #(defined $options{ curl } ? $options{curl} : () ),
1196 0 @request_commands;
1197 }
1198  
1199  
1200 =head2 C<< $r->clone >>
1201  
1202 Returns a shallow copy of the object
1203  
1204 =cut
1205  
1206 0 ▶sub clone( $self, %options ) {
Subroutine clone: not called CC=1 SCAR=6.9
Pod documented
1207 0 (ref $self)->new( %$self, %options )
1208 }
1209  
1210 19 1;
1211  
1212 =head1 REPOSITORY
1213  
1214 The public repository of this module is
1215 L<http://github.com/Corion/HTTP-Request-FromCurl>.
1216  
1217 =head1 SUPPORT
1218  
1219 The public support forum of this module is
1220 L<https://perlmonks.org/>.
1221  
1222 =head1 BUG TRACKER
1223  
1224 Please report bugs in this module via the Github bug queue at
1225 L<https://github.com/Corion/HTTP-Request-FromCurl/issues>
1226  
1227 =head1 AUTHOR
1228  
1229 Max Maischein C<corion@cpan.org>
1230  
1231 =head1 COPYRIGHT (c)
1232  
1233 Copyright 2018-2026 by Max Maischein C<corion@cpan.org>.
1234  
1235 =head1 LICENSE
1236  
1237 This module is released under the same terms as Perl itself.
1238  
1239 =cut
« lib/HTTP/Request/FromWget.pm ↑ Summary lib/HTTP/Request/FromCurl.pm »