Switch default charset to UTF-8 again.

This commit is contained in:
Pachira 2024-03-28 22:35:49 +00:00
parent ce855061ec
commit 77ab1e7f9f

View file

@ -22,18 +22,28 @@ class HttpRouter implements IRouter {
private array $contentHandlers = []; private array $contentHandlers = [];
private string $defaultCharSet;
private IErrorHandler $errorHandler; private IErrorHandler $errorHandler;
public function __construct( public function __construct(
string $charSet = '',
IErrorHandler|string $errorHandler = 'html', IErrorHandler|string $errorHandler = 'html',
bool $registerDefaultContentHandlers = true, bool $registerDefaultContentHandlers = true,
) { ) {
$this->defaultCharSet = $charSet;
$this->setErrorHandler($errorHandler); $this->setErrorHandler($errorHandler);
if($registerDefaultContentHandlers) if($registerDefaultContentHandlers)
$this->registerDefaultContentHandlers(); $this->registerDefaultContentHandlers();
} }
public function getCharSet(): string {
if($this->defaultCharSet === '')
return strtolower(mb_preferred_mime_name(mb_internal_encoding()));
return $this->defaultCharSet;
}
public function getErrorHandler(): IErrorHandler { public function getErrorHandler(): IErrorHandler {
return $this->errorHandler; return $this->errorHandler;
} }
@ -191,14 +201,16 @@ class HttpRouter implements IRouter {
$response->setContent(new StringContent($result)); $response->setContent(new StringContent($result));
if(!$response->hasContentType()) { if(!$response->hasContentType()) {
$charset = strtolower(mb_preferred_mime_name(mb_detect_encoding($result)));
if(strtolower(substr($result, 0, 14)) === '<!doctype html') if(strtolower(substr($result, 0, 14)) === '<!doctype html')
$response->setTypeHTML($charset); $response->setTypeHTML($this->getCharSet());
elseif(strtolower(substr($result, 0, 5)) === '<?xml') else {
$response->setTypeXML($charset); $charset = strtolower(mb_preferred_mime_name(mb_detect_encoding($result)));
else
$response->setTypePlain($charset); if(strtolower(substr($result, 0, 5)) === '<?xml')
$response->setTypeXML($charset);
else
$response->setTypePlain($charset);
}
} }
} }
} }