Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5e98336

Browse files
bug #53432 [HttpFoundation] Request without content-type or content-length header should result in null values, not empty strings (priyadi)
This PR was squashed before being merged into the 5.4 branch. Discussion ---------- [HttpFoundation] Request without content-type or content-length header should result in null values, not empty strings | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | | License | MIT An HTTP request without content-type and content length will result in the variables `$_SERVER['CONTENT_TYPE']` and `$_SERVER['CONTENT_LENGTH']` having an empty string in PHP. For example, the request: > POST / HTTP/1.1 > Host: app.dev.localhost:8000 > User-Agent: curl/8.4.0 > Accept: */* (from curl invocation: `curl -X 'POST' 'http://app.dev.localhost:8000/' -H 'Content-Type:' -H 'Content-length:' -d '' -v> /dev/null`) It will result in the variables `$_SERVER['CONTENT_TYPE']` and `$_SERVER['CONTENT_LENGTH']` containing an empty string. In turn, `$request->headers->get('Content-type')` and `$request->headers->get('Content-length')` also result in an empty string. This PR changes so that `$request->headers->get('Content-type')` and `$request->headers->get('Content-length')` will reflect the original state of the request header, that they return null (due to the fact the headers did not exist in the request), not an empty string (erroneously implying the client sent an empty content-type and content-length headers). Commits ------- 3a5b25c [HttpFoundation] Request without content-type or content-length header should result in null values, not empty strings
2 parents 1b62f3c + 3a5b25c commit 5e98336

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/Symfony/Component/HttpFoundation/ServerBag.php

Lines changed: 1 addition & 1 deletion
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
}

src/Symfony/Component/HttpFoundation/Tests/ServerBagTest.php

Lines changed: 16 additions & 0 deletions
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)