Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bee0056

Browse files
bug #33871 [HttpClient] bugfix exploding values of headers (michaljusiega)
This PR was squashed before being merged into the 4.3 branch (closes #33871). Discussion ---------- [HttpClient] bugfix exploding values of headers | Q | A | ------------- | --- | Branch? | 4.3 for bug fixes | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - I tried to use CachingHttpClient yesterday and I received an error. ``` explode() expects parameter 2 to be string, array given C:\PROJEKTY\PHPStorm\symfony\src\Symfony\Component\HttpClient\HttpClientTrait.php:200 C:\PROJEKTY\PHPStorm\symfony\src\Symfony\Component\HttpClient\HttpClientTrait.php:131 C:\PROJEKTY\PHPStorm\symfony\src\Symfony\Component\HttpClient\HttpClientTrait.php:45 C:\PROJEKTY\PHPStorm\symfony\src\Symfony\Component\HttpClient\CurlHttpClient.php:105 C:\PROJEKTY\PHPStorm\symfony\src\Symfony\Component\HttpKernel\HttpClientKernel.php:54 C:\PROJEKTY\PHPStorm\symfony\src\Symfony\Component\HttpKernel\HttpCache\SubRequestHandler.php:85 C:\PROJEKTY\PHPStorm\symfony\src\Symfony\Component\HttpKernel\HttpCache\HttpCache.php:477 C:\PROJEKTY\PHPStorm\symfony\src\Symfony\Component\HttpKernel\HttpCache\HttpCache.php:450 C:\PROJEKTY\PHPStorm\symfony\src\Symfony\Component\HttpKernel\HttpCache\HttpCache.php:347 C:\PROJEKTY\PHPStorm\symfony\src\Symfony\Component\HttpKernel\HttpCache\HttpCache.php:222 C:\PROJEKTY\PHPStorm\symfony\src\Symfony\Component\HttpClient\CachingHttpClient.php:96 C:\PROJEKTY\PHPStorm\symfony\src\Symfony\Component\HttpClient\Tests\CachingHttpClientTest.php:34 ``` This PR fix this. Commits ------- 5cd8895 [HttpClient] bugfix exploding values of headers
2 parents b92d944 + 5cd8895 commit bee0056

File tree

3 files changed

+60
-4
lines changed

3 files changed

+60
-4
lines changed

src/Symfony/Component/HttpClient/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
}

src/Symfony/Component/HttpClient/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

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)