fix: api.php 설정이 전혀 동작하지 않던 문제 수정

This commit is contained in:
2022-04-02 01:14:46 +09:00
parent 79b9cc77b0
commit 26a6327fee
3 changed files with 27 additions and 14 deletions
+1 -1
View File
@@ -42,7 +42,7 @@ d_setting/*.php
*/d_setting/templates/*.php */d_setting/templates/*.php
!*.orig.php !*.orig.php
*/data/file_cache **/data/file_cache
**/old/* **/old/*
test.* test.*
+2 -3
View File
@@ -3,10 +3,9 @@ namespace sammo;
class APICacheResult{ class APICacheResult{
function __construct( function __construct(
public ?\DateTimeInterface $lastModified, public ?\DateTimeInterface $lastModified = null,
public ?string $etag, public ?string $etag = null,
) )
{ {
} }
} }
+24 -10
View File
@@ -19,17 +19,16 @@ class APIHelper
Json::dieWithReason($e->getMessage()); Json::dieWithReason($e->getMessage());
} }
if($actionPath !== null){ if ($actionPath !== null) {
if ($input && !is_array($input)) { if ($input && !is_array($input)) {
Json::dieWithReason('args가 array가 아닙니다.' . gettype($input)); Json::dieWithReason('args가 array가 아닙니다.' . gettype($input));
} }
if(!$input){ if (!$input) {
$input = []; $input = [];
} }
$actionArgs = $input; $actionArgs = $input;
} } else {
else{ if (!$input) {
if(!$input){
Json::dieWithReason("input이 비어있습니다. {$rawInput}"); Json::dieWithReason("input이 비어있습니다. {$rawInput}");
} }
@@ -55,7 +54,7 @@ class APIHelper
$sessionMode = $obj->getRequiredSessionMode(); $sessionMode = $obj->getRequiredSessionMode();
if ($sessionMode === BaseAPI::NO_SESSION) { if ($sessionMode === BaseAPI::NO_SESSION) {
$session = Session::getInstance();//XXX: NoSession이면 진짜 NoSession이어야..? $session = Session::getInstance(); //XXX: NoSession이면 진짜 NoSession이어야..?
} else { } else {
if ($sessionMode & BaseAPI::REQ_GAME_LOGIN) { if ($sessionMode & BaseAPI::REQ_GAME_LOGIN) {
$session = Session::requireGameLogin(); $session = Session::requireGameLogin();
@@ -81,34 +80,49 @@ class APIHelper
} }
$cacheResult = $obj->tryCache(); $cacheResult = $obj->tryCache();
$setCache = false;
if ($cacheResult !== null) { if ($cacheResult !== null) {
$lastModified = $cacheResult->lastModified; $lastModified = $cacheResult->lastModified;
$etag = $cacheResult->etag; $etag = $cacheResult->etag;
if ($lastModified !== null) { if ($lastModified !== null) {
header("Last-Modified: " . gmdate("D, d M Y H:i:s", Util::toInt(TimeUtil::DateTimeToSeconds($lastModified, true))) . " GMT"); header("Last-Modified: " . gmdate("D, d M Y H:i:s", Util::toInt(TimeUtil::DateTimeToSeconds($lastModified, true))) . " GMT");
$setCache = true;
} }
if ($etag !== null) { if ($etag !== null) {
header("Etag: $etag"); header("Etag: $etag");
$setCache = true;
} }
if ($modifiedSince !== null && $lastModified !== null && TimeUtil::DateIntervalToSeconds($modifiedSince->diff($lastModified)) == 0) { if ($modifiedSince !== null && $lastModified !== null && TimeUtil::DateIntervalToSeconds($modifiedSince->diff($lastModified)) == 0) {
header("HTTP/1.1 304 Not Modified"); header('cache-control: private, max-age=86400');
header("Pragma: cache");
header("HTTP/1.1 304 Not Modified", true, 304);
die(); die();
} }
if ($reqEtags !== null && $reqEtags === $etag) { if ($reqEtags !== null && $reqEtags === $etag) {
header("HTTP/1.1 304 Not Modified"); header('cache-control: private, max-age=86400');
header("Pragma: cache");
header("HTTP/1.1 304 Not Modified", true, 304);
die(); die();
} }
} }
if ($result === null) { if ($result === null) {
if ($setCache) {
header('cache-control: private, max-age=86400');
header("Pragma: cache");
}
Json::die([ Json::die([
'result' => true, 'result' => true,
'reason' => 'success' 'reason' => 'success'
], $cacheResult === null ? Json::NO_CACHE : 0); ], $setCache ? 0 : Json::NO_CACHE);
} }
Json::die($result, $cacheResult === null ? Json::NO_CACHE : 0); if ($setCache) {
header('cache-control: private, max-age=86400');
header("Pragma: cache");
}
Json::die($result, $setCache ? 0 : Json::NO_CACHE);
} catch (\Throwable $e) { } catch (\Throwable $e) {
Json::dieWithReason($e->getMessage()); Json::dieWithReason($e->getMessage());
} catch (mixed $e) { } catch (mixed $e) {