lib/HTTP/Request/FromCurl.pm

filter: Statementstmt 94.7% 217 / 229 Branchbran 81.9% 95 / 116 Conditioncond 84.4% 49 / 58 MC/DCmcdc 76.6% 23 / 30 Subroutinesub 100% 21 / 21 Podpod 100% 2 / 2 totaltotal 89.2% 407 / 456 CC 87 SCAR 36.9
CC
87
Cov
90%
_build_request
66.2
squash_uri
9.0
new
8.4
CRAP 40.0
« lib/HTTP/Request/CurlParameters.pm ↑ Summary lib/HTTP/Request/FromFetch.pm »
1 package HTTP::Request::FromCurl 0.57;
2 14 ▶use 5.020;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 14 ▶use File::Basename 'basename';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 14 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 14 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 14 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 14 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 14 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 14 ▶use File::Spec::Unix;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 14 ▶use HTTP::Request::CurlParameters;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 14 ▶use HTTP::Request::Generator 'generate_requests';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 14 ▶use PerlX::Maybe;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 14 ▶use MIME::Base64 'encode_base64';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 14 ▶use File::Basename 'basename';
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 14 ▶use feature 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 14 ▶no warnings 'experimental::signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 =head1 NAME
20  
21 HTTP::Request::FromCurl - create a HTTP::Request from a curl command line
22  
23 =head1 SYNOPSIS
24  
25 my $req = HTTP::Request::FromCurl->new(
26 # Note - curl itself may not appear
27 argv => ['https://example.com'],
28 );
29  
30 my $req = HTTP::Request::FromCurl->new(
31 command => 'https://example.com',
32 );
33  
34 my $req = HTTP::Request::FromCurl->new(
35 command_curl => 'curl -A mycurl/1.0 https://example.com',
36 );
37  
38 my @requests = HTTP::Request::FromCurl->new(
39 command_curl => 'curl -A mycurl/1.0 https://example.com https://www.example.com',
40 );
41 # Send the requests
42 for my $r (@requests) {
43 $ua->request( $r->as_request )
44 }
45  
46 =head1 RATIONALE
47  
48 C<curl> command lines are found everywhere in documentation. The Firefox
49 developer tools can also copy network requests as C<curl> command lines from
50 the network panel. This module enables converting these to Perl code.
51  
52 =head1 METHODS
53  
54 =head2 C<< ->new >>
55  
56 my $req = HTTP::Request::FromCurl->new(
57 # Note - curl itself may not appear
58 argv => ['--user-agent', 'myscript/1.0', 'https://example.com'],
59 );
60  
61 my $req = HTTP::Request::FromCurl->new(
62 # Note - curl itself may not appear
63 command => '--user-agent myscript/1.0 https://example.com',
64 );
65  
66 The constructor returns one or more L<HTTP::Request::CurlParameters> objects
67 that encapsulate the parameters. If the command generates multiple requests,
68 they will be returned in list context. In scalar context, only the first request
69 will be returned. Note that the order of URLs between C<--url> and unadorned URLs will be changed in the sense that all unadorned URLs will be handled first.
70  
71 my $req = HTTP::Request::FromCurl->new(
72 command => '--data-binary @/etc/passwd https://example.com',
73 read_files => 1,
74 );
75  
76 =head3 Options
77  
78 =over 4
79  
80 =item B<argv>
81  
82 An arrayref of commands as could be given in C< @ARGV >.
83  
84 =item B<command>
85  
86 A scalar in a command line, excluding the C<curl> command
87  
88 =item B<command_curl>
89  
90 A scalar in a command line, including the C<curl> command
91  
92 =item B<read_files>
93  
94 Do read in the content of files specified with (for example)
95 C<< --data=@/etc/passwd >>. The default is to not read the contents of files
96 specified this way.
97  
98 =back
99  
100 =head1 GLOBAL VARIABLES
101  
102 =head2 C<< %default_headers >>
103  
104 Contains the default headers added to every request
105  
106 =cut
107  
108 14 our %default_headers = (
109 'Accept' => '*/*',
110 'User-Agent' => 'curl/7.55.1',
111 );
112  
113 =head2 C<< @option_spec >>
114  
115 Contains the L<Getopt::Long> specification of the recognized command line
116 parameters.
117  
118 The following C<curl> options are recognized but largely ignored:
119  
120 =over 4
121  
122 =item C< --disable >
123  
124 =item C< --dump-header >
125  
126 =item C< --include >
127  
128 =item C< --follow >
129  
130 =item C< --location >
131  
132 =item C< --progress-bar >
133  
134 =item C< --show-error >
135  
136 =item C< --fail >
137  
138 =item C< --silent >
139  
140 =item C< --verbose >
141  
142 =item C< --junk-session-cookies >
143  
144 If you want to keep session cookies between subsequent requests, you need to
145 provide a cookie jar in your user agent.
146  
147 =item C<--next>
148  
149 Resetting the UA between requests is something you need to handle yourself
150  
151 =item C<--parallel>
152  
153 =item C<--parallel-immediate>
154  
155 =item C<--parallel-max>
156  
157 Parallel requests is something you need to handle in the UA
158  
159 =back
160  
161 =cut
162  
163 14 our @option_spec = (
164 'user-agent|A=s',
165 'verbose|v', # ignored
166 'show-error|S', # ignored
167 'fail|f',
168 'silent|s', # ignored
169 'anyauth', # ignored
170 'basic',
171 'buffer!',
172 'capath=s',
173 # 'cacert=s', # to be added
174 'cert|E=s', # this is the client certificate
175 'compressed',
176 'cookie|b=s',
177 'cookie-jar|c=s',
178 'data|d=s@',
179 'data-ascii=s@',
180 'data-binary=s@',
181 'data-raw=s@',
182 'data-urlencode=s@',
183 'digest',
184 'disable|q!', # ignored
185 'dump-header|D=s', # ignored
186 'referrer|e=s',
187 'follow', # ignored, we always follow redirects
188 'form|F=s@',
189 'form-string=s@',
190 'get|G',
191 'globoff|g',
192 'head|I',
193 'header|H=s@',
194 'include|i', # ignored
195 'interface=s',
196 'insecure|k',
197 'json=s@',
198 'location|L', # ignored, we always follow redirects
199 'max-filesize=s',
200 'max-time|m=s',
201 'ntlm',
202 'keepalive!',
203 'range=s',
204 'request|X=s',
205 'oauth2-bearer=s',
206 'output|o=s',
207 'progress-bar|#', # ignored
208 'user|u=s',
209 'next', # ignored
210 'parallel|Z', # ignored
211 'parallel-immediate', # ignored
212 'parallel-max=i', # ignored
213 'junk-session-cookies|j', # ignored, must be set in code using the HTTP request
214 'unix-socket=s',
215 'url=s@',
216 );
217  
218 151 ▶sub new( $class, %options ) {
Subroutine new: called CC=8 SCAR=21.3
Pod documented
219 151 my $cmd = $options{ argv };
220  
221 151 ▶ if( $options{ command }) {
Branch: 2 branches
BranchTrueFalseTotal
if ($options{'command'}) { } 1 150 151
elsif ($options{'command_curl'}) { } 0 150 150
222 1 require Text::ParseWords;
223 1 $cmd = [ Text::ParseWords::shellwords($options{ command }) ];
224  
225 } elsif( $options{ command_curl }) {
226 0 require Text::ParseWords;
227 0 $cmd = [ Text::ParseWords::shellwords($options{ command_curl }) ];
228  
229 # remove the implicit curl command:
230 0 shift @$cmd;
231 };
232  
233 151 for (@$cmd) {
234 930 ▶ $_ = '--next'
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "-:" 1 929 930
235 if $_ eq '-:'; # GetOptions does not like "next|:" as specification
236 };
237  
238 151 my $p = Getopt::Long::Parser->new(
239 config => [ 'bundling', 'no_auto_abbrev', 'no_ignore_case_always' ],
240 );
241 151 ▶ $p->getoptionsfromarray( $cmd,
Branch: 1 branch
BranchTrueFalseTotal
$p->getoptionsfromarray($cmd, \my %curl_options, @HTTP::Request::FromCurl::option_spec) or return 151 0 151
242 \my %curl_options,
243 @option_spec,
244 ) or return;
245 151 ▶ my @urls = (@$cmd, @{ $curl_options{ url } || [] });
Condition100%2/2
$curl_options{'url'} || []
l!l
1150
MC/DC100%1/1
$curl_options{'url'} || []
$curl_options{'url'}
Truth table100%2/2
A
A= $curl_options{'url'}
Aresult
11
00
246  
247 return
248 151 ▶ wantarray ? map { $class->_build_request( $_, \%curl_options, %options ) } @urls
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 150 1 151
249 : ($class->_build_request( $urls[0], \%curl_options, %options ))[0]
250 ;
251 }
252  
253 =head1 METHODS
254  
255 =head2 C<< ->squash_uri( $uri ) >>
256  
257 my $uri = HTTP::Request::FromCurl->squash_uri(
258 URI->new( 'https://example.com/foo/bar/..' )
259 );
260 # https://example.com/foo/
261  
262 Helper method to clean up relative path elements from the URI the same way
263 that curl does.
264  
265 =cut
266  
267 173 ▶sub squash_uri( $class, $uri ) {
Subroutine squash_uri: called CC=9 SCAR=22.0
Pod documented
268 173 my $u = $uri->clone;
269 173 my @segments = $u->path_segments;
270  
271 173 ▶ if( $segments[-1] and ($segments[-1] eq '..' or $segments[-1] eq '.' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($segments[-1] and $segments[-1] eq ".." || $segments[-1] eq ".") 7 166 173
Condition100%6/6
$segments[-1] and $segments[-1] eq ".." || $segments[-1] eq "."
!ll&&!rl&&r
16247
$segments[-1] eq ".." or $segments[-1] eq "."
l!l&&r!l&&!r
614
MC/DC100%3/3
$segments[-1] and $segments[-1] eq ".." || $segments[-1] eq "."
$segments[-1] $segments[-1] eq ".." $segments[-1] eq "."
Truth table100%4/4
A and B or C
A= $segments[-1]B= $segments[-1] eq ".."C= $segments[-1] eq "."
ABCresult
0XX0
1000
11X1
1011
272 7 push @segments, '';
273 };
274  
275 173 @segments = grep { $_ ne '.' } @segments;
276  
277 # While we find a pair ( "foo", ".." ) remove that pair
278 173 ▶ while( grep { $_ eq '..' } @segments ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq "..";} @segments) 11 173 184
279 11 my $i = 0;
280 11 ▶ while( $i < $#segments ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $#segments) 31 11 42
281 31 ▶ if( $segments[$i] ne '..' and $segments[$i+1] eq '..') {
Branch: 1 branch
BranchTrueFalseTotal
if ($segments[$i] ne '..' and $segments[$i + 1] eq '..') { } 13 18 31
Condition100%3/3
$segments[$i] ne ".." and $segments[$i + 1] eq ".."
!ll&&!rl&&r
21613
MC/DC100%2/2
$segments[$i] ne ".." and $segments[$i + 1] eq ".."
$segments[$i] ne ".." $segments[$i + 1] eq ".."
Truth table100%3/3
A and B
A= $segments[$i] ne ".."B= $segments[$i + 1] eq ".."
ABresult
0X0
100
111
282 13 splice @segments, $i, 2;
283 } else {
284 18 $i++
285 };
286 };
287 };
288  
289 173 ▶ if( @segments < 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@segments < 2) 11 162 173
290 11 @segments = ('','');
291 };
292  
293 173 $u->path_segments( @segments );
294 173 return $u
295 }
296  
297 560 ▶sub _add_header( $self, $headers, $h, $value ) {
Subroutine _add_header: called CC=3 SCAR=11.0
298 560 ▶ if( exists $headers->{ $h }) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $headers->{$h}) { } 2 558 560
299 2 ▶ if (!ref( $headers->{ $h })) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $headers->{$h}) 2 0 2
300 2 $headers->{ $h } = [ $headers->{ $h }];
301 }
302 2 push @{ $headers->{ $h } }, $value;
303 } else {
304 558 $headers->{ $h } = $value;
305 }
306 }
307  
308 13 ▶sub _maybe_read_data_file( $self, $read_files, $data ) {
Subroutine _maybe_read_data_file: called CC=5 SCAR=16.2
309 13 my $res;
310 13 ▶ if( $read_files ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($read_files) { } 12 1 13
311 12 ▶ if( $data =~ /^\@(.*)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data =~ /^\@(.*)/u) { } 6 6 12
312 6 ▶ open my $fh, '<', $1
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $1 or die "$1: $!" 6 0 6
313 or die "$1: $!";
314 6 local $/; # / for Filter::Simple
315 6 binmode $fh;
316 6 $res = <$fh>
317 } else {
318 6 $res = $data
319 }
320 } else {
321 1 ▶ $res = ($data =~ /^\@(.*)/)
Branch: 1 branch
BranchTrueFalseTotal
$data =~ /^\@(.*)/u ? : 1 0 1
322 ? "... contents of $1 ..."
323 : $data
324 }
325 13 return $res
326 }
327  
328 4 ▶sub _maybe_read_upload_file( $self, $read_files, $data ) {
Subroutine _maybe_read_upload_file: called CC=6 SCAR=19.5
329 4 my $res;
330 4 ▶ if( $read_files ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($read_files) { } 4 0 4
331 4 ▶ if( $data =~ /^<(.*)/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($data =~ /^<(.*)/u) { } 1 3 4
elsif ($data =~ /^\@(.*)/u) { } 1 2 3
332 1 ▶ open my $fh, '<', $1
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $1 or die "$1: $!" 1 0 1
333 or die "$1: $!";
334 1 local $/; # / for Filter::Simple
335 1 binmode $fh;
336 1 $res = <$fh>
337 } elsif( $data =~ /^\@(.*)/ ) {
338 # Upload the file
339 1 $res = [ $1 => basename($1), Content_Type => 'application/octet-stream' ];
340 } else {
341 2 $res = $data
342 }
343 } else {
344 0 ▶ if( $data =~ /^[<@](.*)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data =~ /^[<\@](.*)/u) { } 0 0 0
345 0 $res = [ undef, basename($1), Content_Type => 'application/octet-stream', Content => "... contents of $1 ..." ],
346 } else {
347 0 $res = $data
348 }
349 }
350 4 return $res
351 }
352  
353 161 ▶sub _build_request( $self, $uri, $options, %build_options ) {
Subroutine _build_request: called CC=61 SCAR=41.9
354 161 my $body;
355 161 ▶ my @headers = @{ $options->{header} || []};
Condition100%2/2
$options->{'header'} || []
l!l
31130
MC/DC100%1/1
$options->{'header'} || []
$options->{'header'}
Truth table100%2/2
A
A= $options->{'header'}
Aresult
11
00
356 161 my $method = $options->{request};
357 # Ideally, we shouldn't sort the data but process it in-order
358 161 ▶ my @post_read_data = (@{ $options->{'data'} || []},
Condition100%2/2
$options->{'data'} || []
l!l
7154
MC/DC100%1/1
$options->{'data'} || []
$options->{'data'}
Truth table100%2/2
A
A= $options->{'data'}
Aresult
11
00
359 161 ▶ @{ $options->{'data-ascii'} || [] }
Condition100%2/2
$options->{'data-ascii'} || []
l!l
1160
MC/DC100%1/1
$options->{'data-ascii'} || []
$options->{'data-ascii'}
Truth table100%2/2
A
A= $options->{'data-ascii'}
Aresult
11
00
360 );
361 ;
362 161 ▶ my @post_raw_data = @{ $options->{'data-raw'} || [] },
Condition100%2/2
$options->{'data-raw'} || []
l!l
12149
MC/DC100%1/1
$options->{'data-raw'} || []
$options->{'data-raw'}
Truth table100%2/2
A
A= $options->{'data-raw'}
Aresult
11
00
363 ;
364 161 ▶ my @post_urlencode_data = @{ $options->{'data-urlencode'} || [] };
Condition100%2/2
$options->{'data-urlencode'} || []
l!l
1160
MC/DC100%1/1
$options->{'data-urlencode'} || []
$options->{'data-urlencode'}
Truth table100%2/2
A
A= $options->{'data-urlencode'}
Aresult
11
00
365 161 ▶ my @post_binary_data = @{ $options->{'data-binary'} || [] };
Condition100%2/2
$options->{'data-binary'} || []
l!l
3158
MC/DC100%1/1
$options->{'data-binary'} || []
$options->{'data-binary'}
Truth table100%2/2
A
A= $options->{'data-binary'}
Aresult
11
00
366 161 ▶ my @post_json_data = @{ $options->{'json'} || [] };
Condition100%2/2
$options->{'json'} || []
l!l
1160
MC/DC100%1/1
$options->{'json'} || []
$options->{'json'}
Truth table100%2/2
A
A= $options->{'json'}
Aresult
11
00
367  
368 161 my @form_args;
369 161 ▶ if( $options->{form}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"form"}) 2 159 161
370 # support --form uploaded_file=@myfile
371 # and --form "uploaded_text=<~/texts/content.txt"
372 push @form_args, map { /^([^=]+)=(.*)$/
373 4 ▶ ? ($1 => $self->_maybe_read_upload_file( $build_options{ read_files }, $2 ))
Branch: 1 branch
BranchTrueFalseTotal
/^([^=]+)=(.*)$/u ? : 4 0 4
374 2 : () } @{$options->{form}
375 2 };
376 };
377 161 ▶ if( $options->{'form-string'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"form-string"}) 2 159 161
378 2 ▶ push @form_args, map {; /^([^=]+)=(.*)$/ ? ($1 => $2) : (); } @{ $options->{'form-string'}};
Branch: 1 branch
BranchTrueFalseTotal
/^([^=]+)=(.*)$/u ? : 3 1 4
379 };
380  
381 # expand the URI here if wanted
382 161 my @uris = ($uri);
383 161 ▶ if( ! $options->{ globoff }) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($options->{"globoff"}) 94 67 161
384 94 @uris = map { $_->{url} } generate_requests( pattern => shift @uris, limit => $build_options{ limit } );
385 }
386  
387 161 ▶ if( $options->{'max-filesize'}
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"max-filesize"} and $options->{"max-filesize"} =~ /^(\d+)([kmg])$/iu) 1 160 161
Condition100%3/3
$options->{"max-filesize"} and $options->{"max-filesize"} =~ /^(\d+)([kmg])$/iu
!ll&&!rl&&r
15911
MC/DC100%2/2
$options->{"max-filesize"} and $options->{"max-filesize"} =~ /^(\d+)([kmg])$/iu
$options->{"max-filesize"} $options->{"max-filesize"} =~ /^(\d+)([kmg])$/iu
Truth table100%3/3
A and B
A= $options->{"max-filesize"}B= $options->{"max-filesize"} =~ /^(\d+)([kmg])$/iu
ABresult
0X0
100
111
388 and $options->{'max-filesize'} =~ m/^(\d+)([kmg])$/i ) {
389 my $mult = {
390 'k' => 1024,
391 'g' => 1024*1024*1024,
392 'm' => 1024*1024,
393 1 }->{ $2 };
394 1 $options->{'max-filesize'} = $1 * $mult;
395 }
396  
397 161 my @res;
398 161 for my $uri (@uris) {
399 163 $uri = URI->new( $uri );
400 163 $uri = $self->squash_uri( $uri );
401  
402 163 ▶ my $host = $uri->can( 'host_port' ) ? $uri->host_port : "$uri";
Branch: 1 branch
BranchTrueFalseTotal
$uri->can('host_port') ? : 159 4 163
403  
404 # Stuff we use unless nothing else hits
405 163 my %request_default_headers = %default_headers;
406  
407 # Sluuuurp
408 # Thous should be hoisted out of the loop
409 @post_binary_data = map {
410 163 $self->_maybe_read_data_file( $build_options{ read_files }, $_ );
411 } @post_binary_data;
412  
413 @post_json_data = map {
414 163 $self->_maybe_read_data_file( $build_options{ read_files }, $_ );
415 } @post_json_data;
416  
417 @post_read_data = map {
418 163 my $v = $self->_maybe_read_data_file( $build_options{ read_files }, $_ );
419 8 $v =~ s![\r\n]!!g;
420 8 $v
421 } @post_read_data;
422  
423 @post_urlencode_data = map {
424 163 ▶ m/\A([^@=]*)([=@])?(.*)\z/sm
Branch: 1 branch
BranchTrueFalseTotal
/\A([^\@=]*)([=\@])?(.*)\z/msu or die "This should never happen" 1 0 1
425 or die "This should never happen";
426 1 my ($name, $op, $content) = ($1,$2,$3);
427 1 ▶ if(! $op) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $op) { } 0 1 1
elsif ($op eq '@') { } 1 0 1
428 0 $content = $name;
429 } elsif( $op eq '@' ) {
430 1 $content = "$op$content";
431 };
432 1 ▶ if( defined $name and length $name ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $name and length $name) { } 0 1 1
Condition33%1/3
defined $name and length $name
!ll&&!rl&&r
010
MC/DC0%0/2
defined $name and length $name
defined $name length $name
Truth table33%1/3
A and B
A= defined $nameB= length $name
ABresult
0X0
100
111
433 0 $name .= '=';
434 } else {
435 1 $name = '';
436 };
437 1 my $v = $self->_maybe_read_data_file( $build_options{ read_files }, $content );
438 1 $name . uri_escape( $v )
439 } @post_urlencode_data;
440  
441 163 my $data;
442 163 ▶ if( @post_read_data
Branch: 2 branches
BranchTrueFalseTotal
if (@post_read_data or @post_binary_data or @post_raw_data or @post_urlencode_data) { } 24 139 163
elsif (@post_json_data) { } 1 138 139
Condition100%9/9
@post_read_data or @post_binary_data or @post_raw_data or @post_urlencode_data
l!l&&r!l&&!r
231139
@post_read_data or @post_binary_data or @post_raw_data
l!l&&r!l&&!r
1112140
@post_read_data or @post_binary_data
l!l&&r!l&&!r
83152
MC/DC100%4/4
@post_read_data or @post_binary_data or @post_raw_data or @post_urlencode_data
@post_read_data @post_binary_data @post_raw_data @post_urlencode_data
Truth table100%5/5
A or B or C or D
A= @post_read_dataB= @post_binary_dataC= @post_raw_dataD= @post_urlencode_data
ABCDresult
1XXX1
01XX1
001X1
00011
00000
443 or @post_binary_data
444 or @post_raw_data
445 or @post_urlencode_data
446 ) {
447 24 $data = join "&",
448 @post_read_data,
449 @post_binary_data,
450 @post_raw_data,
451 @post_urlencode_data
452 ;
453 } elsif( @post_json_data ) {
454 1 $data = join '', @post_json_data;
455 }
456  
457 163 ▶ if( @form_args) {
Branch: 4 branches
BranchTrueFalseTotal
if (@form_args) { } 4 159 163
elsif ($options->{'get'}) { } 2 157 159
elsif ($options->{'head'}) { } 2 155 157
elsif (defined $data) { } 23 132 155
458 4 ▶ $method //= 'POST';
Condition100%2/2
$method //= "POST"
l!l
22
MC/DC100%1/1
$method //= "POST"
$method
Truth table100%2/2
A
A= $method
Aresult
11
00
459  
460 #my $req = HTTP::Request::Common::POST(
461 # 'https://example.com',
462 # Content_Type => 'form-data',
463 # Content => \@form_args,
464 #);
465 #$body = $req->content;
466 #$request_default_headers{ 'Content-Type' } = join "; ", $req->headers->content_type;
467  
468 } elsif( $options->{ get }) {
469 2 $method = 'GET';
470 # Also, append the POST data to the URL
471 2 ▶ if( $data ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data) 2 0 2
472 2 my $q = $uri->query;
473 2 ▶ if( defined $q and length $q ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $q and length $q) { } 1 1 2
Condition66%2/3
defined $q and length $q
!ll&&!rl&&r
101
MC/DC50%1/2
defined $q and length $q
defined $q length $q
Truth table66%2/3
A and B
A= defined $qB= length $q
ABresult
0X0
100
111
474 1 $q .= "&";
475 } else {
476 1 $q = "";
477 };
478 2 $q .= $data;
479 2 $uri->query( $q );
480 };
481  
482 } elsif( $options->{ head }) {
483 2 $method = 'HEAD';
484  
485 } elsif( defined $data ) {
486 23 ▶ $method //= 'POST';
Condition100%2/2
$method //= "POST"
l!l
149
MC/DC100%1/1
$method //= "POST"
$method
Truth table100%2/2
A
A= $method
Aresult
11
00
487 23 $body = $data;
488  
489 23 ▶ if( @post_json_data ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@post_json_data) { } 1 22 23
490 1 $request_default_headers{ 'Content-Type' } = "application/json";
491 1 $request_default_headers{ 'Accept' } = "application/json";
492  
493 } else {
494 22 $request_default_headers{ 'Content-Type' } = 'application/x-www-form-urlencoded';
495 };
496  
497 } else {
498 132 ▶ $method ||= 'GET';
Condition100%2/2
$method ||= "GET"
l!l
8124
MC/DC100%1/1
$method ||= "GET"
$method
Truth table100%2/2
A
A= $method
Aresult
11
00
499 };
500  
501 163 ▶ if( defined $body ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $body) 23 140 163
502 23 $request_default_headers{ 'Content-Length' } = length $body;
503 };
504  
505 163 ▶ if( $options->{ 'oauth2-bearer' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"oauth2-bearer"}) 1 162 163
506 1 push @headers, sprintf 'Authorization: Bearer %s', $options->{'oauth2-bearer'};
507 };
508  
509 163 ▶ if( $options->{ 'user' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"user"}) 5 158 163
510 5 ▶ if( $options->{anyauth}
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{'anyauth'} or $options->{'digest'} or $options->{'ntlm'} or $options->{'negotiate'}) { } 0 5 5
Condition33%3/9
$options->{"anyauth"} or $options->{"digest"} or $options->{"ntlm"} or $options->{"negotiate"}
l!l&&r!l&&!r
005
$options->{"anyauth"} or $options->{"digest"} or $options->{"ntlm"}
l!l&&r!l&&!r
005
$options->{"anyauth"} or $options->{"digest"}
l!l&&r!l&&!r
005
MC/DC0%0/4
$options->{"anyauth"} or $options->{"digest"} or $options->{"ntlm"} or $options->{"negotiate"}
$options->{"anyauth"} $options->{"digest"} $options->{"ntlm"} $options->{"negotiate"}
Truth table20%1/5
A or B or C or D
A= $options->{"anyauth"}B= $options->{"digest"}C= $options->{"ntlm"}D= $options->{"negotiate"}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
511 || $options->{digest}
512 || $options->{ntlm}
513 || $options->{negotiate}
514 ) {
515 # Nothing to do here, just let LWP::UserAgent do its thing
516 # This means one additional request to fetch the appropriate
517 # 401 response asking for credentials, but ...
518 } else {
519 # $options->{basic} or none at all
520 5 my $info = delete $options->{'user'};
521 5 ▶ if( $info !~ /:/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($info =~ /:/u) 0 5 5
522 # No password given, so append it
523 0 $info .= ':';
524 };
525  
526 # We need to bake this into the header here?!
527 5 push @headers, sprintf 'Authorization: Basic %s', encode_base64( $info );
528 }
529 };
530  
531 163 my %headers;
532 163 for my $kv (
533 43 ▶ (map { /^\s*([^:\s]+)\s*:\s*(.*)$/ ? [$1 => $2] : () } @headers),) {
Branch: 1 branch
BranchTrueFalseTotal
/^\s*([^:\s]+)\s*:\s*(.*)$/u ? : 43 0 43
534 43 $self->_add_header( \%headers, @$kv );
535 };
536  
537 163 ▶ if( defined $options->{ 'user-agent' }) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options->{"user-agent"}) 74 89 163
538 74 $self->_add_header( \%headers, "User-Agent", $options->{ 'user-agent' } );
539 };
540  
541 163 ▶ if( defined $options->{ referrer }) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options->{"referrer"}) 0 163 163
542 0 $self->_add_header( \%headers, "Referer" => $options->{ 'referrer' } );
543 };
544  
545 163 ▶ if( defined $options->{ range }) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options->{"range"}) 0 163 163
546 0 $self->_add_header( \%headers, "Range" => $options->{ 'range' } );
547 };
548  
549 # We want to compare the headers case-insensitively
550 163 my %headers_lc = map { lc $_ => 1 } keys %headers;
551  
552 163 for my $k (keys %request_default_headers) {
553 372 ▶ if( ! $headers_lc{ lc $k }) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($headers_lc{lc $k}) 282 90 372
554 282 $self->_add_header( \%headers, $k, $request_default_headers{ $k });
555 };
556 };
557 163 ▶ if( ! $headers{ 'Host' }) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($headers{"Host"}) 159 4 163
558 159 $self->_add_header( \%headers, 'Host' => $host );
559 };
560  
561 163 ▶ if( defined $options->{ 'cookie-jar' }) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options->{"cookie-jar"}) 1 162 163
562 1 $options->{'cookie-jar-options'}->{ 'write' } = 1;
563 };
564  
565 163 ▶ if( defined( my $c = $options->{ cookie })) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $c = $options->{"cookie"})) 3 160 163
566 3 ▶ if( $c =~ /=/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c =~ /=/u) { } 2 1 3
567 2 $headers{ Cookie } = $options->{ 'cookie' };
568 } else {
569 1 $options->{'cookie-jar'} = $c;
570 1 $options->{'cookie-jar-options'}->{ 'read' } = 1;
571 };
572 };
573  
574 # Curl 7.61.0 ignores these:
575 #if( $options->{ keepalive }) {
576 # $headers{ 'Keep-Alive' } = 1;
577 #} elsif( exists $options->{ keepalive }) {
578 # $headers{ 'Keep-Alive' } = 0;
579 #};
580  
581 163 ▶ if( $options->{ compressed }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"compressed"}) 2 161 163
582 2 my $compressions = HTTP::Message::decodable();
583 2 $self->_add_header( \%headers, 'Accept-Encoding' => $compressions );
584 };
585  
586 163 my $auth;
587 163 for my $kind (qw(basic ntlm negotiate)) {
588 489 ▶ if( $options->{$kind}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{$kind}) 0 489 489
589 0 $auth = $kind;
590 }
591 };
592  
593 push @res, HTTP::Request::CurlParameters->new({
594 method => $method,
595 uri => $uri,
596 headers => \%headers,
597 body => $body,
598 maybe auth => $auth,
599 maybe cert => $options->{cert},
600 maybe capath => $options->{capath},
601 maybe credentials => $options->{ user },
602 maybe output => $options->{ output },
603 maybe timeout => $options->{ 'max-time' },
604 maybe cookie_jar => $options->{'cookie-jar'},
605 maybe cookie_jar_options => $options->{'cookie-jar-options'},
606 maybe insecure => $options->{'insecure'},
607 maybe max_filesize => $options->{'max-filesize'},
608 maybe show_error => $options->{'show-error'},
609 maybe fail => $options->{'fail'},
610 maybe unix_socket => $options->{'unix-socket'},
611 163 ▶ maybe local_address => $options->{'interface'},
Branch: 1 branch
BranchTrueFalseTotal
scalar @form_args ? : 4 159 163
612 maybe form_args => scalar @form_args ? \@form_args : undef,
613 });
614 }
615  
616 return @res
617 161 };
618  
619 14 1;
620  
621 =head1 LIVE DEMO
622  
623 L<https://corion.net/curl2lwp.psgi>
624  
625 =head1 KNOWN DIFFERENCES
626  
627 =head2 Incompatible cookie jar formats
628  
629 Until somebody writes a robust Netscape cookie file parser and proper loading
630 and storage for L<HTTP::CookieJar>, this module will not be able to load and
631 save files in the format that Curl uses.
632  
633 =head2 Loading/saving cookie jars is the job of the UA
634  
635 You're expected to instruct your UA to load/save cookie jars:
636  
637 use Path::Tiny;
638 use HTTP::CookieJar::LWP;
639  
640 if( my $cookies = $r->cookie_jar ) {
641 $ua->cookie_jar( HTTP::CookieJar::LWP->new()->load_cookies(
642 path($cookies)->lines
643 ));
644 };
645  
646 =head2 Different Content-Length for POST requests
647  
648 =head2 Different delimiter for form data
649  
650 The delimiter is built by L<HTTP::Message>, and C<curl> uses a different
651 mechanism to come up with a unique data delimiter. This results in differences
652 in the raw body content and the C<Content-Length> header.
653  
654 =head1 MISSING FUNCTIONALITY
655  
656 =over 4
657  
658 =item *
659  
660 File uploads / content from files
661  
662 While file uploads and reading POST data from files are supported, the content
663 is slurped into memory completely. This can be problematic for large files
664 and little available memory.
665  
666 =item *
667  
668 Mixed data instances
669  
670 Multiple mixed instances of C<--data>, C<--data-ascii>, C<--data-raw>,
671 C<--data-binary> or C<--data-raw> are sorted by type first instead of getting
672 concatenated in the order they appear on the command line.
673 If the order is important to you, use one type only.
674  
675 =item *
676  
677 Multiple sets of parameters from the command line
678  
679 Curl supports the C<< --next >> command line switch which resets
680 parameters for the next URL.
681  
682 This is not (yet) supported.
683  
684 =back
685  
686 =head1 SEE ALSO
687  
688 L<LWP::Curl>
689  
690 L<LWP::Protocol::Net::Curl>
691  
692 L<LWP::CurlLog>
693  
694 L<HTTP::Request::AsCurl> - for the inverse function
695  
696 The module HTTP::Request::AsCurl likely also implements a much better version
697 of C<< ->as_curl >> than this module.
698  
699 L<https://github.com/NickCarneiro/curlconverter> - a converter for multiple
700 target languages
701  
702 L<The cURL manpage|https://curl.se/docs/manpage.html>
703  
704 =head1 REPOSITORY
705  
706 The public repository of this module is
707 L<http://github.com/Corion/HTTP-Request-FromCurl>.
708  
709 =head1 SUPPORT
710  
711 The public support forum of this module is
712 L<https://perlmonks.org/>.
713  
714 =head1 BUG TRACKER
715  
716 Please report bugs in this module via the Github bug queue at
717 L<https://github.com/Corion/HTTP-Request-FromCurl/issues>
718  
719 =head1 AUTHOR
720  
721 Max Maischein C<corion@cpan.org>
722  
723 =head1 COPYRIGHT (c)
724  
725 Copyright 2018-2026 by Max Maischein C<corion@cpan.org>.
726  
727 =head1 LICENSE
728  
729 This module is released under the same terms as Perl itself.
730  
731 =cut
« lib/HTTP/Request/CurlParameters.pm ↑ Summary lib/HTTP/Request/FromFetch.pm »