Skip to content

Commit 7995f55

Browse files
authored
Merge pull request #638 from 94noni/ttl-header-cache-control
feat: allow to configure header name for reverse_proxy_ttl specific value
2 parents cfb93c3 + 8c3ee8b commit 7995f55

File tree

20 files changed

+76
-7
lines changed

20 files changed

+76
-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/reference/configuration/headers.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,3 +345,26 @@ section:
345345
s_maxage: 60
346346
347347
This example adds the header ``X-Reverse-Proxy-TTL: 3600`` to your responses.
348+
349+
``ttl_header``
350+
--------------
351+
352+
**type**: ``string`` default `X-Reverse-Proxy-TTL`
353+
354+
Change the name for the header for reverse proxy.
355+
356+
.. code-block:: yaml
357+
358+
# app/config/config.yml
359+
fos_http_cache:
360+
cache_control:
361+
ttl_header: X-My-Cache-Control
362+
rules:
363+
-
364+
headers:
365+
reverse_proxy_ttl: 3600
366+
cache_control:
367+
public: true
368+
s_maxage: 60
369+
370+
This example adds the header ``X-My-Cache-Control: 3600`` to your responses.

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)