8000 bug #45813 [HttpClient] Move Content-Type after Content-Length (nicol… · symfony/symfony@13e0671 · GitHub
[go: up one dir, main page]

Skip to content

Commit 13e0671

Browse files
bug #45813 [HttpClient] Move Content-Type after Content-Length (nicolas-grekas)
This PR was merged into the 4.4 branch. Discussion ---------- [HttpClient] Move Content-Type after Content-Length | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - Some nitpicking found after opening php-src. See https://bugs.php.net/44603 Commits ------- b87868a [HttpClient] Move Content-Type after Content-Length
2 parents 01f6749 + b87868a commit 13e0671

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

src/Symfony/Component/HttpClient/HttpClientTrait.php

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

7878
if (!isset($options['normalized_headers']['content-type'])) {
79-
$options['normalized_headers']['content-type'] = [$options['headers'][] = 'Content-Type: application/json'];
79+
$options['normalized_headers']['content-type'] = ['Content-Type: application/json'];
8080
}
8181
}
8282

8383
if (!isset($options['normalized_headers']['accept'])) {
84-
$options['normalized_headers']['accept'] = [$options['headers'][] = 'Accept: */*'];
84+
$options['normalized_headers']['accept'] = ['Accept: */*'];
8585
}
8686

8787
if (isset($options['body'])) {
@@ -92,7 +92,6 @@ private static function prepareRequest(?string $method, ?string $url, array $opt
9292
&& ('' !== $h || ('' !== $options['body'] && !isset($options['normalized_headers']['transfer-encoding'])))
9393
) {
9494
$options['normalized_headers']['content-length'] = [substr_replace($h ?: 'Content-Length: ', \strlen($options['body']), 16)];
95-
$options['headers'] = array_merge(...array_values($options['normalized_headers']));
9695
}
9796
}
9897

@@ -134,11 +133,11 @@ private static function prepareRequest(?string $method, ?string $url, array $opt
134133
if (null !== $url) {
135134
// Merge auth with headers
136135
if (($options['auth_basic'] ?? false) && !($options['normalized_headers']['authorization'] ?? false)) {
137-
$options['normalized_headers']['authorization'] = [$options['headers'][] = 'Authorization: Basic '.base64_encode($options['auth_basic'])];
136+
$options['normalized_headers']['authorization'] = ['Authorization: Basic '.base64_encode($options['auth_basic'])];
138137
}
139138
// Merge bearer with headers
140139
if (($options['auth_bearer'] ?? false) && !($options['normalized_headers']['authorization'] ?? false)) {
141-
$options['normalized_headers']['authorization'] = [$options['headers'][] = 'Authorization: Bearer '.$options['auth_bearer']];
140+
$options['normalized_headers']['authorization'] = ['Authorization: Bearer '.$options['auth_bearer']];
142141
}
143142

144143
unset($options['auth_basic'], $options['auth_bearer']);
@@ -161,6 +160,14 @@ private static function prepareRequest(?string $method, ?string $url, array $opt
161160

162161
$options['max_duration'] = isset($options['max_duration']) ? (float) $options['max_duration'] : 0;
163162

163+
if (isset($options['normalized_headers']['content-length']) && $contentType = $options['normalized_headers']['content-type'] ?? null) {
164+
// Move Content-Type after Content-Length, see https://bugs.php.net/44603
165+
unset($options['normalized_headers']['content-type']);
166+
$options['normalized_headers']['content-type'] = $contentType;
167+
}
168+
169+
$options['headers'] = array_merge(...array_values($options['normalized_headers']));
170+
164171
return [$url, $options];
165172
}
166173

src/Symfony/Component/HttpClient/Tests/ScopingHttpClientTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function testMatchingUrlsAndOptions()
7373

7474
$response = $client->request('GET', 'http://example.com/foo-bar', ['json' => ['url' => 'http://example.com']]);
7575
$requestOptions = $response->getRequestOptions();
76-
$this->assertSame('Content-Type: application/json', $requestOptions['headers'][1]);
76+
$this->assertSame('Content-Type: application/json', $requestOptions['headers'][3]);
7777
$requestJson = json_decode($requestOptions['body'], true);
7878
$this->assertSame('http://example.com', $requestJson['url']);
7979
$this->assertSame('X-FooBar: '.$defaultOptions['.*/foo-bar']['headers']['X-FooBar'], $requestOptions['headers'][0]);

0 commit comments

Comments
 (0)
0