File Coverage

lib/LWP/CurlLog.pm
Criterion Covered Total %
statement 42 56 75.0
branch 13 24 54.1
condition 2 6 33.3
subroutine 7 7 100.0
pod 0 1 0.0
total 64 94 68.0


line stmt bran cond sub pod time code
1             package LWP::CurlLog;
2 1     1   22667 use strict;
  1         4  
  1         35  
3 1     1   6 use warnings;
  1         2  
  1         29  
4 1     1   732 use LWP::UserAgent ();
  1         60338  
  1         88  
5              
6             our $VERSION = "0.02";
7             our $log_file ||= "~/curl.log";
8             our $log_output = defined $log_output ? $log_output : 1;
9             our $curl_options = defined $curl_options ? $curl_options : "-k";
10             our $logfh = undef;
11              
12 1     1   12 no strict "refs";
  1         2  
  1         34  
13 1     1   6 no warnings "redefine";
  1         2  
  1         731  
14              
15             my $orig_sub = \&LWP::UserAgent::send_request;
16             *{"LWP::UserAgent::send_request"} = sub {
17 2     2   18607 my ($self, $request) = @_;
18              
19 2         12 open_log();
20 2         7 my $cmd = "curl ";
21 2         10 my $url = $request->uri();
22 2 100       26 if ($url =~ /[=&;?]/) {
23 1         12 $cmd .= "\"$url\" ";
24             }
25             else {
26 1         15 $cmd .= "$url "
27             }
28 2 50       21 if ($curl_options) {
29 2         9 $cmd .= "$curl_options ";
30             }
31 2 50 33     9 if ($request->method() && ($request->method() ne "GET" || $request->content_length())) {
      33        
32 0         0 $cmd .= "-X " . $request->method() . " ";
33             }
34 2         443 for my $header ($request->header_field_names) {
35 2 50       96 if ($header =~ /^(Content-Length|User-Agent)$/i) {
36 2         8 next;
37             }
38 0         0 my $value = $request->header($header);
39 0         0 $value =~ s{([\\\$"])}{\\$1}g;
40 0         0 $cmd .= "-H \"$header: $value\" ";
41             }
42 2 50       14 if ($request->header("Content-Length")) {
43 0         0 my $content = $request->decoded_content();
44 0         0 $content =~ s{([\\\$"])}{\\$1}g;
45 0         0 $cmd .= "-d \"$content\" ";
46             }
47 2         165 $cmd =~ s/\s*$//;
48              
49 2         265 print $logfh "# " . localtime() . " LWP request\n";
50 2         30 print $logfh "$cmd\n";
51 2         14 my $response = $orig_sub->(@_);
52              
53 2 50       418820 if ($log_output) {
54 0         0 print $logfh "# " . localtime() . " LWP response\n";
55 0         0 print $logfh $response->as_string . "\n";
56             }
57              
58 2         11 return $response;
59             };
60              
61             sub open_log {
62 2 100   2 0 12 if ($logfh) {
63 1         3 return;
64             }
65 1 50       10 if ($log_file eq "STDOUT") {
    50          
    50          
66 0         0 $logfh = \*STDOUT;
67             }
68             elsif ($log_file eq "STDERR") {
69 0         0 $logfh = \*STDERR;
70             }
71             elsif ($log_file =~ m{^~/}) {
72 0         0 my $home = (getpwuid($>))[7];
73 0         0 $log_file =~ s{^~/}{$home/};
74 0 0       0 open $logfh, ">>", $log_file or die "Can't open $log_file: $!";
75             }
76             else {
77 1 50       118 open $logfh, ">>", $log_file or die "Can't open $log_file: $!";
78             }
79 1         7 select($logfh);
80 1         6 $| = 1;
81 1         3 select(STDOUT);
82             }
83              
84             1;
85              
86             __END__