blib/lib/Apache/Log/Parser.pm

filter: Statementstmt 96.0% 171 / 178 Branchbran 89.5% 86 / 96 Conditioncond 64.1% 25 / 39 MC/DCmcdc 35.0% 7 / 20 Subroutinesub 94.4% 17 / 18 Podpod 0.0% 0 / 8 totaltotal 85.2% 306 / 359 CC 68 SCAR 24.5
CC
68
Cov
90%
new
21.7
parse_fast
15.7
separate_log_items
11.0
CRAP 11.6
↑ Summary
1 # -*- coding: utf-8 -*-
2  
3 package Apache::Log::Parser;
4  
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 our $VERSION = '0.02';
8  
9 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use List::Util qw( reduce );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 our @FAST_COMMON_FIELDS = qw( rhost logname user datetime date time timezone request method path proto status bytes );
13 2 our @FAST_COMBINED_FIELDS = qw( referer agent );
14 2 our @FAST_DEBUG_FIELDS = qw( referer agent duration );
15  
16 2 my $COMMON = [" ", [qw(rhost logname user datetime request status bytes)], undef];
17 29 ▶my $COMBINED = [" ", [qw(rhost logname user datetime request status bytes referer agent)], sub{my $x=shift; defined($x->{agent}) and defined($x->{referer})}];
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
defined $x->{"agent"} and defined $x->{"referer"} 16 13 29
18 29 ▶my $DEBUG = [" ", [qw(rhost logname user datetime request status bytes referer agent duration)], sub{my $x=shift; defined($x->{agent}) and defined($x->{referer}) and defined($x->{duration}) and $x->{duration} =~ m!^\d+!o}];
Subroutine __ANON__: called CC=4 SCAR=13.9
Branch: 1 branch
BranchTrueFalseTotal
defined $x->{"agent"} and defined $x->{"referer"} and defined $x->{"duration"} and $x->{"duration"} =~ /^\d+/o 4 25 29
Condition83%5/6
defined $x->{"agent"} and defined $x->{"referer"} and defined $x->{"duration"}
!ll&&!rl&&r
13124
defined $x->{"agent"} and defined $x->{"referer"}
!ll&&!rl&&r
13016
MC/DC66%2/3
defined $x->{"agent"} and defined $x->{"referer"} and defined $x->{"duration"}
defined $x->{"agent"} defined $x->{"referer"} defined $x->{"duration"}
Truth table75%3/4
A and B and C
A= defined $x->{"agent"}B= defined $x->{"referer"}C= defined $x->{"duration"}
ABCresult
0XX0
10X0
1100
1111
19 2 my $VHOST_COMMON = [" ", [qw( vhost rhost logname user datetime request status bytes )], undef];
20  
21 2 my $STRICT_DEFAULT_FORMATS = [$DEBUG, $COMBINED, $COMMON, $VHOST_COMMON];
22  
23 2 our @REQUIRED_FIELDS = qw( rhost logname user datetime request status bytes );
24 2 our @FIELDS_ALWAYS_RETURNED = qw( host logname user datetime date time timezone request method path proto status bytes );
25  
26 10 ▶sub new {
Subroutine new: called CC=21 SCAR=30.8
Pod undocumented
27 10 my $class = shift;
28 10 my $self = bless {}, $class;
29  
30 10 my %args = @_;
31 10 ▶ croak "only one option from 'strict' or 'fast' required." unless ($args{strict} xor $args{fast});
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"strict"} xor $args{"fast"} 0 10 10
Condition50%2/4
$args{"strict"} xor $args{"fast"}
l&&rl&&!r!l&&r!l&&!r
0550
MC/DC0%0/2
$args{"strict"} xor $args{"fast"}
$args{"strict"} $args{"fast"}
Truth table50%2/4
A xor B
A= $args{"strict"}B= $args{"fast"}
ABresult
110
101
011
000
32  
33 10 ▶ $self->{verbose} = exists($args{verbose}) ? $args{verbose} : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'verbose'} ? : 0 10 10
34  
35 10 ▶ if ($args{strict}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'strict'}) { } 5 5 10
36 5 ▶ my @formats = (ref($args{strict}) ? @{$args{strict}} : ());
Branch: 1 branch
BranchTrueFalseTotal
ref $args{'strict'} ? : 2 3 5
37 5 ▶ if (scalar(@formats) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @formats > 0) { } 2 3 5
38 2 ▶ for (my $i = 0; $i < scalar(@formats); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @formats) 9 2 11
39 9 ▶ if ($formats[$i] eq 'common') {
Branch: 4 branches
BranchTrueFalseTotal
if ($formats[$i] eq 'common') { } 2 7 9
elsif ($formats[$i] eq 'combined') { } 2 5 7
elsif ($formats[$i] eq 'debug') { } 1 4 5
elsif ($formats[$i] eq 'vhost_common') { } 1 3 4
40 2 $formats[$i] = $COMMON;
41 }
42 elsif ($formats[$i] eq 'combined') {
43 2 $formats[$i] = $COMBINED;
44 }
45 elsif ($formats[$i] eq 'debug') {
46 1 $formats[$i] = $DEBUG;
47 }
48 elsif ($formats[$i] eq 'vhost_common') {
49 1 $formats[$i] = $VHOST_COMMON;
50 }
51 else {
52 3 ▶ unless (length($formats[$i]->[0]) > 0 and
Branch: 1 branch
BranchTrueFalseTotal
unless (length $formats[$i][0] > 0 and scalar &reduce(sub { $a += scalar grep({$_ eq $b;} @REQUIRED_FIELDS); } , 0, @{$formats[$i][1];}) == scalar @Apache::Log::Parser::REQUIRED_FIELDS) 0 3 3
Condition33%1/3
length $formats[$i][0] > 0 and scalar &reduce(sub { $a += scalar grep({$_ eq $b;} @REQUIRED_FIELDS); } , 0, @{$formats[$i][1];}) == scalar @Apache::Log::Parser::REQUIRED_FIELDS
!ll&&!rl&&r
003
MC/DC0%0/2
length $formats[$i][0] > 0 and scalar &reduce(sub { $a += scalar grep({$_ eq $b;} @REQUIRED_FIELDS); } , 0, @{$formats[$i][1];}) == scalar @Apache::Log::Parser::REQUIRED_FIELDS
length $formats[$i][0] > 0 scalar &reduce(sub { $a += scalar grep({$_ eq $b;} @REQUIRED_FIELDS); } , 0, @{$formats[$i][1];}) == scalar @Apache::Log::Parser::REQUIRED_FIELDS
Truth table33%1/3
A and B
A= length $formats[$i][0] > 0B= scalar &reduce(sub { $a += scalar grep({$_ eq $b;} @REQUIRED_FIELDS); } , 0, @{$formats[$i][1];}) == scalar @Apache::Log::Parser::REQUIRED_FIELDS
ABresult
0X0
100
111
53 3 ▶ scalar(reduce{$a += scalar(grep{$_ eq $b}@REQUIRED_FIELDS)} 0, @{$formats[$i]->[1]}) == scalar(@REQUIRED_FIELDS)) {
Subroutine __ANON__: called CC=1 SCAR=0.0
54 0 croak "specified fields not enough";
55 }
56 }
57 }
58 2 $self->{try_maps} = \@formats;
59 }
60 else {
61 3 $self->{try_maps} = $STRICT_DEFAULT_FORMATS;
62 }
63 5 $self->{strict} = 1;
64 }
65 else {
66 5 $self->{strict} = 0;
67 5 ▶ if (ref($args{fast}) and scalar(@{$args{fast}}) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args{'fast'} and scalar @{$args{'fast'};} > 0) { } 2 3 5
Condition66%2/3
ref $args{"fast"} and scalar @{$args{"fast"};} > 0
!ll&&!rl&&r
302
MC/DC50%1/2
ref $args{"fast"} and scalar @{$args{"fast"};} > 0
ref $args{"fast"} scalar @{$args{"fast"};} > 0
Truth table66%2/3
A and B
A= ref $args{"fast"}B= scalar @{$args{"fast"};} > 0
ABresult
0X0
100
111
68 2 my @args = @{$args{fast}};
69 2 my @fields = ();
70 2 foreach my $arg (@args) {
71 7 ▶ if ($arg eq 'common') {
Branch: 4 branches
BranchTrueFalseTotal
if ($arg eq 'common') { } 2 5 7
elsif ($arg eq 'combined') { } 2 3 5
elsif ($arg eq 'debug') { } 1 2 3
elsif (ref $arg) { } 2 0 2
72 2 push @fields, [0, []];
73 }
74 elsif ($arg eq 'combined') {
75 2 push @fields, [scalar(@FAST_COMBINED_FIELDS), \@FAST_COMBINED_FIELDS];
76 }
77 elsif ($arg eq 'debug') {
78 1 push @fields, [scalar(@FAST_DEBUG_FIELDS), \@FAST_DEBUG_FIELDS];
79 }
80 elsif (ref($arg)) {
81 2 my @matchers = @{$arg};
82 2 push @fields, [scalar(@matchers), \@matchers];
83 }
84 else {
85 0 croak "unknow definition for fast parse";
86 }
87 }
88 2 $self->{field_lists} = \@fields;
89 }
90 else {
91 $self->{field_lists} = [
92 3 [scalar(@FAST_COMBINED_FIELDS), \@FAST_COMBINED_FIELDS],
93 [0, []]
94 ];
95 }
96 5 my $part = q{\s*(?:"([^"]*)"|([^\s]+))?};
97 5 my $common = q{([^\s]*)\s+([^\s]*)\s+([^\s]*)\s+\[(([^: ]+):([^ ]+) ([-+0-9]+))\]\s+"(([^\s]+) ([^\s]+)( ([^\s"]*))?)"\s+([^\s]*)\s+([^\s]*)};
98 5 my $common_parts = 14;
99 5 ▶ my $max_match_parts = reduce {$a > $b ? $a : $b} 0, map {$_->[0]} @{$self->{field_lists}};
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$a > $b ? : 8 5 13
100 5 my $regex = $common . ($part x $max_match_parts);
101 5 $self->{fastpattern} = qr/\A$regex/;
102 }
103 10 return $self;
104 }
105  
106 # our @FAST_COMMON_FIELDS = qw( rhost logname user datetime date time timezone request method path proto status bytes );
107 38 ▶sub parse_fast {
Subroutine parse_fast: called CC=14 SCAR=27.5
Pod undocumented
108 38 my ($self, $line) = @_;
109 38 chomp $line;
110 38 my $pairs = {};
111 38 my $dummy;
112 my @values;
113 ($pairs->{rhost}, $pairs->{logname}, $pairs->{user}, $pairs->{datetime},
114 $pairs->{date}, $pairs->{time}, $pairs->{timezone}, $pairs->{request},
115 $pairs->{method}, $pairs->{path}, $dummy, $pairs->{proto}, $pairs->{status},
116 38 $pairs->{bytes}, @values) = ($line =~ $self->{fastpattern});
117  
118 38 ▶ unless (defined($pairs->{status}) and $pairs->{status} ne '' and
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pairs->{"status"} and $pairs->{"status"} ne "" and defined $pairs->{"request"} and $pairs->{"request"} ne "" and defined $pairs->{"datetime"} and $pairs->{"datetime"} ne "") 5 33 38
Condition66%10/15
defined $pairs->{"status"} and $pairs->{"status"} ne "" and defined $pairs->{"request"} and $pairs->{"request"} ne "" and defined $pairs->{"datetime"} and $pairs->{"datetime"} ne ""
!ll&&!rl&&r
5033
defined $pairs->{"status"} and $pairs->{"status"} ne "" and defined $pairs->{"request"} and $pairs->{"request"} ne "" and defined $pairs->{"datetime"}
!ll&&!rl&&r
5033
defined $pairs->{"status"} and $pairs->{"status"} ne "" and defined $pairs->{"request"} and $pairs->{"request"} ne ""
!ll&&!rl&&r
5033
defined $pairs->{"status"} and $pairs->{"status"} ne "" and defined $pairs->{"request"}
!ll&&!rl&&r
5033
defined $pairs->{"status"} and $pairs->{"status"} ne ""
!ll&&!rl&&r
5033
MC/DC16%1/6
defined $pairs->{"status"} and $pairs->{"status"} ne "" and defined $pairs->{"request"} and $pairs->{"request"} ne "" and defined $pairs->{"datetime"} and $pairs->{"datetime"} ne ""
defined $pairs->{"status"} $pairs->{"status"} ne "" defined $pairs->{"request"} $pairs->{"request"} ne "" defined $pairs->{"datetime"} $pairs->{"datetime"} ne ""
Truth table28%2/7
A and B and C and D and E and F
A= defined $pairs->{"status"}B= $pairs->{"status"} ne ""C= defined $pairs->{"request"}D= $pairs->{"request"} ne ""E= defined $pairs->{"datetime"}F= $pairs->{"datetime"} ne ""
ABCDEFresult
0XXXXX0
10XXXX0
110XXX0
1110XX0
11110X0
1111100
1111111
119 defined($pairs->{request}) and $pairs->{request} ne '' and
120 defined($pairs->{datetime}) and $pairs->{datetime} ne '') {
121 5 ▶ carp "unknown format: $line" if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 5 5
122 5 return undef;
123 }
124  
125 33 foreach my $ref (@{$self->{field_lists}}) {
126 63 my %result = (%{$pairs}); # copy not to break $pairs while trying to parse
127 63 ▶ for (my $i = $ref->[0] - 1; $i >= 0; $i--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 90 33 123
128 90 my $x = $i * 2;
129 90 ▶ my $v = defined($values[$x]) ? $values[$x] : $values[$x + 1];
Branch: 1 branch
BranchTrueFalseTotal
defined $values[$x] ? : 53 37 90
130 90 ▶ last if not defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 30 60 90
131 60 $result{$ref->[1]->[$i]} = $v;
132 }
133 63 ▶ return \%result if scalar(keys %result) >= $ref->[0] + 13;
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %result >= $ref->[0] + 13 33 30 63
134 }
135 0 ▶ carp "unknown format: $line" if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 0 0
136 0 return undef;
137 }
138  
139 262 ▶sub dequote {
Subroutine dequote: called CC=2 SCAR=6.9
Pod undocumented
140 262 my $str = shift;
141 262 ▶ return $str unless substr($str, 0, 1) eq '"';
Branch: 1 branch
BranchTrueFalseTotal
unless substr($str, 0, 1) eq "\"" 3 259 262
142 259 my $body = substr($str, 1, length($str) - 2);
143 259 $body =~ s/\\"/"/g;
144 259 $body;
145 }
146  
147 640 ▶sub has_unquoted_tail_doublequote {
Subroutine has_unquoted_tail_doublequote: called CC=6 SCAR=17.9
Pod undocumented
148 640 my $part = shift;
149 640 ▶ return 0 if substr($part, -1, 1) ne '"';
Branch: 1 branch
BranchTrueFalseTotal
if substr($part, -1, 1) ne "\"" 377 263 640
150 263 ▶ return 1 if substr($part, -2, 1) ne '\\';
Branch: 1 branch
BranchTrueFalseTotal
if substr($part, -2, 1) ne "\\" 258 5 263
151  
152 5 my $reversed_body = substr(reverse($part), 1);
153 5 my $backslash_count = 0;
154 5 ▶ foreach my $c (split(//, $reversed_body)) { $c eq '\\' and $backslash_count += 1 or last; }
Branch: 1 branch
BranchTrueFalseTotal
$c eq "\\" and $backslash_count += 1 or last 15 5 20
Condition66%2/3
$c eq "\\" and $backslash_count += 1
!ll&&!rl&&r
5015
MC/DC50%1/2
$c eq "\\" and $backslash_count += 1
$c eq "\\" $backslash_count += 1
Truth table66%2/3
A and B
A= $c eq "\\"B= $backslash_count += 1
ABresult
0X0
100
111
155 5 not ($backslash_count % 2);
156 }
157  
158 134 ▶sub separate_log_items {
Subroutine separate_log_items: called CC=11 SCAR=24.0
Pod undocumented
159 134 my ($sep, $buf) = @_;
160 134 my $continuing = 0;
161 134 my $flag_in_quoted = 1;
162 134 my $flag_in_bracket = 2;
163 134 my @items = ();
164 134 my @item_parts_in_quote = ();
165 134 my @item_parts_in_brackets = ();
166  
167 134 foreach my $part (split($sep, $buf)) {
168 1526 ▶ unless ($continuing) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($continuing) 1039 487 1526
169 1039 ▶ if (substr($part, 0, 1) eq '"') {
Branch: 2 branches
BranchTrueFalseTotal
if (substr($part, 0, 1) eq '"') { } 257 782 1039
elsif (substr($part, 0, 1) eq '[') { } 123 659 782
170 257 ▶ if (has_unquoted_tail_doublequote($part)) {
Branch: 1 branch
BranchTrueFalseTotal
if (has_unquoted_tail_doublequote($part)) { } 108 149 257
171 108 push @items, dequote($part);
172 }
173 else {
174 149 $continuing = $flag_in_quoted;
175 149 push @item_parts_in_quote, $part;
176 }
177 } elsif (substr($part, 0, 1) eq '[') {
178 123 ▶ if (substr($part, -1, 1) eq ']') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($part, -1, 1) eq ']') { } 10 113 123
179 10 push @items, substr($part, 1, length($part) - 2);
180 }
181 else {
182 113 $continuing = $flag_in_bracket;
183 113 push @item_parts_in_brackets, $part;
184 }
185 } else {
186 659 push @items, $part;
187 }
188 next
189 1039 }
190  
191 487 ▶ if ($continuing == $flag_in_quoted) {
Branch: 2 branches
BranchTrueFalseTotal
if ($continuing == $flag_in_quoted) { } 374 113 487
elsif ($continuing == $flag_in_bracket) { } 113 0 113
192 374 push @item_parts_in_quote, $part;
193 374 ▶ next unless has_unquoted_tail_doublequote($part);
Branch: 1 branch
BranchTrueFalseTotal
unless has_unquoted_tail_doublequote($part) 225 149 374
194  
195 149 push @items, dequote(join(' ', @item_parts_in_quote));
196 149 @item_parts_in_quote = ();
197 149 $continuing = 0;
198 }
199 elsif ($continuing == $flag_in_bracket) {
200 113 push @item_parts_in_brackets, $part;
201 113 ▶ next if substr($part, -1 , 1) ne ']';
Branch: 1 branch
BranchTrueFalseTotal
if substr($part, -1, 1) ne "]" 0 113 113
202  
203 113 my $s = join(' ', @item_parts_in_brackets);
204 113 push @items, substr($s, 1, length($s) - 2);
205 113 @item_parts_in_brackets = ();
206 113 $continuing = 0;
207 } else {
208 0 croak "invalid status about continuing: '$continuing'.";
209 }
210 }
211 134 return @items;
212 }
213  
214 0 ▶sub set_strict_mode {
Subroutine set_strict_mode: not called CC=2 SCAR=17.9
Pod undocumented
215 0 my $self = shift;
216 0 ▶ $self->{strict} = shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
217 }
218  
219 42 ▶sub parse_strict {
Subroutine parse_strict: called CC=10 SCAR=23.0
Pod undocumented
220 42 my ($self, $line) = @_;
221 42 chomp $line;
222 42 foreach my $rule (@{$self->{try_maps}}) {
223 112 my @values = separate_log_items($rule->[0], $line);
224 112 ▶ next if scalar(@values) < 2;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @values < 2 11 101 112
225  
226 101 my $pairs = {};
227 101 my $fnum = 0;
228 101 foreach my $f (@{$rule->[1]}) {
229 960 $pairs->{$f} = $values[$fnum];
230 960 $fnum += 1;
231 }
232 101 ▶ next unless reduce { $a and defined($pairs->{$b}) } 1, qw( bytes status request datetime user logname rhost );
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 2 branches
BranchTrueFalseTotal
unless &reduce(sub { $a and defined $pairs->{$b}; } , 1, ("bytes", "status", "request", "datetime", "user", "logname", "rhost")) 1 100 101
$a and defined $pairs->{$b} 701 6 707
233  
234 100 my $req;
235 100 ($pairs->{date}, $pairs->{time}, $pairs->{timezone}) = ($pairs->{datetime} =~ m!^([^: ]+):([^ ]+)\s([-+0-9]+)$!);
236 100 ▶ if ($pairs->{request} =~ m!^(.*) (HTTP/\d\.\d)$!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pairs->{'request'} =~ m[^(.*) (HTTP/\d\.\d)$]) { } 84 16 100
237 84 $pairs->{proto} = $2;
238 84 $req = $1;
239 }
240 else {
241 16 $pairs->{proto} = undef;
242 16 $req = $pairs->{request};
243 }
244 100 ($pairs->{method}, $pairs->{path}) = split(/\s+/, $req, 2);
245 100 ▶ next unless reduce { $a and defined($pairs->{$b}) } 1, qw( path time date );
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 2 branches
BranchTrueFalseTotal
unless &reduce(sub { $a and defined $pairs->{$b}; } , 1, ("path", "time", "date")) 10 90 100
$a and defined $pairs->{$b} 287 13 300
246  
247 90 ▶ next if defined($rule->[2]) and not $rule->[2]->($pairs);
Branch: 1 branch
BranchTrueFalseTotal
if defined $rule->[2] and not $rule->[2]->($pairs) 49 41 90
Condition100%3/3
defined $rule->[2] and not $rule->[2]->($pairs)
!ll&&!rl&&r
152649
MC/DC100%2/2
defined $rule->[2] and not $rule->[2]->($pairs)
defined $rule->[2] not $rule->[2]->($pairs)
Truth table100%3/3
A and not(B)
A= defined $rule->[2]B= not $rule->[2]->($pairs)
ABresult
0X0
100
111
248  
249 41 return $pairs;
250 }
251 1 ▶ carp "unknown format: $line" if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 1 1
252 1 return undef;
253 }
254  
255 80 ▶sub parse {
Subroutine parse: called CC=2 SCAR=6.9
Pod undocumented
256 80 my ($self, $line) = @_;
257 80 ▶ if ($self->{strict}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"strict"}) 42 38 80
258 42 return $self->parse_strict($line);
259 }
260 38 $self->parse_fast($line);
261 }
262  
263 2 1;
264  
265 __END__
↑ Summary