8000 [HttpClient] Fix Content-Length header when possible by nicolas-grekas · Pull Request #45261 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[HttpClient] Fix Content-Length header when possible #45261

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

Merged
merged 1 commit into from
Feb 1, 2022
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
8 changes: 8 additions & 0 deletions src/Symfony/Component/HttpClient/HttpClientTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ private static function prepareRequest(?string $method, ?string $url, array $opt

if (isset($options['body'])) {
$options['body'] = self::normalizeBody($options['body']);

if (\is_string($options['body'])
&& (string) \strlen($options['body']) !== substr($h = $options['normalized_headers']['content-length'][0] ?? '', 16)
&& ('' !== $h || ('' !== $options['body'] && !isset($options['normalized_headers']['transfer-encoding'])))
) {
$options['normalized_headers']['content-length'] = [substr_replace($h ?: 'Content-Length: ', \strlen($options['body']), 16)];
$options['headers'] = array_merge(...array_values($options['normalized_headers']));
}
}

if (isset($options['peer_fingerprint'])) {
Expand Down
14 changes: 14 additions & 0 deletions src/Symfony/Component/HttpClient/Tests/HttpClientTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,20 @@ public function testDebugInfoOnDestruct()
$this->assertNotEmpty($traceInfo['debug']);
}

public function testFixContentLength()
{
$client = $this->getHttpClient(__FUNCTION__);

$response = $client->request('POST', 'http://localhost:8057/post', [
'body' => 'abc=def',
'headers' => ['Content-Length: 4'],
]);

$body = $response->toArray();

$this->assertSame(['abc' => 'def', 'REQUEST_METHOD' => 'POST'], $body);
}

public function testNegativeTimeout()
{
$client = $this->getHttpClient(__FUNCTION__);
Expand Down
37 changes: 37 additions & 0 deletions src/Symfony/Component/HttpClient/Tests/MockHttpClientTest.php
52F6
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,43 @@ public function testZeroStatusCode()
$this->assertSame(0, $response->getStatusCode());
}

public function testFixContentLength()
{
$client = new MockHttpClient();

$response = $client->request('POST', 'http://localhost:8057/post', [
'body' => 'abc=def',
'headers' => ['Content-Length: 4'],
]);

$requestOptions = $response->getRequestOptions();
$this->assertSame('Content-Length: 7', $requestOptions['headers'][0]);
$this->assertSame(['Content-Length: 7'], $requestOptions['normalized_headers']['content-length']);

$response = $client->request('POST', 'http://localhost:8057/post', [
'body' => 'abc=def',
]);

$requestOptions = $response->getRequestOptions();
$this->assertSame('Content-Length: 7', $requestOptions['headers'][1]);
$this->assertSame(['Content-Length: 7'], $requestOptions['normalized_headers']['content-length']);

$response = $client->request('POST', 'http://localhost:8057/post', [
'body' => 'abc=def',
'headers' => ['Transfer-Encoding: chunked'],
]);

$requestOptions = $response->getRequestOptions();
$this->assertFalse(isset($requestOptions['normalized_headers']['content-length']));

$response = $client->request('POST', 'http://localhost:8057/post', [
'body' => '',
]);

$requestOptions = $response->getRequestOptions();
$this->assertFalse(isset($requestOptions['normalized_headers']['content-length']));
}

public function testThrowExceptionInBodyGenerator()
{
$mockHttpClient = new MockHttpClient([
Expand Down
0