8000 [FrameworkBundle] change the way http clients are configured by leveraging ScopingHttpClient by nicolas-grekas · Pull Request #30674 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[FrameworkBundle] change the way http clients are configured by leveraging ScopingHttpClient #30674

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
343 changes: 216 additions & 127 deletions src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@
use Symfony\Component\Form\FormTypeGuesserInterface;
use Symfony\Component\Form\FormTypeInterface;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Component\HttpClient\HttpClientTrait;
use Symfony\Component\HttpClient\Psr18Client;
use Symfony\Component\HttpClient\ScopingHttpClient;
use Symfony\Component\HttpKernel\CacheClearer\CacheClearerInterface;
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
use Symfony\Component\HttpKernel\Controller\ArgumentValueResolverInterface;
Expand Down Expand Up @@ -117,7 +117,6 @@
use Symfony\Component\Yaml\Command\LintCommand as BaseYamlLintCommand;
use Symfony\Component\Yaml\Yaml;
use Symfony\Contracts\Cache\CacheInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\Service\ResetInterface;
use Symfony\Contracts\Service\ServiceSubscriberInterface;
Expand Down Expand Up @@ -1803,42 +1802,23 @@ private function registerHttpClientConfiguration(array $config, ContainerBuilder

$loader->load('http_client.xml');

$merger = new class() {
use HttpClientTrait;

public function merge(array $options, array $defaultOptions)
{
try {
[, $mergedOptions] = $this->prepareRequest(null, null, $options, $defaultOptions);

foreach ($mergedOptions as $k => $v) {
if (!isset($options[$k]) && !isset($defaultOptions[$k])) {
// Remove options added by prepareRequest()
unset($mergedOptions[$k]);
}
}

return $mergedOptions;
} catch (TransportExceptionInterface $e) {
throw new InvalidArgumentException($e->getMessage(), 0, $e);
}
}
};

$defaultOptions = $merger->merge($config['default_options'] ?? [], []);
$container->getDefinition('http_client')->setArguments([$defaultOptions, $config['max_host_connections'] ?? 6]);
$container->getDefinition('http_client')->setArguments([$config['default_options'] ?? [], $config['max_host_connections'] ?? 6]);

if (!$hasPsr18 = interface_exists(ClientInterface::class)) {
$container->removeDefinition('psr18.http_client');
$container->removeAlias(ClientInterface::class);
}

foreach ($config['clients'] as $name => $clientConfig) {
$options = $merger->merge($clientConfig['default_options'] ?? [], $defaultOptions);
foreach ($config['scoped_clients'] as $name => $scopeConfig) {
if ('http_client' === $name) {
throw new InvalidArgumentException(sprintf('Invalid scope name: "%s" is reserved.', $name));
}

$scope = $scopeConfig['scope'];
unset($scopeConfig['scope']);

$container->register($name, HttpClientInterface::class)
->setFactory([HttpClient::class, 'create'])
->setArguments([$options, $clientConfig['max_host_connections'] ?? $config['max_host_connections'] ?? 6]);
$container->register($name, ScopingHttpClient::class)
->setArguments([new Reference('http_client'), [$scope => $scopeConfig], $scope]);

$container->registerAliasForArgument($name, HttpClientInterface::class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -458,30 +458,55 @@

<xsd:complexType name="http_client">
<xsd:sequence>
<xsd:element name="default-options" type="http_client_options" minOccurs="0" />
<xsd:element name="client" type="http_client_client" minOccurs="0" maxOccurs="unbounded" />
<xsd:element name="default-options" type="http_client_default_options" minOccurs="0" />
<xsd:element name="scoped-client" type="http_client_scope_options" minOccurs="0" maxOccurs="unbounded" />
</xsd:sequence>
<xsd:attribute name="enabled" type="xsd:boolean" />
<xsd:attribute name="max-host-connections" type="xsd:integer" />
</xsd:complexType>

<xsd:complexType name="http_client_options" mixed="true">
<xsd:complexType name="http_client_default_options" mixed="true">
<xsd:choice maxOccurs="unbounded">
<xsd:element name="query" type="http_query" minOccurs="0" maxOccurs="unbounded" />
<xsd:element name="resolve" type="http_resolve" minOccurs="0" maxOccurs="unbounded" />
<xsd:element name="header" type="http_header" minOccurs="0" maxOccurs="unbounded" />
<xsd:element name="peer-fingerprint" type="fingerprint" minOccurs="0" maxOccurs="unbounded" />
</xsd:choice>
<xsd:attribute name="max-redirects" type="xsd:integer" />
<xsd:attribute name="http-version" type="xsd:string" />
<xsd:attribute name="proxy" type="xsd:string" />
<xsd:attribute name="no-proxy" type="xsd:string" />
<xsd:attribute name="timeout" type="xsd:float" />
<xsd:attribute name="bindto" type="xsd:string" />
<xsd:attribute name="verify-peer" type="xsd:boolean" />
<xsd:attribute name="verify-host" type="xsd:boolean" />
<xsd:attribute name="cafile" type="xsd:string" />
<xsd:attribute name="capath" type="xsd:string" />
<xsd:attribute name="local-cert" type="xsd:string" />
<xsd:attribute name="local-pk" type="xsd:string" />
<xsd:attribute name="passphrase" type="xsd:string" />
<xsd:attribute name="ciphers" type="xsd:string" />

</xsd:complexType>

<xsd:complexType name="http_client_scope_options" mixed="true">
<xsd:choice maxOccurs="unbounded">
<xsd:element name="query" type="http_query" minOccurs="0" maxOccurs="unbounded" />
<xsd:element name="resolve" type="http_resolve" minOccurs="0" maxOccurs="unbounded" />
<xsd:element name="header" type="http_header" minOccurs="0" maxOccurs="unbounded" />
<xsd:element name="peer-fingerprint" type="fingerprint" minOccurs="0" maxOccurs="unbounded" />
</xsd:choice>
<xsd:attribute name="name" type="xsd:string" />
<xsd:attribute name="scope" type="xsd:string" />
<xsd:attribute name="base-uri" type="xsd:string" />
<xsd:attribute name="auth-basic" type="xsd:string" />
<xsd:attribute name="auth-bearer" type="xsd:string" />
<xsd:attribute name="max-redirects" type="xsd:integer" />
<xsd:attribute name="http-version" type="xsd:string" />
<xsd:attribute name="base-uri" type="xsd:string" />
<xsd:attribute name="proxy" type="xsd:string" />
<xsd:attribute name="no-proxy" type="xsd:string" />
<xsd:attribute name="timeout" type="xsd:float" />
<xsd:attribute name="bindto" type="xsd:string" />
<xsd:attribute name="verify-peer" type="xsd:boolean" />
<xsd:attribute name="verify-host" type="xsd:boolean" />
<xsd:attribute name="cafile" type="xsd:string" />
<xsd:attribute name="capath" type="xsd:string" />
Expand All @@ -491,14 +516,6 @@
<xsd:attribute name="ciphers" type="xsd:string" />
</xsd:complexType>

<xsd:complexType name="http_client_client">
<xsd:sequence>
<xsd:element name="default-options" type="http_client_options" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" />
<xsd:attribute name="max-host-connections" type="xsd:integer" />
</xsd:complexType>

<xsd:complexType name="fingerprint">
<xsd:choice maxOccurs="unbounded">
<xsd:element name="pin-sha256" type="xsd:string" minOccurs="0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ class_exists(SemaphoreStore::class) && SemaphoreStore::isSupported() ? 'semaphor
'disallow_search_engine_index' => true,
'http_client' => [
'enabled' => !class_exists(FullStack::class) && class_exists(HttpClient::class),
'clients' => [],
'scoped_clients' => [],
],
'mailer' => [
'dsn' => 'smtp://null',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
'http_client' => [
'max_host_connections' => 4,
'default_options' => null,
'clients' => [
'scoped_clients' => [
'foo' => [
'default_options' => null,
'base_uri' => 'http://example.com',
],
],
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@
$container->loadFromExtension('framework', [
'http_client' => [
'default_options' => [
'auth_basic' => 'foo:bar',
'query' => ['foo' => 'bar', 'bar' => 'baz'],
'headers' => ['X-powered' => 'PHP'],
'max_redirects' => 2,
'http_version' => '2.0',
'base_uri' => 'http://example.com',
'resolve' => ['localhost' => '127.0.0.1'],
'proxy' => 'proxy.org',
'timeout' => 3.5,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@
'default_options' => [
'headers' => ['foo' => 'bar'],
],
'clients' => [
'scoped_clients' => [
'foo' => [
'max_host_connections' => 5,
'default_options' => [
'headers' => ['bar' => 'baz'],
],
'base_uri' => 'http://example.com',
'headers' => ['bar' => 'baz'],
],
],
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
<framework:config>
<framework:http-client max-host-connections="4">
<framework:default-options />
<framework:client name="foo">
<framework:default-options />
</framework:client>
<framework:scoped-client
name="foo"
base-uri="http://example.com"
/>
</framework:http-client>
</framework:config>
</container>
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@
bindto="127.0.0.1"
timeout="3.5"
verify-peer="true"
auth-basic="foo:bar"
max-redirects="2"
http-version="2.0"
base-uri="http://example.com"
verify-host="true"
cafile="/etc/ssl/cafile"
capath="/etc/ssl"
Expand All @@ -24,8 +22,6 @@
passphrase="password123456"
ciphers="RC4-SHA:TLS13-AES-128-GCM-SHA256"
>
<framework:query key="foo">bar</framework:query>
<framework:query key="bar">baz</framework:query>
<framework:header name="X-powered">PHP</framework:header>
<framework:resolve host="localhost">127.0.0.1</framework:resolve>
<framework:peer-fingerprint>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@
<framework:default-options>
<framework:header name="foo">bar</framework:header>
</framework:default-options>
<framework:client name="foo" max-host-connections="5">
<framework:default-options>
<framework:header name="bar">baz</framework:header>
</framework:default-options>
</framework:client>
<framework:scoped-client name="foo" base-uri="http://example.com">
<framework:header name="bar">baz</framework:header>
</framework:scoped-client>
</framework:http-client>
</framework:config>
</container>
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ framework:
http_client:
max_host_connections: 4
default_options: ~
clients:
scoped_clients:
foo:
default_options: ~
base_uri: http://example.com
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
framework:
http_client:
default_options:
auth_basic: foo:bar
query: {'foo': 'bar', 'bar': 'baz'}
headers:
X-powered: PHP
max_redirects: 2
http_version: 2.0
base_uri: 'http://example.com'
resolve: {'localhost': '127.0.0.1'}
proxy: proxy.org
timeout: 3.5
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ framework:
max_host_connections: 4
default_options:
headers: {'foo': 'bar'}
clients:
scoped_clients:
foo:
max_host_connections: 5
default_options:
headers: {'bar': 'baz'}
base_uri: http://example.com
headers: {'bar': 'baz'}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpClient\ScopingHttpClient;
use Symfony\Component\HttpKernel\DependencyInjection\LoggerPass;
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage;
use Symfony\Component\Messenger\Tests\Fixtures\SecondMessage;
Expand All @@ -53,7 +54,6 @@
use Symfony\Component\Validator\Mapping\Loader\PropertyInfoLoader;
use Symfony\Component\Validator\Validation;
use Symfony\Component\Workflow;
use Symfony\Contracts\HttpClient\HttpClientInterface;

abstract class FrameworkExtensionTest extends TestCase
{
Expand Down Expand Up @@ -1406,25 +1406,34 @@ public function testHttpClientDefaultOptions()
$this->assertTrue($container->hasDefinition('http_client'), '->registerHttpClientConfiguration() loads http_client.xml');

$defaultOptions = [
'query' => [],
'headers' => [],
'resolve' => [],
];
$this->assertSame([$defaultOptions, 4], $container->getDefinition('http_client')->getArguments());

$this->assertTrue($container->hasDefinition('foo'), 'should have the "foo" service.');
$this->assertSame(HttpClientInterface::class, $container->getDefinition('foo')->getClass());
$this->assertSame([$defaultOptions, 4], $container->getDefinition('foo')->getArguments());
$this->assertSame(ScopingHttpClient::class, $container->getDefinition('foo')->getClass());
}

public function testHttpClientOverrideDefaultOptions()
{
$container = $this->createContainerFromFile('http_client_override_default_options');

$this->assertSame(['foo' => ['bar']], $container->getDefinition('http_client')->getArgument(0)['headers']);
$this->assertSame(['foo' => 'bar'], $container->getDefinition('http_client')->getArgument(0)['headers']);
$this->assertSame(4, $container->getDefinition('http_client')->getArgument(1));
$this->assertSame(['bar' => ['baz'], 'foo' => ['bar']], $container->getDefinition('foo')->getArgument(0)['headers']);
$this->assertSame(5, $container->getDefinition('foo')->getArgument(1));

$expected = [
'http\://example\.com/' => [
'base_uri' => 'http://example.com',
'headers' => [
'bar' => 'baz',
],
'query' => [],
'resolve' => [],
],
];

$this->assertSame($expected, $container->getDefinition('foo')->getArgument(1));
}

public function testHttpClientFullDefaultOptions()
Expand All @@ -1433,12 +1442,9 @@ public function testHttpClientFullDefaultOptions()

$defaultOptions = $container->getDefinition('http_client')->getArgument(0);

$this->assertSame('foo:bar', $defaultOptions['auth_basic']);
$this->assertSame(['foo' => 'bar', 'bar' => 'baz'], $defaultOptions['query']);
$this->assertSame(['x-powered' => ['PHP']], $defaultOptions['headers']);
$this->assertSame(['X-powered' => 'PHP'], $defaultOptions['headers']);
$this->assertSame(2, $defaultOptions['max_redirects']);
$this->assertSame(2.0, (float) $defaultOptions['http_version']);
$this->assertSame('http://example.com', $defaultOptions['base_uri']);
$this->assertSame(['localhost' => '127.0.0.1'], $defaultOptions['resolve']);
$this->assertSame('proxy.org', $defaultOptions['proxy']);
$this->assertSame(3.5, $defaultOptions['timeout']);
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/HttpClient/Response/MockResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ private static function readResponse(self $response, array $options, ResponseInt
$info = $mock->getInfo() ?: [];
$response->info['http_code'] = ($info['http_code'] ?? 0) ?: $mock->getStatusCode(false) ?: 200;
$response->addResponseHeaders($info['response_headers'] ?? [], $response->info, $response->headers);
$dlSize = (int) ($response->headers['content-length'][0] ?? 0);
$dlSize = isset($response->headers['content-encoding']) ? 0 : (int) ($response->headers['content-length'][0] ?? 0);

$response->info = [
'start_time' => $response->info['start_time'],
Expand Down Expand Up @@ -282,7 +282,7 @@ private static function readResponse(self $response, array $options, ResponseInt
// "notify" completion
$onProgress($offset, $dlSize, $response->info);

if (isset($response->headers['content-length']) && $offset !== $dlSize) {
if ($dlSize && $offset !== $dlSize) {
throw new TransportException(sprintf('Transfer closed with %s bytes remaining to read.', $dlSize - $offset));
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/Symfony/Component/HttpFoundation/ResponseHeaderBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,7 @@ public function set($key, $values, $replace = true)
parent::set($key, $values, $replace);

// ensure the cache-control header has sensible defaults
if (\in_array($uniqueKey, ['cache-control', 'etag', 'last-modified', 'expires'], true)) {
$computed = $this->computeCacheControlValue();
if (\in_array($uniqueKey, ['cache-control', 'etag', 'last-modified', 'expires'], true) && '' !== $computed = $this->computeCacheControlValue()) {
$this->headers['cache-control'] = [$computed];
$this->headerNames['cache-control'] = 'Cache-Control';
$this->computedCacheControl = $this->parseCacheControl($computed);
Expand Down
Loading
0