diff --git a/src/Dispatcher.php b/src/Dispatcher.php index c6431215..fb70a1ef 100644 --- a/src/Dispatcher.php +++ b/src/Dispatcher.php @@ -65,9 +65,12 @@ final class Dispatcher $json['__statements'] = $this->databaseConnection->getPDO()->getStatements(); } - $this->httpHelper->setResponseCode($code); - $this->httpHelper->setHeader('Content-Type', 'application/json'); - $this->httpHelper->outputJSON($json); + if (!$this->httpHelper->isRedirecting()) + { + $this->httpHelper->setResponseCode($code); + $this->httpHelper->setHeader('Content-Type', 'application/json'); + $this->httpHelper->outputJSON($json); + } return $json; } diff --git a/src/Helpers/HttpHelper.php b/src/Helpers/HttpHelper.php index f8a35a38..eeeeb36a 100644 --- a/src/Helpers/HttpHelper.php +++ b/src/Helpers/HttpHelper.php @@ -3,6 +3,8 @@ namespace Szurubooru\Helpers; class HttpHelper { + private $redirected = false; + public function setResponseCode($code) { http_response_code($code); @@ -67,4 +69,23 @@ class HttpHelper $requestUri = preg_replace('/\?.*$/', '', $requestUri); return $requestUri; } + + public function redirect($destination) + { + $this->setResponseCode(307); + $this->setHeader('Location', $destination); + $this->redirected = true; + } + + public function nonCachedRedirect($destination) + { + $this->setResponseCode(303); + $this->setHeader('Location', $destination); + $this->redirected = true; + } + + public function isRedirecting() + { + return $this->redirected; + } } diff --git a/src/Services/NetworkingService.php b/src/Services/NetworkingService.php index b7777f57..453cdd58 100644 --- a/src/Services/NetworkingService.php +++ b/src/Services/NetworkingService.php @@ -92,13 +92,11 @@ class NetworkingService public function redirect($destination) { - $this->httpHelper->setResponseCode(307); - $this->httpHelper->setHeader('Location', $destination); + $this->httpHelper->redirect($destination); } public function nonCachedRedirect($destination) { - $this->httpHelper->setResponseCode(303); - $this->httpHelper->setHeader('Location', $destination); + $this->httpHelper->nonCachedRedirect($destination); } }