| 1 | package LWPx::Record::DataSection; | ||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use LWP::Protocol; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use Data::Section::Simple; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use B::Hooks::EndOfScope; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use HTTP::Response; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use CGI::Simple; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use CGI::Simple::Cookie; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 2 | our $VERSION = '0.01'; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 2 | our $Data; | |||||||||||||||||||||||||||
| 14 | 2 | our ($Pkg, $File, $Fh); | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | our $Option = { | ||||||||||||||||||||||||||||
| 17 | decode_content => 1, | ||||||||||||||||||||||||||||
| 18 | record_response_header => undef, | ||||||||||||||||||||||||||||
| 19 | record_request_cookie => undef, | ||||||||||||||||||||||||||||
| 20 | record_post_param => undef, | ||||||||||||||||||||||||||||
| 21 | append_data_section => !!$ENV{LWPX_RECORD_APPEND_DATA}, | ||||||||||||||||||||||||||||
| 22 | 2 | }; | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | # From HTTP::Headers | ||||||||||||||||||||||||||||
| 25 | 2 | our @CommonHeaders = qw( | |||||||||||||||||||||||||||
| 26 | Cache-Control Connection Date Pragma Trailer Transfer-Encoding Upgrade | ||||||||||||||||||||||||||||
| 27 | Via Warning | ||||||||||||||||||||||||||||
| 28 | Accept-Ranges Age ETag Location Proxy-Authenticate Retry-After Server | ||||||||||||||||||||||||||||
| 29 | Vary WWW-Authenticate | ||||||||||||||||||||||||||||
| 30 | Allow Content-Encoding Content-Language Content-Length Content-Location | ||||||||||||||||||||||||||||
| 31 | Content-MD5 Content-Range Content-Type Expires Last-Modified | ||||||||||||||||||||||||||||
| 32 | ); | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 2 | ▶ | sub import { | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=6 SCAR=19.9
| |||||||||||||||||||||||||||||
| 35 | 2 | my ($class, %args) = @_; | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 2 | ▶ | while (my ($key, $value) = each %args) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | 1 | $key =~ s/^-//; | |||||||||||||||||||||||||||
| 39 | 1 | $Option->{$key} = $value; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 2 | for (my $level = 0; ; $level++) { | |||||||||||||||||||||||||||
| 43 | 7 | ▶ | my ($pkg, $file) = caller($level) or last; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | 7 | ▶ | next unless $file eq $0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 2 | ▶ | if (defined $Pkg && $pkg ne $Pkg) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $LWPx::Record::DataSection::Pkg and $pkg ne $LWPx::Record::DataSection::Pkg
MC/DC0%0/2
defined $LWPx::Record::DataSection::Pkg and $pkg ne $LWPx::Record::DataSection::Pkg
defined $LWPx::Record::DataSection::Pkg
$pkg ne $LWPx::Record::DataSection::Pkg
Truth table33%1/3
A and B
A= defined $LWPx::Record::DataSection::PkgB= $pkg ne $LWPx::Record::DataSection::Pkg
| |||||||||||||||||||||||||||||
| 47 | 0 | require Carp; | |||||||||||||||||||||||||||
| 48 | 0 | Carp::croak("only one class can use $class"); | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 2 | ($Pkg, $File) = ($pkg, $file); | |||||||||||||||||||||||||||
| 52 | 2 | ▶ | on_scope_end { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=12.7
| |||||||||||||||||||||||||||||
| 53 | 2 | $class->load_data; | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | # append __DATA__ section only when direct import | ||||||||||||||||||||||||||||
| 56 | 2 | ▶ | if ($level == 0 && not defined $Data) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$level == 0 and not defined $LWPx::Record::DataSection::Data
MC/DC0%0/2
$level == 0 and not defined $LWPx::Record::DataSection::Data
$level == 0
not defined $LWPx::Record::DataSection::Data
Truth table66%2/3
A and not(B)
A= $level == 0B= not defined $LWPx::Record::DataSection::Data
| |||||||||||||||||||||||||||||
| 57 | 0 | __PACKAGE__->append_to_file("\n__DATA__\n\n"); | |||||||||||||||||||||||||||
| 58 | 0 | $Data = {}; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 2 | LWP::Protocol::Fake->fake; | |||||||||||||||||||||||||||
| 62 | 2 | }; | |||||||||||||||||||||||||||
| 63 | 2 | return; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 0 | require Carp; | |||||||||||||||||||||||||||
| 67 | 0 | Carp::croak "Suitable file not found: $0"; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 2 | ▶ | sub load_data { | ||||||||||||||||||||||||||
|
Subroutine
load_data: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 71 | 2 | my $class = shift; | |||||||||||||||||||||||||||
| 72 | 2 | $Data = Data::Section::Simple->new($Pkg)->get_data_section; | |||||||||||||||||||||||||||
| 73 | 2 | return $Data; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 0 | ▶ | sub append_to_file { | ||||||||||||||||||||||||||
|
Subroutine
append_to_file: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 77 | 0 | my $class = shift; | |||||||||||||||||||||||||||
| 78 | 0 | ▶ | return unless $Option->{append_data_section}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 0 | ▶ | unless ($Fh && fileno $Fh) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$LWPx::Record::DataSection::Fh and fileno $LWPx::Record::DataSection::Fh
MC/DC0%0/2
$LWPx::Record::DataSection::Fh and fileno $LWPx::Record::DataSection::Fh
$LWPx::Record::DataSection::Fh
fileno $LWPx::Record::DataSection::Fh
Truth table0%0/3
A and B
A= $LWPx::Record::DataSection::FhB= fileno $LWPx::Record::DataSection::Fh
| |||||||||||||||||||||||||||||
| 80 | 0 | ▶ | open $Fh, '>>', $File or die $!; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | 0 | print $Fh @_; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 2 | ▶ | sub request_to_key { | ||||||||||||||||||||||||||
|
Subroutine
request_to_key: called CC=3 SCAR=17.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 86 | 2 | my ($class, $req) = @_; | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 2 | my @keys = ( $req->method, $req->uri ); | |||||||||||||||||||||||||||
| 89 | 2 | ▶ | if (my $cookie_keys = $Option->{record_request_cookie}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 0 | my $cookie = $req->header('Cookie'); | |||||||||||||||||||||||||||
| 91 | 0 | my %cookies = CGI::Simple::Cookie->parse($cookie); | |||||||||||||||||||||||||||
| 92 | 0 | push @keys, 'Cookie:' . join ',', map { "$_=" . $cookies{$_}->value } grep { $cookies{$_} } sort @$cookie_keys; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | 2 | ▶ | if (my $post_params = $Option->{record_post_param}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 0 | my $q = CGI::Simple->new($req->content); | |||||||||||||||||||||||||||
| 96 | 0 | push @keys, 'Post:' . join ',', map { my $key = $_; map { "$key=$_" } $q->param($_) } grep { $q->param($_) } sort @$post_params; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 2 | return join ' ', @keys; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 2 | ▶ | sub restore_response { | ||||||||||||||||||||||||||
|
Subroutine
restore_response: called CC=4 SCAR=14.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 103 | 2 | my ($class, $req) = @_; | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 2 | my $key = $class->request_to_key($req); | |||||||||||||||||||||||||||
| 106 | 2 | ▶ | if (my $string = $Data && $Data->{$key}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$LWPx::Record::DataSection::Data && $LWPx::Record::DataSection::Data->{$key}
MC/DC0%0/2
$LWPx::Record::DataSection::Data && $LWPx::Record::DataSection::Data->{$key}
$LWPx::Record::DataSection::Data
$LWPx::Record::DataSection::Data->{$key}
Truth table33%1/3
A && B
A= $LWPx::Record::DataSection::DataB= $LWPx::Record::DataSection::Data->{$key}
| |||||||||||||||||||||||||||||
| 107 | 2 | $string =~ s/\n\z//; | |||||||||||||||||||||||||||
| 108 | 2 | ▶ | utf8::encode $string if utf8::is_utf8 $string; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 2 | my $res = HTTP::Response->parse($string); | |||||||||||||||||||||||||||
| 110 | 2 | $res->request($req); | |||||||||||||||||||||||||||
| 111 | 2 | return $res; | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 0 | ▶ | sub store_response { | ||||||||||||||||||||||||||
|
Subroutine
store_response: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 116 | 0 | my ($class, $res, $req) = @_; | |||||||||||||||||||||||||||
| 117 | 0 | my $key = $class->request_to_key($req); | |||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 0 | my $res_to_store = $res->clone; | |||||||||||||||||||||||||||
| 120 | 0 | ▶ | if ($Option->{decode_content}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | 0 | my $content = $res_to_store->decoded_content; | |||||||||||||||||||||||||||
| 122 | 0 | ▶ | utf8::encode $content if utf8::is_utf8 $content; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 0 | $res_to_store->content($content); | |||||||||||||||||||||||||||
| 124 | 0 | $res_to_store->content_length(length $content); | |||||||||||||||||||||||||||
| 125 | 0 | $res_to_store->remove_header('Content-Encoding'); | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 0 | ▶ | my $record_response_header = $Option->{record_response_header} || []; | ||||||||||||||||||||||||||
|
Condition0%0/2
$LWPx::Record::DataSection::Option->{'record_response_header'} || []
MC/DC0%0/1
$LWPx::Record::DataSection::Option->{'record_response_header'} || []
$LWPx::Record::DataSection::Option->{'record_response_header'}
Truth table0%0/2
A
A= $LWPx::Record::DataSection::Option->{'record_response_header'}
| |||||||||||||||||||||||||||||
| 129 | 0 | ▶ | unless ($record_response_header eq ':all') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | 0 | my %header_to_keep = map { uc $_ => 1 } ( @CommonHeaders, @$record_response_header ); | |||||||||||||||||||||||||||
| 131 | 0 | foreach ($res_to_store->header_field_names) { | |||||||||||||||||||||||||||
| 132 | 0 | ▶ | $res_to_store->remove_header($_) unless $header_to_keep{ uc $_ }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 0 | $class->append_to_file("@@ $key\n"); | |||||||||||||||||||||||||||
| 137 | 0 | $class->append_to_file($res_to_store->as_string("\n"), "\n"); | |||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 0 | $Data->{$key} = $res_to_store->as_string; | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | package # | ||||||||||||||||||||||||||||
| 143 | LWP::Protocol::Fake; | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 2 | our $ORIGINAL_LWP_Protocol_create = \&LWP::Protocol::create; | |||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 2 | ▶ | sub fake { | ||||||||||||||||||||||||||
|
Subroutine
fake: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 148 | 2 | my $class = shift; | |||||||||||||||||||||||||||
| 149 | 2 | ▶ | no warnings 'redefine'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 150 | 2 | ▶ | *LWP::Protocol::create = sub { LWP::Protocol::Fake->new(@_) }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 0 | ▶ | sub unfake { | ||||||||||||||||||||||||||
|
Subroutine
unfake: not called CC=1 SCAR=0.6
| |||||||||||||||||||||||||||||
| 154 | 0 | my $class = shift; | |||||||||||||||||||||||||||
| 155 | 2 | ▶ | no warnings 'redefine'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 156 | 0 | *LWP::Protocol::create = $ORIGINAL_LWP_Protocol_create; | |||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 2 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 160 | 2 | my ($class, $scheme, $ua) = @_; | |||||||||||||||||||||||||||
| 161 | 2 | bless { scheme => $scheme, ua => $ua, real => &$ORIGINAL_LWP_Protocol_create($scheme, $ua) }, $class; | |||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 2 | ▶ | sub request { | ||||||||||||||||||||||||||
|
Subroutine
request: called CC=2 SCAR=9.2
| |||||||||||||||||||||||||||||
| 165 | 2 | my ($self, $request, $proxy, $arg, $size, $timeout) = @_; | |||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 2 | ▶ | if (my $res = LWPx::Record::DataSection->restore_response($request)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 168 | 2 | return $res; | |||||||||||||||||||||||||||
| 169 | } else { | ||||||||||||||||||||||||||||
| 170 | 0 | my $res = $self->{real}->request($request, $proxy, $arg, $size, $timeout); | |||||||||||||||||||||||||||
| 171 | 0 | LWPx::Record::DataSection->store_response($res, $request); | |||||||||||||||||||||||||||
| 172 | 0 | return $res; | |||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 2 | 1; | |||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | __END__ | ||||||||||||||||||||||||||||