diff --git a/src/Symfony/Component/HttpClient/CachingHttpClient.php b/src/Symfony/Component/HttpClient/CachingHttpClient.php index 65426ef647437..5eab04973233d 100644 --- a/src/Symfony/Component/HttpClient/CachingHttpClient.php +++ b/src/Symfony/Component/HttpClient/CachingHttpClient.php @@ -77,17 +77,20 @@ public function request(string $method, string $url, array $options = []): Respo $request = Request::create($url, $method); $request->attributes->set('http_client_options', $options); - foreach ($options['headers'] as $name => $values) { + foreach ($options['normalized_headers'] as $name => $values) { if ('cookie' !== $name) { - $request->headers->set($name, $values); + foreach ($values as $value) { + $request->headers->set($name, substr($value, 2 + \strlen($name)), false); + } + continue; } foreach ($values as $cookies) { - foreach (explode('; ', $cookies) as $cookie) { + foreach (explode('; ', substr($cookies, \strlen('Cookie: '))) as $cookie) { if ('' !== $cookie) { $cookie = explode('=', $cookie, 2); - $request->cookies->set($cookie[0], $cookie[1] ?? null); + $request->cookies->set($cookie[0], $cookie[1] ?? ''); } } } diff --git a/src/Symfony/Component/HttpClient/HttpClientTrait.php b/src/Symfony/Component/HttpClient/HttpClientTrait.php index 9fc37f72992e4..3fd9814df68f0 100644 --- a/src/Symfony/Component/HttpClient/HttpClientTrait.php +++ b/src/Symfony/Component/HttpClient/HttpClientTrait.php @@ -196,10 +196,21 @@ private static function normalizeHeaders(array $headers): array $normalizedHeaders = []; foreach ($headers as $name => $values) { + if (\is_object($values) && method_exists('__toString')) { + $values = (string) $values; + } + if (\is_int($name)) { + if (!\is_string($values)) { + throw new InvalidArgumentException(sprintf('Invalid value for header "%s": expected string, %s given.', $name, \gettype($values))); + } [$name, $values] = explode(':', $values, 2); $values = [ltrim($values)]; } elseif (!is_iterable($values)) { + if (\is_object($values)) { + throw new InvalidArgumentException(sprintf('Invalid value for header "%s": expected string, %s given.', $name, \get_class($values))); + } + $values = (array) $values; } diff --git a/src/Symfony/Component/HttpClient/Tests/CachingHttpClientTest.php b/src/Symfony/Component/HttpClient/Tests/CachingHttpClientTest.php new file mode 100644 index 0000000000000..4d1ca6de17822 --- /dev/null +++ b/src/Symfony/Component/HttpClient/Tests/CachingHttpClientTest.php @@ -0,0 +1,42 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\HttpClient\Tests; + +use PHPUnit\Framework\TestCase; +use Symfony\Component\HttpClient\CachingHttpClient; +use Symfony\Component\HttpClient\MockHttpClient; +use Symfony\Component\HttpClient\Response\MockResponse; +use Symfony\Component\HttpKernel\HttpCache\Store; + +class CachingHttpClientTest extends TestCase +{ + public function testRequestHeaders() + { + $options = [ + 'headers' => [ + 'Application-Name' => 'test1234', + 'Test-Name-Header' => 'test12345', + ], + ]; + + $mockClient = new MockHttpClient(); + $store = new Store(sys_get_temp_dir().'/sf_http_cache'); + $client = new CachingHttpClient($mockClient, $store, $options); + + $response = $client->request('GET', 'http://example.com/foo-bar'); + + rmdir(sys_get_temp_dir().'/sf_http_cache'); + self::assertInstanceOf(MockResponse::class, $response); + self::assertSame($response->getRequestOptions()['normalized_headers']['application-name'][0], 'Application-Name: test1234'); + self::assertSame($response->getRequestOptions()['normalized_headers']['test-name-header'][0], 'Test-Name-Header: test12345'); + } +}
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: