header_ok: Log all non-2xx HTTP status codes

This commit is contained in:
Richard Hansen 2024-05-17 23:21:25 -04:00
parent 7fe7fd0e18
commit 211d59fccc
2 changed files with 13 additions and 13 deletions

View file

@ -3776,17 +3776,16 @@ sub header_ok {
my ($host, $line) = @_; my ($host, $line) = @_;
$line =~ s/\r?\n.*//s; $line =~ s/\r?\n.*//s;
# TODO: What is this leading /s*/? Is it a typo of /\s*/? # TODO: What is this leading /s*/? Is it a typo of /\s*/?
my ($result) = ($line =~ qr%^s*HTTP/.*\s+(\d+)%i); my ($code, $msg) = ($line =~ qr%^s*HTTP/.*\s+(\d+)\s*(?:\s+([^\s].*))?$%i);
if (!defined($result)) { if (!defined($code)) {
failed('updating %s: unexpected HTTP response: %s', $host, $line); failed('updating %s: unexpected HTTP response: %s', $host, $line);
return 0; return 0;
} elsif ($result eq '401') { } elsif ($code !~ qr/^2\d\d$/) {
failed("updating %s: authentication failed (%s)", $host, $line); my %msgs = (
return 0; '401' => 'authentication failed',
} elsif ($result eq '403') { '403' => 'not authorized',
failed("updating %s: not authorized (%s)", $host, $line); );
return 0; failed('updating %s: %s %s', $host, $code, $msg // $msgs{$code} // '');
} elsif ($result !~ qr/^2\d\d$/) {
return 0; return 0;
} }
return 1; return 1;

View file

@ -35,14 +35,14 @@ my @test_cases = (
want => 1, want => 1,
}, },
{ {
desc => '401 not OK', desc => '401 not OK, fallback message',
input => 'HTTP/1.1 401 bad', input => 'HTTP/1.1 401 ',
want => 0, want => 0,
wantmsg => qr/authentication failed/, wantmsg => qr/authentication failed/,
}, },
{ {
desc => '403 not OK', desc => '403 not OK, fallback message',
input => 'HTTP/1.1 403 bad', input => 'HTTP/1.1 403 ',
want => 0, want => 0,
wantmsg => qr/not authorized/, wantmsg => qr/not authorized/,
}, },
@ -50,6 +50,7 @@ my @test_cases = (
desc => 'other 4xx not OK', desc => 'other 4xx not OK',
input => 'HTTP/1.1 456 bad', input => 'HTTP/1.1 456 bad',
want => 0, want => 0,
wantmsg => qr/bad/,
}, },
{ {
desc => 'only first line is logged on error', desc => 'only first line is logged on error',