lib/HTTP/OAI/UserAgent.pm

filter: Statementstmt 59.3% 95 / 160 Branchbran 35.7% 25 / 70 Conditioncond 38.4% 20 / 52 MC/DCmcdc 10.7% 3 / 28 Subroutinesub 78.9% 15 / 19 Podpod 37.5% 3 / 8 totaltotal 47.7% 161 / 337 CC 54 SCAR 44.5
CC
54
Cov
50%
_oai
175.1
decompress
42.0
lwp_callback
9.4
CRAP 86.0
↑ Summary script/oai_browser.pl »
1 package HTTP::OAI::UserAgent;
2  
3 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 11 ▶use vars qw(@ISA $ACCEPT);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 11 our $VERSION = '4.13';
9  
10 # Do not use eval()
11 11 our $USE_EVAL = 1;
12 # Ignore bad utf8 characters
13 11 our $IGNORE_BAD_CHARS = 1;
14 # Silence bad utf8 warnings
15 11 our $SILENT_BAD_CHARS = 0;
16  
17 11 ▶use constant MAX_UTF8_BYTES => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 11 require LWP::UserAgent;
20 11 @ISA = qw(LWP::UserAgent);
21  
22 11 ▶unless( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 11 0 11
23 11 $ACCEPT = "gzip";
24 }
25  
26 1 ▶sub delay { shift->_elem( "delay", @_ ) }
Subroutine delay: called CC=1 SCAR=0.0
Pod documented
27 1 ▶sub last_request_completed { shift->_elem( "last_request_completed", @_ ) }
Subroutine last_request_completed: called CC=1 SCAR=0.0
Pod undocumented
28  
29 0 ▶sub redirect_ok { 1 }
Subroutine redirect_ok: not called CC=1 SCAR=6.9
Pod documented
30  
31 10 ▶sub _oai {
Subroutine _oai: called CC=33 SCAR=51.7
32 10 my( $self, @args ) = @_;
33 10 ▶ my $cb = ref($args[0]) eq "CODE" ? shift @args : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $args[0] eq 'CODE' ? : 0 10 10
34 10 my %args = @args;
35 10 ▶ $cb = delete $args{onRecord} || $cb || $self->{onRecord};
Condition33%2/6
delete $args{'onRecord'} || $cb || $self->{'onRecord'}
l!l&&r!l&&!r
0010
delete $args{"onRecord"} or $cb
l!l&&r!l&&!r
0010
MC/DC0%0/3
delete $args{'onRecord'} || $cb || $self->{'onRecord'}
delete $args{"onRecord"} $cb $self->{'onRecord'}
Truth table25%1/4
A or B || C
A= delete $args{"onRecord"}B= $cbC= $self->{'onRecord'}
ABCresult
1XX1
01X1
0011
0000
36  
37 10 ▶ my $handlers = delete $args{handlers} || {};
Condition50%1/2
delete $args{'handlers'} || {}
l!l
010
MC/DC0%0/1
delete $args{'handlers'} || {}
delete $args{'handlers'}
Truth table50%1/2
A
A= delete $args{'handlers'}
Aresult
11
00
38  
39 10 ▶ if( !$args{force} && (my @errors = HTTP::OAI::Repository::validate_request(%args)) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $args{"force"} and my(@errors) = HTTP::OAI::Repository::validate_request(%args)) 0 10 10
Condition66%2/3
not $args{"force"} and my(@errors) = HTTP::OAI::Repository::validate_request(%args)
!ll&&!rl&&r
190
MC/DC0%0/2
not $args{"force"} and my(@errors) = HTTP::OAI::Repository::validate_request(%args)
not $args{"force"} my(@errors) = HTTP::OAI::Repository::validate_request(%args)
Truth table66%2/3
not(A) and B
A= not $args{"force"}B= my(@errors) = HTTP::OAI::Repository::validate_request(%args)
ABresult
0X0
100
111
40 0 return new HTTP::OAI::Response(
41 code=>503,
42 message=>'Invalid Request (use \'force\' to force a non-conformant request): ' . $errors[0]->toString,
43 errors=>\@errors
44 );
45 }
46  
47 # Get rid of any empty arguments
48 10 for( keys %args ) {
49 20 ▶ delete $args{$_} if !defined($args{$_}) || !length($args{$_});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{$_} and length $args{$_} 0 20 20
Condition33%1/3
defined $args{$_} and length $args{$_}
!ll&&!rl&&r
0020
MC/DC0%0/2
defined $args{$_} and length $args{$_}
defined $args{$_} length $args{$_}
Truth table33%1/3
A and B
A= defined $args{$_}B= length $args{$_}
ABresult
0X0
100
111
50 }
51  
52 10 my $request = HTTP::Request->new( GET => $self->_buildurl(%args) );
53  
54 10 delete $args{force};
55  
56 10 my $response = HTTP::OAI::Response->new(
57 %args,
58 handlers => $handlers,
59 onRecord => $cb,
60 );
61 10 $response->request( $request );
62 10 my $parser = XML::LibXML->new(
63 Handler => HTTP::OAI::SAX::Trace->new(
64 Handler => HTTP::OAI::SAX::Text->new(
65 Handler => $response
66 ) ) );
67 10 $parser->{content_length} = 0;
68 10 $parser->{content_buffer} = Encode::encode('UTF-8','');
69  
70 10 HTTP::OAI::Debug::trace( $args{verb} . " " . ref($parser) . "->parse_chunk()" );
71 10 my $r;
72 {
73 10 local $SIG{__DIE__};
74 14 ▶ $r = $self->SUPER::request($request,sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
75 14 $self->lwp_callback( $parser, @_ )
76 10 });
77 10 ▶ if( $r->is_success && !defined $r->headers->header( 'Client-Aborted' ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($r->is_success and not defined $r->headers->header("Client-Aborted")) 1 9 10
Condition100%3/3
$r->is_success and not defined $r->headers->header("Client-Aborted")
!ll&&!rl&&r
181
MC/DC100%2/2
$r->is_success and not defined $r->headers->header("Client-Aborted")
$r->is_success not defined $r->headers->header("Client-Aborted")
Truth table100%3/3
A and not(B)
A= $r->is_successB= not defined $r->headers->header("Client-Aborted")
ABresult
0X0
100
111
78 {
79 1 eval { $self->lwp_endparse( $parser ) };
80 1 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 1 1
81 {
82 0 $r->headers->header( 'Client-Aborted', 'die' );
83 0 $r->headers->header( 'X-Died', $@ );
84 }
85 }
86 }
87 10 ▶ if( defined($r->headers->header( 'Client-Aborted' )) && $r->headers->header( 'Client-Aborted' ) eq 'die' )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $r->headers->header("Client-Aborted") and $r->headers->header("Client-Aborted") eq "die") 8 2 10
Condition66%2/3
defined $r->headers->header("Client-Aborted") and $r->headers->header("Client-Aborted") eq "die"
!ll&&!rl&&r
208
MC/DC50%1/2
defined $r->headers->header("Client-Aborted") and $r->headers->header("Client-Aborted") eq "die"
defined $r->headers->header("Client-Aborted") $r->headers->header("Client-Aborted") eq "die"
Truth table66%2/3
A and B
A= defined $r->headers->header("Client-Aborted")B= $r->headers->header("Client-Aborted") eq "die"
ABresult
0X0
100
111
88 {
89 8 my $err = $r->headers->header( 'X-Died' );
90 8 ▶ if( $err eq "done" )
Branch: 1 branch
BranchTrueFalseTotal
if ($err eq 'done') { } 8 0 8
91 {
92 8 $r->code(200);
93 8 $r->message("OK");
94 }
95 else
96 {
97 0 $r->code(500);
98 0 $r->message( 'An error occurred while parsing: ' . $err );
99 }
100 }
101  
102 10 my $cnt_len = $parser->{content_length};
103 10 undef $parser;
104  
105 # OAI retry-after
106 10 ▶ if( defined($r) && ( $r->code == 503 || $r->code == 429 ) && defined(my $timeout = $r->headers->header('Retry-After')) ) {
Branch: 4 branches
BranchTrueFalseTotal
if (defined $r and $r->code == 503 || $r->code == 429 and defined(my $timeout = $r->headers->header('Retry-After'))) { } 0 10 10
elsif (defined $r and $r->is_success and $cnt_len == 0) { } 0 10 10
elsif ($r->is_error) { } 1 9 10
elsif ($@) { } 0 9 9
Condition33%3/9
defined $r and $r->code == 503 || $r->code == 429 and defined(my $timeout = $r->headers->header("Retry-After"))
!ll&&!rl&&r
1000
defined $r and $r->code == 503 || $r->code == 429
!ll&&!rl&&r
0100
$r->code == 503 or $r->code == 429
l!l&&r!l&&!r
0010
MC/DC0%0/4
defined $r and $r->code == 503 || $r->code == 429 and defined(my $timeout = $r->headers->header("Retry-After"))
defined $r $r->code == 503 $r->code == 429 defined(my $timeout = $r->headers->header("Retry-After"))
Truth table16%1/6
A and B or C and D
A= defined $rB= $r->code == 503C= $r->code == 429D= defined(my $timeout = $r->headers->header("Retry-After"))
ABCDresult
0XXX0
100X0
11X00
10100
11X11
10111
107 0 ▶ if( $self->{recursion}++ > 10 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"recursion"}++ > 10) 0 0 0
108 0 $r->code(500);
109 0 $r->message("Server did not give a response after 10 retries");
110 0 return $r;
111 }
112 0 ▶ if( !$timeout or $timeout =~ /\D/ or $timeout < 0 or $timeout > 86400 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $timeout or $timeout =~ /\D/ or $timeout < 0 or $timeout > 86400) 0 0 0
Condition0%0/9
not $timeout or $timeout =~ /\D/ or $timeout < 0 or $timeout > 86400
l!l&&r!l&&!r
000
not $timeout or $timeout =~ /\D/ or $timeout < 0
l!l&&r!l&&!r
000
not $timeout or $timeout =~ /\D/
l!l&&r!l&&!r
000
MC/DC0%0/4
not $timeout or $timeout =~ /\D/ or $timeout < 0 or $timeout > 86400
not $timeout $timeout =~ /\D/ $timeout < 0 $timeout > 86400
Truth table0%0/5
not(A) or B or C or D
A= not $timeoutB= $timeout =~ /\D/C= $timeout < 0D= $timeout > 86400
ABCDresult
1XXX1
01XX1
001X1
00011
00000
113 0 $r->code(500);
114 0 ▶ $r->message("Server specified an unsupported duration to wait (\"".($timeout||'null')."\"");
Condition0%0/2
$timeout || 'null'
l!l
00
MC/DC0%0/1
$timeout || 'null'
$timeout
Truth table0%0/2
A
A= $timeout
Aresult
11
00
115 0 return $r;
116 }
117 0 HTTP::OAI::Debug::trace( "Waiting $timeout seconds" );
118 0 sleep($timeout+10); # We wait an extra 10 secs for safety
119 0 return $self->_oai(@args);
120 # Got an empty response
121 ▶ } elsif( defined($r) && $r->is_success && $cnt_len == 0 ) {
Condition66%4/6
defined $r and $r->is_success and $cnt_len == 0
!ll&&!rl&&r
190
defined $r and $r->is_success
!ll&&!rl&&r
019
MC/DC0%0/3
defined $r and $r->is_success and $cnt_len == 0
defined $r $r->is_success $cnt_len == 0
Truth table50%2/4
A and B and C
A= defined $rB= $r->is_successC= $cnt_len == 0
ABCresult
0XX0
10X0
1100
1111
122 0 ▶ if( $self->{recursion}++ > 10 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"recursion"}++ > 10) 0 0 0
123 0 $r->code(500);
124 0 $r->message("No content in server response");
125 0 return $r;
126 }
127 0 HTTP::OAI::Debug::trace( "Retrying on empty response" );
128 0 sleep(5);
129 0 return $self->_oai(@args);
130 # An HTTP error occurred
131 } elsif( $r->is_error ) {
132 1 return $r;
133 # An error occurred during parsing
134 } elsif( $@ ) {
135 0 ▶ $r->code(my $code = $@ =~ /read timeout/ ? 504 : 600);
Branch: 1 branch
BranchTrueFalseTotal
$@ =~ /read timeout/ ? : 0 0 0
136 0 $r->message($@);
137 0 return $r;
138 }
139  
140 # access the original response via previous
141 9 $response->previous($r);
142  
143 9 return $response;
144 }
145  
146 sub request
147 1 ▶{
Subroutine request: called CC=4 SCAR=18.6
Pod documented
148 1 my( $self, @args ) = @_;
149  
150 1 my $delay = $self->delay;
151 1 ▶ if( defined $delay )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $delay) 0 1 1
152 {
153 0 ▶ if( ref($delay) eq "CODE" )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $delay eq "CODE") 0 0 0
154 {
155 0 $delay = &$delay( $self->last_request_completed );
156 }
157 0 ▶ select(undef,undef,undef,$delay) if $delay > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $delay > 0 0 0 0
158 }
159  
160 1 my $r = $self->SUPER::request( @args );
161  
162 1 $self->last_request_completed( time );
163  
164 1 return $r;
165 }
166  
167 sub lwp_badchar
168 0 ▶{
Subroutine lwp_badchar: not called CC=2 SCAR=17.9
Pod undocumented
169 0 my $codepoint = sprintf('U+%04x', ord($_[2]));
170 0 ▶ unless( $SILENT_BAD_CHARS )
Branch: 1 branch
BranchTrueFalseTotal
unless ($HTTP::OAI::UserAgent::SILENT_BAD_CHARS) 0 0 0
171 {
172 0 warn "Bad Unicode character $codepoint at byte offset ".$_[1]->{content_length}." from ".$_[1]->{request}->uri."\n";
173 }
174 0 return $codepoint;
175 }
176  
177 sub lwp_endparse
178 1 ▶{
Subroutine lwp_endparse: called CC=4 SCAR=17.9
Pod undocumented
179 1 my( $self, $parser ) = @_;
180  
181 1 my $utf8 = $parser->{content_buffer};
182 # Replace bad chars with '?'
183 1 ▶ if( $IGNORE_BAD_CHARS and length($utf8) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($HTTP::OAI::UserAgent::IGNORE_BAD_CHARS and length $utf8) 0 1 1
Condition33%1/3
$HTTP::OAI::UserAgent::IGNORE_BAD_CHARS and length $utf8
!ll&&!rl&&r
010
MC/DC0%0/2
$HTTP::OAI::UserAgent::IGNORE_BAD_CHARS and length $utf8
$HTTP::OAI::UserAgent::IGNORE_BAD_CHARS length $utf8
Truth table33%1/3
A and B
A= $HTTP::OAI::UserAgent::IGNORE_BAD_CHARSB= length $utf8
ABresult
0X0
100
111
184 0 ▶ $utf8 = Encode::decode('UTF-8', $utf8, sub { $self->lwp_badchar($parser, @_) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
185 }
186 1 ▶ if( length($utf8) > 0 )
Branch: 1 branch
BranchTrueFalseTotal
if (length $utf8 > 0) 0 1 1
187 {
188 0 _ccchars($utf8); # Fix control chars
189 0 $parser->{content_length} += length($utf8);
190 0 $parser->parse_chunk($utf8);
191 }
192 1 delete($parser->{content_buffer});
193 1 $parser->parse_chunk('', 1);
194 }
195  
196 sub lwp_callback
197 14 ▶{
Subroutine lwp_callback: called CC=6 SCAR=22.4
Pod undocumented
198 14 my( $self, $parser ) = @_;
199  
200 11 ▶ use bytes; # fixing utf-8 will need byte semantics
Subroutine BEGIN: called CC=1 SCAR=0.0
201  
202 14 $parser->{content_buffer} .= $_[2];
203  
204 do
205 {
206 # FB_QUIET won't split multi-byte chars on input
207 14 my $utf8 = Encode::decode('UTF-8', $parser->{content_buffer}, Encode::FB_QUIET);
208  
209 14 ▶ if( length($utf8) > 0 )
Branch: 1 branch
BranchTrueFalseTotal
if (length $utf8 > 0) 14 0 14
210 {
211 11 ▶ use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
212 14 _ccchars($utf8); # Fix control chars
213 14 $parser->{content_length} += length($utf8);
214 14 $parser->parse_chunk($utf8);
215 }
216  
217 6 ▶ if( length($parser->{content_buffer}) > MAX_UTF8_BYTES )
Branch: 1 branch
BranchTrueFalseTotal
if (length $parser->{"content_buffer"} > 4) 0 6 6
218 {
219 0 $parser->{content_buffer} =~ s/^([\x80-\xff]{1,4})//s;
220 0 my $badbytes = $1;
221 0 ▶ if( length($badbytes) == 0 )
Branch: 1 branch
BranchTrueFalseTotal
if (length $badbytes == 0) 0 0 0
222 {
223 0 Carp::confess "Internal error - bad bytes but not in 0x80-0xff range???";
224 }
225 0 ▶ if( $IGNORE_BAD_CHARS )
Branch: 1 branch
BranchTrueFalseTotal
if ($HTTP::OAI::UserAgent::IGNORE_BAD_CHARS) 0 0 0
226 {
227 $badbytes = join('', map {
228 0 $self->lwp_badchar($parser, $_)
229 } split //, $badbytes);
230 }
231 0 $parser->parse_chunk( $badbytes );
232 }
233 14 ▶ } while( length($parser->{content_buffer}) > MAX_UTF8_BYTES );
Branch: 1 branch
BranchTrueFalseTotal
length $parser->{"content_buffer"} > 4 and do { do { my $utf8 = &Encode::decode("UTF-8", $parser->{"content_buffer"}, 4); if (length $utf8 > 0) { _ccchars($utf8); $parser->{"content_length"} += length $utf8; $parser->parse_chunk($utf8); } ; if (length $parser->{"content_buffer"} > 4) { $parser->{"content_buffer"} =~ s/^([\x80-\xff]{1,4})//s; my $badbytes = $1; if (length $badbytes == 0) { Carp::confess("Internal error - bad bytes but not in 0x80-0xff range???"); } ; if ($HTTP::OAI::UserAgent::IGNORE_BAD_CHARS) { $badbytes = join("", map({$self->lwp_badchar($parser, $_);} split(//, $badbytes, 0))); } ; $parser->parse_chunk($badbytes); } } } 0 6 6
234 }
235  
236 14 ▶sub _ccchars {
Subroutine _ccchars: called CC=1 SCAR=1.2
237 0 $_[0] =~ s/([\x00-\x08\x0b-\x0c\x0e-\x1f])/sprintf("\\%04d",ord($1))/seg;
238 }
239  
240 10 ▶sub _buildurl {
Subroutine _buildurl: called CC=5 SCAR=17.1
241 10 my( $self, %args ) = @_;
242  
243 10 ▶ Carp::confess "Requires verb parameter" unless $args{'verb'};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"verb"} 0 10 10
244  
245 10 my $uri = URI->new( $self->baseURL );
246 10 ▶ return $uri->as_string if $uri->scheme eq "file";
Branch: 1 branch
BranchTrueFalseTotal
if $uri->scheme eq "file" 9 1 10
247  
248 1 ▶ if( defined($args{resumptionToken}) && !$args{force} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{'resumptionToken'} and not $args{'force'}) { } 0 1 1
Condition33%1/3
defined $args{"resumptionToken"} and not $args{"force"}
!ll&&!rl&&r
100
MC/DC0%0/2
defined $args{"resumptionToken"} and not $args{"force"}
defined $args{"resumptionToken"} not $args{"force"}
Truth table33%1/3
A and not(B)
A= defined $args{"resumptionToken"}B= not $args{"force"}
ABresult
0X0
100
111
249 0 $uri->query_form(verb=>$args{'verb'},resumptionToken=>$args{'resumptionToken'});
250 } else {
251 1 delete $args{force};
252 # http://www.cshc.ubc.ca/oai/ breaks if verb isn't first, doh
253 1 $uri->query_form(verb=>delete($args{'verb'}),%args);
254 }
255  
256 1 return $uri->as_string;
257 }
258  
259 0 ▶sub decompress {
Subroutine decompress: not called CC=6 SCAR=37.4
Pod undocumented
260 0 my ($response) = @_;
261 0 my $type = $response->headers->header("Content-Encoding");
262 0 ▶ return $response->{_content_filename} unless defined($type);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type 0 0 0
263 0 ▶ if( $type eq 'gzip' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'gzip') { } 0 0 0
264 0 my $filename = File::Temp->new( UNLINK => 1 );
265 0 ▶ my $gz = Compress::Zlib::gzopen($response->{_content_filename}, "r") or die $!;
Branch: 1 branch
BranchTrueFalseTotal
my $gz = Compress::Zlib::gzopen($response->{"_content_filename"}, "r") or die $! 0 0 0
266 0 my ($buffer,$c);
267 0 my $fh = IO::File->new($filename,"w");
268 0 binmode($fh,":utf8");
269 0 ▶ while( ($c = $gz->gzread($buffer)) > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($c = $gz->gzread($buffer)) > 0) 0 0 0
270 0 print $fh $buffer;
271 }
272 0 $fh->close();
273 0 $gz->gzclose();
274 0 ▶ die "Error decompressing gziped response: " . $gz->gzerror() if -1 == $c;
Branch: 1 branch
BranchTrueFalseTotal
if -1 == $c 0 0 0
275 0 return $response->{_content_filename} = $filename;
276 } else {
277 0 die "Unsupported compression returned: $type\n";
278 }
279 }
280  
281 11 1;
282  
283 __END__
↑ Summary script/oai_browser.pl »