lib/Mail/AuthenticationResults/Parser.pm

filter: Statementstmt 100% 207 / 207 Branchbran 96.5% 112 / 116 Conditioncond 100% 12 / 12 MC/DCmcdc 100% 8 / 8 Subroutinesub 100% 23 / 23 Podpod 100% 5 / 5 totaltotal 98.9% 367 / 371 CC 65 SCAR 26.1
CC
65
Cov
99%
_parse_entry
22.0
tokenise
18.0
_parse_authservid
15.0
CRAP 13.6
« lib/Mail/AuthenticationResults/Header/Base.pm ↑ Summary lib/Mail/AuthenticationResults/FoldableHeader.pm »
1 package Mail::AuthenticationResults::Parser;
2 # ABSTRACT: Class for parsing Authentication Results Headers
3  
4 25 require 5.008;
5 25 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 25 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 25 our $VERSION = '2.20261029'; # VERSION
8 25 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 25 ▶use Mail::AuthenticationResults::Header;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 25 ▶use Mail::AuthenticationResults::Header::AuthServID;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 25 ▶use Mail::AuthenticationResults::Header::Comment;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 25 ▶use Mail::AuthenticationResults::Header::Entry;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 25 ▶use Mail::AuthenticationResults::Header::SubEntry;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 25 ▶use Mail::AuthenticationResults::Header::Version;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 25 ▶use Mail::AuthenticationResults::Token::Assignment;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 25 ▶use Mail::AuthenticationResults::Token::Comment;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 25 ▶use Mail::AuthenticationResults::Token::QuotedString;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 25 ▶use Mail::AuthenticationResults::Token::Separator;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 25 ▶use Mail::AuthenticationResults::Token::String;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 25 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25  
26 64 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
27 64 my ( $class, $auth_header ) = @_;
28 64 my $self = {};
29 64 bless $self, $class;
30  
31 64 ▶ if ( $auth_header ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($auth_header) 12 52 64
32 12 $self->parse( $auth_header );
33 }
34  
35 64 return $self;
36 }
37  
38  
39 62 ▶sub parse {
Subroutine parse: called CC=2 SCAR=6.9
Pod documented
40 62 my ( $self, $header ) = @_;
41  
42 62 $self->tokenise( $header );
43  
44 57 $self->_parse_authservid();
45  
46 50 ▶ while ( @{ $self->{ 'tokenised' } } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"tokenised"};}) 2114 46 2160
47 2114 $self->_parse_entry();
48 }
49  
50 46 return $self->parsed();
51 }
52  
53  
54 1 ▶sub from_authentication_results_json {
Subroutine from_authentication_results_json: called CC=1 SCAR=0.0
Pod documented
55 1 my ( $self, $json ) = @_;
56 1 my $j = JSON->new();
57 1 my $hashref = $j->decode( $json );
58 1 return $self->_from_hashref( $hashref );
59 }
60  
61 24 ▶sub _from_hashref {
Subroutine _from_hashref: called CC=11 SCAR=24.0
62 24 my ( $self, $hashref ) = @_;
63 24 my $type = $hashref->{'type'};
64 24 ▶ my $object
Branch: 5 branches
BranchTrueFalseTotal
$type eq 'header' ? : 1 23 24
$type eq 'authservid' ? : 1 22 23
$type eq 'entry' ? : 8 14 22
$type eq 'subentry' ? : 8 6 14
$type eq 'comment' ? : 6 0 6
65 = $type eq 'header' ? Mail::AuthenticationResults::Header->new()
66 : $type eq 'authservid' ? Mail::AuthenticationResults::Header::AuthServID->new()
67 : $type eq 'entry' ? Mail::AuthenticationResults::Header::Entry->new()
68 : $type eq 'subentry' ? Mail::AuthenticationResults::Header::SubEntry->new()
69 : $type eq 'comment' ? Mail::AuthenticationResults::Header::Comment->new()
70 : croak "unknown type $type";
71  
72 24 ▶ if ( $type eq 'header' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'header') { } 1 23 24
73 1 my $authserv_id = $self->_from_hashref( $hashref->{ 'authserv_id' } );
74 1 $object->set_value( $authserv_id );
75 }
76 else {
77 23 ▶ $object->set_key( $hashref->{'key'} ) if exists $hashref->{'key'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $hashref->{"key"} 16 7 23
78 23 ▶ $object->safe_set_value( $hashref->{'value'} ) if exists $hashref->{'value'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $hashref->{"value"} 23 0 23
79 }
80  
81 24 ▶ if ( exists $hashref->{'children'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $hashref->{"children"}) 18 6 24
82 18 for my $child ( @{ $hashref->{'children'} } ) {
83 22 my $child_object = $self->_from_hashref( $child );
84 22 $object->add_child( $child_object );
85 }
86 }
87  
88 24 return $object;
89 }
90  
91  
92  
93 63 ▶sub tokenise {
Subroutine tokenise: called CC=18 SCAR=28.9
Pod documented
94 63 my ( $self, $header ) = @_;
95  
96 63 my @tokenised;
97  
98 63 $header =~ s/\n/ /g;
99 63 $header =~ s/\r/ /g;
100 63 $header =~ s/^\s+//;
101  
102 # Remove Header part if present
103 63 ▶ if ( $header =~ /^Authentication-Results:/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($header =~ /^Authentication-Results:/i) 15 48 63
104 15 $header =~ s/^Authentication-Results://i;
105 }
106  
107 63 my $args = {};
108 63 ▶ while ( length($header) > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $header > 0) 53250 58 53308
109  
110 53250 my $token;
111 53250 $header =~ s/^\s+//;
112  
113 53250 ▶ my $last_non_comment_type = exists( $args->{ 'last_non_comment_type' } ) ? $args->{ 'last_non_comment_type' }->is() : 'none';
Branch: 1 branch
BranchTrueFalseTotal
exists $args->{'last_non_comment_type'} ? : 53188 62 53250
114  
115 53250 ▶ if ( length( $header ) == 0 ) {
Branch: 9 branches
BranchTrueFalseTotal
if (length $header == 0) { } 2 53248 53250
elsif ($header =~ /^\(/) { } 2739 50509 53248
elsif ($header =~ /^;/) { } 4132 46377 50509
elsif ($header =~ /^"/) { } 38 46339 46377
elsif ($last_non_comment_type ne 'assignment' and $header =~ /^\./) { } 6749 39590 46339
elsif ($last_non_comment_type eq 'assignment' and $header =~ /^\./) { } 4 39586 39590
elsif ($last_non_comment_type ne 'assignment' and $header =~ m[^/]) { } 4 39582 39586
elsif ($last_non_comment_type eq 'assignment' and $header =~ m[^/]) { } 2 39580 39582
elsif ($header =~ /^=/) { } 10935 28645 39580
116 2 last;
117 }
118 elsif ( $header =~ /^\(/ ) {
119 2739 $token = Mail::AuthenticationResults::Token::Comment->new( $header, $args );
120 }
121 elsif ( $header =~ /^;/ ) {
122 4132 $token = Mail::AuthenticationResults::Token::Separator->new( $header, $args );
123 4132 $args->{ 'last_non_comment_type' } = $token;
124 }
125 elsif ( $header =~ /^"/ ) {
126 38 $token = Mail::AuthenticationResults::Token::QuotedString->new( $header, $args );
127 38 $args->{ 'last_non_comment_type' } = $token;
128 }
129 ▶ elsif ( $last_non_comment_type ne 'assignment' && $header =~ /^\./ ) {
Condition100%3/3
$last_non_comment_type ne "assignment" and $header =~ /^\./
!ll&&!rl&&r
17661219296749
MC/DC100%2/2
$last_non_comment_type ne "assignment" and $header =~ /^\./
$last_non_comment_type ne "assignment" $header =~ /^\./
Truth table100%3/3
A and B
A= $last_non_comment_type ne "assignment"B= $header =~ /^\./
ABresult
0X0
100
111
130 6749 $token = Mail::AuthenticationResults::Token::Assignment->new( $header, $args );
131 6749 $args->{ 'last_non_comment_type' } = $token;
132 }
133 ▶ elsif ( $last_non_comment_type eq 'assignment' && $header =~ /^\./ ) {
Condition100%3/3
$last_non_comment_type eq "assignment" and $header =~ /^\./
!ll&&!rl&&r
21929176574
MC/DC100%2/2
$last_non_comment_type eq "assignment" and $header =~ /^\./
$last_non_comment_type eq "assignment" $header =~ /^\./
Truth table100%3/3
A and B
A= $last_non_comment_type eq "assignment"B= $header =~ /^\./
ABresult
0X0
100
111
134 # a . after an assignment cannot be another assignment, likely an unquoted string.
135 4 $token = Mail::AuthenticationResults::Token::String->new( $header, $args );
136 2 $args->{ 'last_non_comment_type' } = $token;
137 }
138 ▶ elsif ( $last_non_comment_type ne 'assignment' && $header =~ /^\// ) {
Condition100%3/3
$last_non_comment_type ne "assignment" and $header =~ m[^/]
!ll&&!rl&&r
17657219254
MC/DC100%2/2
$last_non_comment_type ne "assignment" and $header =~ m[^/]
$last_non_comment_type ne "assignment" $header =~ m[^/]
Truth table100%3/3
A and B
A= $last_non_comment_type ne "assignment"B= $header =~ m[^/]
ABresult
0X0
100
111
139 4 $token = Mail::AuthenticationResults::Token::Assignment->new( $header, $args );
140 4 $args->{ 'last_non_comment_type' } = $token;
141 }
142 ▶ elsif ( $last_non_comment_type eq 'assignment' && $header =~ /^\// ) {
Condition100%3/3
$last_non_comment_type eq "assignment" and $header =~ m[^/]
!ll&&!rl&&r
21925176552
MC/DC100%2/2
$last_non_comment_type eq "assignment" and $header =~ m[^/]
$last_non_comment_type eq "assignment" $header =~ m[^/]
Truth table100%3/3
A and B
A= $last_non_comment_type eq "assignment"B= $header =~ m[^/]
ABresult
0X0
100
111
143 # a / after an assignment cannot be another assignment, likely an unquoted string.
144 2 $token = Mail::AuthenticationResults::Token::String->new( $header, $args );
145 1 $args->{ 'last_non_comment_type' } = $token;
146 }
147 elsif ( $header =~ /^=/ ) {
148 10935 $token = Mail::AuthenticationResults::Token::Assignment->new( $header, $args );
149 10935 $args->{ 'last_non_comment_type' } = $token;
150 }
151 else {
152 28645 $token = Mail::AuthenticationResults::Token::String->new( $header, $args );
153 28645 $args->{ 'last_non_comment_type' } = $token;
154 }
155  
156 53245 $header = $token->remainder();
157 # Do not retain a copy of the remaining header in every token,
158 # doing so uses memory quadratic in the number of tokens.
159 # remainder() will return undef for these tokens, this is
160 # documented in Mail::AuthenticationResults::Token.
161 53245 delete $token->{ 'header' };
162 53245 push @tokenised, $token;
163 }
164  
165 60 ▶ croak 'Nothing to parse' if ! @tokenised;
Branch: 1 branch
BranchTrueFalseTotal
unless @tokenised 2 58 60
166  
167 58 $self->{ 'tokenised' } = \@tokenised;
168  
169 58 return;
170 }
171  
172 57 ▶sub _parse_authservid {
Subroutine _parse_authservid: called CC=15 SCAR=27.1
173 57 my ( $self ) = @_;
174 57 my $tokenised = $self->{ 'tokenised' };
175 57 my $token;
176  
177 57 my $authserv_id = Mail::AuthenticationResults::Header::AuthServID->new();
178  
179 # Find the ServID
180 57 ▶ while ( @$tokenised ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$tokenised) 58 0 58
181 58 $token = shift @$tokenised;
182 58 ▶ if ( $token->is() eq 'string' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($token->is eq 'string') { } 56 2 58
elsif ($token->is eq 'comment') { } 1 1 2
183 56 $authserv_id->set_value( $token->value() );
184 56 last;
185 }
186 elsif ( $token->is() eq 'comment' ) {
187 1 $authserv_id->add_child( Mail::AuthenticationResults::Header::Comment->new()->set_value( $token->value() ) );
188 }
189 else {
190 # assignment or separator, both are bogus
191 1 croak 'Invalid AuthServ-ID';
192 }
193 }
194  
195 56 my $expecting = 'key';
196 56 my $key;
197  
198 TOKEN:
199 56 ▶ while ( @$tokenised ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$tokenised) 70 1 71
200 70 $token = shift @$tokenised;
201  
202 70 ▶ if ( $token->is() eq 'assignment' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($token->is eq 'assignment') { } 7 63 70
elsif ($token->is eq 'comment') { } 3 60 63
elsif ($token->is eq 'separator') { } 49 11 60
203 7 ▶ if ( $expecting eq 'assignment' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expecting eq 'assignment') { } 4 3 7
204 4 ▶ if ( $token->value() eq '=' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->value eq '=') { } 2 2 4
205 2 $expecting = 'value';
206 }
207 else {
208 2 croak 'unexpected token';
209 }
210 }
211 else {
212 3 croak 'not expecting an assignment';
213 }
214 }
215 elsif ( $token->is() eq 'comment' ) {
216 3 $authserv_id->add_child( Mail::AuthenticationResults::Header::Comment->new()->set_value( $token->value() ) );
217 }
218 elsif ( $token->is() eq 'separator' ) {
219 49 last TOKEN;
220 }
221 16 ▶ if ( $token->is() eq 'string' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->is eq "string") 11 5 16
222 11 ▶ if ( $expecting eq 'key' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($expecting eq 'key') { } 9 2 11
elsif ($expecting eq 'value') { } 1 1 2
223 9 $key = $token;
224 9 $expecting = 'assignment';
225 }
226 elsif ( $expecting eq 'value' ) {
227 1 $authserv_id->add_child( Mail::AuthenticationResults::Header::SubEntry->new()->set_key( $key->value() )->set_value( $token->value() ) );
228 1 $expecting = 'key';
229 1 undef $key;
230 }
231 else {
232 1 croak 'not expecting a string';
233 }
234 }
235  
236 }
237 50 ▶ if ( $expecting ne 'key' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expecting ne "key") 4 46 50
238 4 ▶ if ( $key->value() =~ /^[0-9]+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key->value =~ /^[0-9]+$/) { } 2 2 4
239 # Looks like a version
240 2 $authserv_id->add_child( Mail::AuthenticationResults::Header::Version->new()->set_value( $key->value() ) );
241 }
242 else {
243 # Probably bogus, but who knows!
244 2 $authserv_id->add_child( Mail::AuthenticationResults::Header::SubEntry->new()->set_key( $key->value() ) );
245 }
246 }
247  
248 50 $self->{ 'header' } = Mail::AuthenticationResults::Header->new()->set_value( $authserv_id );
249 50 $self->{ 'tokenised' } = $tokenised;
250  
251 50 return;
252 }
253  
254 2114 ▶sub _parse_entry {
Subroutine _parse_entry: called CC=22 SCAR=30.9
255 2114 my ( $self ) = @_;
256 2114 my $tokenised = $self->{ 'tokenised' };
257  
258 2114 my $entry = Mail::AuthenticationResults::Header::Entry->new();
259 2114 my $working_on = $entry;
260  
261 2114 my $expecting = 'key';
262 2114 my $is_subentry = 0;
263 TOKEN:
264 2114 ▶ while ( @$tokenised ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$tokenised) 27078 39 27117
265 27078 my $token = shift @$tokenised;
266  
267 27078 ▶ if ( $token->is() eq 'assignment' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($token->is eq 'assignment') { } 9005 18073 27078
elsif ($token->is eq 'comment') { } 1402 16671 18073
elsif ($token->is eq 'separator') { } 2071 14600 16671
268 9005 ▶ if ( $expecting eq 'assignment' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expecting eq 'assignment') { } 9004 1 9005
269 9004 ▶ if ( $token->value() eq '=' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($token->value eq '=') { } 5590 3414 9004
elsif ($token->value eq '.') { } 3412 2 3414
elsif ($token->value eq '/') { } 2 0 2
270 5590 $expecting = 'value';
271 }
272 elsif ( $token->value() eq '.' ) {
273 3412 $expecting = 'keymod';
274 }
275 elsif ( $token->value() eq '/' ) {
276 2 $expecting = 'version';
277 }
278 }
279 else {
280 1 croak 'not expecting an assignment';
281 }
282 }
283 elsif ( $token->is() eq 'comment' ) {
284 1402 $working_on->add_child( Mail::AuthenticationResults::Header::Comment->new()->set_value( $token->value() ) );
285 }
286 elsif ( $token->is() eq 'separator' ) {
287 2071 last TOKEN;
288 }
289 25006 ▶ if ( $token->is() eq 'string' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->is eq "string") 14600 10406 25006
290 14600 ▶ if ( $expecting eq 'key' ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($expecting eq 'key') { } 5601 8999 14600
elsif ($expecting eq 'keymod') { } 3412 5587 8999
elsif ($expecting eq 'version') { } 2 5585 5587
elsif ($expecting eq 'value') { } 5583 2 5585
291 5601 ▶ if ( ! $is_subentry ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $is_subentry) { } 2114 3487 5601
292 2114 ▶ if ( $token->value() eq 'none' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->value eq 'none') { } 7 2107 2114
293 # Special case the none
294 7 $expecting = 'no_more_after_none';
295 }
296 else {
297 2107 $entry->set_key( $token->value() );
298 2107 $expecting = 'assignment';
299 }
300 }
301 else {
302 3487 $working_on = Mail::AuthenticationResults::Header::SubEntry->new()->set_key( $token->value() );
303 3487 $expecting = 'assignment';
304 }
305 }
306 elsif ( $expecting eq 'keymod' ) {
307 3412 $working_on->set_key( $working_on->key() . '.' . $token->value() );
308 3412 $expecting = 'assignment';
309 }
310 elsif ( $expecting eq 'version' ) {
311 2 ▶ if ( $token->value() =~ /^[0-9]+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->value =~ /^[0-9]+$/) { } 1 1 2
312 # Looks like a version
313 1 $working_on->add_child( Mail::AuthenticationResults::Header::Version->new()->set_value( $token->value() ) );
314 }
315 else {
316 1 croak 'bad version token';
317 }
318 1 $expecting = 'assignment';
319 }
320 elsif ( $expecting eq 'value' ) {
321 5583 ▶ if ( ! $is_subentry ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $is_subentry) { } 2100 3483 5583
322 2100 $entry->set_value( $token->value() );
323 2100 $is_subentry = 1;
324 }
325 else {
326 3483 $entry->add_child( $working_on->set_value( $token->value() ) );
327 }
328 5583 $expecting = 'key';
329 }
330 else {
331 2 croak 'not expecting a string';
332 }
333 }
334  
335 }
336  
337 2110 ▶ if ( $expecting eq 'no_more_after_none' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expecting eq "no_more_after_none") 5 2105 2110
338 5 $self->{ 'tokenised' } = $tokenised;
339 # We may have comment entries, if so add those to the header object
340 5 foreach my $child ( @{ $entry->children() } ) {
341 2 delete $child->{ 'parent' };
342 2 $self->{ 'header' }->add_child( $child );
343 }
344 5 return;
345 }
346  
347 2105 ▶ if ( $expecting ne 'key' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expecting ne "key") 9 2096 2105
348 9 ▶ if ( $is_subentry ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_subentry) 4 5 9
349 4 $entry->add_child( $working_on );
350 }
351 }
352  
353 2105 $self->{ 'header' }->add_child( $entry );
354 2105 $self->{ 'tokenised' } = $tokenised;
355  
356 2105 return;
357 }
358  
359  
360 58 ▶sub parsed {
Subroutine parsed: called CC=1 SCAR=0.0
Pod documented
361 58 my ( $self ) = @_;
362 58 return $self->{ 'header' };
363 }
364  
365 25 1;
366  
367 __END__
« lib/Mail/AuthenticationResults/Header/Base.pm ↑ Summary lib/Mail/AuthenticationResults/FoldableHeader.pm »