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
This commit is contained in:
Hypolite Petovan 2023-09-21 12:45:24 -04:00
parent da1416c07f
commit e424b7bacb
5 changed files with 35 additions and 11 deletions

View file

@ -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);
}
}

View file

@ -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);
}
/**

View file

@ -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

View file

@ -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']);

View file

@ -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();
}
/**