fix,refac: ETag 오기록 수정, Cache 설정을 WebUtil로
This commit is contained in:
+15
-37
@@ -9,18 +9,6 @@ class APIHelper
|
||||
//static only
|
||||
}
|
||||
|
||||
private static function setCacheHeader(){
|
||||
header('cache-control: private, max-age=60');
|
||||
header("Pragma: cache");
|
||||
header_remove('expires');
|
||||
}
|
||||
|
||||
private static function DieWithNotModified(): never{
|
||||
static::setCacheHeader();
|
||||
header("HTTP/1.1 304 Not Modified");
|
||||
die();
|
||||
}
|
||||
|
||||
public static function launch(string $rootPath, string $actionPath, array $eParams = [], bool $loadRawInput = true)
|
||||
{
|
||||
if($loadRawInput){
|
||||
@@ -73,51 +61,41 @@ class APIHelper
|
||||
}
|
||||
}
|
||||
|
||||
$modifiedSince = isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])
|
||||
? new \DateTimeImmutable($_SERVER['HTTP_IF_MODIFIED_SINCE'], new \DateTimeZone("UTC"))
|
||||
: null;
|
||||
$reqEtags = isset($_SERVER['HTTP_IF_NONE_MATCH']) ? trim($_SERVER['HTTP_IF_NONE_MATCH']) : null;
|
||||
$modifiedSince = WebUtil::parseLastModified();
|
||||
$reqEtags = WebUtil::parseETag();
|
||||
|
||||
$result = $obj->launch($session, $modifiedSince, $reqEtags);
|
||||
if (is_string($result)) {
|
||||
Json::dieWithReason($result);
|
||||
}
|
||||
|
||||
$cacheResult = $obj->tryCache();
|
||||
$cache = $obj->tryCache();
|
||||
$setCache = false;
|
||||
if ($cacheResult !== null) {
|
||||
$lastModified = $cacheResult->lastModified;
|
||||
$etag = $cacheResult->etag;
|
||||
|
||||
if ($lastModified !== null) {
|
||||
header("Last-Modified: " . gmdate("D, d M Y H:i:s", Util::toInt(TimeUtil::DateTimeToSeconds($lastModified, true))) . " GMT");
|
||||
$setCache = true;
|
||||
}
|
||||
if ($etag !== null) {
|
||||
header("Etag: $etag");
|
||||
if ($cache !== null) {
|
||||
if($cache->lastModified !== null || $cache->etag !== null){
|
||||
$setCache = true;
|
||||
WebUtil::setCacheHeader($cache);
|
||||
}
|
||||
|
||||
if ($modifiedSince !== null && $lastModified !== null && TimeUtil::DateIntervalToSeconds($modifiedSince->diff($lastModified)) == 0) {
|
||||
static::DieWithNotModified();
|
||||
|
||||
if ($modifiedSince !== null && $cache->lastModified !== null){
|
||||
$lastModifiedUnixTime = Util::toInt(TimeUtil::DateTimeToSeconds($cache->lastModified, true));
|
||||
$modifiedSinceUnixTime = Util::toInt(TimeUtil::DateTimeToSeconds($modifiedSince));
|
||||
if($lastModifiedUnixTime === $modifiedSinceUnixTime){
|
||||
WebUtil::dieWithNotModified();
|
||||
}
|
||||
}
|
||||
if ($reqEtags !== null && $reqEtags === $etag) {
|
||||
static::DieWithNotModified();
|
||||
if ($reqEtags !== null && $reqEtags === $cache->etag) {
|
||||
WebUtil::dieWithNotModified();
|
||||
}
|
||||
}
|
||||
|
||||
if ($result === null) {
|
||||
if ($setCache) {
|
||||
static::setCacheHeader();
|
||||
}
|
||||
Json::die([
|
||||
'result' => true,
|
||||
'reason' => 'success'
|
||||
], $setCache ? 0 : Json::NO_CACHE);
|
||||
}
|
||||
if ($setCache) {
|
||||
static::setCacheHeader();
|
||||
}
|
||||
Json::die($result, $setCache ? 0 : Json::NO_CACHE);
|
||||
} catch (\Throwable $e) {
|
||||
Json::dieWithReason($e->getMessage());
|
||||
|
||||
Reference in New Issue
Block a user