fix,refac: ETag 오기록 수정, Cache 설정을 WebUtil로

This commit is contained in:
2022-04-04 01:44:35 +09:00
parent 0048b865c6
commit 2f417fa944
4 changed files with 71 additions and 50 deletions
+15 -37
View File
@@ -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());
+42
View File
@@ -36,6 +36,48 @@ class WebUtil
return strtolower($_SERVER['HTTP_X_REQUESTED_WITH'] ?? null) === 'xmlhttprequest';
}
public static function setCacheHeader(?APICacheResult $cache, int $maxAge=60){
header_remove('expires');
header("Cache-Control: private, max-age={$maxAge}");
header("Pragma: cache");
if($cache->etag !== null){
header("ETag: \"{$cache->etag}\"");
}
if($cache->lastModified !== null){
$lastModifiedUnixTime = Util::toInt(TimeUtil::DateTimeToSeconds($cache->lastModified, true));
$lastModified = gmdate("D, d M Y H:i:s", $lastModifiedUnixTime);
header("Last-Modified: $lastModified GMT");
}
}
public static function dieWithNotModified(): never{
header("HTTP/1.1 304 Not Modified");
die();
}
public static function parseETag(): ?string{
$etag = $_SERVER['HTTP_IF_NONE_MATCH']??null;
if($etag === null){
return $etag;
}
$etag = trim($etag);
if(str_starts_with($etag, 'W/"') && str_ends_with($etag, '"')){
return substr($etag, 3, strlen($etag) - 4);
}
if(str_starts_with($etag, '"') && str_ends_with($etag, '"')){
return substr($etag, 1, strlen($etag) - 2);
}
return $etag;
}
public static function parseLastModified(): ?\DateTimeInterface{
$modifiedSinceStr = $_SERVER['HTTP_IF_MODIFIED_SINCE']??null;
if($modifiedSinceStr === null){
return null;
}
return new \DateTimeImmutable($modifiedSinceStr, new \DateTimeZone("UTC"));
}
public static function requireAJAX(): void
{
if (!static::isAJAX()) {