caching cleanup

This commit is contained in:
Jeremy Kauffman 2019-09-22 10:06:46 -04:00
parent 00fa2329ee
commit 5238e52575
7 changed files with 41 additions and 34 deletions

View file

@ -173,7 +173,7 @@ class Controller
return static::redirect('/' . strtolower($slug), 301); return static::redirect('/' . strtolower($slug), 301);
} }
if (View::exists('page/' . $slug)) { if (View::exists('page/' . $slug)) {
Response::enableHttpCache(); Response::enablePublicImmutableCache();
return ['page/' . $slug, []]; return ['page/' . $slug, []];
} else { } else {
return NavActions::execute404(); return NavActions::execute404();

View file

@ -16,11 +16,18 @@ class Session
{ {
ini_set('session.cookie_secure', IS_PRODUCTION); // send cookie over ssl only ini_set('session.cookie_secure', IS_PRODUCTION); // send cookie over ssl only
ini_set('session.cookie_httponly', true); // no js access to cookies ini_set('session.cookie_httponly', true); // no js access to cookies
session_start(); session_start();
/*
* session_start automatically adds headers because lolphp, let's remove them and handle it ourselves
*/
header_remove('cache-control');
header_remove('pragma');
header_remove('expires');
if (!static::get('secure_and_httponly_set')) { if (!static::get('secure_and_httponly_set')) {
session_regenerate_id(); // ensure that old cookies get new settings session_regenerate_id(); // ensure that old cookies get new settings
} }
static::set('secure_and_httponly_set', true); static::set('secure_and_httponly_set', true);

View file

@ -36,16 +36,19 @@ class AcquisitionActions extends Actions
public static function executeVerify(string $token) public static function executeVerify(string $token)
{ {
Response::disableHttpCache();
return ['acquisition/verify', ['token' => $token]]; return ['acquisition/verify', ['token' => $token]];
} }
public static function executeAutoVerify() public static function executeAutoVerify()
{ {
Response::disableHttpCache();
return ['acquisition/auto-verify']; return ['acquisition/auto-verify'];
} }
public static function executeYoutubeToken() public static function executeYoutubeToken()
{ {
Response::disableHttpCache();
$channelName = Request::encodeStringFromUser($_POST['desired_lbry_channel_name']); $channelName = Request::encodeStringFromUser($_POST['desired_lbry_channel_name']);
$immediateSync = (boolean)$_POST['immediate_sync']; $immediateSync = (boolean)$_POST['immediate_sync'];
@ -65,6 +68,7 @@ class AcquisitionActions extends Actions
public static function executeYoutubeStatus(string $token) public static function executeYoutubeStatus(string $token)
{ {
Response::disableHttpCache();
$data = LBRY::statusYoutube($token); $data = LBRY::statusYoutube($token);
if (!$data['success']) { if (!$data['success']) {

View file

@ -28,27 +28,27 @@ class ContentActions extends Actions
public static function executeHome(): array public static function executeHome(): array
{ {
Response::enableHttpCache(); Response::enablePublicImmutableCache();
return ['page/home']; return ['page/home'];
} }
// //
public static function executeOrg(): array public static function executeOrg(): array
{ {
Response::enableHttpCache(); Response::enablePublicImmutableCache();
return ['page/org']; return ['page/org'];
} }
public static function executeTv(): array public static function executeTv(): array
{ {
Response::enableHttpCache(); Response::enablePublicImmutableCache();
return ['page/tv']; return ['page/tv'];
} }
// //
public static function executeNews(string $slug = null): array public static function executeNews(string $slug = null): array
{ {
Response::enableHttpCache(); Response::enablePublicImmutableCache();
if (!$slug || $slug == static::SLUG_RSS) { if (!$slug || $slug == static::SLUG_RSS) {
$posts = array_filter( $posts = array_filter(
@ -90,7 +90,7 @@ class ContentActions extends Actions
public static function executeFaq(string $slug = null): array public static function executeFaq(string $slug = null): array
{ {
Response::enableHttpCache(); Response::enablePublicImmutableCache();
if (!$slug) { if (!$slug) {
$allPosts = Post::find(static::VIEW_FOLDER_FAQ, Post::SORT_ORD_ASC); $allPosts = Post::find(static::VIEW_FOLDER_FAQ, Post::SORT_ORD_ASC);
@ -139,7 +139,7 @@ class ContentActions extends Actions
public static function executeCreditReports(string $year = null, string $month = null): array public static function executeCreditReports(string $year = null, string $month = null): array
{ {
Response::enableHttpCache(); Response::enablePublicImmutableCache();
$posts = Post::find(static::VIEW_FOLDER_CREDIT_REPORTS); $posts = Post::find(static::VIEW_FOLDER_CREDIT_REPORTS);
@ -150,7 +150,7 @@ class ContentActions extends Actions
public static function executeCreditReport(string $year = null, string $quarter = null): array public static function executeCreditReport(string $year = null, string $quarter = null): array
{ {
Response::enableHttpCache(); Response::enablePublicImmutableCache();
try { try {
$post = Post::load(static::SLUG_CREDIT_REPORTS . '/' . $year . '-Q' . $quarter); $post = Post::load(static::SLUG_CREDIT_REPORTS . '/' . $year . '-Q' . $quarter);
@ -172,7 +172,7 @@ class ContentActions extends Actions
public static function executeBounty(string $slug = null): array public static function executeBounty(string $slug = null): array
{ {
Response::enableHttpCache(); Response::enablePublicImmutableCache();
@ -276,7 +276,7 @@ class ContentActions extends Actions
// //
// $zip->close(); // $zip->close();
// //
// Response::enableHttpCache(); // Response::enablePublicImmutableCache();
// Response::setDownloadHttpHeaders($zipFileName, 'application/zip', filesize($zipPath)); // Response::setDownloadHttpHeaders($zipFileName, 'application/zip', filesize($zipPath));
// //
// return ['internal/zip', [ // return ['internal/zip', [
@ -320,7 +320,7 @@ class ContentActions extends Actions
} }
public static function executePostCategoryFilter(string $category) public static function executePostCategoryFilter(string $category)
{ {
Response::enableHttpCache(); Response::enablePublicImmutableCache();
$filter_post = []; $filter_post = [];

View file

@ -51,6 +51,7 @@ class MailActions extends Actions
public static function executeUnsubscribe(string $email) public static function executeUnsubscribe(string $email)
{ {
Response::disableHttpCache();
$decodedEmail = Encoding::base64DecodeUrlsafe(urldecode($email)); $decodedEmail = Encoding::base64DecodeUrlsafe(urldecode($email));
if (!$decodedEmail) { if (!$decodedEmail) {
return ['mail/unsubscribe', ['error' => 'Invalid unsubscribe link']]; return ['mail/unsubscribe', ['error' => 'Invalid unsubscribe link']];
@ -62,6 +63,7 @@ class MailActions extends Actions
public static function editEmailSettings(string $token) public static function editEmailSettings(string $token)
{ {
Response::disableHttpCache();
list($status, $headers, $response) = LBRY::emailStatus($token); list($status, $headers, $response) = LBRY::emailStatus($token);
if ($status == 403) { if ($status == 403) {
//Catch 403 to return elegant error message. //Catch 403 to return elegant error message.

View file

@ -32,7 +32,7 @@ class i18nActions extends Actions
$json = Transifex::getTranslationResourceFile($project, $resource, $language); $json = Transifex::getTranslationResourceFile($project, $resource, $language);
Response::setHeader(Response::HEADER_CROSS_ORIGIN, "*"); Response::setHeader(Response::HEADER_CROSS_ORIGIN, "*");
Response::setHeader(Response::HEADER_ETAG, md5(json_encode($json))); Response::enablePublicMutableCache(md5(json_encode($json)));
return View::renderJson($json); return View::renderJson($json);
} }

View file

@ -6,7 +6,6 @@ class Response
const HEADER_LOCATION = 'Location'; const HEADER_LOCATION = 'Location';
const HEADER_CACHE_CONTROL = 'Cache-Control'; const HEADER_CACHE_CONTROL = 'Cache-Control';
const HEADER_LAST_MODIFIED = 'Last-Modified';
const HEADER_ETAG = 'Etag'; const HEADER_ETAG = 'Etag';
const HEADER_CONTENT_TYPE = 'Content-Type'; const HEADER_CONTENT_TYPE = 'Content-Type';
@ -26,7 +25,9 @@ class Response
], ],
'css' => ['/css/all.css'] 'css' => ['/css/all.css']
]; ];
protected static $headers = []; protected static $headers = [
'Cache-Control' => 'private, no-cache'
];
protected static $headersSent = false; protected static $headersSent = false;
protected static $content = ''; protected static $content = '';
protected static $contentSent = false; protected static $contentSent = false;
@ -192,30 +193,23 @@ class Response
])); ]));
} }
public static function enableHttpCache(int $seconds = 300) //public immutable cache = hard-caching (no server checks) until time limit passes
public static function enablePublicImmutableCache(int $seconds = 300)
{ {
static::addCacheControlHeader('max-age', $seconds); static::setHeader(static::HEADER_CACHE_CONTROL, 'public, max-age=' . $seconds);
static::setHeader('Pragma', 'public');
} }
public static function addCacheControlHeader(string $name, $value = null) //public mutable cache = soft-caching (requires at least one round trip for headers) as long as etag identifier matches
public static function enablePublicMutableCache(string $etag)
{ {
$cacheControl = static::getHeader(static::HEADER_CACHE_CONTROL); static::setHeader(static::HEADER_CACHE_CONTROL, 'public, no-cache');
$currentHeaders = []; static::setHeader(static::HEADER_ETAG, $etag);
if ($cacheControl) { }
foreach (preg_split('/\s*,\s*/', $cacheControl) as $tmp) {
$tmp = explode('=', $tmp);
$currentHeaders[$tmp[0]] = $tmp[1] ?? null;
}
}
$currentHeaders[strtr(strtolower($name), '_', '-')] = $value;
$headers = []; //always reload and re-execute this resource, disable any local or intermediary caching
foreach ($currentHeaders as $key => $currentVal) { public static function disableHttpCache()
$headers[] = $key . ($currentVal !== null ? '=' . $currentVal : ''); {
} static::setHeader(static::HEADER_CACHE_CONTROL, 'private, no-cache, no-store');
static::setHeader(static::HEADER_CACHE_CONTROL, implode(', ', $headers));
} }
public static function setHeader($name, $value) public static function setHeader($name, $value)