Skip to content

Commit b0cc94a

Browse files
Merge branch '5.4' into 6.0
* 5.4: [Semaphore] fix tests [HttpClient] fix destructing CurlResponse [Cache] Fix connecting to Redis via a socket file [DependencyInjection][FrameworkBundle] Fix using PHP 8.1 enum as parameters [PropertyAccessor] Add missing TypeError catch [HttpKernel] Fixed error count by log not displayed in WebProfilerBundle Added `kernel.event_listener` to the default list of behavior describing tags, fixing AsEventListener attribute not working on decorators. [WebProfilerBundle] Fixes weird spacing in log message context/trace output [Notifier] fix Microsoft Teams webhook url [FrameworkBundle] Fix log channel of TagAwareAdapter [Postmark] ensure only a single tag can be used with Postmark [Mailer] allow Mailchimp to handle multiple TagHeader's [HttpClient] Fix Content-Length header when possible [DependencyInjection] Don't dump polyfilled classes in preload script
2 parents 45b9501 + 1badd3d commit b0cc94a

File tree

4 files changed

+62
-1
lines changed

4 files changed

+62
-1
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'])) {

Response/CurlResponse.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,9 @@ public function __destruct()
257257

258258
$this->doDestruct();
259259
} finally {
260-
curl_setopt($this->handle, \CURLOPT_VERBOSE, false);
260+
if (\is_resource($this->handle) || $this->handle instanceof \CurlHandle) {
261+
curl_setopt($this->handle, \CURLOPT_VERBOSE, false);
262+
}
261263
}
262264
}
263265

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)