Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions public/pre-release-builds.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
require_once __DIR__ . '/../include/release-qa.php';

if (isset($_GET["format"])) {
header('Cache-Control: public, max-age=300'); // 5 minutes

Copy link
Copy Markdown
Member

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.


$output = $QA_RELEASES;

if (($_GET['only'] ?? null) === 'dev_versions') {
Expand Down Expand Up @@ -67,6 +69,7 @@

site_header("Pre-Release Builds", [
'current' => 'downloads',
'cache_control' => 5 * 60, // 5 minutes
]);

?>
Expand Down
1 change: 1 addition & 0 deletions public/releases/branches.php
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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 site_header().

The default of 5 minutes seems short — most other TTLs are 15 minutes.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I can introduce a helper function.
Where should I define it and what should I name it?

public/releases/branches.php, public/releases/index.php and public/pre-release-builds.php already require include/prepend.inc, which pulls in site.inc, so I could introduce a new function there next to header_nocache(). Something like header_cacheable(string $contentType, int $maxAge): void?

public/releases/feed.php doesn't require prepend.inc. I'm not sure if adding it would cause any unintended side effects?

I'll change the TTL to 15 minutes.


function formatDate($date = null) {
return $date !== null ? $date->format('c') : null;
Expand Down
1 change: 1 addition & 0 deletions public/releases/feed.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

header("Content-Type: application/atom+xml");
header("Cache-Control: public, max-age=300"); // 5 minutes

require __DIR__ . "/../../include/version.inc";

Expand Down
2 changes: 2 additions & 0 deletions public/releases/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
require_once ProjectGlobals::getProjectRoot() . '/include/branches.inc';

if (isset($_GET["serialize"]) || isset($_GET["json"])) {
header('Cache-Control: public, max-age=300'); // 5 minutes

$RELEASES = $RELEASES + $OLDRELEASES;

$machineReadable = [];
Expand Down
1 change: 1 addition & 0 deletions public/releases/states.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
require_once ProjectGlobals::getProjectRoot() . '/include/branches.inc';

header('Content-Type: application/json; charset=UTF-8');
header('Cache-Control: public, max-age=300'); // 5 minutes

$states = [];

Expand Down
99 changes: 99 additions & 0 deletions tests/EndToEnd/CacheControlTest.php
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,
];
}
}