blib/lib/Apache/LogFormat/Compiler.pm

filter: Statementstmt 88.7% 79 / 89 Branchbran 78.1% 25 / 32 Conditioncond 88.8% 8 / 9 MC/DCmcdc 80.0% 4 / 5 Subroutinesub 87.5% 14 / 16 Podpod 33.3% 2 / 6 totaltotal 84.0% 132 / 157 CC 21 SCAR 12.7
CC
21
Cov
86%
__ANON__
6.1
new
5.0
__ANON__
4.3
CRAP 3.6
↑ Summary
1 package Apache::LogFormat::Compiler;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use POSIX::strftime::Compiler qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ENVS => 0,
10 RES => 1,
11 LENGTH => 2,
12 REQTIME => 3,
13 TIME => 4,
14 5 };
15  
16 5 our $VERSION = '0.36';
17  
18 # copy from Plack::Middleware::AccessLog
19 5 our %formats = (
20 common => '%h %l %u %t "%r" %>s %b',
21 combined => '%h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-agent}i"',
22 );
23  
24 9 ▶sub _safe {
Subroutine _safe: called CC=2 SCAR=7.4
25 9 my $string = shift;
26 9 ▶ return unless defined $string;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $string 0 9 9
27 0 $string =~ s/([^[:print:]])/"\\x" . unpack("H*", $1)/eg;
28 9 return $string;
29 }
30  
31 14 ▶sub _string {
Subroutine _string: called CC=3 SCAR=11.2
32 14 my $string = shift;
33 14 ▶ return '-' if ! defined $string;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $string 6 8 14
34 8 ▶ return '-' if ! length $string;
Branch: 1 branch
BranchTrueFalseTotal
unless length $string 0 8 8
35 0 $string =~ s/([^[:print:]])/"\\x" . unpack("H*", $1)/eg;
36 8 return $string;
37 }
38  
39 3 ▶sub header_get {
Subroutine header_get: called CC=3 SCAR=11.0
Pod undocumented
40 3 my ($headers, $key) = @_;
41 3 $key = lc $key;
42 3 my @headers = @$headers; # copy
43 3 my $value;
44 3 ▶ while (my($hdr, $val) = splice @headers, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@headers, 0, 2)) 4 0 4
45 4 ▶ if ( lc $hdr eq $key ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $hdr eq $key) 3 1 4
46 3 $value = $val;
47 3 last;
48 }
49 }
50 3 return $value;
51 }
52  
53 5 my $psgi_reserved = { CONTENT_LENGTH => 1, CONTENT_TYPE => 1 };
54  
55 19 ▶my $block_handler = sub {
Subroutine __ANON__: called CC=6 SCAR=18.0
56 19 my($block, $type, $extra) = @_;
57 19 my $cb;
58 19 ▶ if ($type eq 'i') {
Branch: 4 branches
BranchTrueFalseTotal
if ($type eq 'i') { } 9 10 19
elsif ($type eq 'o') { } 3 7 10
elsif ($type eq 't') { } 6 1 7
elsif (exists $extra->{$type}) { } 1 0 1
59 9 $block =~ s/-/_/g;
60 9 $block = uc($block);
61 9 ▶ $block = "HTTP_${block}" unless $psgi_reserved->{$block};
Branch: 1 branch
BranchTrueFalseTotal
unless $psgi_reserved->{$block} 7 2 9
62 9 $cb = q!_string($_[ENVS]->{'!.$block.q!'})!;
63 } elsif ($type eq 'o') {
64 3 $cb = q!_string(header_get($_[RES]->[1],'!.$block.q!'))!;
65 } elsif ($type eq 't') {
66 6 $cb = q!"[" . POSIX::strftime::Compiler::strftime('!.$block.q!', @lt) . "]"!;
67 } elsif (exists $extra->{$type}) {
68 1 $cb = q!_string($extra_block_handlers->{'!.$type.q!'}->('!.$block.q!',$_[ENVS],$_[RES],$_[LENGTH],$_[REQTIME]))!;
69 } else {
70 0 Carp::croak("{$block}$type not supported");
71 0 $cb = "-";
72 }
73 19 return q|! . | . $cb . q|
74 . q!|;
75 5 };
76  
77 5 our %char_handler = (
78 '%' => q!'%'!,
79 h => q!($_[ENVS]->{REMOTE_ADDR} || '-')!,
80 l => q!'-'!,
81 u => q!($_[ENVS]->{REMOTE_USER} || '-')!,
82 t => q!'[' . $t . ']'!,
83 r => q!_safe($_[ENVS]->{REQUEST_METHOD}) . " " . _safe($_[ENVS]->{REQUEST_URI}) .
84 " " . $_[ENVS]->{SERVER_PROTOCOL}!,
85 s => q!$_[RES]->[0]!,
86 b => q!(defined $_[LENGTH] ? $_[LENGTH] : '-')!,
87 T => q!(defined $_[REQTIME] ? int($_[REQTIME]/1_000_000) : '-')!,
88 D => q!(defined $_[REQTIME] ? $_[REQTIME] : '-')!,
89 v => q!($_[ENVS]->{SERVER_NAME} || '-')!,
90 V => q!($_[ENVS]->{HTTP_HOST} || $_[ENVS]->{SERVER_NAME} || '-')!,
91 p => q!$_[ENVS]->{SERVER_PORT}!,
92 P => q!$$!,
93 m => q!_safe($_[ENVS]->{REQUEST_METHOD})!,
94 U => q!_safe($_[ENVS]->{PATH_INFO})!,
95 q => q!(($_[ENVS]->{QUERY_STRING} ne '') ? '?' . _safe($_[ENVS]->{QUERY_STRING}) : '' )!,
96 H => q!$_[ENVS]->{SERVER_PROTOCOL}!,
97  
98 );
99  
100 34 ▶my $char_handler = sub {
Subroutine __ANON__: called CC=4 SCAR=14.6
101 34 my ($char, $extra) = @_;
102 34 my $cb = $char_handler{$char};
103 34 ▶ if (!$cb && exists $extra->{$char}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $cb and exists $extra->{$char}) 1 33 34
Condition66%2/3
not $cb and exists $extra->{$char}
!ll&&!rl&&r
3301
MC/DC50%1/2
not $cb and exists $extra->{$char}
not $cb exists $extra->{$char}
Truth table66%2/3
not(A) and B
A= not $cbB= exists $extra->{$char}
ABresult
0X0
100
111
104 1 $cb = q!_string($extra_char_handlers->{'!.$char.q!'}->($_[ENVS],$_[RES],$_[LENGTH],$_[REQTIME]))!;
105 }
106 34 ▶ unless ($cb) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cb) 0 34 34
107 0 Carp::croak "\%$char not supported.";
108 0 return "-";
109 }
110 34 q|! . | . $cb . q|
111 . q!|;
112 5 };
113  
114 17 ▶sub new {
Subroutine new: called CC=5 SCAR=16.1
Pod documented
115 17 my $class = shift;
116  
117 17 ▶ my $fmt = shift || "combined";
Condition100%2/2
shift() || 'combined'
l!l
161
MC/DC100%1/1
shift() || 'combined'
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
118 17 ▶ $fmt = $formats{$fmt} if exists $formats{$fmt};
Branch: 1 branch
BranchTrueFalseTotal
if exists $Apache::LogFormat::Compiler::formats{$fmt} 1 16 17
119  
120 17 my %opts = @_;
121  
122 17 ▶ my ($code_ref, $code) = compile($fmt, $opts{block_handlers} || {}, $opts{char_handlers} || {});
Condition100%4/4
$opts{'block_handlers'} || {}
l!l
116
$opts{'char_handlers'} || {}
l!l
116
MC/DC100%1/1
$opts{'block_handlers'} || {}
$opts{'block_handlers'}
MC/DC100%1/1
$opts{'char_handlers'} || {}
$opts{'char_handlers'}
Truth table100%2/2
A
A= $opts{'block_handlers'}
Aresult
11
00
Truth table100%2/2
A
A= $opts{'char_handlers'}
Aresult
11
00
123 17 bless [$code_ref, $code], $class;
124 }
125  
126 17 ▶sub compile {
Subroutine compile: called CC=4 SCAR=13.9
Pod undocumented
127 17 my $fmt = shift;
128 17 my $extra_block_handlers = shift;
129 17 my $extra_char_handlers = shift;
130 17 $fmt =~ s/!/\\!/g;
131 17 $fmt =~ s!
132 (?:
133 \%\{(.+?)\}([a-zA-Z]) |
134 \%(?:[<>])?([a-zA-Z\%])
135 )
136 53 ▶ ! $1 ? $block_handler->($1, $2, $extra_block_handlers) : $char_handler->($3, $extra_char_handlers) !egx;
Branch: 1 branch
BranchTrueFalseTotal
$1 ? : 19 34 53
137
138 17 my @abbr = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec );
139 17 my $c = {};
140 17 $fmt = q~sub {
141 $_[TIME] = time() if ! defined $_[TIME];
142 my @lt = localtime($_[TIME]);
143 if ( ! exists $c->{tz_cache} || ! exists $c->{isdst_cache} || $lt[8] != $c->{isdst_cache} ) {
144 $c->{tz_cache} = POSIX::strftime::Compiler::strftime('%z',@lt);
145 $c->{isdst_cache} = $lt[8];
146 }
147 my $t = sprintf '%02d/%s/%04d:%02d:%02d:%02d %s', $lt[3], $abbr[$lt[4]], $lt[5]+1900,
148 $lt[2], $lt[1], $lt[0], $c->{tz_cache};
149 q!~ . $fmt . q~!
150 }~;
151 17 my $code_ref = eval $fmt; ## no critic
152 17 ▶ die $@ . "\n===\n" . $fmt if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 17 17
153 17 ▶ wantarray ? ($code_ref, $fmt) : $code_ref;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 17 0 17
154 }
155  
156 47 ▶sub log_line {
Subroutine log_line: called CC=1 SCAR=0.0
Pod documented
157 47 my $self = shift;
158 47 $self->[0]->(@_) . "\n";
159 }
160  
161 0 ▶sub code {
Subroutine code: not called CC=1 SCAR=6.9
Pod undocumented
162 0 my $self = shift;
163 0 $self->[1];
164 }
165  
166 0 ▶sub code_ref {
Subroutine code_ref: not called CC=1 SCAR=6.9
Pod undocumented
167 0 my $self = shift;
168 0 $self->[0];
169 }
170  
171 5 1;
172 __END__
↑ Summary