Repository navigation
Set short Cache-Control on release data endpoints #2091
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think these two headers, and for other occurrences in this PR, these should be set through a new helper function, setting both the content type, and cache time, like is done in The default of 5 minutes seems short — most other TTLs are 15 minutes.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, I can introduce a helper function.
I'll change the TTL to 15 minutes. |
||
|
|
||
| function formatDate($date = null) { | ||
| return $date !== null ? $date->format('c') : null; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace phpweb\Test\EndToEnd; | ||
|
|
||
| use Generator; | ||
| use PHPUnit\Framework\Attributes\CoversNothing; | ||
| use PHPUnit\Framework\Attributes\DataProvider; | ||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| /** | ||
| * Without a Cache-Control header, BunnyCDN appears to apply a default of 30 days, | ||
| * so new releases and branch state changes would not show up in these endpoints | ||
| * until the cache expires or is purged. | ||
| */ | ||
| #[CoversNothing] | ||
| final class CacheControlTest extends TestCase | ||
| { | ||
| #[DataProvider('getPaths')] | ||
| public function testReleaseDataIsCachedBriefly(string $path): void | ||
| { | ||
| $response = self::get($path); | ||
|
|
||
| self::assertSame(200, $response['status'], sprintf( | ||
| 'Expected "%s" to return 200, got "%d".', | ||
| $path, | ||
| $response['status'], | ||
| )); | ||
|
|
||
| self::assertSame('public, max-age=300', $response['cacheControl'], sprintf( | ||
| 'Unexpected Cache-Control header for "%s".', | ||
| $path, | ||
| )); | ||
| } | ||
|
|
||
| /** | ||
| * @return Generator<string, array{0: string}> | ||
| */ | ||
| 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, | ||
| ]; | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't link it that this is done by hand-crafting a header, especially because
site_header()already has logic. Now, this is done in two places in this file too.