Skip to content

Commit

Permalink
Added support for the new MaxHeaderValueLengthFormatter
Browse files Browse the repository at this point in the history
  • Loading branch information
Toflar committed Sep 27, 2018
1 parent 19e8057 commit 3affecb
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 1 deletion.
26 changes: 26 additions & 0 deletions Resources/doc/reference/configuration/tags.rst
Expand Up @@ -49,6 +49,32 @@ section of the tag configuration and ``@tag`` :ref:`annotations<tag>`.
tags:
expression_language: app.expression_language
``max_header_value_length``
---------------------

**type**: ``integer`` **default**: ``null``

By default, the generated response header will not be split into multiple headers.
This means that depending on the amount of tags generated in your application the
value of that header might become pretty long. This again might cause issues with
your webserver which usually come with a pre-defined maximum header value length and
will throw an exception if you exceed this. Using this configuration key you can
configure a maximum length **in bytes** which will split your value into multiple
headers. Note that you might update your proxy configuration because it needs
to be able to handle multiple headers instead of just one.

.. code-block:: yaml
# app/config/config.yml
fos_http_cache:
tags:
max_header_value_length: 4096
.. note::

4096 bytes is generally a good choice because it seems like most webservers have
a maximum value of 4 KB configured.

``strict``
----------

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -22,7 +22,7 @@
],
"require": {
"php": "^7.1",
"friendsofsymfony/http-cache": "^2.3",
"friendsofsymfony/http-cache": "^2.5",
"symfony/framework-bundle": "^2.8.18||^3.3||^4.0",
"symfony/http-foundation": "^2.8.18.13||^3.3||^4.0"
},
Expand Down
4 changes: 4 additions & 0 deletions src/DependencyInjection/Configuration.php
Expand Up @@ -655,6 +655,10 @@ private function addTagSection(ArrayNodeDefinition $rootNode)
->defaultNull()
->info('Character(s) to use to separate multiple tags. Defaults to " " for Varnish xkey or "," otherwise')
->end()
->scalarNode('max_header_value_length')
->defaultNull()
->info('If configured the tag header value will be split into multiple response headers of the same name (see "response_header" configuration key) that all do not exceed the configured "max_header_value_length" (recommended is 4KB = 4096) - configure in bytes.')
->end()
->arrayNode('rules')
->prototype('array')
->fixXmlConfig('tag')
Expand Down
8 changes: 8 additions & 0 deletions src/DependencyInjection/FOSHttpCacheExtension.php
Expand Up @@ -15,6 +15,7 @@
use FOS\HttpCache\ProxyClient\ProxyClient;
use FOS\HttpCache\ProxyClient\Varnish;
use FOS\HttpCache\SymfonyCache\KernelDispatcher;
use FOS\HttpCache\TagHeaderFormatter\MaxHeaderValueLengthFormatter;
use FOS\HttpCacheBundle\DependencyInjection\Compiler\HashGeneratorPass;
use FOS\HttpCacheBundle\Http\ResponseMatcher\ExpressionResponseMatcher;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
Expand Down Expand Up @@ -492,6 +493,13 @@ private function loadCacheTagging(ContainerBuilder $container, XmlFileLoader $lo
if (!empty($config['rules'])) {
$this->loadTagRules($container, $config['rules']);
}

if (null !== $config['max_header_value_length']) {
$container->register('fos_http_cache.tag_handler.max_header_value_length_header_formatter', MaxHeaderValueLengthFormatter::class)
->setDecoratedService('fos_http_cache.tag_handler.header_formatter')
->addArgument(new Reference('fos_http_cache.tag_handler.max_header_value_length_header_formatter.inner'))
->addArgument((int) $config['max_header_value_length']);
}
}

private function loadTest(ContainerBuilder $container, XmlFileLoader $loader, array $config)
Expand Down
2 changes: 2 additions & 0 deletions tests/Unit/DependencyInjection/ConfigurationTest.php
Expand Up @@ -132,6 +132,7 @@ public function testSupportsAllConfigFormats()
],
],
'separator' => ',',
'max_header_value_length' => null,
],
'invalidation' => [
'enabled' => 'auto',
Expand Down Expand Up @@ -689,6 +690,7 @@ private function getEmptyConfig()
'expression_language' => null,
'rules' => [],
'separator' => ',',
'max_header_value_length' => null,
],
'invalidation' => [
'enabled' => false,
Expand Down
21 changes: 21 additions & 0 deletions tests/Unit/DependencyInjection/FOSHttpCacheExtensionTest.php
Expand Up @@ -245,6 +245,27 @@ public function testConfigLoadTagRules()

$this->assertRequestMatcherCreated($container, ['_controller' => '^AcmeBundle:Default:index$']);
$this->assertListenerHasRule($container, 'fos_http_cache.event_listener.tag');
$this->assertFalse($container->hasDefinition('fos_http_cache.tag_handler.max_header_value_length_header_formatter'));
}


public function testConfigWithMaxHeaderLengthValueDecoratesTagService()
{
$config = $this->getBaseConfig() + [
'tags' => [
'max_header_value_length' => 2048,
],
];

$container = $this->createContainer();
$this->extension->load([$config], $container);

$this->assertTrue($container->hasDefinition('fos_http_cache.tag_handler.max_header_value_length_header_formatter'));
$definition = $container->getDefinition('fos_http_cache.tag_handler.max_header_value_length_header_formatter');

$this->assertSame('fos_http_cache.tag_handler.header_formatter', $definition->getDecoratedService()[0]);
$this->assertSame('fos_http_cache.tag_handler.max_header_value_length_header_formatter.inner', (string) $definition->getArgument(0));
$this->assertSame(2048, $definition->getArgument(1));
}

public function testConfigLoadInvalidatorRules()
Expand Down

0 comments on commit 3affecb

Please sign in to comment.