lib/LWP/CurlLog.pm

filter: Statementstmt 75.3% 49 / 65 Branchbran 46.4% 13 / 28 Conditioncond 37.5% 3 / 8 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 7 / 7 Podpod 0.0% 0 / 1 totaltotal 63.7% 72 / 113 SCAR 33.2
CC
16
Cov
64%
__ANON__
17.7
open_log
14.8
BEGIN
1.0
CRAP 27.6
« blib/lib/LWP/CurlLog.pm ↑ Summary
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"
l!l
01
MC/DC0%0/1
our $log_file ||= "~/curl.log"
our $log_file
Truth table50%1/2
A
A= our $log_file
Aresult
11
00
8 1 ▶our $log_output = defined $log_output ? $log_output : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $LWP::CurlLog::log_output ? : 0 1 1
9 1 ▶our $curl_options = defined $curl_options ? $curl_options : "-k";
Branch: 1 branch
BranchTrueFalseTotal
defined $LWP::CurlLog::curl_options ? : 0 1 1
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
BranchTrueFalseTotal
if ($url =~ /[=&;?]/) { } 0 1 1
23 0 $cmd .= "\"$url\" ";
24 }
25 else {
26 1 $cmd .= "$url "
27 }
28 1 ▶ if ($curl_options) {
Branch: 1 branch
BranchTrueFalseTotal
if ($LWP::CurlLog::curl_options) 1 0 1
29 1 $cmd .= "$curl_options ";
30 }
31 1 ▶ if ($request->method() && ($request->method() ne "GET" || $request->content_length())) {
Branch: 1 branch
BranchTrueFalseTotal
if ($request->method and $request->method ne "GET" || $request->content_length) 0 1 1
Condition33%2/6
$request->method and $request->method ne "GET" || $request->content_length
!ll&&!rl&&r
010
$request->method ne "GET" or $request->content_length
l!l&&r!l&&!r
001
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
ABCresult
0XX0
1000
11X1
1011
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
BranchTrueFalseTotal
if ($header =~ /^(Content-Length|User-Agent)$/i) 1 0 1
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
BranchTrueFalseTotal
if ($request->header("Content-Length")) 0 1 1
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
BranchTrueFalseTotal
if ($LWP::CurlLog::log_output) 0 1 1
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
BranchTrueFalseTotal
if ($LWP::CurlLog::logfh) 0 1 1
63 0 return;
64 }
65 1 ▶ if ($log_file eq "STDOUT") {
Branch: 3 branches
BranchTrueFalseTotal
if ($LWP::CurlLog::log_file eq 'STDOUT') { } 0 1 1
elsif ($LWP::CurlLog::log_file eq 'STDERR') { } 0 1 1
elsif ($LWP::CurlLog::log_file =~ m[^~/]) { } 0 1 1
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
BranchTrueFalseTotal
open $LWP::CurlLog::logfh, ">>", $LWP::CurlLog::log_file or die "Can't open $LWP::CurlLog::log_file: $!" 0 0 0
75 }
76 else {
77 1 ▶ open $logfh, ">>", $log_file or die "Can't open $log_file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open $LWP::CurlLog::logfh, ">>", $LWP::CurlLog::log_file or die "Can't open $LWP::CurlLog::log_file: $!" 0 1 1
78 }
79 1 select($logfh);
80 1 $| = 1;
81 1 select(STDOUT);
82 }
83  
84 1 1;
85  
86 __END__
« blib/lib/LWP/CurlLog.pm ↑ Summary