Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show empty response body size in access log when response body MUST be empty #50

Merged
merged 1 commit into from
Nov 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/AccessLogHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,18 @@ private function logWhenClosed(ServerRequestInterface $request, ResponseInterfac
*/
private function log(ServerRequestInterface $request, ResponseInterface $response, int $responseSize, float $time): void
{
$method = $request->getMethod();
$status = $response->getStatusCode();

// HEAD requests and `204 No Content` and `304 Not Modified` always use an empty response body
if ($method === 'HEAD' || $status === 204 || $status === 304) {
$responseSize = 0;
}

$this->sapi->log(
($request->getServerParams()['REMOTE_ADDR'] ?? '-') . ' ' .
'"' . $this->escape($request->getMethod()) . ' ' . $this->escape($request->getRequestTarget()) . ' HTTP/' . $request->getProtocolVersion() . '" ' .
$response->getStatusCode() . ' ' . $responseSize . ' ' . sprintf('%.3F', $time < 0 ? 0 : $time)
'"' . $this->escape($method) . ' ' . $this->escape($request->getRequestTarget()) . ' HTTP/' . $request->getProtocolVersion() . '" ' .
$status . ' ' . $responseSize . ' ' . sprintf('%.3F', $time < 0 ? 0 : $time)
);
}

Expand Down
36 changes: 36 additions & 0 deletions tests/AccessLogHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,42 @@ public function testInvokePrintsRequestWithEscapedSpecialCharactersInRequestMeth
$handler($request, function () use ($response) { return $response; });
}

public function testInvokePrintsRequestLogForHeadRequestWithResponseSizeAsZero()
{
$handler = new AccessLogHandler();

$request = new ServerRequest('HEAD', 'http://localhost:8080/users', [], '', '1.1', ['REMOTE_ADDR' => '127.0.0.1']);
$response = new Response(200, [], "HEAD\n");

// 2021-01-29 12:22:01.717 127.0.0.1 "HEAD /users HTTP/1.1" 200 0 0.000\n
$this->expectOutputRegex("/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3} 127\.0\.0\.1 \"HEAD \/users HTTP\/1\.1\" 200 0 0\.0\d\d" . PHP_EOL . "$/");
$handler($request, function () use ($response) { return $response; });
}

public function testInvokePrintsRequestLogForNoContentResponseWithResponseSizeAsZero()
{
$handler = new AccessLogHandler();

$request = new ServerRequest('GET', 'http://localhost:8080/users', [], '', '1.1', ['REMOTE_ADDR' => '127.0.0.1']);
$response = new Response(204, [], "No Content\n");

// 2021-01-29 12:22:01.717 127.0.0.1 "GET /users HTTP/1.1" 204 0 0.000\n
$this->expectOutputRegex("/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3} 127\.0\.0\.1 \"GET \/users HTTP\/1\.1\" 204 0 0\.0\d\d" . PHP_EOL . "$/");
$handler($request, function () use ($response) { return $response; });
}

public function testInvokePrintsRequestLogForNotModifiedResponseWithResponseSizeAsZero()
{
$handler = new AccessLogHandler();

$request = new ServerRequest('GET', 'http://localhost:8080/users', [], '', '1.1', ['REMOTE_ADDR' => '127.0.0.1']);
$response = new Response(304, [], "Not Modified\n");

// 2021-01-29 12:22:01.717 127.0.0.1 "GET /users HTTP/1.1" 304 0 0.000\n
$this->expectOutputRegex("/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3} 127\.0\.0\.1 \"GET \/users HTTP\/1\.1\" 304 0 0\.0\d\d" . PHP_EOL . "$/");
$handler($request, function () use ($response) { return $response; });
}

public function testInvokePrintsPlainProxyRequestLogWithCurrentDateAndTime()
{
$handler = new AccessLogHandler();
Expand Down