Move System::httpExit to BaseModule->httpExit

- This will ensure headers set in BaseModule->run will be carried in httpExit scenarios
- Deprecate httpExit() method in Core\System
This commit is contained in:
Hypolite Petovan 2023-09-21 12:35:55 -04:00
parent 94e3dde2e3
commit da1416c07f
24 changed files with 58 additions and 32 deletions

View file

@ -496,7 +496,9 @@ class Page implements ArrayAccess
}
if ($_GET["mode"] == "raw") {
System::httpExit(substr($target->saveHTML(), 6, -8), Response::TYPE_HTML);
$response->withBody(Utils::streamFor($target->saveHTML()));
System::echoResponse($response);
System::exit();
}
}

View file

@ -27,6 +27,7 @@ use Friendica\Capabilities\ICanCreateResponses;
use Friendica\Core\Hook;
use Friendica\Core\L10n;
use Friendica\Core\Logger;
use Friendica\Core\System;
use Friendica\Model\User;
use Friendica\Module\Response;
use Friendica\Module\Special\HTTPException as ModuleHTTPException;
@ -106,8 +107,7 @@ abstract class BaseModule implements ICanHandleRequests
*/
protected function rawContent(array $request = [])
{
// echo '';
// exit;
// $this->httpExit(...);
}
/**
@ -234,7 +234,8 @@ abstract class BaseModule implements ICanHandleRequests
$timestamp = microtime(true);
// "rawContent" is especially meant for technical endpoints.
// This endpoint doesn't need any theme initialization or other comparable stuff.
// This endpoint doesn't need any theme initialization or
// templating and is expected to exit on its own if it is set.
$this->rawContent($request);
try {
@ -456,4 +457,23 @@ abstract class BaseModule implements ICanHandleRequests
return $tabs;
}
/**
* This function adds the content and a content-type HTTP header to the output.
* After finishing the process is getting killed.
*
* @param string $content
* @param string $type
* @param string|null $content_type
* @return void
* @throws HTTPException\InternalServerErrorException
*/
public function httpExit(string $content, string $type = Response::TYPE_HTML, ?string $content_type = null)
{
$this->response->setType($type, $content_type);
$this->response->addContent($content);
System::echoResponse($this->response->generate());
System::exit();
}
}

View file

@ -70,7 +70,7 @@ interface ICanCreateResponses
*
* @throws InternalServerErrorException
*/
public function setType(string $type, ?string $content_type = null): void;
public function setType(string $type = ICanCreateResponses::TYPE_HTML, ?string $content_type = null): void;
/**
* Sets the status and the reason for the response

View file

@ -28,6 +28,7 @@ use Friendica\DI;
use Friendica\Model\User;
use Friendica\Module\Response;
use Friendica\Network\HTTPException\FoundException;
use Friendica\Network\HTTPException\InternalServerErrorException;
use Friendica\Network\HTTPException\MovedPermanentlyException;
use Friendica\Network\HTTPException\TemporaryRedirectException;
use Friendica\Util\BasePath;
@ -357,12 +358,15 @@ class System
* This function adds the content and a content-type HTTP header to the output.
* After finishing the process is getting killed.
*
* @param string $content
* @param string $type
* @param string $content
* @param string $type
* @param string|null $content_type
* @return void
* @throws InternalServerErrorException
* @deprecated since 2023.09 Use BaseModule->httpExit() instead
*/
public static function httpExit(string $content, string $type = Response::TYPE_HTML, ?string $content_type = null) {
public static function httpExit(string $content, string $type = Response::TYPE_HTML, ?string $content_type = null)
{
DI::apiResponse()->setType($type, $content_type);
DI::apiResponse()->addContent($content);
self::echoResponse(DI::apiResponse()->generate());

View file

@ -183,7 +183,7 @@ class API extends BaseModule
if (strcmp($finish, $start) < 0 && !$noFinish) {
if ($isPreview) {
System::httpExit($this->t('Event can not end before it has started.'));
$this->httpExit($this->t('Event can not end before it has started.'));
} else {
$this->sysMessages->addNotice($this->t('Event can not end before it has started.'));
$this->baseUrl->redirect($redirectOnError);
@ -192,7 +192,7 @@ class API extends BaseModule
if (empty($summary) || ($start === DBA::NULL_DATETIME)) {
if ($isPreview) {
System::httpExit($this->t('Event title and start time are required.'));
$this->httpExit($this->t('Event title and start time are required.'));
} else {
$this->sysMessages->addNotice($this->t('Event title and start time are required.'));
$this->baseUrl->redirect($redirectOnError);
@ -251,7 +251,7 @@ class API extends BaseModule
];
if (intval($request['preview'])) {
System::httpExit(Event::getHTML($datarray));
$this->httpExit(Event::getHTML($datarray));
}
$eventId = Event::store($datarray);

View file

@ -81,6 +81,6 @@ class Show extends BaseModule
'$event' => $tplEvent,
]);
System::httpExit($o);
$this->httpExit($o);
}
}

View file

@ -115,6 +115,6 @@ class Hovercard extends BaseModule
],
]);
System::httpExit($o);
$this->httpExit($o);
}
}

View file

@ -48,6 +48,6 @@ class Poll extends BaseModule
}
$last_update = $request['last_update'] ?? '';
System::httpExit(OStatus::feed($owner['nickname'], $last_update, 10) ?? '', Response::TYPE_ATOM);
$this->httpExit(OStatus::feed($owner['nickname'], $last_update, 10) ?? '', Response::TYPE_ATOM);
}
}

