Skip to content

feat: allow to configure header name for reverse_proxy_ttl specific value #638

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
Jan 8, 2025
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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
-----

Expand Down
23 changes: 23 additions & 0 deletions Resources/doc/reference/configuration/headers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
6 changes: 5 additions & 1 deletion src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions src/DependencyInjection/FOSHttpCacheExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

Expand Down
5 changes: 3 additions & 2 deletions src/EventListener/CacheControlListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
) {
}

Expand Down Expand Up @@ -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'])) {
Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/cache_control_listener.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
class="FOS\HttpCacheBundle\EventListener\CacheControlListener"
public="true">
<argument>%fos_http_cache.debug_header%</argument>
<argument>%fos_http_cache.ttl_header%</argument>
<tag name="kernel.event_subscriber" />
</service>
<service id="FOS\HttpCacheBundle\EventListener\CacheControlListener" alias="fos_http_cache.event_listener.cache_control" public="true"/>
Expand Down
1 change: 1 addition & 0 deletions tests/Resources/Fixtures/config/etag_true.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
],
],
],
'ttl_header' => 'X-Reverse-Proxy-TTL',
],
]
);
1 change: 1 addition & 0 deletions tests/Resources/Fixtures/config/etag_true.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<headers etag="true">
</headers>
</rule>
<ttl-header>X-Reverse-Proxy-TTL</ttl-header>
</cache-control>
</config>

Expand Down
3 changes: 2 additions & 1 deletion tests/Resources/Fixtures/config/etag_true.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ fos_http_cache:
match:
path: null
headers:
etag: true
etag: true
ttl_header: X-Reverse-Proxy-TTL
1 change: 1 addition & 0 deletions tests/Resources/Fixtures/config/etag_weak.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
],
],
],
'ttl_header' => 'X-Reverse-Proxy-TTL',
],
]
);
1 change: 1 addition & 0 deletions tests/Resources/Fixtures/config/etag_weak.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<headers etag="weak">
</headers>
</rule>
<ttl-header>X-Reverse-Proxy-TTL</ttl-header>
</cache-control>
</config>

Expand Down
3 changes: 2 additions & 1 deletion tests/Resources/Fixtures/config/etag_weak.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ fos_http_cache:
match:
path: null
headers:
etag: "weak"
etag: "weak"
ttl_header: X-Reverse-Proxy-TTL
1 change: 1 addition & 0 deletions tests/Resources/Fixtures/config/full.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
],
],
],
'ttl_header' => 'X-Reverse-Proxy-TTL',
],
'proxy_client' => [
'varnish' => [
Expand Down
1 change: 1 addition & 0 deletions tests/Resources/Fixtures/config/full.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
<vary>Authorization</vary>
</headers>
</rule>
<ttl-header>X-Reverse-Proxy-TTL</ttl-header>
</cache-control>
<proxy-client>
<varnish tags-header="My-Cache-Tags" header-length="1234">
Expand Down
1 change: 1 addition & 0 deletions tests/Resources/Fixtures/config/full.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ fos_http_cache:
vary:
- Cookie
- Authorization
ttl_header: X-Reverse-Proxy-TTL
proxy_client:
varnish:
tags_header: My-Cache-Tags
Expand Down
1 change: 1 addition & 0 deletions tests/Resources/Fixtures/config/split.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

$container->loadFromExtension('fos_http_cache', [
'cache_control' => [
'ttl_header' => 'X-My-Header',
'rules' => [
[
'match' => [
Expand Down
1 change: 1 addition & 0 deletions tests/Resources/Fixtures/config/split.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

<config xmlns="http://example.org/schema/dic/fos_http_cache">
<cache-control>
<ttl-header>X-My-Header</ttl-header>
<rule>
<match>
<methods>GET,POST</methods>
Expand Down
1 change: 1 addition & 0 deletions tests/Resources/Fixtures/config/split.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
fos_http_cache:

cache_control:
ttl_header: X-My-Header
rules:
-
match:
Expand Down
4 changes: 4 additions & 0 deletions tests/Unit/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public function testSupportsAllConfigFormats(): void
],
],
],
'ttl_header' => 'X-Reverse-Proxy-TTL',
],
'proxy_client' => [
'varnish' => [
Expand Down Expand Up @@ -474,6 +475,7 @@ public function testSplitOptions(): void
{
$expectedConfiguration = $this->getEmptyConfig();
$expectedConfiguration['cache_control'] = [
'ttl_header' => 'X-My-Header',
'rules' => [
[
'match' => [
Expand Down Expand Up @@ -637,6 +639,7 @@ public function testWeakETags(): void
],
],
],
'ttl_header' => 'X-Reverse-Proxy-TTL',
'defaults' => [
'overwrite' => false,
],
Expand Down Expand Up @@ -679,6 +682,7 @@ public function testStrongETags(): void
],
],
],
'ttl_header' => 'X-Reverse-Proxy-TTL',
'defaults' => [
'overwrite' => false,
],
Expand Down
19 changes: 17 additions & 2 deletions tests/Unit/EventListener/CacheControlListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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])
Expand Down
Loading