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
This commit is contained in:
Hypolite Petovan 2023-09-21 09:17:38 -04:00
parent dd7bea4bd1
commit 94e3dde2e3
5 changed files with 38 additions and 37 deletions

View file

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

View file

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

View file

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

View file

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

View file

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