From 2a5bfb9f44169f94b50d2afc7aefd03a8edf62ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C4=81ris=20Narti=C5=A1s?= Date: Thu, 25 Jan 2024 15:37:16 +0200 Subject: [PATCH] Allow to provide custom Cache-Control header values (fixes #322) (#329) --- include/mapcache.h | 8 ++++++++ lib/configuration_xml.c | 5 +++++ lib/core.c | 13 ++++++++++++- 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/include/mapcache.h b/include/mapcache.h index 82b27626..3c3ea081 100644 --- a/include/mapcache.h +++ b/include/mapcache.h @@ -1199,6 +1199,14 @@ struct mapcache_tileset { */ int auto_expire; + /** + * Cache-Control directives to be set for a tiled response (MAPCACHE_REQUEST_GET_TILE) + * + * complements expiration set by the #expires parameter. + * \sa expires + */ + const char *cache_control; + int read_only; int subdimension_read_only; diff --git a/lib/configuration_xml.c b/lib/configuration_xml.c index cfd0ad0b..96e47295 100644 --- a/lib/configuration_xml.c +++ b/lib/configuration_xml.c @@ -1163,6 +1163,11 @@ void parseTileset(mapcache_context *ctx, ezxml_t node, mapcache_cfg *config) } } + tileset->cache_control = NULL; + if ((cur_node = ezxml_child(node,"cache-control")) != NULL) { + tileset->cache_control = apr_pstrdup(ctx->pool, cur_node->txt); + } + if ((cur_node = ezxml_child(node,"metabuffer")) != NULL) { char *endptr; tileset->metabuffer = (int)strtol(cur_node->txt,&endptr,10); diff --git a/lib/core.c b/lib/core.c index b7aba323..569c7f51 100644 --- a/lib/core.c +++ b/lib/core.c @@ -346,11 +346,22 @@ mapcache_http_response *mapcache_core_get_tile(mapcache_context *ctx, mapcache_r apr_time_t now = apr_time_now(); apr_time_t additional = apr_time_from_sec(expires); apr_time_t texpires = now + additional; - apr_table_set(response->headers, "Cache-Control",apr_psprintf(ctx->pool, "max-age=%d", expires)); timestr = apr_palloc(ctx->pool, APR_RFC822_DATE_LEN); apr_rfc822_date(timestr, texpires); apr_table_setn(response->headers, "Expires", timestr); } + if (expires || req_tile->tiles[0]->tileset->cache_control) { + apr_table_set( + response->headers, "Cache-Control", + apr_pstrcat( + ctx->pool, + expires ? apr_psprintf(ctx->pool, "max-age=%d", expires) : "", + expires && req_tile->tiles[0]->tileset->cache_control ? ", " : "", + req_tile->tiles[0]->tileset->cache_control + ? req_tile->tiles[0]->tileset->cache_control + : "", + NULL)); + } return response; }