Skip to content

Commit 0a6d049

Browse files
Merge branch '5.4' into 6.0
* 5.4: Revert "bug #45813 [HttpClient] Move Content-Type after Content-Length (nicolas-grekas)" [FrameworkBundle] Fix exit codes in debug:translation command [Cache] Declaratively declare/hide DoctrineProvider to avoid breaking static analysis [HttpClient] Let curl handle Content-Length headers Improve testsuite [HttpClient] Move Content-Type after Content-Length [HttpClient] minor cs fix [Config] Fix using null values with config builders
2 parents 9a07cb1 + f9bd075 commit 0a6d049

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

CurlHttpClient.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,12 @@ public function request(string $method, string $url, array $options = []): Respo
200200
$options['headers'][] = 'Accept-Encoding: gzip'; // Expose only one encoding, some servers mess up when more are provided
201201
}
202202

203+
$hasContentLength = isset($options['normalized_headers']['content-length'][0]);
204+
203205
foreach ($options['headers'] as $header) {
206+
if ($hasContentLength && 0 === stripos($header, 'Content-Length:')) {
207+
continue; // Let curl handle Content-Length headers
208+
}
204209
if (':' === $header[-2] && \strlen($header) - 2 === strpos($header, ': ')) {
205210
// curl requires a special syntax to send empty headers
206211
$curlopts[\CURLOPT_HTTPHEADER][] = substr_replace($header, ';', -2);
@@ -227,7 +232,7 @@ public function request(string $method, string $url, array $options = []): Respo
227232
};
228233
}
229234

230-
if (isset($options['normalized_headers']['content-length'][0])) {
235+
if ($hasContentLength) {
231236
$curlopts[\CURLOPT_INFILESIZE] = substr($options['normalized_headers']['content-length'][0], \strlen('Content-Length: '));
232237
} elseif (!isset($options['normalized_headers']['transfer-encoding'])) {
233238
$curlopts[\CURLOPT_HTTPHEADER][] = 'Transfer-Encoding: chunked'; // Enable chunked request bodies

HttpClientTrait.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,12 @@ private static function prepareRequest(?string $method, ?string $url, array $opt
8888
unset($options['json']);
8989

9090
if (!isset($options['normalized_headers']['content-type'])) {
91-
$options['normalized_headers']['content-type'] = [$options['headers'][] = 'Content-Type: application/json'];
91+
$options['normalized_headers']['content-type'] = ['Content-Type: application/json'];
9292
}
9393
}
9494

9595
if (!isset($options['normalized_headers']['accept'])) {
96-
$options['normalized_headers']['accept'] = [$options['headers'][] = 'Accept: */*'];
96+
$options['normalized_headers']['accept'] = ['Accept: */*'];
9797
}
9898

9999
if (isset($options['body'])) {
@@ -104,7 +104,6 @@ private static function prepareRequest(?string $method, ?string $url, array $opt
104104
&& ('' !== $h || ('' !== $options['body'] && !isset($options['normalized_headers']['transfer-encoding'])))
105105
) {
106106
$options['normalized_headers']['content-length'] = [substr_replace($h ?: 'Content-Length: ', \strlen($options['body']), 16)];
107-
$options['headers'] = array_merge(...array_values($options['normalized_headers']));
108107
}
109108
}
110109

@@ -146,11 +145,11 @@ private static function prepareRequest(?string $method, ?string $url, array $opt
146145
if (null !== $url) {
147146
// Merge auth with headers
148147
if (($options['auth_basic'] ?? false) && !($options['normalized_headers']['authorization'] ?? false)) {
149-
$options['normalized_headers']['authorization'] = [$options['headers'][] = 'Authorization: Basic '.base64_encode($options['auth_basic'])];
148+
$options['normalized_headers']['authorization'] = ['Authorization: Basic '.base64_encode($options['auth_basic'])];
150149
}
151150
// Merge bearer with headers
152151
if (($options['auth_bearer'] ?? false) && !($options['normalized_headers']['authorization'] ?? false)) {
153-
$options['normalized_headers']['authorization'] = [$options['headers'][] = 'Authorization: Bearer '.$options['auth_bearer']];
152+
$options['normalized_headers']['authorization'] = ['Authorization: Bearer '.$options['auth_bearer']];
154153
}
155154

156155
unset($options['auth_basic'], $options['auth_bearer']);
@@ -172,6 +171,7 @@ private static function prepareRequest(?string $method, ?string $url, array $opt
172171
}
173172

174173
$options['max_duration'] = isset($options['max_duration']) ? (float) $options['max_duration'] : 0;
174+
$options['headers'] = array_merge(...array_values($options['normalized_headers']));
175175

176176
return [$url, $options];
177177
}

Tests/HttpClientTestCase.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -397,11 +397,22 @@ public function testNegativeTimeout()
397397
])->getStatusCode());
398398
}
399399

400+
public function testRedirectAfterPost()
401+
{
402+
$client = $this->getHttpClient(__FUNCTION__);
403+
404+
$response = $client->request('POST', 'http://localhost:8057/302/relative', [
405+
'body' => 'abc',
406+
]);
407+
408+
$this->assertSame(200, $response->getStatusCode());
409+
}
410+
400411
public function testNullBody()
401412
{
402-
$httpClient = $this->getHttpClient(__FUNCTION__);
413+
$client = $this->getHttpClient(__FUNCTION__);
403414

404-
$httpClient->request('POST', 'http://localhost:8057/post', [
415+
$client->request('POST', 'http://localhost:8057/post', [
405416
'body' => null,
406417
]);
407418

0 commit comments

Comments
 (0)