From 54c806f82f1754d482dd4f008e4b9bfb489a66d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rn=20A=CC=8Ane?= <jorn.dejong@uninett.no> Date: Fri, 6 Sep 2019 11:41:55 +0200 Subject: [PATCH] Simplify Module.php static asset cache configuration --- config-templates/config.php | 44 +++++++++++-------------------------- lib/SimpleSAML/Module.php | 15 ++++--------- 2 files changed, 17 insertions(+), 42 deletions(-) diff --git a/config-templates/config.php b/config-templates/config.php index ea110c29a..ae4f32325 100644 --- a/config-templates/config.php +++ b/config-templates/config.php @@ -862,37 +862,19 @@ $config = [ 'production' => true, /* - * Set this option to adjust basic caching directives sent by module.php. - * - * Defaults to ['public' => true, 'max_age' => 86400] - */ - // 'module.php.cache' => ['public' => true, 'max_age' => 86400], - - /* - * Set this option to adjust expiration timestamp sent by module.php. - * The value (in seconds) is added to current time. - * If set to null, the Expiration header is not sent at all. - * - * Defaults to 10 * 60 (ten minutes). - */ - // 'module.php.expires' => 10 * 60, - - /* - * Set this option to make module.php automatically send Etag. - * - * Defaults to false. - */ - // 'module.php.etag' => false, - - /* - * Set this option to adjust more caching directives sent by module.php, - * for example must-revalidate. The option takes an array - * where keys are the caching directive names and values are the corresponding values. - * - * Defaults to [] (empty array). - */ - // 'module.php.directives' => [], - + * SimpleSAMLphp modules can host static resources which are served through PHP. + * The serving of the resources can be configured through these settings. + */ + 'assets' => [ + 'caching' => [ + /** + * These settings adjust the caching headers that are sent + * when serving static resources. + */ + 'max_age' => 86400, + 'etag' => false, + ], + ], /********************* | DISCOVERY SERVICE | diff --git a/lib/SimpleSAML/Module.php b/lib/SimpleSAML/Module.php index 25b3160ee..84d2f4a63 100644 --- a/lib/SimpleSAML/Module.php +++ b/lib/SimpleSAML/Module.php @@ -264,20 +264,13 @@ class Module } } + $assetConfig = $config->getArray('assets', ['caching' => ['max_age' => 86400, 'etag' => false]]); $response = new BinaryFileResponse($path); - $cacheSettings = $config->getArray('module.php.cache', ['public' => true, 'max_age' => 86400]); - $response->setCache($cacheSettings); - $cacheExpiration = $config->getValue('module.php.expires', 10 * 60); - $expires = $cacheExpiration === null ? null : new \DateTime('@' . (time() + $cacheExpiration)); - $response->setExpires($expires); - $cacheEtag = $config->getBoolean('module.php.etag', false); - if ($cacheEtag) { + $response->setCache(['public' => true, 'max_age' => $assetConfig['caching']['max_age']]); + $response->setAutoLastModified(); + if ($assetConfig['caching']['etag']) { $response->setAutoEtag(); } - $cacheDirectives = $config->getArray('module.php.directives', []); - foreach ($cacheDirectives as $directiveName => $directiveValue) { - $response->headers->addCacheControlDirective($directiveName, $directiveValue); - } $response->isNotModified($request); $response->headers->set('Content-Type', $contentType); $response->setContentDisposition(ResponseHeaderBag::DISPOSITION_INLINE); -- GitLab