lib/LWP/CurlLog.pm

filter: Statementstmt 73.6% 56 / 76 Branchbran 50.0% 10 / 20 Conditioncond 22.2% 2 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 64.1% 75 / 117 SCAR 33.0
CC
16
Cov
65%
__ANON__
22.1
import
8.5
BEGIN
1.0
CRAP 27.2
↑ Summary blib/lib/LWP/CurlLog.pm »
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
BranchTrueFalseTotal
if (not $LWP::CurlLog::opts{'file'}) { } 0 1 1
21 0 $opts{fh} = \*STDERR;
22 }
23 else {
24 1 my $file2 = $opts{file};
25 1 ▶ if ($file2 =~ m{^~/}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file2 =~ m[^~/]) 0 1 1
26 0 ▶ my $home = $ENV{HOME} || (getpwuid($<))[7];
Condition0%0/3
$ENV{'HOME'} || (getpwuid $<)[7]
l!l&&r!l&&!r
000
MC/DC0%0/2
$ENV{'HOME'} || (getpwuid $<)[7]
$ENV{'HOME'} (getpwuid $<)[7]
Truth table0%0/3
A || B
A= $ENV{'HOME'}B= (getpwuid $<)[7]
ABresult
1X1
011
000
27 0 $file2 =~ s{^~/}{$home/};
28 }
29 1 ▶ open $opts{fh}, ">>", $file2 or die "Can't open $opts{file}: $!";
Branch: 1 branch
BranchTrueFalseTotal
open $LWP::CurlLog::opts{"fh"}, ">>", $file2 or die qq[Can't open $LWP::CurlLog::opts{"file"}: $!] 0 1 1
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
BranchTrueFalseTotal
if ($url =~ /[=&;?]/) { } 0 1 1
46 0 $cmd .= "\"$url\" ";
47 }
48 else {
49 1 $cmd .= "$url "
50 }
51 1 ▶ if ($opts{options}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($LWP::CurlLog::opts{"options"}) 1 0 1
52 1 $cmd .= "$opts{options} ";
53 }
54 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
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
BranchTrueFalseTotal
if ($header =~ /^(Content-Length|User-Agent)$/i) 1 0 1
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
BranchTrueFalseTotal
if ($request->header("Content-Length")) 0 1 1
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
BranchTrueFalseTotal
if ($LWP::CurlLog::opts{"response"}) 0 1 1
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
BranchTrueFalseTotal
if ($LWP::CurlLog::opts{"timing"}) 0 1 1
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__
↑ Summary blib/lib/LWP/CurlLog.pm »