| 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.02"; | |||||||||||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | our $log_file ||= "~/curl.log"; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
our $log_file ||= "~/curl.log"
MC/DC0%0/1
our $log_file ||= "~/curl.log"
our $log_file
Truth table50%1/2
A
A= our $log_file
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | our $log_output = defined $log_output ? $log_output : 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 1 | ▶ | our $curl_options = defined $curl_options ? $curl_options : "-k"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 1 | our $logfh = undef; | |||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | 1 | ▶ | no strict "refs"; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 1 | ▶ | no warnings "redefine"; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | 1 | my $orig_sub = \&LWP::UserAgent::send_request; | |||||||||||||||||||||||||||||||||||||||||
| 16 | 1 | *{"LWP::UserAgent::send_request"} = sub { | |||||||||||||||||||||||||||||||||||||||||
| 17 | 1 | ▶ | my ($self, $request) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=10 SCAR=28.7
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | 1 | open_log(); | |||||||||||||||||||||||||||||||||||||||||
| 20 | 1 | my $cmd = "curl "; | |||||||||||||||||||||||||||||||||||||||||
| 21 | 1 | my $url = $request->uri(); | |||||||||||||||||||||||||||||||||||||||||
| 22 | 1 | ▶ | if ($url =~ /[=&;?]/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | 0 | $cmd .= "\"$url\" "; | |||||||||||||||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||||||||||||||||
| 25 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 26 | 1 | $cmd .= "$url " | |||||||||||||||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||||||||||||||||
| 28 | 1 | ▶ | if ($curl_options) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | 1 | $cmd .= "$curl_options "; | |||||||||||||||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||||||||||||||||
| 31 | 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
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | 0 | $cmd .= "-X " . $request->method() . " "; | |||||||||||||||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||||||||||||||||
| 34 | 1 | for my $header ($request->header_field_names) { | |||||||||||||||||||||||||||||||||||||||||
| 35 | 1 | ▶ | if ($header =~ /^(Content-Length|User-Agent)$/i) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | 1 | next; | |||||||||||||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||||||||||||
| 38 | 0 | my $value = $request->header($header); | |||||||||||||||||||||||||||||||||||||||||
| 39 | 0 | $value =~ s{([\\\$"])}{\\$1}g; | |||||||||||||||||||||||||||||||||||||||||
| 40 | 0 | $cmd .= "-H \"$header: $value\" "; | |||||||||||||||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||||||||||||||
| 42 | 1 | ▶ | if ($request->header("Content-Length")) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | 0 | my $content = $request->decoded_content(); | |||||||||||||||||||||||||||||||||||||||||
| 44 | 0 | $content =~ s{([\\\$"])}{\\$1}g; | |||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | $cmd .= "-d \"$content\" "; | |||||||||||||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||||||||||||
| 47 | 1 | $cmd =~ s/\s*$//; | |||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | 1 | print $logfh "# " . localtime() . " LWP request\n"; | |||||||||||||||||||||||||||||||||||||||||
| 50 | 1 | print $logfh "$cmd\n"; | |||||||||||||||||||||||||||||||||||||||||
| 51 | 1 | my $response = $orig_sub->(@_); | |||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | 1 | ▶ | if ($log_output) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | 0 | print $logfh "# " . localtime() . " LWP response\n"; | |||||||||||||||||||||||||||||||||||||||||
| 55 | 0 | print $logfh $response->as_string . "\n"; | |||||||||||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | 1 | return $response; | |||||||||||||||||||||||||||||||||||||||||
| 59 | 1 | }; | |||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | sub open_log { | ||||||||||||||||||||||||||||||||||||||||||
| 62 | 1 | ▶ | if ($logfh) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
open_log: called CC=7 SCAR=26.9
Pod
undocumented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||||||||||||
| 65 | 1 | ▶ | if ($log_file eq "STDOUT") { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | $logfh = \*STDOUT; | |||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||
| 68 | elsif ($log_file eq "STDERR") { | ||||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | $logfh = \*STDERR; | |||||||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||||||
| 71 | elsif ($log_file =~ m{^~/}) { | ||||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | my $home = (getpwuid($>))[7]; | |||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | $log_file =~ s{^~/}{$home/}; | |||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | ▶ | open $logfh, ">>", $log_file or die "Can't open $log_file: $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||
| 76 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 77 | 1 | ▶ | open $logfh, ">>", $log_file or die "Can't open $log_file: $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||||||||||
| 79 | 1 | select($logfh); | |||||||||||||||||||||||||||||||||||||||||
| 80 | 1 | $| = 1; | |||||||||||||||||||||||||||||||||||||||||
| 81 | 1 | select(STDOUT); | |||||||||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||
| 84 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | __END__ | ||||||||||||||||||||||||||||||||||||||||||