| 1 | package LWP::ConsoleLogger::Easy; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 10 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 10 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 10 | our $VERSION = '1.000003'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 10 | ▶ | use Class::Method::Modifiers (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 10 | ▶ | use Hash::Util::FieldHash qw( fieldhash ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 10 | ▶ | use HTTP::Headers (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 10 | ▶ | use HTTP::Request (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 10 | ▶ | use HTTP::Response (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 10 | ▶ | use LWP::ConsoleLogger (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 10 | ▶ | use Module::Load::Conditional qw( can_load ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 10 | ▶ | use Ref::Util qw( is_plain_arrayref is_ref ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 10 | ▶ | use Sub::Exporter -setup => { exports => ['debug_ua'] }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 10 | ▶ | use String::Trim qw( trim ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 10 | ▶ | use URI (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 10 | fieldhash my %http_tiny_loggers; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 10 | my $http_tiny_wrapped; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 10 | my %VERBOSITY = ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | dump_content => 8, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | dump_cookies => 6, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | dump_headers => 5, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | dump_params => 4, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | dump_status => 2, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | dump_text => 7, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | dump_title => 3, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | dump_uri => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 34 | ▶ | sub debug_ua { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
debug_ua: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 34 | my $ua = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 34 | my $level = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 34 | ▶ | $level //= 10; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$level //= 10
MC/DC100%1/1
$level //= 10
$level
Truth table100%2/2
A
A= $level
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 34 | my %args = map { $_ => $VERBOSITY{$_} <= $level } keys %VERBOSITY; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 34 | my $console_logger = LWP::ConsoleLogger->new(%args); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 34 | add_ua_handlers( $ua, $console_logger ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 34 | ▶ | if ( can_load( modules => { 'HTML::FormatText::Lynx' => 23 } ) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | $console_logger->text_pre_filter( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 19 | ▶ | sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 19 | my $text = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 19 | my $content_type = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 19 | my $base_url = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 19 | ▶ | return $text | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$content_type and $content_type =~ /html/i
MC/DC50%1/2
$content_type and $content_type =~ /html/i
$content_type
$content_type =~ /html/i
Truth table66%2/3
A and B
A= $content_typeB= $content_type =~ /html/i
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | unless $content_type && $content_type =~ m{html}i; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | return ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 13 | trim( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | HTML::FormatText::Lynx->format_string( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | $text, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | base => $base_url, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | ), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 'text/plain' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 34 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 34 | return $console_logger; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 34 | ▶ | sub add_ua_handlers { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_ua_handlers: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 34 | my $ua = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 34 | my $console_logger = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 34 | ▶ | if ( $ua->isa('Test::WWW::Mechanize::Mojo') ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | $ua = $ua->tester->ua; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 34 | ▶ | if ( $ua->isa('Mojo::UserAgent') ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | $ua->on( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 'start', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 6 | ▶ | sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 6 | my $the_ua = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 6 | my $tx = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 6 | my $request = HTTP::Request->parse( $tx->req->to_string ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 6 | $console_logger->request_callback( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | $request, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | $the_ua, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | $tx->on( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 'finish', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 1 | ▶ | sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 1 | my $tx = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 1 | my $res | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | = HTTP::Response->parse( $tx->res->to_string ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 1 | $res->request($request); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 1 | $console_logger->response_callback( $res, $the_ua ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 6 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 7 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 7 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 27 | ▶ | if ( $ua->isa('HTTP::Tiny') ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 8 | _instrument_http_tiny( $ua, $console_logger ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 8 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | $ua->add_handler( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 'response_done', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 21 | ▶ | sub { $console_logger->response_callback(@_) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 19 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | $ua->add_handler( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 'request_send', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 21 | ▶ | sub { $console_logger->request_callback(@_) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 19 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 8 | ▶ | sub _instrument_http_tiny { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_instrument_http_tiny: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 8 | my $ua = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | 8 | my $console_logger = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 8 | ▶ | push @{ $http_tiny_loggers{$ua} ||= [] }, $console_logger; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$http_tiny_loggers{$ua} ||= []
MC/DC100%1/1
$http_tiny_loggers{$ua} ||= []
$http_tiny_loggers{$ua}
Truth table100%2/2
A
A= $http_tiny_loggers{$ua}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 8 | ▶ | return if $http_tiny_wrapped; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 2 | $http_tiny_wrapped = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | Class::Method::Modifiers::install_modifier( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | 'HTTP::Tiny', 'around', 'request', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 6 | ▶ | sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=20.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 6 | my $orig = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 6 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | 6 | my ( $method, $url, $args ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | my $console_loggers | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 6 | ▶ | = is_ref($self) ? $http_tiny_loggers{$self} : undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 6 | ▶ | return $self->$orig(@_) unless $console_loggers; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | # Logging must never break the caller: any die while translating | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | # the request or running the request_callback is caught here so | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | # the real HTTP call still happens below. Seed $request with a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | # minimal object first so that, even if the richer translation | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | # below dies, the response can still be logged against a valid | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | # request rather than being discarded too. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | 6 | my $request = HTTP::Request->new( $method, $url ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | eval { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | 6 | $request = _http_tiny_request_object( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | $self, $method, $url, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | $args | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | $_->request_callback( $request, $self ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 6 | for @{$console_loggers}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | 6 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | 6 | ▶ | } or do { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 0 | warn | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | "LWP::ConsoleLogger: HTTP::Tiny request logging failed: $@"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | # Always perform the real request and always return its response, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | # even if logging blew up above. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | 6 | my $response = $self->$orig(@_); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | # Likewise, never let response translation or the response_callback | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | # discard an already-completed HTTP response. $request is always a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | # valid object here (seeded above), even if the richer translation | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | # died, so the response is still logged against a real request. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | eval { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | 6 | my $res = _http_tiny_response_object( $response, $request ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | 6 | $_->response_callback( $res, $self ) for @{$console_loggers}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | 6 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | 6 | ▶ | } or do { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | 0 | warn | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | "LWP::ConsoleLogger: HTTP::Tiny response logging failed: $@"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | 6 | return $response; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | 2 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | 17 | ▶ | sub _copy_headers { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_copy_headers: called CC=4 SCAR=14.3
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | 17 | my $headers = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | 17 | my $hashref = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | 17 | ▶ | return unless $hashref; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | 17 | foreach my $name ( keys %{$hashref} ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | 25 | my $val = $hashref->{$name}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | $headers->push_header( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | $name, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | 0 | ▶ | is_plain_arrayref($val) ? @{$val} : $val | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | 17 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | 11 | ▶ | sub _http_tiny_request_object { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_http_tiny_request_object: called CC=23 SCAR=32.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | 11 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | 11 | my $method = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | 11 | my $url = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | 11 | my $args = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | 11 | ▶ | $args ||= {}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$args ||= {}
MC/DC0%0/1
$args ||= {}
$args
Truth table50%1/2
A
A= $args
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | 11 | my $headers = HTTP::Headers->new; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | # HTTP::Tiny merges default_headers first, then per-request headers, with | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | # the per-request value winning (last-wins) rather than being appended. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | # Merge both sources into one hash so a duplicated name produces a single | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | # overriding row, then copy once. Header names are compared | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | # case-insensitively (as HTTP::Tiny and HTTP::Headers both treat them) so | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | # that e.g. a default "Content-Type" and a per-request "content-type" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | # collapse to one value instead of two appended rows. Arrayref values (a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | # single header carrying multiple values) are preserved. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | 11 | my %merged; # lc name => [ display name => value ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | 11 | foreach my $source ( $self->default_headers, $args->{headers} ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | 22 | ▶ | next unless $source; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | 3 | $merged{ lc $_ } = [ $_ => $source->{$_} ] for keys %{$source}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | 11 | my %clean = map { @{$_} } values %merged; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | 11 | _copy_headers( $headers, \%clean ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | # HTTP::Tiny synthesizes a Host header at send time. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | 11 | ▶ | unless ( defined $headers->header('Host') ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | 11 | my $uri = URI->new($url); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | 11 | my $host = eval { $uri->host }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | 11 | ▶ | $host = "[$host]" if defined $host && $host =~ /:/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $host and $host =~ /:/
MC/DC50%1/2
defined $host and $host =~ /:/
defined $host
$host =~ /:/
Truth table66%2/3
A and B
A= defined $hostB= $host =~ /:/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | 11 | ▶ | if ( defined $host ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | 11 | my %default_port = ( http => 80, https => 443 ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | 11 | ▶ | my $scheme = lc( $uri->scheme // q{} ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$uri->scheme // ""
MC/DC0%0/1
$uri->scheme // ""
$uri->scheme
Truth table50%1/2
A
A= $uri->scheme
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | 11 | my $port = eval { $uri->port }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | 11 | ▶ | if ( defined $port | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
defined $port and defined $default_port{$scheme} and $port != $default_port{$scheme}
defined $port and defined $default_port{$scheme}
MC/DC66%2/3
defined $port and defined $default_port{$scheme} and $port != $default_port{$scheme}
defined $port
defined $default_port{$scheme}
$port != $default_port{$scheme}
Truth table75%3/4
A and B and C
A= defined $portB= defined $default_port{$scheme}C= $port != $default_port{$scheme}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | && defined $default_port{$scheme} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | && $port != $default_port{$scheme} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | 5 | $host .= ":$port"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | 11 | $headers->header( Host => $host ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | # HTTP::Tiny synthesizes a User-Agent header from its agent attribute. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | 11 | ▶ | if ( defined $self->agent | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $self->agent and not defined $headers->header("User-Agent")
MC/DC0%0/2
defined $self->agent and not defined $headers->header("User-Agent")
defined $self->agent
not defined $headers->header("User-Agent")
Truth table33%1/3
A and not(B)
A= defined $self->agentB= not defined $headers->header("User-Agent")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | && !defined $headers->header('User-Agent') ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | 11 | $headers->header( 'User-Agent' => $self->agent ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | 11 | my $content = $args->{content}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | # A coderef content is a streaming body we cannot capture. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | 11 | ▶ | $content = undef if is_ref($content); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | 11 | my $request = HTTP::Request->new( $method, $url, $headers, $content ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | 11 | ▶ | if ( defined $content ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | # HTTP::Tiny computes Content-Length internally, so it is absent from | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | # the request options. Supply it so the body params get logged. Use | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | # a byte length (not a character length) to match what HTTP::Tiny | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | # actually sends on the wire. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | 1 | ▶ | if ( !defined $request->header('Content-Length') ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | 10 | ▶ | my $length = do { use bytes; length $content }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | 1 | $request->header( 'Content-Length' => $length ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | # HTTP::Tiny defaults raw content to application/octet-stream when no | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | # Content-Type is supplied; _log_params/_log_text rely on it. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | 1 | ▶ | if ( !defined $request->header('Content-Type') ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | 0 | $request->header( 'Content-Type' => 'application/octet-stream' ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | ▶ | elsif ( ( $method eq 'POST' || $method eq 'PUT' ) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition88%8/9
$method eq "POST" || $method eq "PUT" and not is_ref($args->{"content"}) and not defined $request->header("Content-Length")
$method eq "POST" || $method eq "PUT" and not is_ref($args->{"content"})
$method eq "POST" or $method eq "PUT"
MC/DC75%3/4
$method eq "POST" || $method eq "PUT" and not is_ref($args->{"content"}) and not defined $request->header("Content-Length")
$method eq "POST"
$method eq "PUT"
not is_ref($args->{"content"})
not defined $request->header("Content-Length")
Truth table57%4/7
A or B and not(C) and not(D)
A= $method eq "POST"B= $method eq "PUT"C= not is_ref($args->{"content"})D= not defined $request->header("Content-Length")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | && !is_ref( $args->{content} ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | && !defined $request->header('Content-Length') ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | # HTTP::Tiny sends an explicit zero length for empty POST and PUT | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | # requests so that servers do not wait for a body. A coderef body is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | # excluded: HTTP::Tiny streams it with chunked transfer-encoding | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | # rather than a zero Content-Length. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | 2 | $request->header( 'Content-Length' => 0 ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | 11 | return $request; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | 6 | ▶ | sub _http_tiny_response_object { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_http_tiny_response_object: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | 6 | my $response = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | 6 | my $request = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | 6 | my $headers = HTTP::Headers->new; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | 6 | _copy_headers( $headers, $response->{headers} ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | my $res = HTTP::Response->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | $response->{status}, $response->{reason}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | $headers, $response->{content}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | 6 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | 6 | ▶ | $res->protocol( $response->{protocol} ) if $response->{protocol}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | 6 | $res->request($request); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | 6 | return $res; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | 10 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | =pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | =encoding UTF-8 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | LWP::ConsoleLogger::Easy - Easy LWP tracing and debugging | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | version 1.000003 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | use LWP::ConsoleLogger::Easy qw( debug_ua ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | use WWW::Mechanize; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | my $mech = WWW::Mechanize->new; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | my $logger = debug_ua( $mech ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | $mech->get('https://google.com'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | # now watch the console for debugging output | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | # ... | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | # stop dumping headers | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | $logger->dump_headers( 0 ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | # Redact sensitive data | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | $ENV{LWPCL_REDACT_HEADERS} = 'Authorization,Foo,Bar'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | $ENV{LWPCL_REDACT_PARAMS} = 'seekrit,password,credit_card'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | my $quiet_logger = debug_ua( $mech, 1 ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | my $noisy_logger = debug_ua( $mech, 5 ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | This module gives you the easiest possible introduction to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | L<LWP::ConsoleLogger>. It offers one wrapper around L<LWP::ConsoleLogger>: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | C<debug_ua>. This function allows you to get up and running quickly with just | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | a couple of lines of code. It instantiates user-agent logging and also returns | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 344 | a L<LWP::ConsoleLogger> object, which you may then tweak to your heart's | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 345 | desire. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 347 | If you're able to install L<HTML::FormatText::Lynx> then you'll get highly | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 348 | readable HTML to text conversions. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 350 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 352 | =head2 debug_ua( $ua, $verbosity ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 354 | When called without a verbosity argument, this function turns on all logging. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 355 | I'd suggest going with this to start with and then turning down the verbosity | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 356 | after that. This method returns an L<LWP::ConsoleLogger> object, which you | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 357 | may tweak to your heart's desire. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 359 | my $ua_logger = debug_ua( $ua ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 360 | $ua_logger->content_pre_filter( sub {...} ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 361 | $ua_logger->logger( Log::Dispatch->new(...) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 363 | $ua->get(...); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 365 | C<$ua> may be one of several user-agents, including C<LWP::UserAgent>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 366 | C<Mojo::UserAgent>, C<HTTP::Tiny>, and C<WWW::Mechanize>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 368 | When C<$ua> is an L<HTTP::Tiny> object, be aware of a few limitations that | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 369 | the L<LWP::UserAgent> and L<Mojo::UserAgent> paths do not share: redirect | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 370 | chains are collapsed so only the final response is logged, C<< $ua->mirror >> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 371 | streams the body to a file so the body and text tables are empty, and some | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 372 | transport-layer headers (for example C<Connection>, or an C<Authorization> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 373 | header synthesized from C<userinfo> in the URL) are reconstructed on a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 374 | best-effort basis and may not match what goes out on the wire. See | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 375 | L<LWP::ConsoleLogger::Everywhere/CAVEATS> for the full list. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 377 | You can provide a verbosity level of 0 or more. (Currently 0 - 8 supported.) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 378 | This will turn up the verbosity on your output gradually. A verbosity of 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 379 | will display nothing. 8 will display all available outputs. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 381 | # don't get too verbose | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 382 | my $ua_logger = debug_ua( $ua, 4 ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 384 | =head2 add_ua_handlers | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 386 | This method sets up response and request handlers on your user agent. This is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 387 | done for you automatically if you're using C<debug_ua>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 389 | =head1 ENVIRONMENT VARIABLES | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 391 | =head2 LWPCL_REDACT_HEADERS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 393 | A comma-separated list of header values to redact from output. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 395 | $ENV{LWPCL_REDACT_HEADERS} = 'Authorization,Foo,Bar'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 397 | Output will be something like: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 399 | .----------------+------------------. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 400 | | Request Header | Value | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 401 | +----------------+------------------+ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 402 | | Authorization | [REDACTED] | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 403 | | Content-Length | 0 | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 404 | | User-Agent | libwww-perl/6.15 | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 405 | '----------------+------------------' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 407 | Use at the command line. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 409 | LWPCL_REDACT_HEADERS='Authorization,Foo,Bar' perl script.pl | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 411 | =head2 LWPCL_REDACT_PARAMS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 413 | A comma-separated list of parameter values to redact from output. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 415 | $ENV{LWPCL_REDACT_PARAMS} = 'credit_card,foo,bar'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 417 | Use at the command line. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 419 | LWPCL_REDACT_PARAMS='credit_card,foo,bar' perl script.pl | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 421 | .-------------+------------. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 422 | | Key | Value | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 423 | +-------------+------------+ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 424 | | credit_card | [REDACTED] | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 425 | '-------------+------------' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 427 | =head2 CAVEATS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 429 | Text formatting now defaults to attempting to use L<HTML::FormatText::Lynx> to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 430 | format HTML as text. If you do not have this installed, we'll fall back to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 431 | using HTML::Restrict to remove any HTML tags which you have not specifically | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 432 | whitelisted. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 434 | If you have L<HTML::FormatText::Lynx> installed, but you don't want to use it, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 435 | override the default filter: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 437 | my $logger = debug_ua( $mech ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 438 | $logger->text_pre_filter( sub { return shift } ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 440 | =head2 EXAMPLES | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 442 | Please see the "examples" folder in this distribution for more ideas on how to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 443 | use this module. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 445 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 447 | Olaf Alders <olaf@wundercounter.com> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 449 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 451 | This software is Copyright (c) 2014 by MaxMind, Inc. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 453 | This is free software, licensed under: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 455 | The Artistic License 2.0 (GPL Compatible) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 457 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 459 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||