Skip to content

Commit

Permalink
add cache header to response
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnRDOrazio committed Feb 6, 2022
1 parent 1716486 commit d2ae04d
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions includes/LitCalAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ( "<?xml version=\"1.0\" encoding=\"UTF-8\"?" . "><LiturgicalCalendar xmlns=\"https://www.bibleget.io/catholicliturgy\"/>" );
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();
}

Expand Down

0 comments on commit d2ae04d

Please sign in to comment.