| 1 | package LWP::CurlLog; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use LWP::UserAgent (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 1 | our $VERSION = "0.03"; | |||||||||||||||||||||||||||||||||||||||||
| 7 | 1 | our %opts = ( | |||||||||||||||||||||||||||||||||||||||||
| 8 | file => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 9 | response => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 10 | options => "-k", | ||||||||||||||||||||||||||||||||||||||||||
| 11 | timing => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 12 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | sub import { | ||||||||||||||||||||||||||||||||||||||||||
| 15 | 1 | ▶ | my ($package, %args) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
import: called CC=6 SCAR=21.4
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 1 | for my $key (keys %args) { | |||||||||||||||||||||||||||||||||||||||||
| 17 | 2 | $opts{$key} = $args{$key}; | |||||||||||||||||||||||||||||||||||||||||
| 18 | } | ||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | 1 | ▶ | if (!$opts{file}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | 0 | $opts{fh} = \*STDERR; | |||||||||||||||||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||||||||||||||||
| 23 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 24 | 1 | my $file2 = $opts{file}; | |||||||||||||||||||||||||||||||||||||||||
| 25 | 1 | ▶ | if ($file2 =~ m{^~/}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | 0 | ▶ | my $home = $ENV{HOME} || (getpwuid($<))[7]; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$ENV{'HOME'} || (getpwuid $<)[7]
MC/DC0%0/2
$ENV{'HOME'} || (getpwuid $<)[7]
$ENV{'HOME'}
(getpwuid $<)[7]
Truth table0%0/3
A || B
A= $ENV{'HOME'}B= (getpwuid $<)[7]
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | 0 | $file2 =~ s{^~/}{$home/}; | |||||||||||||||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||||||||||||||||
| 29 | 1 | ▶ | open $opts{fh}, ">>", $file2 or die "Can't open $opts{file}: $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||||||||||||||||
| 31 | 1 | select($opts{fh}); | |||||||||||||||||||||||||||||||||||||||||
| 32 | 1 | $| = 1; | |||||||||||||||||||||||||||||||||||||||||
| 33 | 1 | select(STDOUT); | |||||||||||||||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | 1 | ▶ | no strict "refs"; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 37 | 1 | ▶ | no warnings "redefine"; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | 1 | my $orig_sub = \&LWP::UserAgent::send_request; | |||||||||||||||||||||||||||||||||||||||||
| 40 | 1 | *{"LWP::UserAgent::send_request"} = sub { | |||||||||||||||||||||||||||||||||||||||||
| 41 | 1 | ▶ | my ($self, $request) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=11 SCAR=31.0
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | 1 | my $cmd = "curl "; | |||||||||||||||||||||||||||||||||||||||||
| 44 | 1 | my $url = $request->uri(); | |||||||||||||||||||||||||||||||||||||||||
| 45 | 1 | ▶ | if ($url =~ /[=&;?]/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | $cmd .= "\"$url\" "; | |||||||||||||||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||||||||||||||
| 48 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 49 | 1 | $cmd .= "$url " | |||||||||||||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||||||||||||
| 51 | 1 | ▶ | if ($opts{options}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | 1 | $cmd .= "$opts{options} "; | |||||||||||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||||||||||
| 54 | 1 | ▶ | if ($request->method() && ($request->method() ne "GET" || $request->content_length())) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
$request->method and $request->method ne "GET" || $request->content_length
$request->method ne "GET" or $request->content_length
MC/DC0%0/3
$request->method and $request->method ne "GET" || $request->content_length
$request->method
$request->method ne "GET"
$request->content_length
Truth table25%1/4
A and B or C
A= $request->methodB= $request->method ne "GET"C= $request->content_length
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | 0 | $cmd .= "-X " . $request->method() . " "; | |||||||||||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||||||||||
| 57 | 1 | for my $header ($request->header_field_names) { | |||||||||||||||||||||||||||||||||||||||||
| 58 | 1 | ▶ | if ($header =~ /^(Content-Length|User-Agent)$/i) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | 1 | next; | |||||||||||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | my $value = $request->header($header); | |||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | $value =~ s{([\\\$"])}{\\$1}g; | |||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | $cmd .= "-H \"$header: $value\" "; | |||||||||||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||||||||||||
| 65 | 1 | ▶ | if ($request->header("Content-Length")) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | my $content = $request->decoded_content(); | |||||||||||||||||||||||||||||||||||||||||
| 67 | 0 | $content =~ s{([\\\$"])}{\\$1}g; | |||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | $cmd .= "-d \"$content\" "; | |||||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||||
| 70 | 1 | $cmd =~ s/\s*$//; | |||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | 1 | print {$opts{fh}} "# " . localtime() . " LWP request\n"; | |||||||||||||||||||||||||||||||||||||||||
| 73 | 1 | print {$opts{fh}} "$cmd\n"; | |||||||||||||||||||||||||||||||||||||||||
| 74 | 1 | my $time1 = time(); | |||||||||||||||||||||||||||||||||||||||||
| 75 | 1 | my $response = $orig_sub->(@_); | |||||||||||||||||||||||||||||||||||||||||
| 76 | 1 | my $time2 = time(); | |||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | 1 | ▶ | if ($opts{response}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | print {$opts{fh}} "\n# " . localtime() . " LWP response\n"; | |||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | my $response2 = $response->as_string; | |||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | $response2 =~ s/\s*$//g; | |||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | print {$opts{fh}} "$response2\n"; | |||||||||||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||||||||||||
| 84 | 1 | ▶ | if ($opts{timing}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | my $diff = $time2 - $time1; | |||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | print {$opts{fh}} "# ${diff}s\n"; | |||||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | 1 | print {$opts{fh}} "\n"; | |||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | 1 | return $response; | |||||||||||||||||||||||||||||||||||||||||
| 92 | 1 | }; | |||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | __END__ | ||||||||||||||||||||||||||||||||||||||||||