Skip to content

Commit fd0f86c

Browse files
committed
Added support for the new MaxHeaderValueLengthFormatter
1 parent 19e8057 commit fd0f86c

File tree

6 files changed

+61
-1
lines changed

6 files changed

+61
-1
lines changed

Resources/doc/reference/configuration/tags.rst

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,32 @@ section of the tag configuration and ``@tag`` :ref:`annotations<tag>`.
4949
tags:
5050
expression_language: app.expression_language
5151
52+
``max_header_value_length``
53+
---------------------
54+
55+
**type**: ``integer`` **default**: ``null``
56+
57+
By default, the generated response header will not be split into multiple headers.
58+
This means that depending on the amount of tags generated in your application the
59+
value of that header might become pretty long. This again might cause issues with
60+
your webserver which usually come with a pre-defined maximum header value length and
61+
will throw an exception if you exceed this. Using this configuration key you can
62+
configure a maximum length **in bytes** which will split your value into multiple
63+
headers. Note that you might update your proxy configuration because it needs
64+
to be able to handle multiple headers instead of just one.
65+
66+
.. code-block:: yaml
67+
68+
# app/config/config.yml
69+
fos_http_cache:
70+
tags:
71+
max_header_value_length: 4096
72+
73+
.. note::
74+
75+
4096 bytes is generally a good choice because it seems like most webservers have
76+
a maximum value of 4 KB configured.
77+
5278
``strict``
5379
----------
5480

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
],
2323
"require": {
2424
"php": "^7.1",
25-
"friendsofsymfony/http-cache": "^2.3",
25+
"friendsofsymfony/http-cache": "^2.5",
2626
"symfony/framework-bundle": "^2.8.18||^3.3||^4.0",
2727
"symfony/http-foundation": "^2.8.18.13||^3.3||^4.0"
2828
},

src/DependencyInjection/Configuration.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,10 @@ private function addTagSection(ArrayNodeDefinition $rootNode)
655655
->defaultNull()
656656
->info('Character(s) to use to separate multiple tags. Defaults to " " for Varnish xkey or "," otherwise')
657657
->end()
658+
->scalarNode('max_header_value_length')
659+
->defaultNull()
660+
->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.')
661+
->end()
658662
->arrayNode('rules')
659663
->prototype('array')
660664
->fixXmlConfig('tag')

src/DependencyInjection/FOSHttpCacheExtension.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use FOS\HttpCache\ProxyClient\ProxyClient;
1616
use FOS\HttpCache\ProxyClient\Varnish;
1717
use FOS\HttpCache\SymfonyCache\KernelDispatcher;
18+
use FOS\HttpCache\TagHeaderFormatter\MaxHeaderValueLengthFormatter;
1819
use FOS\HttpCacheBundle\DependencyInjection\Compiler\HashGeneratorPass;
1920
use FOS\HttpCacheBundle\Http\ResponseMatcher\ExpressionResponseMatcher;
2021
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
@@ -492,6 +493,13 @@ private function loadCacheTagging(ContainerBuilder $container, XmlFileLoader $lo
492493
if (!empty($config['rules'])) {
493494
$this->loadTagRules($container, $config['rules']);
494495
}
496+
497+
if (null !== $config['max_header_value_length']) {
498+
$container->register('fos_http_cache.tag_handler.max_header_value_length_header_formatter', MaxHeaderValueLengthFormatter::class)
499+
->setDecoratedService('fos_http_cache.tag_handler.header_formatter')
500+
->addArgument(new Reference('fos_http_cache.tag_handler.max_header_value_length_header_formatter.inner'))
501+
->addArgument((int) $config['max_header_value_length']);
502+
}
495503
}
496504

497505
private function loadTest(ContainerBuilder $container, XmlFileLoader $loader, array $config)

tests/Unit/DependencyInjection/ConfigurationTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ public function testSupportsAllConfigFormats()
132132
],
133133
],
134134
'separator' => ',',
135+
'max_header_value_length' => null,
135136
],
136137
'invalidation' => [
137138
'enabled' => 'auto',
@@ -689,6 +690,7 @@ private function getEmptyConfig()
689690
'expression_language' => null,
690691
'rules' => [],
691692
'separator' => ',',
693+
'max_header_value_length' => null,
692694
],
693695
'invalidation' => [
694696
'enabled' => false,

tests/Unit/DependencyInjection/FOSHttpCacheExtensionTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,26 @@ public function testConfigLoadTagRules()
245245

246246
$this->assertRequestMatcherCreated($container, ['_controller' => '^AcmeBundle:Default:index$']);
247247
$this->assertListenerHasRule($container, 'fos_http_cache.event_listener.tag');
248+
$this->assertFalse($container->hasDefinition('fos_http_cache.tag_handler.max_header_value_length_header_formatter'));
249+
}
250+
251+
public function testConfigWithMaxHeaderLengthValueDecoratesTagService()
252+
{
253+
$config = $this->getBaseConfig() + [
254+
'tags' => [
255+
'max_header_value_length' => 2048,
256+
],
257+
];
258+
259+
$container = $this->createContainer();
260+
$this->extension->load([$config], $container);
261+
262+
$this->assertTrue($container->hasDefinition('fos_http_cache.tag_handler.max_header_value_length_header_formatter'));
263+
$definition = $container->getDefinition('fos_http_cache.tag_handler.max_header_value_length_header_formatter');
264+
265+
$this->assertSame('fos_http_cache.tag_handler.header_formatter', $definition->getDecoratedService()[0]);
266+
$this->assertSame('fos_http_cache.tag_handler.max_header_value_length_header_formatter.inner', (string) $definition->getArgument(0));
267+
$this->assertSame(2048, $definition->getArgument(1));
248268
}
249269

250270
public function testConfigLoadInvalidatorRules()

0 commit comments

Comments
 (0)