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

Skip to content

Commit 8223b4d

Browse files
author
Anthony MARTIN
committed
[HttpClient] Add a ConditionalHttpClient
1 parent 59e6380 commit 8223b4d

File tree

4 files changed

+128
-4
lines changed

4 files changed

+128
-4
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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;
13+
14+
use Symfony\Contracts\HttpClient\HttpClientInterface;
15+
use Symfony\Contracts\HttpClient\ResponseInterface;
16+
use Symfony\Contracts\HttpClient\ResponseStreamInterface;
17+
18+
/**
19+
* @author Anthony Martin <[email protected]>
20+
*
21+
* @experimental in 4.3
22+
*/
23+
class ConditionalHttpClient implements HttpClientInterface
24+
{
25+
private $options;
26+
27+
private $client;
28+
29+
/**
30+
* @param HttpClientInterface $client
31+
* @param array $options should contain an array with regexp as key to filter the url and an array of options as value to be given for the HttpClient request() function
32+
*/
33+
public function __construct(HttpClientInterface $client, array $options)
34+
{
35+
$this->client = $client;
36+
$this->options = $options;
37+
}
38+
39+
/**
40+
* {@inheritdoc}
41+
*/
42+
public function request(string $method, string $url, array $options = []): ResponseInterface
43+
{
44+
foreach ($this->options as $regexp => $clientOptions) {
45+
if (preg_match($regexp, $url)) {
46+
return $this->client->request($method, $url, array_merge($clientOptions, $options));
47+
}
48+
}
49+
50+
return $this->client->request($method, $url, $options);
51+
}
52+
53+
/**
54+
* {@inheritdoc}
55+
*/
56+
public function stream($responses, float $timeout = null): ResponseStreamInterface
57+
{
58+
return $this->client->stream($responses, $timeout);
59+
}
60+
}

src/Symfony/Component/HttpClient/CurlHttpClient.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ public function __construct(array $defaultOptions = [], int $maxHostConnections
6464
'dnsCache' => [[], [], []],
6565
];
6666

67-
// Skip configuring HTTP/2 push when it's unsupported or buggy, see https://bugs.php.net/76675
68-
if (\PHP_VERSION_ID < 70215 || \PHP_VERSION_ID === 70300 || \PHP_VERSION_ID === 70301) {
67+
// Skip configuring HTTP/2 push when it's unsupported or buggy, see https://bugs.php.net/76675 and https://bugs.php.net/bug.php?id=77535
68+
if (\PHP_VERSION_ID < 70217 || (\PHP_VERSION_ID >= 70300 && \PHP_VERSION_ID <= 70303)) {
6969
return;
7070
}
7171

@@ -74,8 +74,7 @@ public function __construct(array $defaultOptions = [], int $maxHostConnections
7474
return;
7575
}
7676

77-
// Keep a dummy "onPush" reference to work around a refcount bug in PHP
78-
curl_multi_setopt($mh, CURLMOPT_PUSHFUNCTION, $multi->onPush = static function ($parent, $pushed, array $rawHeaders) use ($multi) {
77+
curl_multi_setopt($mh, CURLMOPT_PUSHFUNCTION, static function ($parent, $pushed, array $rawHeaders) use ($multi) {
7978
return self::handlePush($parent, $pushed, $rawHeaders, $multi);
8079
});
8180
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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 Symfony\Component\HttpClient\ConditionalHttpClient;
15+
use Symfony\Component\HttpClient\CurlHttpClient;
16+
use Symfony\Contracts\HttpClient\HttpClientInterface;
17+
use Symfony\Contracts\HttpClient\Test\HttpClientTestCase;
18+
19+
/**
20+
* @requires extension curl
21+
*/
22+
class CurlConditionalHttpClientTest extends HttpClientTestCase
23+
{
24+
protected function getHttpClient(): HttpClientInterface
25+
{
26+
return new ConditionalHttpClient(new CurlHttpClient(), [
27+
'#^.*length-broken$#' => ['headers' => [
28+
'ConditionalHttpClient' => 'CurlHttpClient',
29+
'ConditionalHttpClient2' => 'url:length-broken',
30+
]],
31+
'#^.*$#' => ['headers' => ['ConditionalHttpClient' => 'CurlHttpClient']],
32+
]);
33+
}
34+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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 Symfony\Component\HttpClient\ConditionalHttpClient;
15+
use Symfony\Component\HttpClient\NativeHttpClient;
16+
use Symfony\Contracts\HttpClient\HttpClientInterface;
17+
use Symfony\Contracts\HttpClient\Test\HttpClientTestCase;
18+
19+
abstract class NativeConditionalHttpClientTest extends HttpClientTestCase
20+
{
21+
protected function getHttpClient(): HttpClientInterface
22+
{
23+
return new ConditionalHttpClient(new NativeHttpClient(), [
24+
'#^.*length-broken$#' => ['headers' => [
25+
'ConditionalHttpClient' => 'NativeHttpClient',
26+
'ConditionalHttpClient2' => 'url:length-broken',
27+
]],
28+
'#^.*$#' => ['headers' => ['ConditionalHttpClient' => 'NativeHttpClient']],
29+
]);
30+
}
31+
}

0 commit comments

Comments
 (0)