Skip to content

Commit 013b4a4

Browse files
michaljusieganicolas-grekas
authored andcommitted
[HttpClient] bugfix exploding values of headers
1 parent 26f4b1d commit 013b4a4

File tree

3 files changed

+60
-4
lines changed

3 files changed

+60
-4
lines changed

CachingHttpClient.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,20 @@ public function request(string $method, string $url, array $options = []): Respo
7777
$request = Request::create($url, $method);
7878
$request->attributes->set('http_client_options', $options);
7979

80-
foreach ($options['headers'] as $name => $values) {
80+
foreach ($options['normalized_headers'] as $name => $values) {
8181
if ('cookie' !== $name) {
82-
$request->headers->set($name, $values);
82+
foreach ($values as $value) {
83+
$request->headers->set($name, substr($value, 2 + \strlen($name)), false);
84+
}
85+
8386
continue;
8487
}
8588

8689
foreach ($values as $cookies) {
87-
foreach (explode('; ', $cookies) as $cookie) {
90+
foreach (explode('; ', substr($cookies, \strlen('Cookie: '))) as $cookie) {
8891
if ('' !== $cookie) {
8992
$cookie = explode('=', $cookie, 2);
90-
$request->cookies->set($cookie[0], $cookie[1] ?? null);
93+
$request->cookies->set($cookie[0], $cookie[1] ?? '');
9194
}
9295
}
9396
}

HttpClientTrait.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,21 @@ private static function normalizeHeaders(array $headers): array
196196
$normalizedHeaders = [];
197197

198198
foreach ($headers as $name => $values) {
199+
if (\is_object($values) && method_exists('__toString')) {
200+
$values = (string) $values;
201+
}
202+
199203
if (\is_int($name)) {
204+
if (!\is_string($values)) {
205+
throw new InvalidArgumentException(sprintf('Invalid value for header "%s": expected string, %s given.', $name, \gettype($values)));
206+
}
200207
[$name, $values] = explode(':', $values, 2);
201208
$values = [ltrim($values)];
202209
} elseif (!is_iterable($values)) {
210+
if (\is_object($values)) {
211+
throw new InvalidArgumentException(sprintf('Invalid value for header "%s": expected string, %s given.', $name, \get_class($values)));
212+
}
213+
203214
$values = (array) $values;
204215
}
205216

Tests/CachingHttpClientTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpClient\Tests;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\HttpClient\CachingHttpClient;
16+
use Symfony\Component\HttpClient\MockHttpClient;
17+
use Symfony\Component\HttpClient\Response\MockResponse;
18+
use Symfony\Component\HttpKernel\HttpCache\Store;
19+
20+
class CachingHttpClientTest extends TestCase
21+
{
22+
public function testRequestHeaders()
23+
{
24+
$options = [
25+
'headers' => [
26+
'Application-Name' => 'test1234',
27+
'Test-Name-Header' => 'test12345',
28+
],
29+
];
30+
31+
$mockClient = new MockHttpClient();
32+
$store = new Store(sys_get_temp_dir().'/sf_http_cache');
33+
$client = new CachingHttpClient($mockClient, $store, $options);
34+
35+
$response = $client->request('GET', 'http://example.com/foo-bar');
36+
37+
rmdir(sys_get_temp_dir().'/sf_http_cache');
38+
self::assertInstanceOf(MockResponse::class, $response);
39+
self::assertSame($response->getRequestOptions()['normalized_headers']['application-name'][0], 'Application-Name: test1234');
40+
self::assertSame($response->getRequestOptions()['normalized_headers']['test-name-header'][0], 'Test-Name-Header: test12345');
41+
}
42+
}

0 commit comments

Comments
 (0)