Skip to content

Commit d427998

Browse files
Merge branch '6.0' into 6.1
* 6.0: 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 914c62a + 0a6d049 commit d427998

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
@@ -398,11 +398,22 @@ public function testNegativeTimeout()
398398
])->getStatusCode());
399399
}
400400

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

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

0 commit comments

Comments
 (0)