View file

@ -85,6 +85,6 @@ class Fetch extends BaseModule
$xml = Diaspora::buildPostXml($status["type"], $status["message"]);
// Send the envelope
System::httpExit(Diaspora::buildMagicEnvelope($xml, $user), Response::TYPE_XML, 'application/magic-envelope+xml');
$this->httpExit(Diaspora::buildMagicEnvelope($xml, $user), Response::TYPE_XML, 'application/magic-envelope+xml');
}
}

View file

@ -71,6 +71,6 @@ class Feed extends BaseModule
$feed = ProtocolFeed::atom($owner, $last_update, 10, $type);
System::httpExit($feed, Response::TYPE_ATOM);
$this->httpExit($feed, Response::TYPE_ATOM);
}
}

View file

@ -86,6 +86,6 @@ class Feed extends BaseModule
throw new HTTPException\InternalServerErrorException($this->t('The feed for this item is unavailable.', ['uri-id' => $uriId]));
}
System::httpExit($xml, Response::TYPE_ATOM);
$this->httpExit($xml, Response::TYPE_ATOM);
}
}

View file

@ -80,7 +80,7 @@ class Browser extends BaseModule
]);
if (empty($request['mode'])) {
System::httpExit($output);
$this->httpExit($output);
}
return $output;

View file

@ -91,7 +91,7 @@ class Browser extends BaseModule
]);
if (empty($request['mode'])) {
System::httpExit($output);
$this->httpExit($output);
}
return $output;

View file

@ -303,7 +303,7 @@ class Ping extends BaseModule
if (isset($_GET['callback'])) {
// JSONP support
System::httpExit($_GET['callback'] . '(' . json_encode(['result' => $data]) . ')', Response::TYPE_BLANK, 'application/javascript');
$this->httpExit($_GET['callback'] . '(' . json_encode(['result' => $data]) . ')', Response::TYPE_BLANK, 'application/javascript');
} else {
System::jsonExit(['result' => $data]);
}

View file

@ -155,6 +155,6 @@ class PubSub extends \Friendica\BaseModule
$this->logger->notice('Success for contact.', ['mode' => $hub_mode, 'contact' => $contact_id]);
}
System::httpExit($hub_challenge);
$this->httpExit($hub_challenge);
}
}

View file

@ -85,6 +85,6 @@ class OpenSearch extends BaseModule
'template' => "$baseUrl/opensearch",
]);
System::httpExit($xml->saveXML(), Response::TYPE_XML, 'application/opensearchdescription+xml');
$this->httpExit($xml->saveXML(), Response::TYPE_XML, 'application/opensearchdescription+xml');
}
}

View file

@ -102,7 +102,7 @@ class ParseUrl extends BaseModule
if ($format == 'json') {
System::jsonExit($arr['text']);
} else {
System::httpExit($arr['text']);
$this->httpExit($arr['text']);
}
}
@ -135,7 +135,7 @@ class ParseUrl extends BaseModule
System::jsonExit($ret);
} else {
System::httpExit(BBCode::embedURL($url, empty($_GET['noAttachment']), $title, $description, $_GET['tags'] ?? ''));
$this->httpExit(BBCode::embedURL($url, empty($_GET['noAttachment']), $title, $description, $_GET['tags'] ?? ''));
}
}
}

View file

@ -166,9 +166,9 @@ class PermissionTooltip extends \Friendica\BaseModule
}
if (!empty($l)) {
System::httpExit($o . implode(', ', $l));
$this->httpExit($o . implode(', ', $l));
} else {
System::httpExit($o . $receivers);;
$this->httpExit($o . $receivers);;
}
}

View file

@ -74,6 +74,6 @@ class Share extends \Friendica\BaseModule
$content = '[share]' . $item['uri'] . '[/share]';
}
System::httpExit($content);
$this->httpExit($content);
}
}

View file

@ -45,7 +45,7 @@ class PublicRSAKey extends BaseModule
throw new BadRequestException();
}
System::httpExit(
$this->httpExit(
Salmon::salmonKey($user['spubkey']),
Response::TYPE_BLANK,
'application/magic-public-key'

View file

@ -67,6 +67,6 @@ class ReallySimpleDiscovery extends BaseModule
],
],
]);
System::httpExit($content, Response::TYPE_XML);
$this->httpExit($content, Response::TYPE_XML);
}
}

View file

@ -89,7 +89,7 @@ class Response implements ICanCreateResponses
/**
* {@inheritDoc}
*/
public function setType(string $type, ?string $content_type = null): void
public function setType(string $type = Response::TYPE_HTML, ?string $content_type = null): void
{
if (!in_array($type, static::ALLOWED_TYPES)) {
throw new InternalServerErrorException('wrong type');

View file

@ -90,6 +90,6 @@ class HostMeta extends BaseModule
],
], $xml, false, ['hm' => 'http://host-meta.net/xrd/1.0', 'mk' => 'http://salmon-protocol.org/ns/magic-key']);
System::httpExit($xml->saveXML(), Response::TYPE_XML, 'application/xrd+xml');
$this->httpExit($xml->saveXML(), Response::TYPE_XML, 'application/xrd+xml');
}
}

View file

@ -330,6 +330,6 @@ class Xrd extends BaseModule
]);
header('Access-Control-Allow-Origin: *');
System::httpExit($xmlString, Response::TYPE_XML, 'application/xrd+xml');
$this->httpExit($xmlString, Response::TYPE_XML, 'application/xrd+xml');
}
}