From d2ae04de03a2b08360c7ab62e04830c41a34a0f7 Mon Sep 17 00:00:00 2001 From: John D'Orazio Date: Sun, 6 Feb 2022 23:33:07 +0100 Subject: [PATCH] add cache header to response --- includes/LitCalAPI.php | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/includes/LitCalAPI.php b/includes/LitCalAPI.php index 539b8eeb..03a3d80a 100644 --- a/includes/LitCalAPI.php +++ b/includes/LitCalAPI.php @@ -2139,39 +2139,43 @@ private function generateResponse() { $SerializeableLitCal->Metadata->FeastsMemorials = $this->Cal->getFeastsAndMemorials(); //make sure we have an engineCache folder for the current Version - if( realpath( "engineCache/v" . str_replace( ".","_",self::API_VERSION ) ) === false ){ - mkdir( "engineCache/v" . str_replace( ".","_",self::API_VERSION ), 0755, true ); + if( realpath( "engineCache/v" . str_replace( ".", "_", self::API_VERSION ) ) === false ) { + mkdir( "engineCache/v" . str_replace( ".", "_", self::API_VERSION ), 0755, true ); } switch ( $this->LitSettings->ReturnType ) { case ReturnType::JSON: - file_put_contents( $this->CACHEFILE, json_encode( $SerializeableLitCal ) ); - echo json_encode( $SerializeableLitCal ); + $response = json_encode( $SerializeableLitCal ); break; case ReturnType::XML: $jsonStr = json_encode( $SerializeableLitCal ); $jsonObj = json_decode( $jsonStr, true ); $xml = new SimpleXMLElement ( "" ); LitFunc::convertArray2XML( $jsonObj, $xml ); - file_put_contents( $this->CACHEFILE, $xml->asXML() ); - print $xml->asXML(); + $response = $xml->asXML(); break; case ReturnType::ICS: $infoObj = $this->getGithubReleaseInfo(); if( $infoObj->status === "success" ) { - $ical = $this->produceIcal( $SerializeableLitCal, $infoObj->obj ); - file_put_contents( $this->CACHEFILE, $ical ); - echo $ical; + $response = $this->produceIcal( $SerializeableLitCal, $infoObj->obj ); } else{ die( 'Error receiving or parsing info from github about latest release: '.$infoObj->message ); } break; default: - file_put_contents( $this->CACHEFILE, json_encode( $SerializeableLitCal ) ); - echo json_encode( $SerializeableLitCal ); + $response = json_encode( $SerializeableLitCal ); break; } + file_put_contents( $this->CACHEFILE, $response ); + $responseHash = md5( $response ); + header("Etag: \"{$responseHash}\""); + if (!empty( $_SERVER['HTTP_IF_NONE_MATCH'] ) && $_SERVER['HTTP_IF_NONE_MATCH'] === $responseHash) { + header( $_SERVER[ "SERVER_PROTOCOL" ] . " 304 Not Modified" ); + header('Content-Length: 0'); + } else { + echo $response; + } die(); }