Skip to content

Commit c0ef6e6

Browse files
[HttpClient] use ucwords() on header names before sending them
1 parent 85827f3 commit c0ef6e6

File tree

3 files changed

+9
-9
lines changed

3 files changed

+9
-9
lines changed

src/Symfony/Component/HttpClient/HttpClientTrait.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ private static function prepareRequest(?string $method, ?string $url, array $opt
6666

6767
foreach ($options['headers'] as $name => $values) {
6868
foreach ($values as $value) {
69-
$requestHeaders[] = $name.': '.$headers[$name][] = $value = (string) $value;
69+
$requestHeaders[] = ucwords($name, '-').': '.$headers[$name][] = $value = (string) $value;
7070

7171
if (\strlen($value) !== strcspn($value, "\r\n\0")) {
7272
throw new InvalidArgumentException(sprintf('Invalid header value: CR/LF/NUL found in "%s".', $value));
@@ -103,11 +103,11 @@ private static function prepareRequest(?string $method, ?string $url, array $opt
103103
if (null !== $url) {
104104
// Merge auth with headers
105105
if (($options['auth_basic'] ?? false) && !($headers['authorization'] ?? false)) {
106-
$requestHeaders[] = 'authorization: '.$headers['authorization'][] = 'Basic '.base64_encode($options['auth_basic']);
106+
$requestHeaders[] = 'Authorization: '.$headers['authorization'][] = 'Basic '.base64_encode($options['auth_basic']);
107107
}
108108
// Merge bearer with headers
109109
if (($options['auth_bearer'] ?? false) && !($headers['authorization'] ?? false)) {
110-
$requestHeaders[] = 'authorization: '.$headers['authorization'][] = 'Bearer '.$options['auth_bearer'];
110+
$requestHeaders[] = 'Authorization: '.$headers['authorization'][] = 'Bearer '.$options['auth_bearer'];
111111
}
112112

113113
$options['request_headers'] = $requestHeaders;

src/Symfony/Component/HttpClient/NativeHttpClient.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,12 @@ public function request(string $method, string $url, array $options = []): Respo
7474
$options['body'] = self::getBodyAsString($options['body']);
7575

7676
if ('' !== $options['body'] && 'POST' === $method && !isset($options['headers']['content-type'])) {
77-
$options['request_headers'][] = 'content-type: application/x-www-form-urlencoded';
77+
$options['request_headers'][] = 'Content-Type: application/x-www-form-urlencoded';
7878
}
7979

8080
if ($gzipEnabled = \extension_loaded('zlib') && !isset($options['headers']['accept-encoding'])) {
8181
// gzip is the most widely available algo, no need to deal with deflate
82-
$options['request_headers'][] = 'accept-encoding: gzip';
82+
$options['request_headers'][] = 'Accept-Encoding: gzip';
8383
}
8484

8585
if ($options['peer_fingerprint']) {
@@ -161,11 +161,11 @@ public function request(string $method, string $url, array $options = []): Respo
161161
[$host, $port, $url['authority']] = self::dnsResolve($url, $this->multi, $info, $onProgress);
162162

163163
if (!isset($options['headers']['host'])) {
164-
$options['request_headers'][] = 'host: '.$host.$port;
164+
$options['request_headers'][] = 'Host: '.$host.$port;
165165
}
166166

167167
if (!isset($options['headers']['user-agent'])) {
168-
$options['request_headers'][] = 'user-agent: Symfony HttpClient/Native';
168+
$options['request_headers'][] = 'User-Agent: Symfony HttpClient/Native';
169169
}
170170

171171
$context = [
@@ -393,7 +393,7 @@ private static function createRedirectResolver(array $options, string $host, ?ar
393393
if (false !== (parse_url($location, PHP_URL_HOST) ?? false)) {
394394
// Authorization and Cookie headers MUST NOT follow except for the initial host name
395395
$requestHeaders = $redirectHeaders['host'] === $host ? $redirectHeaders['with_auth'] : $redirectHeaders['no_auth'];
396-
$requestHeaders[] = 'host: '.$host.$port;
396+
$requestHeaders[] = 'Host: '.$host.$port;
397397
self::configureHeadersAndProxy($context, $host, $requestHeaders, $proxy, $noProxy);
398398
}
399399

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ public function testAuthBearerOption()
173173
{
174174
[, $options] = self::prepareRequest('POST', 'http://example.com', ['auth_bearer' => 'foobar'], HttpClientInterface::OPTIONS_DEFAULTS);
175175
$this->assertSame('Bearer foobar', $options['headers']['authorization'][0]);
176-
$this->assertSame('authorization: Bearer foobar', $options['request_headers'][0]);
176+
$this->assertSame('Authorization: Bearer foobar', $options['request_headers'][0]);
177177
}
178178

179179
/**

0 commit comments

Comments
 (0)