Skip to content

Commit 1ed04ca

Browse files
Merge branch '4.4' into 5.4
* 4.4: [Cache] Fix connecting to Redis via a socket file [DependencyInjection][FrameworkBundle] Fix using PHP 8.1 enum as parameters [PropertyAccessor] Add missing TypeError catch [FrameworkBundle] Fix log channel of TagAwareAdapter [HttpClient] Fix Content-Length header when possible [DependencyInjection] Don't dump polyfilled classes in preload script
2 parents a5a467b + 35a6371 commit 1ed04ca

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

HttpClientTrait.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,14 @@ private static function prepareRequest(?string $method, ?string $url, array $opt
9898

9999
if (isset($options['body'])) {
100100
$options['body'] = self::normalizeBody($options['body']);
101+
102+
if (\is_string($options['body'])
103+
&& (string) \strlen($options['body']) !== substr($h = $options['normalized_headers']['content-length'][0] ?? '', 16)
104+
&& ('' !== $h || ('' !== $options['body'] && !isset($options['normalized_headers']['transfer-encoding'])))
105+
) {
106+
$options['normalized_headers']['content-length'] = [substr_replace($h ?: 'Content-Length: ', \strlen($options['body']), 16)];
107+
$options['headers'] = array_merge(...array_values($options['normalized_headers']));
108+
}
101109
}
102110

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

Tests/HttpClientTestCase.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,20 @@ public function testDebugInfoOnDestruct()
374374
$this->assertNotEmpty($traceInfo['debug']);
375375
}
376376

377+
public function testFixContentLength()
378+
{
379+
$client = $this->getHttpClient(__FUNCTION__);
380+
381+
$response = $client->request('POST', 'http://localhost:8057/post', [
382+
'body' => 'abc=def',
383+
'headers' => ['Content-Length: 4'],
384+
]);
385+
386+
$body = $response->toArray();
387+
388+
$this->assertSame(['abc' => 'def', 'REQUEST_METHOD' => 'POST'], $body);
389+
}
390+
377391
public function testNegativeTimeout()
378392
{
379393
$client = $this->getHttpClient(__FUNCTION__);

Tests/MockHttpClientTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,43 @@ public function testZeroStatusCode()
194194
$this->assertSame(0, $response->getStatusCode());
195195
}
196196

197+
public function testFixContentLength()
198+
{
199+
$client = new MockHttpClient();
200+
201+
$response = $client->request('POST', 'http://localhost:8057/post', [
202+
'body' => 'abc=def',
203+
'headers' => ['Content-Length: 4'],
204+
]);
205+
206+
$requestOptions = $response->getRequestOptions();
207+
$this->assertSame('Content-Length: 7', $requestOptions['headers'][0]);
208+
$this->assertSame(['Content-Length: 7'], $requestOptions['normalized_headers']['content-length']);
209+
210+
$response = $client->request('POST', 'http://localhost:8057/post', [
211+
'body' => 'abc=def',
212+
]);
213+
214+
$requestOptions = $response->getRequestOptions();
215+
$this->assertSame('Content-Length: 7', $requestOptions['headers'][1]);
216+
$this->assertSame(['Content-Length: 7'], $requestOptions['normalized_headers']['content-length']);
217+
218+
$response = $client->request('POST', 'http://localhost:8057/post', [
219+
'body' => 'abc=def',
220+
'headers' => ['Transfer-Encoding: chunked'],
221+
]);
222+
223+
$requestOptions = $response->getRequestOptions();
224+
$this->assertFalse(isset($requestOptions['normalized_headers']['content-length']));
225+
226+
$response = $client->request('POST', 'http://localhost:8057/post', [
227+
'body' => '',
228+
]);
229+
230+
$requestOptions = $response->getRequestOptions();
231+
$this->assertFalse(isset($requestOptions['normalized_headers']['content-length']));
232+
}
233+
197234
public function testThrowExceptionInBodyGenerator()
198235
{
199236
$mockHttpClient = new MockHttpClient([

0 commit comments

Comments
 (0)