Parser/Reader.pm

filter: Statementstmt 72.1% 106 / 147 Branchbran 44.8% 35 / 78 Conditioncond 66.6% 18 / 27 MC/DCmcdc 62.5% 10 / 16 Subroutinesub 100% 14 / 14 Podpod 0.0% 0 / 12 totaltotal 62.2% 183 / 294 CC 53 SCAR 48.7
CC
53
Cov
63%
read_chunk
249.7
eos_type
5.6
native_handle
4.1
CRAP 130.2
↑ Summary Field/ParamVal.pm »
1 package MIME::Parser::Reader;
2  
3 =head1 NAME
4  
5 MIME::Parser::Reader - a line-oriented reader for a MIME::Parser
6  
7  
8 =head1 SYNOPSIS
9  
10 This module is used internally by MIME::Parser; you probably
11 don't need to be looking at it at all. But just in case...
12  
13 ### Create a top-level reader, where chunks end at EOF:
14 $rdr = MIME::Parser::Reader->new();
15  
16 ### Spawn a child reader, where chunks also end at a boundary:
17 $subrdr = $rdr->spawn->add_boundary($bound);
18  
19 ### Spawn a child reader, where chunks also end at a given string:
20 $subrdr = $rdr->spawn->add_terminator($string);
21  
22 ### Read until boundary or terminator:
23 $subrdr->read_chunk($in, $out);
24  
25  
26 =head1 DESCRIPTION
27  
28 A line-oriented reader which can deal with virtual end-of-stream
29 defined by a collection of boundaries.
30  
31 B<Warning:> this is a private class solely for use by MIME::Parser.
32 This class has no official public interface
33  
34 =cut
35  
36 26 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 ### All possible end-of-line sequences.
39 ### Note that "" is included because last line of stream may have no newline!
40 26 my @EOLs = ("", "\r", "\n", "\r\n", "\n\r");
41  
42 ### Long line:
43 26 my $LONGLINE = ' ' x 1000;
44  
45  
46 #------------------------------
47 #
48 # new
49 #
50 # I<Class method.>
51 # Construct an empty (top-level) reader.
52 #
53 94 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
54 94 my ($class) = @_;
55 94 my $eos;
56 94 return bless {
57 Bounds => [],
58 BH => {},
59 TH => {},
60 LocalBH => {},
61 LocalTH => {},
62 Depth => 0,
63 EOS => \$eos,
64 }, $class;
65 }
66  
67 #------------------------------
68 #
69 # spawn
70 #
71 # I<Instance method.>
72 # Return a reader which is mostly a shallow copy.
73 #
74 5772 ▶sub spawn {
Subroutine spawn: called CC=1 SCAR=0.0
Pod undocumented
75 5772 my $self = shift;
76 5772 my $dup = bless {}, ref($self);
77 5772 $dup->{Parent} = $self; ### Parent we were spawned from
78 5772 $dup->{Bounds} = [ ]; ### do not copy
79 5772 $dup->{BH} = $self->{BH}; ### shallow copy
80 5772 $dup->{TH} = $self->{TH}; ### shallow copy
81 5772 $dup->{EOS} = $self->{EOS}; ### shallow copy; same ref!
82 5772 $dup->{LocalBH} = {}; ### do not copy
83 5772 $dup->{LocalTH} = {}; ### do not copy
84 5772 $dup->{Depth} = $self->{Depth} + 1; ### increment Depth
85 5772 $dup;
86 }
87  
88 #------------------------------
89 #
90 # add_boundary BOUND
91 #
92 # I<Instance method.>
93 # Let BOUND be the new innermost boundary. Returns self.
94 #
95 1797 ▶sub add_boundary {
Subroutine add_boundary: called CC=3 SCAR=11.1
Pod undocumented
96 1797 my ($self, $bound) = @_;
97 1797 unshift @{$self->{Bounds}}, $bound; ### now at index 0
98 1797 ▶ if (!exists($self->{BH}{"--$bound"})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"BH"}{"--$bound"}) 1797 0 1797
99 1797 $self->{BH}{"--$bound"} = "DELIM $bound";
100 1797 $self->{LocalBH}{"--$bound"} = 1;
101 }
102 1797 ▶ if (!exists($self->{BH}{"--$bound--"})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"BH"}{"--$bound--"}) 1797 0 1797
103 1797 $self->{BH}{"--$bound--"} = "CLOSE $bound";
104 1797 $self->{LocalBH}{"--$bound--"} = 1;
105 }
106 1797 $self;
107 }
108  
109 5866 ▶sub DESTROY {
Subroutine DESTROY: called CC=3 SCAR=11.0
110 5866 my ($self) = @_;
111 5866 foreach my $thing (keys(%{$self->{LocalBH}})) {
112 3594 delete($self->{BH}{$thing});
113 }
114 5866 foreach my $thing (keys(%{$self->{LocalTH}})) {
115 31800 delete($self->{TH}{$thing});
116 }
117 }
118  
119 #------------------------------
120 #
121 # add_terminator LINE
122 #
123 # I<Instance method.>
124 # Let LINE be another terminator. Returns self.
125 #
126 7950 ▶sub add_terminator {
Subroutine add_terminator: called CC=3 SCAR=11.0
Pod undocumented
127 7950 my ($self, $line) = @_;
128 7950 foreach (@EOLs) {
129 39750 ▶ if (!exists($self->{TH}{"$line$_"})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"TH"}{"$line$_"}) 31800 7950 39750
130 31800 $self->{TH}{"$line$_"} = "DONE $line";
131 31800 $self->{LocalTH}{"$line$_"} = 1;
132 }
133 }
134 7950 $self;
135 }
136  
137 #------------------------------
138 #
139 # has_bounds
140 #
141 # I<Instance method.>
142 # Are there boundaries to contend with?
143 #
144 6095 ▶sub has_bounds {
Subroutine has_bounds: called CC=3 SCAR=11.2
Pod undocumented
145 6095 my ($self) = @_;
146 6095 ▶ if (scalar(@{$self->{Bounds}})) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$self->{"Bounds"};}) 6067 28 6095
147 6067 return 1;
148 }
149 28 ▶ if ($self->{Parent}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"Parent"}) 0 28 28
150 0 return $self->{Parent}->has_bounds();
151 }
152 28 return 0;
153 }
154  
155 #------------------------------
156 #
157 # depth
158 #
159 # I<Instance method.>
160 # How many levels are there?
161 #
162 1297 ▶sub depth {
Subroutine depth: called CC=1 SCAR=0.0
Pod undocumented
163 1297 my ($self) = @_;
164 1297 return $self->{Depth};
165 }
166  
167 #------------------------------
168 #
169 # eos [EOS]
170 #
171 # I<Instance method.>
172 # Return the last end-of-stream token seen.
173 # See read_chunk() for what these might be.
174 #
175 15210 ▶sub eos {
Subroutine eos: called CC=2 SCAR=6.9
Pod undocumented
176 15210 my $self = shift;
177 15210 ▶ ${$self->{EOS}} = $_[0] if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 26 15184 15210
178 15210 ${$self->{EOS}};
179 }
180  
181 7135 ▶sub most_recent_boundary {
Subroutine most_recent_boundary: called CC=3 SCAR=11.3
Pod undocumented
182 7135 my ($self) = @_;
183 7135 ▶ return $self->{Bounds}[0] if scalar(@{$self->{Bounds}});
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$self->{"Bounds"};} 7131 4 7135
184 4 ▶ return $self->{Parent}->most_recent_boundary() if $self->{Parent};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Parent"} 4 0 4
185 0 return undef;
186 }
187  
188 #------------------------------
189 #
190 # eos_type [EOSTOKEN]
191 #
192 # I<Instance method.>
193 # Return the high-level type of the given token (defaults to our token).
194 #
195 # DELIM saw an innermost boundary like --xyz
196 # CLOSE saw an innermost boundary like --xyz--
197 # DONE callback returned false
198 # EOF end of file
199 # EXT saw boundary of some higher-level
200 #
201 15184 ▶sub eos_type {
Subroutine eos_type: called CC=5 SCAR=17.2
Pod undocumented
202 15184 my ($self, $eos) = @_;
203 15184 ▶ $eos = $self->eos if (@_ == 1);
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 15184 0 15184
204  
205 15184 ▶ if ($eos =~ /^(DONE|EOF)/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($eos =~ /^(DONE|EOF)/) { } 8053 7131 15184
elsif ($eos =~ /^(DELIM|CLOSE) (.*)$/) { } 7131 0 7131
206 8053 return $1;
207 }
208 elsif ($eos =~ /^(DELIM|CLOSE) (.*)$/) {
209 7131 ▶ return (($2 eq $self->most_recent_boundary()) ? $1 : 'EXT');
Branch: 1 branch
BranchTrueFalseTotal
$2 eq $self->most_recent_boundary ? : 7131 0 7131
210 }
211 else {
212 0 die("internal error: unable to classify boundary token ($eos)");
213 }
214 }
215  
216 #------------------------------
217 #
218 # native_handle HANDLE
219 #
220 # I<Function.>
221 # Can we do native i/o on HANDLE? If true, returns the handle
222 # that will respond to native I/O calls; else, returns undef.
223 #
224 18069 ▶sub native_handle {
Subroutine native_handle: called CC=4 SCAR=14.1
Pod undocumented
225 18069 my $fh = shift;
226 18069 ▶ return $fh if ($fh->isa('IO::File') || $fh->isa('IO::Handle'));
Branch: 1 branch
BranchTrueFalseTotal
if $fh->isa("IO::File") or $fh->isa("IO::Handle") 11075 6994 18069
Condition100%3/3
$fh->isa("IO::File") or $fh->isa("IO::Handle")
l!l&&r!l&&!r
893821376994
MC/DC100%2/2
$fh->isa("IO::File") or $fh->isa("IO::Handle")
$fh->isa("IO::File") $fh->isa("IO::Handle")
Truth table100%3/3
A or B
A= $fh->isa("IO::File")B= $fh->isa("IO::Handle")
ABresult
1X1
011
000
227 6994 ▶ return $fh if (ref $fh eq 'GLOB');
Branch: 1 branch
BranchTrueFalseTotal
if ref $fh eq "GLOB" 6994 0 6994
228 0 undef;
229 }
230  
231 #------------------------------
232 #
233 # read_chunk INHANDLE, OUTHANDLE
234 #
235 # I<Instance method.>
236 # Get lines until end-of-stream.
237 # Returns the terminating-condition token:
238 #
239 # DELIM xyz saw boundary line "--xyz"
240 # CLOSE xyz saw boundary line "--xyz--"
241 # DONE xyz saw terminator line "xyz"
242 # EOF end of file
243  
244 # Parse up to (and including) the boundary, and dump output.
245 # Follows the RFC 2046 specification, that the CRLF immediately preceding
246 # the boundary is part of the boundary, NOT part of the input!
247 #
248 # NOTE: while parsing bodies, we take care to remember the EXACT end-of-line
249 # sequence. This is because we *may* be handling 'binary' encoded data, and
250 # in that case we can't just massage \r\n into \n! Don't worry... if the
251 # data is styled as '7bit' or '8bit', the "decoder" will massage the CRLF
252 # for us. For now, we're just trying to chop up the data stream.
253  
254 # NBK - Oct 12, 1999
255 # The CRLF at the end of the current line is considered part
256 # of the boundary. I buffer the current line and output the
257 # last. I strip the last CRLF when I hit the boundary.
258  
259 9033 ▶sub read_chunk {
Subroutine read_chunk: called CC=34 SCAR=55.2
Pod undocumented
260 9033 my ($self, $in, $out, $keep_newline, $normalize_newlines) = @_;
261  
262 # If we're parsing a preamble or epilogue, we need to keep the blank line
263 # that precedes the boundary line.
264 9033 ▶ $keep_newline ||= 0;
Condition100%2/2
$keep_newline ||= 0
l!l
17957238
MC/DC100%1/1
$keep_newline ||= 0
$keep_newline
Truth table100%2/2
A
A= $keep_newline
Aresult
11
00
265  
266 9033 ▶ $normalize_newlines ||= 0;
Condition100%2/2
$normalize_newlines ||= 0
l!l
39755058
MC/DC100%1/1
$normalize_newlines ||= 0
$normalize_newlines
Truth table100%2/2
A
A= $normalize_newlines
Aresult
11
00
267 ### Init:
268 9033 my $bh = $self->{BH};
269 9033 my $th = $self->{TH};
270 9033 my $thx = keys %$th;
271 9033 local $_ = $LONGLINE;
272 9033 my $maybe;
273 9033 my $last = '';
274 9033 my $eos = '';
275  
276 ### Determine types:
277 9033 my $n_in = native_handle($in);
278 9033 my $n_out = native_handle($out);
279  
280 ### Handle efficiently by type:
281 9033 ▶ if ($n_in) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n_in) { } 9033 0 9033
282 9033 ▶ if ($n_out) { ### native input, native output [fastest]
Branch: 1 branch
BranchTrueFalseTotal
if ($n_out) { } 9033 0 9033
283 9033 ▶ while (<$n_in>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $n_in)) 18065 65 18130
284 # Normalize line ending
285 18065 ▶ $_ =~ s/(?:\n\r|\r\n|\r)$/\n/ if $normalize_newlines;
Branch: 1 branch
BranchTrueFalseTotal
if $normalize_newlines 8752 9313 18065
286 18065 ▶ if (substr($_, 0, 2) eq '--') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($_, 0, 2) eq "--") 5025 13040 18065
287 5025 ($maybe = $_) =~ s/[ \t\r\n]+\Z//;
288 5025 ▶ $bh->{$maybe} and do { $eos = $bh->{$maybe}; last };
Branch: 1 branch
BranchTrueFalseTotal
$bh->{$maybe} and do { $eos = $bh->{$maybe}; last } 4996 29 5025
289 }
290 13069 ▶ $thx and $th->{$_} and do { $eos = $th->{$_}; last };
Branch: 1 branch
BranchTrueFalseTotal
$thx and $th->{$_} and do { $eos = $th->{$_}; last } 3972 9097 13069
Condition100%3/3
$thx and $th->{$_}
!ll&&!rl&&r
432047773972
MC/DC100%2/2
$thx and $th->{$_}
$thx $th->{$_}
Truth table100%3/3
A and B
A= $thxB= $th->{$_}
ABresult
0X0
100
111
291 9097 print $n_out $last; $last = $_;
292 }
293 }
294 else { ### native input, OO output [slower]
295 0 ▶ while (<$n_in>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $n_in)) 0 0 0
296 # Normalize line ending
297 0 ▶ $_ =~ s/(?:\n\r|\r\n|\r)$/\n/ if $normalize_newlines;
Branch: 1 branch
BranchTrueFalseTotal
if $normalize_newlines 0 0 0
298 0 ▶ if (substr($_, 0, 2) eq '--') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($_, 0, 2) eq "--") 0 0 0
299 0 ($maybe = $_) =~ s/[ \t\r\n]+\Z//;
300 0 ▶ $bh->{$maybe} and do { $eos = $bh->{$maybe}; last };
Branch: 1 branch
BranchTrueFalseTotal
$bh->{$maybe} and do { $eos = $bh->{$maybe}; last } 0 0 0
301 }
302 0 ▶ $thx and $th->{$_} and do { $eos = $th->{$_}; last };
Branch: 1 branch
BranchTrueFalseTotal
$thx and $th->{$_} and do { $eos = $th->{$_}; last } 0 0 0
Condition0%0/3
$thx and $th->{$_}
!ll&&!rl&&r
000
MC/DC0%0/2
$thx and $th->{$_}
$thx $th->{$_}
Truth table0%0/3
A and B
A= $thxB= $th->{$_}
ABresult
0X0
100
111
303 0 $out->print($last); $last = $_;
304 }
305 }
306 }
307 else {
308 0 ▶ if ($n_out) { ### OO input, native output [even slower]
Branch: 1 branch
BranchTrueFalseTotal
if ($n_out) { } 0 0 0
309 0 ▶ while (defined($_ = $in->getline)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = $in->getline)) 0 0 0
310 # Normalize line ending
311 0 ▶ $_ =~ s/(?:\n\r|\r\n|\r)$/\n/ if $normalize_newlines;
Branch: 1 branch
BranchTrueFalseTotal
if $normalize_newlines 0 0 0
312 0 ▶ if (substr($_, 0, 2) eq '--') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($_, 0, 2) eq "--") 0 0 0
313 0 ($maybe = $_) =~ s/[ \t\r\n]+\Z//;
314 0 ▶ $bh->{$maybe} and do { $eos = $bh->{$maybe}; last };
Branch: 1 branch
BranchTrueFalseTotal
$bh->{$maybe} and do { $eos = $bh->{$maybe}; last } 0 0 0
315 }
316 0 ▶ $thx and $th->{$_} and do { $eos = $th->{$_}; last };
Branch: 1 branch
BranchTrueFalseTotal
$thx and $th->{$_} and do { $eos = $th->{$_}; last } 0 0 0
Condition0%0/3
$thx and $th->{$_}
!ll&&!rl&&r
000
MC/DC0%0/2
$thx and $th->{$_}
$thx $th->{$_}
Truth table0%0/3
A and B
A= $thxB= $th->{$_}
ABresult
0X0
100
111
317 0 print $n_out $last; $last = $_;
318 }
319 }
320 else { ### OO input, OO output [slowest]
321 0 ▶ while (defined($_ = $in->getline)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = $in->getline)) 0 0 0
322 # Normalize line ending
323 0 ▶ $_ =~ s/(?:\n\r|\r\n|\r)$/\n/ if $normalize_newlines;
Branch: 1 branch
BranchTrueFalseTotal
if $normalize_newlines 0 0 0
324 0 ▶ if (substr($_, 0, 2) eq '--') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($_, 0, 2) eq "--") 0 0 0
325 0 ($maybe = $_) =~ s/[ \t\r\n]+\Z//;
326 0 ▶ $bh->{$maybe} and do { $eos = $bh->{$maybe}; last };
Branch: 1 branch
BranchTrueFalseTotal
$bh->{$maybe} and do { $eos = $bh->{$maybe}; last } 0 0 0
327 }
328 0 ▶ $thx and $th->{$_} and do { $eos = $th->{$_}; last };
Branch: 1 branch
BranchTrueFalseTotal
$thx and $th->{$_} and do { $eos = $th->{$_}; last } 0 0 0
Condition0%0/3
$thx and $th->{$_}
!ll&&!rl&&r
000
MC/DC0%0/2
$thx and $th->{$_}
$thx $th->{$_}
Truth table0%0/3
A and B
A= $thxB= $th->{$_}
ABresult
0X0
100
111
329 0 $out->print($last); $last = $_;
330 }
331 }
332 }
333  
334 # Write out last held line, removing terminating CRLF if ended on bound,
335 # unless the line consists only of CRLF and we're wanting to keep the
336 # preceding blank line (as when parsing a preamble)
337 9033 ▶ $last =~ s/[\r\n]+\Z// if ($eos =~ /^(DELIM|CLOSE)/ && !($keep_newline && $last =~ m/^(\r\n|\n\r|\n|\r)\z/));
Branch: 1 branch
BranchTrueFalseTotal
if $eos =~ /^(DELIM|CLOSE)/ and not $keep_newline && $last =~ /^(\r\n|\n\r|\n|\r)\z/ 4977 4056 9033
Condition100%6/6
$eos =~ /^(DELIM|CLOSE)/ and not $keep_newline && $last =~ /^(\r\n|\n\r|\n|\r)\z/
!ll&&!rl&&r
4037194977
$keep_newline && $last =~ /^(\r\n|\n\r|\n|\r)\z/
!ll&&!rl&&r
3203177419
MC/DC100%3/3
$eos =~ /^(DELIM|CLOSE)/ and not $keep_newline && $last =~ /^(\r\n|\n\r|\n|\r)\z/
$eos =~ /^(DELIM|CLOSE)/ $keep_newline $last =~ /^(\r\n|\n\r|\n|\r)\z/
Truth table100%4/4
A and not(B && C)
A= $eos =~ /^(DELIM|CLOSE)/B= $keep_newlineC= $last =~ /^(\r\n|\n\r|\n|\r)\z/
ABCresult
0XX0
1110
10X1
1101
338 9033 $out->print($last);
339  
340 ### Save and return what we finished on:
341 9033 ▶ ${$self->{EOS}} = ($eos || 'EOF');
Condition100%2/2
$eos || 'EOF'
l!l
896865
MC/DC100%1/1
$eos || 'EOF'
$eos
Truth table100%2/2
A
A= $eos
Aresult
11
00
342 9033 1;
343 }
344  
345 #------------------------------
346 #
347 # read_lines INHANDLE, \@OUTLINES
348 #
349 # I<Instance method.>
350 # Read lines into the given array.
351 #
352 1126 ▶sub read_lines {
Subroutine read_lines: called CC=2 SCAR=7.0
Pod undocumented
353 1126 my ($self, $in, $outlines) = @_;
354  
355 1126 my $data = '';
356 1126 ▶ open(my $fh, '>', \$data) or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", \$data or die $! 1126 0 1126
357 1126 $self->read_chunk($in, $fh);
358 1126 @$outlines = split(/^/, $data);
359 1126 close $fh;
360  
361 1126 1;
362 }
363  
364 26 1;
365 __END__
↑ Summary Field/ParamVal.pm »