From f3669a58d778afde7ed41b6822c9ebd5a3e34c5c Mon Sep 17 00:00:00 2001 From: Ad Schellevis Date: Sun, 11 Aug 2024 15:21:52 +0200 Subject: [PATCH] MVC: Routing - remove setJsonContent and make sure Response->send() handles array types properly. closes https://github.com/opnsense/core/issues/7757 Since we already handle stream output in send() as well, it makes sense to push the render decision to that spot in full. --- .../mvc/app/controllers/OPNsense/Base/ApiControllerBase.php | 6 +++--- src/opnsense/mvc/app/library/OPNsense/Mvc/Response.php | 6 +++++- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/opnsense/mvc/app/controllers/OPNsense/Base/ApiControllerBase.php b/src/opnsense/mvc/app/controllers/OPNsense/Base/ApiControllerBase.php index 0821cb1ff..83b2af854 100644 --- a/src/opnsense/mvc/app/controllers/OPNsense/Base/ApiControllerBase.php +++ b/src/opnsense/mvc/app/controllers/OPNsense/Base/ApiControllerBase.php @@ -268,7 +268,7 @@ class ApiControllerBase extends ControllerRoot // not authenticated $this->response->setStatusCode(403, "Forbidden"); $this->response->setContentType('application/json', 'UTF-8'); - $this->response->setJsonContent(['status' => 403,'message' => 'Forbidden']); + $this->response->setContent(['status' => 403,'message' => 'Forbidden']); $this->response->send(); return false; } else { @@ -279,7 +279,7 @@ class ApiControllerBase extends ControllerRoot if ($dispatchError != null) { $this->response->setStatusCode(400, "Bad Request"); $this->response->setContentType('application/json', 'UTF-8'); - $this->response->setJsonContent(['status' => 400, 'message' => $dispatchError]); + $this->response->setContent(['status' => 400, 'message' => $dispatchError]); $this->response->send(); return false; } @@ -301,7 +301,7 @@ class ApiControllerBase extends ControllerRoot // not authenticated $this->response->setStatusCode(401, "Unauthorized"); $this->response->setContentType('application/json', 'UTF-8'); - $this->response->setJsonContent(['status' => 401, 'message' => 'Authentication Failed']); + $this->response->setContent(['status' => 401, 'message' => 'Authentication Failed']); $this->response->send(); return false; } else { diff --git a/src/opnsense/mvc/app/library/OPNsense/Mvc/Response.php b/src/opnsense/mvc/app/library/OPNsense/Mvc/Response.php index b003b25b5..8e3c3d194 100644 --- a/src/opnsense/mvc/app/library/OPNsense/Mvc/Response.php +++ b/src/opnsense/mvc/app/library/OPNsense/Mvc/Response.php @@ -107,7 +107,11 @@ class Response fpassthru($this->content); @fclose($this->content); } elseif (!empty($this->content)) { - echo $this->content; + if (is_array($this->content)) { + echo json_encode($this->content); + } else { + echo $this->content; + } } $this->sent = true;