Skip to content

Commit 3e1c45f

Browse files
committed
feat: allow configuring the reverse_proxy_ttl with cache control directives
1 parent b9a80ac commit 3e1c45f

File tree

3 files changed

+46
-7
lines changed

3 files changed

+46
-7
lines changed

Resources/doc/reference/configuration/headers.rst

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,13 +315,16 @@ keeping previously set Vary options:
315315
``reverse_proxy_ttl``
316316
"""""""""""""""""""""
317317

318-
**type**: ``integer``
318+
**type**: ``integer`` or ``array``
319319

320-
Set a custom TTL header for reverse proxy time-outs not driven by
320+
If defined as integer, set a custom TTL header for reverse proxy time-outs not driven by
321321
``s-maxage``. This keeps your ``s-maxage`` free for use with reverse proxies
322322
not under your control. The default header name is ``X-Reverse-Proxy-TTL`` but
323323
you can customize the header name as explained in the next section.
324324

325+
326+
If defined as an array, it follows logic of ``cache_control`` for supported directives.
327+
325328
.. warning::
326329

327330
This is a custom header. You need to set up your caching proxy to respect
@@ -347,6 +350,25 @@ section:
347350
348351
This example adds the header ``X-Reverse-Proxy-TTL: 3600`` to your responses.
349352

353+
Or as an array:
354+
355+
.. code-block:: yaml
356+
357+
# app/config/config.yml
358+
fos_http_cache:
359+
cache_control:
360+
rules:
361+
-
362+
headers:
363+
reverse_proxy_ttl:
364+
public: true
365+
max_age: 36000
366+
cache_control:
367+
private: true
368+
no_store: true
369+
370+
This example adds the header ``X-Reverse-Proxy-TTL: max-age=36000, public`` to your responses.
371+
350372
``ttl_header``
351373
--------------
352374

src/DependencyInjection/Configuration.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -333,10 +333,18 @@ private function addCacheControlSection(ArrayNodeDefinition $rootNode): void
333333
->end()
334334
->info('Set a default last modified timestamp if none is set yet. Value must be parseable by DateTime')
335335
->end()
336-
->scalarNode('reverse_proxy_ttl')
337-
->defaultNull()
338-
->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.')
339-
->end()
336+
->arrayNode('reverse_proxy_ttl')
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. You can also specify a map like cache_control.')
338+
->beforeNormalization()
339+
->ifNotNull()
340+
->then(function (int $v): array { return ['s_maxage' => $v]; })
341+
->end()
342+
->children()
343+
->scalarNode('max_age')->end()
344+
->scalarNode('s_maxage')->end()
345+
->booleanNode('private')->end()
346+
->booleanNode('public')->end()
347+
->end()
340348
->arrayNode('vary')
341349
->beforeNormalization()->ifString()->then(function ($v) {
342350
return preg_split('/\s*,\s*/', $v);

src/EventListener/CacheControlListener.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,16 @@ public function onKernelResponse(ResponseEvent $event): void
120120
&& null !== $options['reverse_proxy_ttl']
121121
&& !$response->headers->has($this->ttlHeader)
122122
) {
123-
$response->headers->set($this->ttlHeader, $options['reverse_proxy_ttl'], false);
123+
$reverseProxyTtl = $options['reverse_proxy_ttl'];
124+
125+
if (is_int($reverseProxyTtl)) { // BC layer: simple mode as integer
126+
$response->headers->set($this->ttlHeader, $reverseProxyTtl, false);
127+
} elseif (is_array($reverseProxyTtl)) { // config mode: similar to cache_control
128+
// only handle normal directives, no extra
129+
$directives = array_intersect_key($reverseProxyTtl, $this->supportedDirectives);
130+
$directives = array_map(function ($key) {return str_replace('_', '-', $key);}, $directives);
131+
$response->headers->set($this->ttlHeader, implode(' ', $directives), false);
132+
}
124133
}
125134

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

0 commit comments

Comments
 (0)