Skip to content

Commit 528f59f

Browse files
priyadinicolas-grekas
authored andcommitted
[HttpFoundation] Request without content-type or content-length header should result in null values, not empty strings
1 parent 4da1713 commit 528f59f

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

ServerBag.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function getHeaders()
3131
foreach ($this->parameters as $key => $value) {
3232
if (str_starts_with($key, 'HTTP_')) {
3333
$headers[substr($key, 5)] = $value;
34-
} elseif (\in_array($key, ['CONTENT_TYPE', 'CONTENT_LENGTH', 'CONTENT_MD5'], true)) {
34+
} elseif (\in_array($key, ['CONTENT_TYPE', 'CONTENT_LENGTH', 'CONTENT_MD5'], true) && '' !== $value) {
3535
$headers[$key] = $value;
3636
}
3737
}

Tests/ServerBagTest.php

+16
Original file line numberDiff line numberDiff line change
@@ -177,4 +177,20 @@ public function testItDoesNotOverwriteTheAuthorizationHeaderIfItIsAlreadySet()
177177
'PHP_AUTH_PW' => '',
178178
], $bag->getHeaders());
179179
}
180+
181+
/**
182+
* An HTTP request without content-type and content-length will result in
183+
* the variables $_SERVER['CONTENT_TYPE'] and $_SERVER['CONTENT_LENGTH']
184+
* containing an empty string in PHP.
185+
*/
186+
public function testRequestWithoutContentTypeAndContentLength()
187+
{
188+
$bag = new ServerBag([
189+
'CONTENT_TYPE' => '',
190+
'CONTENT_LENGTH' => '',
191+
'HTTP_USER_AGENT' => 'foo',
192+
]);
193+
194+
$this->assertSame(['USER_AGENT' => 'foo'], $bag->getHeaders());
195+
}
180196
}

0 commit comments

Comments
 (0)