| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Apache::LogFormat::Compiler; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 5 |  |  | 5 |  | 196270 | use strict; | 
|  | 5 |  |  |  |  | 10 |  | 
|  | 5 |  |  |  |  | 174 |  | 
| 4 | 5 |  |  | 5 |  | 25 | use warnings; | 
|  | 5 |  |  |  |  | 7 |  | 
|  | 5 |  |  |  |  | 166 |  | 
| 5 | 5 |  |  | 5 |  | 133 | use 5.008001; | 
|  | 5 |  |  |  |  | 19 |  | 
| 6 | 5 |  |  | 5 |  | 28 | use Carp; | 
|  | 5 |  |  |  |  | 6 |  | 
|  | 5 |  |  |  |  | 437 |  | 
| 7 | 5 |  |  | 5 |  | 2886 | use POSIX::strftime::Compiler qw//; | 
|  | 5 |  |  |  |  | 60567 |  | 
|  | 5 |  |  |  |  | 206 |  | 
| 8 |  |  |  |  |  |  | use constant { | 
| 9 | 5 |  |  |  |  | 5051 | ENVS => 0, | 
| 10 |  |  |  |  |  |  | RES => 1, | 
| 11 |  |  |  |  |  |  | LENGTH => 2, | 
| 12 |  |  |  |  |  |  | REQTIME => 3, | 
| 13 |  |  |  |  |  |  | TIME => 4, | 
| 14 | 5 |  |  | 5 |  | 42 | }; | 
|  | 5 |  |  |  |  | 7 |  | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | our $VERSION = '0.35'; | 
| 17 |  |  |  |  |  |  |  | 
| 18 |  |  |  |  |  |  | # copy from Plack::Middleware::AccessLog | 
| 19 |  |  |  |  |  |  | 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 |  |  |  |  |  |  | sub _safe { | 
| 25 | 9 |  |  | 9 |  | 16 | my $string = shift; | 
| 26 | 9 | 50 |  |  |  | 17 | return unless defined $string; | 
| 27 | 9 |  |  |  |  | 19 | $string =~ s/([^[:print:]])/"\\x" . unpack("H*", $1)/eg; | 
|  | 0 |  |  |  |  | 0 |  | 
| 28 | 9 |  |  |  |  | 158 | return $string; | 
| 29 |  |  |  |  |  |  | } | 
| 30 |  |  |  |  |  |  |  | 
| 31 |  |  |  |  |  |  | sub _string { | 
| 32 | 14 |  |  | 14 |  | 2584 | my $string = shift; | 
| 33 | 14 | 100 |  |  |  | 89 | return '-' if ! defined $string; | 
| 34 | 8 | 50 |  |  |  | 18 | return '-' if ! length $string; | 
| 35 | 8 |  |  |  |  | 15 | $string =~ s/([^[:print:]])/"\\x" . unpack("H*", $1)/eg; | 
|  | 0 |  |  |  |  | 0 |  | 
| 36 | 8 |  |  |  |  | 42 | return $string; | 
| 37 |  |  |  |  |  |  | } | 
| 38 |  |  |  |  |  |  |  | 
| 39 |  |  |  |  |  |  | sub header_get { | 
| 40 | 3 |  |  | 3 | 0 | 299 | my ($headers, $key) = @_; | 
| 41 | 3 |  |  |  |  | 6 | $key = lc $key; | 
| 42 | 3 |  |  |  |  | 7 | my @headers = @$headers; # copy | 
| 43 | 3 |  |  |  |  | 2 | my $value; | 
| 44 | 3 |  |  |  |  | 12 | while (my($hdr, $val) = splice @headers, 0, 2) { | 
| 45 | 4 | 100 |  |  |  | 12 | if ( lc $hdr eq $key ) { | 
| 46 | 3 |  |  |  |  | 4 | $value = $val; | 
| 47 | 3 |  |  |  |  | 4 | last; | 
| 48 |  |  |  |  |  |  | } | 
| 49 |  |  |  |  |  |  | } | 
| 50 | 3 |  |  |  |  | 11 | return $value; | 
| 51 |  |  |  |  |  |  | } | 
| 52 |  |  |  |  |  |  |  | 
| 53 |  |  |  |  |  |  | my $psgi_reserved = { CONTENT_LENGTH => 1, CONTENT_TYPE => 1 }; | 
| 54 |  |  |  |  |  |  |  | 
| 55 |  |  |  |  |  |  | my $block_handler = sub { | 
| 56 |  |  |  |  |  |  | my($block, $type, $extra) = @_; | 
| 57 |  |  |  |  |  |  | my $cb; | 
| 58 |  |  |  |  |  |  | if ($type eq 'i') { | 
| 59 |  |  |  |  |  |  | $block =~ s/-/_/g; | 
| 60 |  |  |  |  |  |  | $block = uc($block); | 
| 61 |  |  |  |  |  |  | $block = "HTTP_${block}" unless $psgi_reserved->{$block}; | 
| 62 |  |  |  |  |  |  | $cb =  q!_string($_[ENVS]->{'!.$block.q!'})!; | 
| 63 |  |  |  |  |  |  | } elsif ($type eq 'o') { | 
| 64 |  |  |  |  |  |  | $cb =  q!_string(header_get($_[RES]->[1],'!.$block.q!'))!; | 
| 65 |  |  |  |  |  |  | } elsif ($type eq 't') { | 
| 66 |  |  |  |  |  |  | $cb =  q!"[" . POSIX::strftime::Compiler::strftime('!.$block.q!', @lt) . "]"!; | 
| 67 |  |  |  |  |  |  | } elsif (exists $extra->{$type}) { | 
| 68 |  |  |  |  |  |  | $cb =  q!_string($extra_block_handlers->{'!.$type.q!'}->('!.$block.q!',$_[ENVS],$_[RES],$_[LENGTH],$_[REQTIME]))!; | 
| 69 |  |  |  |  |  |  | } else { | 
| 70 |  |  |  |  |  |  | Carp::croak("{$block}$type not supported"); | 
| 71 |  |  |  |  |  |  | $cb = "-"; | 
| 72 |  |  |  |  |  |  | } | 
| 73 |  |  |  |  |  |  | return q|! . | . $cb . q| | 
| 74 |  |  |  |  |  |  | . q!|; | 
| 75 |  |  |  |  |  |  | }; | 
| 76 |  |  |  |  |  |  |  | 
| 77 |  |  |  |  |  |  | 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 |  |  |  |  |  |  | my $char_handler = sub { | 
| 101 |  |  |  |  |  |  | my ($char, $extra) = @_; | 
| 102 |  |  |  |  |  |  | my $cb = $char_handler{$char}; | 
| 103 |  |  |  |  |  |  | if (!$cb && exists $extra->{$char}) { | 
| 104 |  |  |  |  |  |  | $cb = q!_string($extra_char_handlers->{'!.$char.q!'}->($_[ENVS],$_[RES],$_[LENGTH],$_[REQTIME]))!; | 
| 105 |  |  |  |  |  |  | } | 
| 106 |  |  |  |  |  |  | unless ($cb) { | 
| 107 |  |  |  |  |  |  | Carp::croak "\%$char not supported."; | 
| 108 |  |  |  |  |  |  | return "-"; | 
| 109 |  |  |  |  |  |  | } | 
| 110 |  |  |  |  |  |  | q|! . | . $cb . q| | 
| 111 |  |  |  |  |  |  | . q!|; | 
| 112 |  |  |  |  |  |  | }; | 
| 113 |  |  |  |  |  |  |  | 
| 114 |  |  |  |  |  |  | sub new { | 
| 115 | 17 |  |  | 17 | 1 | 29812 | my $class = shift; | 
| 116 |  |  |  |  |  |  |  | 
| 117 | 17 |  | 100 |  |  | 72 | my $fmt = shift || "combined"; | 
| 118 | 17 | 100 |  |  |  | 60 | $fmt = $formats{$fmt} if exists $formats{$fmt}; | 
| 119 |  |  |  |  |  |  |  | 
| 120 | 17 |  |  |  |  | 42 | my %opts = @_; | 
| 121 |  |  |  |  |  |  |  | 
| 122 | 17 |  | 100 |  |  | 187 | my ($code_ref, $code) = compile($fmt, $opts{block_handlers} || {}, $opts{char_handlers} || {}); | 
|  |  |  | 100 |  |  |  |  | 
| 123 | 17 |  |  |  |  | 77 | bless [$code_ref, $code], $class; | 
| 124 |  |  |  |  |  |  | } | 
| 125 |  |  |  |  |  |  |  | 
| 126 |  |  |  |  |  |  | sub compile { | 
| 127 | 17 |  |  | 17 | 0 | 29 | my $fmt = shift; | 
| 128 | 17 |  |  |  |  | 23 | my $extra_block_handlers = shift; | 
| 129 | 17 |  |  |  |  | 25 | my $extra_char_handlers = shift; | 
| 130 | 17 |  |  |  |  | 45 | $fmt =~ s/!/\\!/g; | 
| 131 | 17 |  |  |  |  | 180 | $fmt =~ s! | 
| 132 |  |  |  |  |  |  | (?: | 
| 133 |  |  |  |  |  |  | \%\{(.+?)\}([a-zA-Z]) | | 
| 134 |  |  |  |  |  |  | \%(?:[<>])?([a-zA-Z\%]) | 
| 135 |  |  |  |  |  |  | ) | 
| 136 | 53 | 100 |  |  |  | 177 | ! $1 ? $block_handler->($1, $2, $extra_block_handlers) : $char_handler->($3, $extra_char_handlers) !egx; | 
| 137 |  |  |  |  |  |  |  | 
| 138 | 17 |  |  |  |  | 90 | my @abbr = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ); | 
| 139 | 17 |  |  |  |  | 28 | my $c = {}; | 
| 140 | 17 |  |  |  |  | 53 | $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 |  |  |  |  | 4368 | my $code_ref = eval $fmt; ## no critic | 
| 152 | 17 | 50 |  |  |  | 60 | die $@ . "\n===\n" . $fmt if $@; | 
| 153 | 17 | 50 |  |  |  | 83 | wantarray ? ($code_ref, $fmt) : $code_ref; | 
| 154 |  |  |  |  |  |  | } | 
| 155 |  |  |  |  |  |  |  | 
| 156 |  |  |  |  |  |  | sub log_line { | 
| 157 | 47 |  |  | 47 | 1 | 93060 | my $self = shift; | 
| 158 | 47 |  |  |  |  | 1488 | $self->[0]->(@_) . "\n"; | 
| 159 |  |  |  |  |  |  | } | 
| 160 |  |  |  |  |  |  |  | 
| 161 |  |  |  |  |  |  | sub code { | 
| 162 | 0 |  |  | 0 | 0 |  | my $self = shift; | 
| 163 | 0 |  |  |  |  |  | $self->[1]; | 
| 164 |  |  |  |  |  |  | } | 
| 165 |  |  |  |  |  |  |  | 
| 166 |  |  |  |  |  |  | sub code_ref { | 
| 167 | 0 |  |  | 0 | 0 |  | my $self = shift; | 
| 168 | 0 |  |  |  |  |  | $self->[0]; | 
| 169 |  |  |  |  |  |  | } | 
| 170 |  |  |  |  |  |  |  | 
| 171 |  |  |  |  |  |  | 1; | 
| 172 |  |  |  |  |  |  | __END__ |