diff --git a/CHANGELOG.md b/CHANGELOG.md index 8bbd4bb0..71ee61b9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,13 @@ Changelog 3.x === +3.1.0 +----- + +### Added + +* New Feature: allow configuring the TTL header name for the special `reverse_proxy_ttl` config value. #638 + 3.0.2 ----- diff --git a/Resources/doc/reference/configuration/headers.rst b/Resources/doc/reference/configuration/headers.rst index cb07df52..fa7754c3 100644 --- a/Resources/doc/reference/configuration/headers.rst +++ b/Resources/doc/reference/configuration/headers.rst @@ -345,3 +345,26 @@ section: s_maxage: 60 This example adds the header ``X-Reverse-Proxy-TTL: 3600`` to your responses. + +``ttl_header`` +-------------- + +**type**: ``string`` default `X-Reverse-Proxy-TTL` + +Change the name for the header for reverse proxy. + +.. code-block:: yaml + + # app/config/config.yml + fos_http_cache: + cache_control: + ttl_header: X-My-Cache-Control + rules: + - + headers: + reverse_proxy_ttl: 3600 + cache_control: + public: true + s_maxage: 60 + +This example adds the header ``X-My-Cache-Control: 3600`` to your responses. diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 3fb9ffdc..07f183bc 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -277,6 +277,10 @@ private function addCacheControlSection(ArrayNodeDefinition $rootNode): void ->end() ->end() ->end() + ->scalarNode('ttl_header') + ->defaultValue('X-Reverse-Proxy-TTL') + ->info('Specify the header name to use with the cache_control.reverse_proxy_ttl setting') + ->end() ->arrayNode('rules') ->prototype('array') ->children(); @@ -330,7 +334,7 @@ private function addCacheControlSection(ArrayNodeDefinition $rootNode): void ->end() ->scalarNode('reverse_proxy_ttl') ->defaultNull() - ->info('Specify an X-Reverse-Proxy-TTL header with a time in seconds for a caching proxy under your control.') + ->info('Specify a custom time to live in seconds for your caching proxy. This value is sent in the custom header configured in cache_control.ttl_header.') ->end() ->arrayNode('vary') ->beforeNormalization()->ifString()->then(function ($v) { diff --git a/src/DependencyInjection/FOSHttpCacheExtension.php b/src/DependencyInjection/FOSHttpCacheExtension.php index 61ae698d..1528857c 100644 --- a/src/DependencyInjection/FOSHttpCacheExtension.php +++ b/src/DependencyInjection/FOSHttpCacheExtension.php @@ -48,7 +48,9 @@ public function load(array $configs, ContainerBuilder $container): void if ($config['debug']['enabled'] || (!empty($config['cache_control']))) { $debugHeader = $config['debug']['enabled'] ? $config['debug']['header'] : false; + $ttlHeader = $config['cache_control']['ttl_header']; $container->setParameter('fos_http_cache.debug_header', $debugHeader); + $container->setParameter('fos_http_cache.ttl_header', $ttlHeader); $loader->load('cache_control_listener.xml'); } diff --git a/src/EventListener/CacheControlListener.php b/src/EventListener/CacheControlListener.php index da040ab8..d9bfbdee 100644 --- a/src/EventListener/CacheControlListener.php +++ b/src/EventListener/CacheControlListener.php @@ -56,6 +56,7 @@ public function __construct( * @var string|false Name of the header or false to add no header */ private readonly string|false $debugHeader = false, + private readonly string $ttlHeader = 'X-Reverse-Proxy-TTL', ) { } @@ -115,9 +116,9 @@ public function onKernelResponse(ResponseEvent $event): void if (array_key_exists('reverse_proxy_ttl', $options) && null !== $options['reverse_proxy_ttl'] - && !$response->headers->has('X-Reverse-Proxy-TTL') + && !$response->headers->has($this->ttlHeader) ) { - $response->headers->set('X-Reverse-Proxy-TTL', $options['reverse_proxy_ttl'], false); + $response->headers->set($this->ttlHeader, $options['reverse_proxy_ttl'], false); } if (!empty($options['vary'])) { diff --git a/src/Resources/config/cache_control_listener.xml b/src/Resources/config/cache_control_listener.xml index cc1129d6..3249d0a3 100644 --- a/src/Resources/config/cache_control_listener.xml +++ b/src/Resources/config/cache_control_listener.xml @@ -9,6 +9,7 @@ class="FOS\HttpCacheBundle\EventListener\CacheControlListener" public="true"> %fos_http_cache.debug_header% + %fos_http_cache.ttl_header% diff --git a/tests/Resources/Fixtures/config/etag_true.php b/tests/Resources/Fixtures/config/etag_true.php index f86fc235..cdde9a9a 100644 --- a/tests/Resources/Fixtures/config/etag_true.php +++ b/tests/Resources/Fixtures/config/etag_true.php @@ -21,6 +21,7 @@ ], ], ], + 'ttl_header' => 'X-Reverse-Proxy-TTL', ], ] ); diff --git a/tests/Resources/Fixtures/config/etag_true.xml b/tests/Resources/Fixtures/config/etag_true.xml index 058858c9..834abe54 100644 --- a/tests/Resources/Fixtures/config/etag_true.xml +++ b/tests/Resources/Fixtures/config/etag_true.xml @@ -9,6 +9,7 @@ + X-Reverse-Proxy-TTL diff --git a/tests/Resources/Fixtures/config/etag_true.yml b/tests/Resources/Fixtures/config/etag_true.yml index 137527a0..3c0398d6 100644 --- a/tests/Resources/Fixtures/config/etag_true.yml +++ b/tests/Resources/Fixtures/config/etag_true.yml @@ -5,4 +5,5 @@ fos_http_cache: match: path: null headers: - etag: true \ No newline at end of file + etag: true + ttl_header: X-Reverse-Proxy-TTL diff --git a/tests/Resources/Fixtures/config/etag_weak.php b/tests/Resources/Fixtures/config/etag_weak.php index 43a0ec7f..df071d7a 100644 --- a/tests/Resources/Fixtures/config/etag_weak.php +++ b/tests/Resources/Fixtures/config/etag_weak.php @@ -21,6 +21,7 @@ ], ], ], + 'ttl_header' => 'X-Reverse-Proxy-TTL', ], ] ); diff --git a/tests/Resources/Fixtures/config/etag_weak.xml b/tests/Resources/Fixtures/config/etag_weak.xml index 61e60c57..1881854f 100644 --- a/tests/Resources/Fixtures/config/etag_weak.xml +++ b/tests/Resources/Fixtures/config/etag_weak.xml @@ -9,6 +9,7 @@ + X-Reverse-Proxy-TTL diff --git a/tests/Resources/Fixtures/config/etag_weak.yml b/tests/Resources/Fixtures/config/etag_weak.yml index 3b8a22fe..3142ccb6 100644 --- a/tests/Resources/Fixtures/config/etag_weak.yml +++ b/tests/Resources/Fixtures/config/etag_weak.yml @@ -5,4 +5,5 @@ fos_http_cache: match: path: null headers: - etag: "weak" \ No newline at end of file + etag: "weak" + ttl_header: X-Reverse-Proxy-TTL diff --git a/tests/Resources/Fixtures/config/full.php b/tests/Resources/Fixtures/config/full.php index a82d5860..d48bef14 100644 --- a/tests/Resources/Fixtures/config/full.php +++ b/tests/Resources/Fixtures/config/full.php @@ -51,6 +51,7 @@ ], ], ], + 'ttl_header' => 'X-Reverse-Proxy-TTL', ], 'proxy_client' => [ 'varnish' => [ diff --git a/tests/Resources/Fixtures/config/full.xml b/tests/Resources/Fixtures/config/full.xml index 715789dc..0946d4ba 100644 --- a/tests/Resources/Fixtures/config/full.xml +++ b/tests/Resources/Fixtures/config/full.xml @@ -43,6 +43,7 @@ Authorization + X-Reverse-Proxy-TTL diff --git a/tests/Resources/Fixtures/config/full.yml b/tests/Resources/Fixtures/config/full.yml index 3156a958..3391373d 100644 --- a/tests/Resources/Fixtures/config/full.yml +++ b/tests/Resources/Fixtures/config/full.yml @@ -43,6 +43,7 @@ fos_http_cache: vary: - Cookie - Authorization + ttl_header: X-Reverse-Proxy-TTL proxy_client: varnish: tags_header: My-Cache-Tags diff --git a/tests/Resources/Fixtures/config/split.php b/tests/Resources/Fixtures/config/split.php index 91ac0c04..4f5e38e1 100644 --- a/tests/Resources/Fixtures/config/split.php +++ b/tests/Resources/Fixtures/config/split.php @@ -11,6 +11,7 @@ $container->loadFromExtension('fos_http_cache', [ 'cache_control' => [ + 'ttl_header' => 'X-My-Header', 'rules' => [ [ 'match' => [ diff --git a/tests/Resources/Fixtures/config/split.xml b/tests/Resources/Fixtures/config/split.xml index f649ebf7..e6c0d263 100644 --- a/tests/Resources/Fixtures/config/split.xml +++ b/tests/Resources/Fixtures/config/split.xml @@ -3,6 +3,7 @@ + X-My-Header GET,POST diff --git a/tests/Resources/Fixtures/config/split.yml b/tests/Resources/Fixtures/config/split.yml index fc573d02..629b0f57 100644 --- a/tests/Resources/Fixtures/config/split.yml +++ b/tests/Resources/Fixtures/config/split.yml @@ -1,6 +1,7 @@ fos_http_cache: cache_control: + ttl_header: X-My-Header rules: - match: diff --git a/tests/Unit/DependencyInjection/ConfigurationTest.php b/tests/Unit/DependencyInjection/ConfigurationTest.php index 42488ea9..2427d422 100644 --- a/tests/Unit/DependencyInjection/ConfigurationTest.php +++ b/tests/Unit/DependencyInjection/ConfigurationTest.php @@ -96,6 +96,7 @@ public function testSupportsAllConfigFormats(): void ], ], ], + 'ttl_header' => 'X-Reverse-Proxy-TTL', ], 'proxy_client' => [ 'varnish' => [ @@ -474,6 +475,7 @@ public function testSplitOptions(): void { $expectedConfiguration = $this->getEmptyConfig(); $expectedConfiguration['cache_control'] = [ + 'ttl_header' => 'X-My-Header', 'rules' => [ [ 'match' => [ @@ -637,6 +639,7 @@ public function testWeakETags(): void ], ], ], + 'ttl_header' => 'X-Reverse-Proxy-TTL', 'defaults' => [ 'overwrite' => false, ], @@ -679,6 +682,7 @@ public function testStrongETags(): void ], ], ], + 'ttl_header' => 'X-Reverse-Proxy-TTL', 'defaults' => [ 'overwrite' => false, ], diff --git a/tests/Unit/EventListener/CacheControlListenerTest.php b/tests/Unit/EventListener/CacheControlListenerTest.php index cb9748ff..60d8d782 100755 --- a/tests/Unit/EventListener/CacheControlListenerTest.php +++ b/tests/Unit/EventListener/CacheControlListenerTest.php @@ -334,6 +334,21 @@ public function testReverseProxyTtl(): void $this->assertEquals(600, $newHeaders['x-reverse-proxy-ttl'][0]); } + public function testReverseProxyTtlHeader(): void + { + $event = $this->buildEvent(); + $headers = [ + 'reverse_proxy_ttl' => 700, + ]; + $listener = $this->getCacheControl($headers, 'X-My-Header'); + + $listener->onKernelResponse($event); + $newHeaders = $event->getResponse()->headers->all(); + + $this->assertTrue(isset($newHeaders['x-my-header']), implode(',', array_keys($newHeaders))); + $this->assertEquals(700, $newHeaders['x-my-header'][0]); + } + public function testDebugHeader(): void { $listener = new CacheControlListener('X-Cache-Debug'); @@ -426,9 +441,9 @@ protected function buildEvent(string $method = 'GET'): ResponseEvent * * @param array $headers The headers to return from the matcher */ - protected function getCacheControl(array $headers): CacheControlListener|MockObject + protected function getCacheControl(array $headers, string $ttlHeader = 'X-Reverse-Proxy-TTL'): CacheControlListener|MockObject { - $listener = new CacheControlListener(); + $listener = new CacheControlListener(false, $ttlHeader); $matcher = \Mockery::mock(RuleMatcherInterface::class) ->shouldReceive(['matches' => true])