blib/lib/HTTP/Request/FromCurl.pm

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