diff --git a/src/Runtime/PhpFpm.php b/src/Runtime/PhpFpm.php index 9b0669755..babba466b 100644 --- a/src/Runtime/PhpFpm.php +++ b/src/Runtime/PhpFpm.php @@ -200,8 +200,6 @@ private function eventToFastCgiRequest(array $event): array $headers = $event['headers'] ?? []; $headers = array_change_key_case($headers, CASE_LOWER); - $serverName = $headers['host'] ?? 'localhost'; - $requestHeaders = [ 'GATEWAY_INTERFACE' => 'FastCGI/1.0', 'REQUEST_METHOD' => $event['httpMethod'], @@ -209,17 +207,19 @@ private function eventToFastCgiRequest(array $event): array 'SCRIPT_FILENAME' => $this->handler, 'SERVER_SOFTWARE' => 'bref', 'REMOTE_ADDR' => '127.0.0.1', - 'REMOTE_PORT' => $headers['X-Forwarded-Port'] ?? 80, + 'REMOTE_PORT' => $headers['x-forwarded-port'] ?? 80, 'SERVER_ADDR' => '127.0.0.1', - 'SERVER_NAME' => $serverName, + 'SERVER_NAME' => $headers['host'] ?? 'localhost', 'SERVER_PROTOCOL' => $protocol, - 'SERVER_PORT' => $headers['X-Forwarded-Port'] ?? 80, + 'SERVER_PORT' => $headers['x-forwarded-port'] ?? 80, 'PATH_INFO' => $event['path'] ?? '/', 'QUERY_STRING' => $queryString, ]; + $method = strtoupper($event['httpMethod']); + // See https://stackoverflow.com/a/5519834/245552 - if ((strtoupper($event['httpMethod']) === 'POST') && ! isset($headers['content-type'])) { + if (! empty($requestBody) && $method !== 'TRACE' && ! isset($headers['content-type'])) { $headers['content-type'] = 'application/x-www-form-urlencoded'; } if (isset($headers['content-type'])) { @@ -227,7 +227,7 @@ private function eventToFastCgiRequest(array $event): array } // Auto-add the Content-Length header if it wasn't provided // See https://github.com/mnapoli/bref/issues/162 - if ((strtoupper($event['httpMethod']) === 'POST') && ! isset($headers['content-length'])) { + if (! empty($requestBody) && $method !== 'TRACE' && ! isset($headers['content-length'])) { $headers['content-length'] = strlen($requestBody); } if (isset($headers['content-length'])) { diff --git a/tests/Runtime/PhpFpmTest.php b/tests/Runtime/PhpFpmTest.php index 95958ab4b..36992c6e4 100644 --- a/tests/Runtime/PhpFpmTest.php +++ b/tests/Runtime/PhpFpmTest.php @@ -207,13 +207,30 @@ public function test POST request with form data() ]); } + public function provideHttpMethodsWithRequestBodySupport(): array + { + return [ + 'POST' => [ + 'method' => 'POST', + ], + 'PUT' => [ + 'method' => 'PUT', + ], + 'PATCH' => [ + 'method' => 'PATCH', + ], + ]; + } + /** * @see https://github.com/mnapoli/bref/issues/162 + * + * @dataProvider provideHttpMethodsWithRequestBodySupport */ - public function test POST request with body and no content length() + public function test request with body and no content length(string $method) { $event = [ - 'httpMethod' => 'POST', + 'httpMethod' => $method, 'headers' => [ 'Content-Type' => 'application/json', // The Content-Length header is purposefully omitted @@ -232,7 +249,7 @@ public function test POST request with body and no content length() 'REQUEST_URI' => '/', 'PHP_SELF' => '/', 'PATH_INFO' => '/', - 'REQUEST_METHOD' => 'POST', + 'REQUEST_METHOD' => $method, 'QUERY_STRING' => '', 'HTTP_CONTENT_TYPE' => 'application/json', 'HTTP_CONTENT_LENGTH' => '14', @@ -241,10 +258,13 @@ public function test POST request with body and no content length() ]); } - public function test POST request supports utf8 characters in body() + /** + * @dataProvider provideHttpMethodsWithRequestBodySupport + */ + public function test request supports utf8 characters in body(string $method) { $event = [ - 'httpMethod' => 'POST', + 'httpMethod' => $method, 'headers' => [ 'Content-Type' => 'text/plain; charset=UTF-8', // The Content-Length header is purposefully omitted @@ -264,7 +284,7 @@ public function test POST request supports utf8 characters in body() 'REQUEST_URI' => '/', 'PHP_SELF' => '/', 'PATH_INFO' => '/', - 'REQUEST_METHOD' => 'POST', + 'REQUEST_METHOD' => $method, 'QUERY_STRING' => '', 'HTTP_CONTENT_TYPE' => 'text/plain; charset=UTF-8', 'HTTP_CONTENT_LENGTH' => '10',