Skip to content

Commit 35a73cc

Browse files
committed
feat: allow to configure header name for reverse_proxy_ttl specific value
1 parent cfb93c3 commit 35a73cc

File tree

20 files changed

+73
-7
lines changed

20 files changed

+73
-7
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ Changelog
44
3.x
55
===
66

7+
3.1.0
8+
-----
9+
10+
### Added
11+
12+
* New Feature: allow configuring the TTL header name for the special `reverse_proxy_ttl` config value. #638
13+
714
3.0.2
815
-----
916

Resources/doc/features/headers.rst

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,25 @@ This is an example configuration. For more, see the
6969
cache_control: { public: true, max_age: 15, s_maxage: 30 }
7070
etag: "strong"
7171
72+
This is an example with the special `` and `` configurations.
73+
74+
.. code-block:: yaml
75+
76+
# app/config/config.yml
77+
fos_http_cache:
78+
cache_control:
79+
ttl_header: X-My-Cache-Control
80+
defaults:
81+
overwrite: true
82+
rules:
83+
# match everything to set defaults
84+
-
85+
match:
86+
path: ^/
87+
headers:
88+
overwrite: false
89+
cache_control: { public: true, max_age: 15, s_maxage: 30, reverse_proxy_ttl: 60 }
90+
etag: "strong"
91+
7292
.. _manually setting cache headers: https://symfony.com/doc/current/http_cache.html#the-cache-control-header
7393
.. _setting caching headers through attributes: https://symfony.com/doc/current/http_cache.html#making-your-responses-http-cacheable

src/DependencyInjection/Configuration.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,10 @@ private function addCacheControlSection(ArrayNodeDefinition $rootNode): void
277277
->end()
278278
->end()
279279
->end()
280+
->scalarNode('ttl_header')
281+
->defaultValue('X-Reverse-Proxy-TTL')
282+
->info('Specify the header name to use with the cache_control.reverse_proxy_ttl setting')
283+
->end()
280284
->arrayNode('rules')
281285
->prototype('array')
282286
->children();
@@ -330,7 +334,7 @@ private function addCacheControlSection(ArrayNodeDefinition $rootNode): void
330334
->end()
331335
->scalarNode('reverse_proxy_ttl')
332336
->defaultNull()
333-
->info('Specify an X-Reverse-Proxy-TTL header with a time in seconds for a caching proxy under your control.')
337+
->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.')
334338
->end()
335339
->arrayNode('vary')
336340
->beforeNormalization()->ifString()->then(function ($v) {

src/DependencyInjection/FOSHttpCacheExtension.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ public function load(array $configs, ContainerBuilder $container): void
4848

4949
if ($config['debug']['enabled'] || (!empty($config['cache_control']))) {
5050
$debugHeader = $config['debug']['enabled'] ? $config['debug']['header'] : false;
51+
$ttlHeader = $config['cache_control']['ttl_header'] ?? '';
5152
$container->setParameter('fos_http_cache.debug_header', $debugHeader);
53+
$container->setParameter('fos_http_cache.ttl_header', $ttlHeader);
5254
$loader->load('cache_control_listener.xml');
5355
}
5456

src/EventListener/CacheControlListener.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public function __construct(
5656
* @var string|false Name of the header or false to add no header
5757
*/
5858
private readonly string|false $debugHeader = false,
59+
private readonly string $ttlHeader = 'X-Reverse-Proxy-TTL',
5960
) {
6061
}
6162

@@ -115,9 +116,9 @@ public function onKernelResponse(ResponseEvent $event): void
115116

116117
if (array_key_exists('reverse_proxy_ttl', $options)
117118
&& null !== $options['reverse_proxy_ttl']
118-
&& !$response->headers->has('X-Reverse-Proxy-TTL')
119+
&& !$response->headers->has($this->ttlHeader)
119120
) {
120-
$response->headers->set('X-Reverse-Proxy-TTL', $options['reverse_proxy_ttl'], false);
121+
$response->headers->set($this->ttlHeader, $options['reverse_proxy_ttl'], false);
121122
}
122123

123124
if (!empty($options['vary'])) {

src/Resources/config/cache_control_listener.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
class="FOS\HttpCacheBundle\EventListener\CacheControlListener"
1010
public="true">
1111
<argument>%fos_http_cache.debug_header%</argument>
12+
<argument>%fos_http_cache.ttl_header%</argument>
1213
<tag name="kernel.event_subscriber" />
1314
</service>
1415
<service id="FOS\HttpCacheBundle\EventListener\CacheControlListener" alias="fos_http_cache.event_listener.cache_control" public="true"/>

tests/Resources/Fixtures/config/etag_true.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
],
2222
],
2323
],
24+
'ttl_header' => 'X-Reverse-Proxy-TTL',
2425
],
2526
]
2627
);

tests/Resources/Fixtures/config/etag_true.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<headers etag="true">
1010
</headers>
1111
</rule>
12+
<ttl-header>X-Reverse-Proxy-TTL</ttl-header>
1213
</cache-control>
1314
</config>
1415

tests/Resources/Fixtures/config/etag_true.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ fos_http_cache:
55
match:
66
path: null
77
headers:
8-
etag: true
8+
etag: true
9+
ttl_header: X-Reverse-Proxy-TTL

tests/Resources/Fixtures/config/etag_weak.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
],
2222
],
2323
],
24+
'ttl_header' => 'X-Reverse-Proxy-TTL',
2425
],
2526
]
2627
);

0 commit comments

Comments
 (0)