Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 582f572

Browse filesBrowse files
committed
bug #45261 [HttpClient] Fix Content-Length header when possible (nicolas-grekas)
This PR was merged into the 4.4 branch. Discussion ---------- [HttpClient] Fix Content-Length header when possible | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #45260 | License | MIT | Doc PR | - When userland fails to compute the correct Content-Length header and we can fix it, as is the case in the linked issue. Commits ------- f5c2e6e [HttpClient] Fix Content-Length header when possible
2 parents c112bf1 + f5c2e6e commit 582f572
Copy full SHA for 582f572

File tree

3 files changed

+59
-0
lines changed
Filter options

3 files changed

+59
-0
lines changed

‎src/Symfony/Component/HttpClient/HttpClientTrait.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpClient/HttpClientTrait.php
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@ private static function prepareRequest(?string $method, ?string $url, array $opt
8686

8787
if (isset($options['body'])) {
8888
$options['body'] = self::normalizeBody($options['body']);
89+
90+
if (\is_string($options['body'])
91+
&& (string) \strlen($options['body']) !== substr($h = $options['normalized_headers']['content-length'][0] ?? '', 16)
92+
&& ('' !== $h || ('' !== $options['body'] && !isset($options['normalized_headers']['transfer-encoding'])))
93+
) {
94+
$options['normalized_headers']['content-length'] = [substr_replace($h ?: 'Content-Length: ', \strlen($options['body']), 16)];
95+
$options['headers'] = array_merge(...array_values($options['normalized_headers']));
96+
}
8997
}
9098

9199
if (isset($options['peer_fingerprint'])) {

‎src/Symfony/Component/HttpClient/Tests/HttpClientTestCase.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpClient/Tests/HttpClientTestCase.php
+14Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,20 @@ public function testDebugInfoOnDestruct()
180180
$this->assertNotEmpty($traceInfo['debug']);
181181
}
182182

183+
public function testFixContentLength()
184+
{
185+
$client = $this->getHttpClient(__FUNCTION__);
186+
187+
$response = $client->request('POST', 'http://localhost:8057/post', [
188+
'body' => 'abc=def',
189+
'headers' => ['Content-Length: 4'],
190+
]);
191+
192+
$body = $response->toArray();
193+
194+
$this->assertSame(['abc' => 'def', 'REQUEST_METHOD' => 'POST'], $body);
195+
}
196+
183197
public function testNegativeTimeout()
184198
{
185199
$client = $this->getHttpClient(__FUNCTION__);

‎src/Symfony/Component/HttpClient/Tests/MockHttpClientTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpClient/Tests/MockHttpClientTest.php
+37Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,43 @@ public function testZeroStatusCode()
7373
$this->assertSame(0, $response->getStatusCode());
7474
}
7575

76+
public function testFixContentLength()
77+
{
78+
$client = new MockHttpClient();
79+
80+
$response = $client->request('POST', 'http://localhost:8057/post', [
81+
'body' => 'abc=def',
82+
'headers' => ['Content-Length: 4'],
83+
]);
84+
85+
$requestOptions = $response->getRequestOptions();
86+
$this->assertSame('Content-Length: 7', $requestOptions['headers'][0]);
87+
$this->assertSame(['Content-Length: 7'], $requestOptions['normalized_headers']['content-length']);
88+
89+
$response = $client->request('POST', 'http://localhost:8057/post', [
90+
'body' => 'abc=def',
91+
]);
92+
93+
$requestOptions = $response->getRequestOptions();
94+
$this->assertSame('Content-Length: 7', $requestOptions['headers'][1]);
95+
$this->assertSame(['Content-Length: 7'], $requestOptions['normalized_headers']['content-length']);
96+
97+
$response = $client->request('POST', 'http://localhost:8057/post', [
98+
'body' => 'abc=def',
99+
'headers' => ['Transfer-Encoding: chunked'],
100+
]);
101+
102+
$requestOptions = $response->getRequestOptions();
103+
$this->assertFalse(isset($requestOptions['normalized_headers']['content-length']));
104+
105+
$response = $client->request('POST', 'http://localhost:8057/post', [
106+
'body' => '',
107+
]);
108+
109+
$requestOptions = $response->getRequestOptions();
110+
$this->assertFalse(isset($requestOptions['normalized_headers']['content-length']));
111+
}
112+
76113
public function testThrowExceptionInBodyGenerator()
77114
{
78115
$mockHttpClient = new MockHttpClient([

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.