Skip to content

Commit

Permalink
Merge pull request #297 from timoschaefer/add-content-length-header-f…
Browse files Browse the repository at this point in the history
…or-more-http-methods

Add content length header for more http methods
  • Loading branch information
mnapoli authored Apr 8, 2019
2 parents bb6f8fa + 76b7d0c commit 4183429
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
14 changes: 7 additions & 7 deletions src/Runtime/PhpFpm.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,34 +200,34 @@ 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'],
'REQUEST_URI' => $uri,
'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'])) {
$requestHeaders['CONTENT_TYPE'] = $headers['content-type'];
}
// 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'])) {
Expand Down
32 changes: 26 additions & 6 deletions tests/Runtime/PhpFpmTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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',
Expand All @@ -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
Expand All @@ -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',
Expand Down

0 comments on commit 4183429

Please sign in to comment.