Skip to content

Commit 57f9528

Browse files
committed
purge: Support purging 'releases' and remove config.json support
* All servers have switched to using ENV. * Add support for purging Git files on releases.jquery.com. Ref https://github.com/jquery/infrastructure/issues/474.
1 parent 9c6b20b commit 57f9528

1 file changed

Lines changed: 24 additions & 27 deletions

File tree

purge.php

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,56 +8,53 @@
88
*
99
* Test Plan:
1010
*
11-
* $ REQUEST_URI="/example" php purge.php
11+
* $ STRIKETRACKER_TOKEN='*' STRIKETRACKER_ACCOUNT='*' PURGE_URI='/example' php purge.php
1212
*/
1313

14-
if ( !isset( $_SERVER[ 'REQUEST_URI' ] )
15-
|| !function_exists( 'curl_init' )
16-
) {
14+
if ( !function_exists( 'curl_init' ) ) {
1715
http_response_code( 500 );
1816
echo "Context error.\n";
1917
exit;
2018
}
2119

20+
// Use a map to validate values and as indirection to ensure trivial bugs
21+
// can't result in a user-supplied string getting used.
22+
$purgeHostnames = [
23+
'code' => 'code.jquery.com',
24+
'releases' => 'releases.jquery.com',
25+
];
26+
2227
// Highwinds StrikeTracker
2328
$striketrackerUrl = getenv( 'STRIKETRACKER_URL' ) ?: 'https://striketracker.highwinds.com';
2429
$striketrackerToken = getenv( 'STRIKETRACKER_TOKEN' ) ?: false;
2530
$striketrackerAccountHash = getenv( 'STRIKETRACKER_ACCOUNT' ) ?: false;
26-
// This is configurable because the purge script may be invoked
27-
// from a hostname different from the one canonically serving the asset,
28-
// or. e.g. from the CLI.
29-
$striketrackerPurgeHostname = getenv( 'STRIKETRACKER_PURGE_HOSTNAME' ) ?: 'code.jquery.com';
31+
32+
// The purge script is generally not called from a purgable site, so the current request
33+
// is unlikely to be on the purgable site itself (e.g. some origin server instead of the
34+
// public hostname). As such, take the target site as input. This also allows the same
35+
// script to be used for multiple sites.
36+
$purgeSite = getenv( 'PURGE_SITE' ) ?: @$_GET['site'] ?: 'releases';
37+
$striketrackerPurgeHostname = @$purgeHostnames[$purgeSite] ?: false;
38+
39+
$purgeUri = getenv( 'PURGE_URI' ) ?: @$_GET['uri'] ?: @$_SERVER['REQUEST_URI'] ?: false;
3040

3141
if ( !$striketrackerUrl
3242
|| !$striketrackerToken
3343
|| !$striketrackerAccountHash
3444
|| !$striketrackerPurgeHostname
45+
|| !$purgeUri
3546
) {
36-
$configFile = __DIR__ . '/config.json';
37-
$configJson = @file_get_contents( $configFile );
38-
$config = $configJson ? json_decode( $configJson ) : false;
39-
$hwConfig = $config ? $config->highwinds : false;
40-
if ( !$hwConfig
41-
|| !$hwConfig->api_url
42-
|| !$hwConfig->api_token
43-
|| !$hwConfig->account_hash
44-
|| !$hwConfig->file_hostname
45-
) {
46-
http_response_code( 500 );
47-
echo "Configuration error.\n";
48-
exit;
49-
}
50-
$striketrackerUrl = $hwConfig->api_url;
51-
$striketrackerToken = $hwConfig->api_token;
52-
$striketrackerAccountHash = $hwConfig->account_hash;
53-
$striketrackerPurgeHostname = $hwConfig->file_hostname;
47+
48+
http_response_code( 400 );
49+
echo "Configuration error.\n";
50+
exit;
5451
}
5552

5653
// The StrikeTracker Purge API is protocol-sensitive.
5754
// HTTP and HTTPS need to be purged separately, or
5855
// we can use a protocol-relative file url, which Highwinds
5956
// supports as short-cut for purging both.
60-
$file = "//{$striketrackerPurgeHostname}/" . ltrim( $_SERVER[ 'REQUEST_URI' ], '/' );
57+
$file = "//{$striketrackerPurgeHostname}/" . ltrim( $purgeUri, '/' );
6158

6259
/**
6360
* Make an HTTP POST request, submitting JSON data, and receiving JSON data.

0 commit comments

Comments
 (0)