WARC/Record/Replay/HTTP/Message.pm

filter: Statementstmt 97.1% 67 / 69 Branchbran 87.5% 21 / 24 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 11 / 11 Podpod 100% 2 / 2 totaltotal 95.2% 101 / 106 CC 14 SCAR 15.1
CC
14
Cov
95%
_load_record
9.0
_load_content
3.0
BEGIN
2.0
CRAP 4.5
« WARC/Record/Block.pm ↑ Summary WARC/Date.pm »
1 package WARC::Record::Replay::HTTP::Message; # -*- 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 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Fcntl qw(:seek);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 4 require HTTP::Message;
10 4 our @ISA = qw(HTTP::Message);
11  
12 4 ▶use WARC; *WARC::Record::Replay::HTTP::Message::VERSION = \$WARC::VERSION;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 4 require WARC::Record::Replay;
15  
16 BEGIN {
17 4 ▶ use WARC::Record::Replay::HTTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 $WARC::Record::Replay::HTTP::Message::{$_} =
19 $WARC::Record::Replay::HTTP::{$_}
20 4 ▶ for WARC::Record::Replay::HTTP::HTTP_PARSE_REs;
Subroutine BEGIN: called CC=2 SCAR=6.9
21 }
22  
23 sub _load_record {
24 74 ▶ my $ob = shift; # partially constructed request/response object
Subroutine _load_record: called CC=9 SCAR=22.0
25 74 my $record = shift; # WARC::Record object
26 74 my $handle = shift; # open handle for reading record data or undef
27  
28 74 local *_;
29  
30 74 $ob->{_warc_record} = $record;
31  
32 74 ▶ if ($handle) {
Branch: 1 branch
BranchTrueFalseTotal
if ($handle) 25 49 74
33 # Read headers from $handle.
34 {
35 25 my @headers = ();
36 25 local $/ = "\012";
37 25 ▶ while (<$handle>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $handle)) 105 0 105
38 105 s/[\015\012]+$//;
39 105 ▶ if (m/^($HTTP__token):\s+(.*)/o) # $1 -- name $2 -- value
Branch: 3 branches
BranchTrueFalseTotal
if (/^($WARC::Record::Replay::HTTP::HTTP__token):\s+(.*)/o) { } 75 30 105
elsif (/^(\s+\S.*)$/) { } 5 25 30
elsif (/^$/) { } 25 0 25
40 75 { push @headers, $1, $2 }
41 elsif (m/^(\s+\S.*)$/) # $1 -- continued value
42 5 { $headers[-1] .= $1 }
43 25 elsif (m/^$/) { last }
44 0 else { warn "unrecogized input: $_"; return undef }
45 }
46 25 local $HTTP::Headers::TRANSLATE_UNDERSCORE;
47 25 $ob->headers->push_header(@headers);
48 }
49  
50 25 my $data_offset = tell *$handle;
51 25 $ob->{_warc_data_offset} = $data_offset;
52 25 $record->{payload_offset} = $data_offset;
53  
54 # Decide whether to read or defer loading the message body.
55 25 ▶ if ($record->field('Content-Length') == $data_offset) {
Branch: 2 branches
BranchTrueFalseTotal
if ($record->field('Content-Length') == $data_offset) { } 14 11 25
elsif ($record->field('Content-Length') - $data_offset < $WARC::Record::Replay::HTTP::Content_Deferred_Loading_Threshold) { } 3 8 11
56 # There is no content. Set an empty message body.
57 14 $ob->content('')
58 } elsif (($record->field('Content-Length') - $data_offset)
59 < $WARC::Record::Replay::HTTP::Content_Deferred_Loading_Threshold) {
60 # After reading headers, the length of the remaining data is less than
61 # the deferred loading threshold. Load the message body immediately.
62 3 { local $/ = undef; $ob->content(<$handle>) } # slurp data
63 } else {
64 # Defer loading the message body.
65 8 $ob->{_warc_defer}{content} = 1
66 }
67 }
68  
69 bless $ob, 'WARC::Record::Replay::'.(ref $ob)
70 74 ▶ if scalar grep $ob->{_warc_defer}{$_}, keys %{$ob->{_warc_defer}};
Branch: 1 branch
BranchTrueFalseTotal
if scalar grep($ob->{"_warc_defer"}{$_}, keys %{$$ob{"_warc_defer"};}) 64 10 74
71  
72 74 return $ob;
73 }
74  
75 sub _load_content {
76 6 ▶ my $self = shift;
Subroutine _load_content: called CC=3 SCAR=11.0
77  
78 croak "loading content larger than maximum length"
79 unless (($self->{_warc_record}->field('Content-Length')
80 - $self->{_warc_data_offset})
81 6 ▶ < $WARC::Record::Replay::HTTP::Content_Maximum_Length);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_warc_record"}->field("Content-Length") - $self->{"_warc_data_offset"} < $WARC::Record::Replay::HTTP::Content_Maximum_Length 1 5 6
82  
83 5 my $handle = $self->{_warc_record}->open_continued;
84 5 ▶ seek($handle, $self->{_warc_data_offset}, SEEK_SET) or confess "seek: $!";
Branch: 1 branch
BranchTrueFalseTotal
seek $handle, $self->{"_warc_data_offset"}, 0 or confess("seek: $!") 0 5 5
85 5 { local $/ = undef; $self->SUPER::content(<$handle>) } # slurp data
86  
87 5 $self->{_warc_defer}{content} = 0;
88 }
89  
90 ## overridden methods for deferred message body loading
91 sub content {
92 18 ▶ my $self = shift;
Subroutine content: called CC=2 SCAR=6.9
Pod documented
93  
94 18 ▶ $self->_load_content if $self->{_warc_defer}{content};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_warc_defer"}{"content"} 5 13 18
95  
96 17 return $self->SUPER::content(@_);
97 }
98  
99 sub content_ref {
100 10 ▶ my $self = shift;
Subroutine content_ref: called CC=2 SCAR=6.9
Pod documented
101  
102 10 ▶ $self->_load_content if $self->{_warc_defer}{content};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_warc_defer"}{"content"} 1 9 10
103  
104 10 return $self->SUPER::content_ref(@_);
105 }
106  
107 4 1;
108 __END__
« WARC/Record/Block.pm ↑ Summary WARC/Date.pm »