Permalink
Browse files

Special-case Content-Length and Content-Type for %{}i because it's re…

…served in PSGI. #387
  • Loading branch information...
1 parent 872ea82 commit 80197f2b0855fb05b9b3419bbe669b5ca70747e5 @miyagawa miyagawa committed Mar 19, 2013
Showing with 17 additions and 2 deletions.
  1. +5 −1 lib/Plack/Middleware/AccessLog.pm
  2. +12 −1 t/Plack-Middleware/access_log.t
@@ -22,6 +22,8 @@ my $tzoffset = POSIX::strftime("%z", localtime) !~ /^[+-]\d{4}$/ && do {
sprintf '%+03d%02u', $min_offset / 60, $min_offset % 60;
};
+my $psgi_reserved = { CONTENT_LENGTH => 1, CONTENT_TYPE => 1 };
+
sub call {
my $self = shift;
my($env) = @_;
@@ -56,7 +58,9 @@ sub log_line {
my($block, $type) = @_;
if ($type eq 'i') {
$block =~ s/-/_/g;
- my $val = _safe($env->{"HTTP_" . uc($block)});
+ $block = uc($block);
+ $block = "HTTP_${block}" unless $psgi_reserved->{$block};
+ my $val = _safe($env->{$block});
return defined $val ? $val : "-";
} elsif ($type eq 'o') {
return scalar $h->get($block) || "-";
@@ -15,7 +15,7 @@ my $test = sub {
my $app = builder {
enable "Plack::Middleware::AccessLog",
logger => sub { $log = "@_" }, format => $format;
- sub { [ 200, [ 'Content-Type' => 'text/plain' ], [ 'OK' ] ] };
+ sub { [ 200, [ 'Content-Type' => 'text/plain', 'Content-Length', 2 ], [ 'OK' ] ] };
};
test_psgi $app, sub { $_[0]->($req) };
};
@@ -56,4 +56,15 @@ my $test = sub {
is $r, $rs;
}
+{
+ my $req = POST "http://example.com/foo", [ "bar", "baz" ];
+ my $fmt = "cti=%{Content-Type}i cli=%{Content-Length}i cto=%{Content-Type}o clo=%{Content-Length}o";
+ $test->($fmt)->($req);
+ chomp $log;
+
+ my %vals = split /[= ]/, $log;
+ is_deeply \%vals, { cti => "application/x-www-form-urlencoded", cli => 7,
+ cto => 'text/plain', clo => 2 };
+}
+
done_testing;

0 comments on commit 80197f2

Please sign in to comment.