WARC/Record/Replay/HTTP/Response.pm

filter: Statementstmt 98.4% 65 / 66 Branchbran 93.7% 15 / 16 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 10 / 10 Podpod 100% 2 / 2 totaltotal 97.9% 97 / 99 CC 11 SCAR 11.6
CC
11
Cov
98%
_load_record
5.0
request
5.0
__ANON__
2.0
CRAP 3.2
« WARC/Index/File/CDX/Builder.pm ↑ Summary WARC/Record/Sponge.pm »
1 package WARC::Record::Replay::HTTP::Response; # -*- CPerl -*-
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 require HTTP::Response;
7 4 require WARC::Record::Replay::HTTP::Message;
8 4 our @ISA = qw(WARC::Record::Replay::HTTP::Message HTTP::Response);
9  
10 4 ▶use WARC; *WARC::Record::Replay::HTTP::Response::VERSION = \$WARC::VERSION;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 4 require WARC::Record::Replay;
13  
14 WARC::Record::Replay::register
15 131 ▶ { $_->field('Content-Type') =~ m|^application/http; msgtype=response| }
Subroutine __ANON__: called CC=1 SCAR=0.0
16 4 \&_load_record;
17 WARC::Record::Replay::register
18 131 ▶ { $_->field('Content-Type') =~ m|^application/http| && $_->type eq 'response' }
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_->field("Content-Type") =~ m[^application/http] and $_->type eq "response" 128 3 131
19 4 \&_load_record;
20  
21 BEGIN {
22 4 ▶ use WARC::Record::Replay::HTTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 $WARC::Record::Replay::HTTP::Response::{$_} =
24 $WARC::Record::Replay::HTTP::{$_}
25 4 ▶ for WARC::Record::Replay::HTTP::HTTP_PARSE_REs;
Subroutine BEGIN: called CC=2 SCAR=6.9
26 }
27  
28 sub _load_record {
29 68 ▶ my $record = shift;
Subroutine _load_record: called CC=5 SCAR=16.1
30  
31 68 my $handle = $record->open_continued;
32 68 my $ob;
33  
34 68 ▶ if ($record->field('Content-Length')
Branch: 1 branch
BranchTrueFalseTotal
if ($record->field('Content-Length') < $WARC::Record::Replay::HTTP::Content_Deferred_Loading_Threshold) { } 53 15 68
35 < $WARC::Record::Replay::HTTP::Content_Deferred_Loading_Threshold) {
36 # The entire WARC block is smaller than the deferred loading threshold;
37 # this is an easy special case.
38 53 my $block;
39 {
40 53 local $/ = undef; # slurp
41 53 $block = <$handle>;
42 }
43 # Work around a bug in LWP that can append a trailing CR to the status
44 # message in an HTTP response parsed from a string.
45 #
46 # The response status message can contain spaces and LWP uses the
47 # LIMIT parameter to split to collect all text after the status code.
48 # This causes the parsed message to include the trailing CR.
49 #
50 # This problem does not occur with requests; the protocol version in a
51 # request cannot contain spaces, so the trailing CR is counted as
52 # whitespace and removed as the delimiter for a trailing empty field.
53 53 my $response = HTTP::Response->parse($block);
54 53 { my $m;
55 53 ▶ if (($m = $response->message) =~ s/\015\z//) { $response->message($m) } }
Branch: 1 branch
BranchTrueFalseTotal
if (($m = $response->message) =~ s/\015\z//) 0 53 53
56 53 ▶ if ($response->protocol =~ $HTTP__Version)
Branch: 1 branch
BranchTrueFalseTotal
if ($response->protocol =~ /$WARC::Record::Replay::HTTP::HTTP__Version/) { } 49 4 53
57 49 { $ob = $response; close $handle; $handle = undef }
58 else
59 4 { return undef }
60 } else {
61 15 my $code; my $reason; my $http_version;
62 {
63 15 local $/ = "\012";
64 15 my $line = <$handle>;
65 15 $line =~ s/[[:space:]]+$//; # trim trailing CR if present
66 15 ▶ return undef unless $line =~ $HTTP__Status_Line;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /$WARC::Record::Replay::HTTP::HTTP__Status_Line/ 2 13 15
67 # $1 -- HTTP-Version $2 -- Status-Code $3 -- Reason-Phrase
68 13 $http_version = $1; $code = $2; $reason = $3;
69 }
70  
71 13 $ob = HTTP::Response->new($code, $reason);
72 13 $ob->protocol($http_version);
73 }
74  
75 62 $ob->{_warc_defer}{request} = 1;
76  
77 62 WARC::Record::Replay::HTTP::Message::_load_record($ob, $record, $handle);
78 }
79  
80 ## overridden methods for deferred loading of requests and redirects
81 sub request {
82 9 ▶ my $self = shift;
Subroutine request: called CC=5 SCAR=16.1
Pod documented
83  
84 9 ▶ if ($self->{_warc_defer}{request} && $self->{_warc_record}{collection}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_warc_defer"}{"request"} and $self->{"_warc_record"}{"collection"}) 7 2 9
Condition100%3/3
$self->{"_warc_defer"}{"request"} and $self->{"_warc_record"}{"collection"}
!ll&&!rl&&r
117
MC/DC100%2/2
$self->{"_warc_defer"}{"request"} and $self->{"_warc_record"}{"collection"}
$self->{"_warc_defer"}{"request"} $self->{"_warc_record"}{"collection"}
Truth table100%3/3
A and B
A= $self->{"_warc_defer"}{"request"}B= $self->{"_warc_record"}{"collection"}
ABresult
0X0
100
111
85 # replay other record
86 7 my $timestamp = $self->{_warc_record}->date;
87 9 ▶ my @requests = grep { $_->type eq 'request' and $_->date <= $timestamp }
Branch: 1 branch
BranchTrueFalseTotal
$_->type eq "request" and $_->date <= $timestamp 7 2 9
88 ($self->{_warc_record}{collection}->search
89 7 (record_id => $self->{_warc_record}->fields->{'WARC-Concurrent-To'},
90 time => $timestamp)); # sorted by nearest timestamp
91 # filter removes all records after "this" record
92 # Therefore the list is sorted/filtered to "timestamp descending"
93 7 my $record = $requests[0]; # use latest record from the list
94  
95 7 ▶ $self->SUPER::request($record->replay) if $record;
Branch: 1 branch
BranchTrueFalseTotal
if $record 6 1 7
96 7 $self->{_warc_defer}{request} = 0;
97 }
98  
99 9 return $self->SUPER::request(@_)
100 }
101  
102 sub previous {
103 1 ▶ my $self = shift;
Subroutine previous: called CC=1 SCAR=0.0
Pod documented
104  
105 # This is a stub until a good way to find these is found.
106 # Additional metadata and/or index support will probably be needed.
107 # Lack of support is documented in WARC::Record::Replay::HTTP.
108  
109 1 return $self->SUPER::previous(@_)
110 }
111  
112 4 1;
113 __END__
« WARC/Index/File/CDX/Builder.pm ↑ Summary WARC/Record/Sponge.pm »