From 94e3dde2e3c35c999288af64463aed17353222f1 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Thu, 21 Sep 2023 09:17:38 -0400 Subject: [PATCH] Move dependency-less Page->exit to Core\System::echoResponse - The method doesn't actually exit - Fix a bug with header handling in System::echoResponse with numerical key header strings - Adding a full-string header with ICanCreateResponses->setHeader was resulting in a wrong header named after the numerical key --- src/App.php | 2 +- src/App/Page.php | 30 -------------------- src/Core/System.php | 39 +++++++++++++++++++++++--- src/Module/Media/Attachment/Upload.php | 2 +- src/Module/Media/Photo/Upload.php | 2 +- 5 files changed, 38 insertions(+), 37 deletions(-) diff --git a/src/App.php b/src/App.php index 10fee0cfc..a708abe64 100644 --- a/src/App.php +++ b/src/App.php @@ -716,7 +716,7 @@ class App } $this->logger->debug('Request processed sucessfully', ['response' => $response->getStatusCode(), 'address' => $_SERVER['REMOTE_ADDR'] ?? '', 'request' => $requeststring, 'referer' => $_SERVER['HTTP_REFERER'] ?? '', 'user-agent' => $_SERVER['HTTP_USER_AGENT'] ?? '']); - $page->exit($response); + System::echoResponse($response); } catch (HTTPException $e) { $this->logger->debug('Request processed with exception', ['response' => $e->getCode(), 'address' => $_SERVER['REMOTE_ADDR'] ?? '', 'request' => $requeststring, 'referer' => $_SERVER['HTTP_REFERER'] ?? '', 'user-agent' => $_SERVER['HTTP_USER_AGENT'] ?? '']); $httpException->rawContent($e); diff --git a/src/App/Page.php b/src/App/Page.php index e369d482e..364da32eb 100644 --- a/src/App/Page.php +++ b/src/App/Page.php @@ -401,36 +401,6 @@ class Page implements ArrayAccess $this->footerScripts[] = trim($url, '/'); } - /** - * Directly exit with the current response (include setting all headers) - * - * @param ResponseInterface $response - */ - public function exit(ResponseInterface $response) - { - header(sprintf("HTTP/%s %s %s", - $response->getProtocolVersion(), - $response->getStatusCode(), - $response->getReasonPhrase()) - ); - - foreach ($response->getHeaders() as $key => $header) { - if (is_array($header)) { - $header_str = implode(',', $header); - } else { - $header_str = $header; - } - - if (empty($key)) { - header($header_str); - } else { - header("$key: $header_str"); - } - } - - echo $response->getBody(); - } - /** * Executes the creation of the current page and prints it to the screen * diff --git a/src/Core/System.php b/src/Core/System.php index f15548346..5c2cd8dc1 100644 --- a/src/Core/System.php +++ b/src/Core/System.php @@ -32,6 +32,7 @@ use Friendica\Network\HTTPException\MovedPermanentlyException; use Friendica\Network\HTTPException\TemporaryRedirectException; use Friendica\Util\BasePath; use Friendica\Util\XML; +use Psr\Http\Message\ResponseInterface; use Psr\Log\LoggerInterface; /** @@ -274,6 +275,36 @@ class System return implode(', ', $callstack2); } + /** + * Display current response, including setting all headers + * + * @param ResponseInterface $response + */ + public static function echoResponse(ResponseInterface $response) + { + header(sprintf("HTTP/%s %s %s", + $response->getProtocolVersion(), + $response->getStatusCode(), + $response->getReasonPhrase()) + ); + + foreach ($response->getHeaders() as $key => $header) { + if (is_array($header)) { + $header_str = implode(',', $header); + } else { + $header_str = $header; + } + + if (is_int($key)) { + header($header_str); + } else { + header("$key: $header_str"); + } + } + + echo $response->getBody(); + } + /** * Generic XML return * Outputs a basic dfrn XML status structure to STDOUT, with a variable @@ -297,7 +328,7 @@ class System DI::apiResponse()->setType(Response::TYPE_XML); DI::apiResponse()->addContent(XML::fromArray(['result' => $result])); - DI::page()->exit(DI::apiResponse()->generate()); + self::echoResponse(DI::apiResponse()->generate()); self::exit(); } @@ -317,7 +348,7 @@ class System } DI::apiResponse()->setStatus($httpCode, $message); DI::apiResponse()->addContent($content); - DI::page()->exit(DI::apiResponse()->generate()); + self::echoResponse(DI::apiResponse()->generate()); self::exit(); } @@ -334,7 +365,7 @@ class System 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); - DI::page()->exit(DI::apiResponse()->generate()); + self::echoResponse(DI::apiResponse()->generate()); self::exit(); } @@ -363,7 +394,7 @@ class System public static function jsonExit($content, $content_type = 'application/json', int $options = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) { DI::apiResponse()->setType(Response::TYPE_JSON, $content_type); DI::apiResponse()->addContent(json_encode($content, $options)); - DI::page()->exit(DI::apiResponse()->generate()); + self::echoResponse(DI::apiResponse()->generate()); self::exit(); } diff --git a/src/Module/Media/Attachment/Upload.php b/src/Module/Media/Attachment/Upload.php index 50c20b364..f6b690343 100644 --- a/src/Module/Media/Attachment/Upload.php +++ b/src/Module/Media/Attachment/Upload.php @@ -150,7 +150,7 @@ class Upload extends \Friendica\BaseModule $this->response->addContent($message); } - $this->page->exit($this->response->generate()); + System::echoResponse($this->response->generate()); System::exit(); } } diff --git a/src/Module/Media/Photo/Upload.php b/src/Module/Media/Photo/Upload.php index e53ca35b8..d5c15ea5b 100644 --- a/src/Module/Media/Photo/Upload.php +++ b/src/Module/Media/Photo/Upload.php @@ -207,7 +207,7 @@ class Upload extends \Friendica\BaseModule $this->response->addContent($message); } - $this->page->exit($this->response->generate()); + System::echoResponse($this->response->generate()); System::exit(); } }