From 09437cf1ac9d62860dbb26a5c53eae15411808c5 Mon Sep 17 00:00:00 2001 From: Nic Wortel Date: Thu, 19 Nov 2026 14:02:01 +0100 Subject: [PATCH] Set short Cache-Control on release data endpoints The machine-readable release endpoints sent no Cache-Control header, so BunnyCDN applied its 30-day default. --- public/pre-release-builds.php | 3 + public/releases/branches.php | 1 + public/releases/feed.php | 1 + public/releases/index.php | 2 + public/releases/states.php | 1 + tests/EndToEnd/CacheControlTest.php | 99 +++++++++++++++++++++++++++++ 6 files changed, 107 insertions(+) create mode 100644 tests/EndToEnd/CacheControlTest.php diff --git a/public/pre-release-builds.php b/public/pre-release-builds.php index 5c3e3432c4..ba4cefc37b 100644 --- a/public/pre-release-builds.php +++ b/public/pre-release-builds.php @@ -7,6 +7,8 @@ require_once __DIR__ . '/../include/release-qa.php'; if (isset($_GET["format"])) { + header('Cache-Control: public, max-age=300'); // 5 minutes + $output = $QA_RELEASES; if (($_GET['only'] ?? null) === 'dev_versions') { @@ -67,6 +69,7 @@ site_header("Pre-Release Builds", [ 'current' => 'downloads', + 'cache_control' => 5 * 60, // 5 minutes ]); ?> diff --git a/public/releases/branches.php b/public/releases/branches.php index da0f62f2ea..541c9e2828 100644 --- a/public/releases/branches.php +++ b/public/releases/branches.php @@ -4,6 +4,7 @@ require_once __DIR__ . '/../../include/branches.inc'; header('Content-Type: application/json; charset=UTF-8'); +header('Cache-Control: public, max-age=300'); // 5 minutes function formatDate($date = null) { return $date !== null ? $date->format('c') : null; diff --git a/public/releases/feed.php b/public/releases/feed.php index bc5eaf97de..d1e5313e0b 100644 --- a/public/releases/feed.php +++ b/public/releases/feed.php @@ -1,6 +1,7 @@ + */ + public static function getPaths(): Generator + { + $paths = [ + '/releases/index.php?json', + '/releases/index.php?json&version=8&max=2', + '/releases/index.php?serialize', + '/releases/branches.php', + '/releases/states.php', + '/releases/feed.php', + '/pre-release-builds.php', + '/pre-release-builds.php?format=json', + '/pre-release-builds.php?format=json&only=dev_versions', + '/pre-release-builds.php?format=serialize', + ]; + + foreach ($paths as $path) { + yield $path => [$path]; + } + } + + /** + * @return array{status: int, cacheControl: string} + */ + private static function get(string $path): array + { + $httpHost = getenv('HTTP_HOST'); + + if (!is_string($httpHost)) { + throw new \RuntimeException('Environment variable "HTTP_HOST" is not set.'); + } + + $cacheControl = ''; + + $handle = curl_init(); + + curl_setopt_array($handle, [ + CURLOPT_RETURNTRANSFER => true, + CURLOPT_URL => sprintf('http://%s%s', $httpHost, $path), + CURLOPT_HEADERFUNCTION => static function ($handle, string $header) use (&$cacheControl): int { + if (stripos($header, 'cache-control:') === 0) { + $cacheControl = trim(substr($header, strlen('cache-control:'))); + } + + return strlen($header); + }, + ]); + + $body = curl_exec($handle); + $status = curl_getinfo($handle, CURLINFO_HTTP_CODE); + + if (!is_string($body)) { + throw new \RuntimeException(sprintf('Failed to request "%s".', $path)); + } + + return [ + 'status' => $status, + 'cacheControl' => $cacheControl, + ]; + } +}