From e424b7bacbd18af53720bb6beaf876e7351f5099 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Thu, 21 Sep 2023 12:45:24 -0400 Subject: [PATCH] Move System::httpError to BaseModule->httpError - This will ensure headers set in BaseModule->run will be carried in httpError scenarios - Deprecate httpError() method in Core\System --- src/BaseModule.php | 19 +++++++++++++++++++ src/Core/System.php | 5 ++--- src/Module/Filer/RemoveTag.php | 2 +- src/Module/Post/Share.php | 4 ++-- src/Module/Special/HTTPException.php | 16 +++++++++++----- 5 files changed, 35 insertions(+), 11 deletions(-) diff --git a/src/BaseModule.php b/src/BaseModule.php index d97cd60a94..5ebeec3c80 100644 --- a/src/BaseModule.php +++ b/src/BaseModule.php @@ -476,4 +476,23 @@ abstract class BaseModule implements ICanHandleRequests System::exit(); } + + /** + * Send HTTP status header and exit. + * + * @param integer $httpCode HTTP status result value + * @param string $message Error message. Optional. + * @param mixed $content Response body. Optional. + * @throws \Exception + */ + public function httpError(int $httpCode, string $message = '', $content = '') + { + if ($httpCode >= 400) { + $this->logger->debug('Exit with error', ['code' => $httpCode, 'message' => $message, 'callstack' => System::callstack(20), 'method' => $this->args->getMethod(), 'agent' => $this->server['HTTP_USER_AGENT'] ?? '']); + } + + $this->response->setStatus($httpCode, $message); + + $this->httpExit($content); + } } diff --git a/src/Core/System.php b/src/Core/System.php index e1e84b775b..48a4ce6141 100644 --- a/src/Core/System.php +++ b/src/Core/System.php @@ -341,6 +341,7 @@ class System * @param string $message Error message. Optional. * @param string $content Response body. Optional. * @throws \Exception + * @deprecated since 2023.09 Use BaseModule->httpError instead */ public static function httpError($httpCode, $message = '', $content = '') { @@ -348,10 +349,8 @@ class System Logger::debug('Exit with error', ['code' => $httpCode, 'message' => $message, 'callstack' => System::callstack(20), 'method' => DI::args()->getMethod(), 'agent' => $_SERVER['HTTP_USER_AGENT'] ?? '']); } DI::apiResponse()->setStatus($httpCode, $message); - DI::apiResponse()->addContent($content); - self::echoResponse(DI::apiResponse()->generate()); - self::exit(); + self::httpExit($content); } /** diff --git a/src/Module/Filer/RemoveTag.php b/src/Module/Filer/RemoveTag.php index 874fa90466..23486fcf09 100644 --- a/src/Module/Filer/RemoveTag.php +++ b/src/Module/Filer/RemoveTag.php @@ -54,7 +54,7 @@ class RemoveTag extends BaseModule protected function post(array $request = []) { - System::httpError($this->removeTag($request)); + $this->httpError($this->removeTag($request)); } protected function content(array $request = []): string diff --git a/src/Module/Post/Share.php b/src/Module/Post/Share.php index 210a3fa6bc..0219eb9953 100644 --- a/src/Module/Post/Share.php +++ b/src/Module/Post/Share.php @@ -57,12 +57,12 @@ class Share extends \Friendica\BaseModule { $post_id = $this->parameters['post_id']; if (!$post_id || !$this->session->getLocalUserId()) { - System::httpError(403); + $this->httpError(403); } $item = Post::selectFirst(['private', 'body', 'uri', 'plink', 'network'], ['id' => $post_id]); if (!$item || $item['private'] == Item::PRIVATE) { - System::httpError(404); + $this->httpError(404); } $shared = $this->contentItem->getSharedPost($item, ['uri']); diff --git a/src/Module/Special/HTTPException.php b/src/Module/Special/HTTPException.php index 2cac142fca..f9468e20bc 100644 --- a/src/Module/Special/HTTPException.php +++ b/src/Module/Special/HTTPException.php @@ -21,12 +21,12 @@ namespace Friendica\Module\Special; -use Friendica\App\Arguments; -use Friendica\App\Request; +use Friendica\App; use Friendica\Core\L10n; use Friendica\Core\Renderer; use Friendica\Core\Session\Model\UserSession; use Friendica\Core\System; +use Friendica\Module\Response; use Psr\Log\LoggerInterface; /** @@ -40,7 +40,7 @@ class HTTPException protected $l10n; /** @var LoggerInterface */ protected $logger; - /** @var Arguments */ + /** @var App\Arguments */ protected $args; /** @var bool */ protected $isSiteAdmin; @@ -49,7 +49,7 @@ class HTTPException /** @var string */ protected $requestId; - public function __construct(L10n $l10n, LoggerInterface $logger, Arguments $args, UserSession $session, Request $request, array $server = []) + public function __construct(L10n $l10n, LoggerInterface $logger, App\Arguments $args, UserSession $session, App\Request $request, array $server = []) { $this->logger = $logger; $this->l10n = $l10n; @@ -113,7 +113,13 @@ class HTTPException } } - System::httpError($e->getCode(), $e->getDescription(), $content); + // We can't use a constructor parameter for this response object because we + // are in an Exception context where we don't want an existing Response. + $response = new Response(); + $response->setStatus($e->getCode(), $e->getDescription()); + $response->addContent($content); + System::echoResponse($response->generate()); + System::exit(); } /** -- 2.39.5