Switch default charset to UTF-8 again.
This commit is contained in:
parent
ce855061ec
commit
77ab1e7f9f
1 changed files with 19 additions and 7 deletions
|
@ -22,18 +22,28 @@ class HttpRouter implements IRouter {
|
|||
|
||||
private array $contentHandlers = [];
|
||||
|
||||
private string $defaultCharSet;
|
||||
|
||||
private IErrorHandler $errorHandler;
|
||||
|
||||
public function __construct(
|
||||
string $charSet = '',
|
||||
IErrorHandler|string $errorHandler = 'html',
|
||||
bool $registerDefaultContentHandlers = true,
|
||||
) {
|
||||
$this->defaultCharSet = $charSet;
|
||||
$this->setErrorHandler($errorHandler);
|
||||
|
||||
if($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 {
|
||||
return $this->errorHandler;
|
||||
}
|
||||
|
@ -191,17 +201,19 @@ class HttpRouter implements IRouter {
|
|||
$response->setContent(new StringContent($result));
|
||||
|
||||
if(!$response->hasContentType()) {
|
||||
if(strtolower(substr($result, 0, 14)) === '<!doctype html')
|
||||
$response->setTypeHTML($this->getCharSet());
|
||||
else {
|
||||
$charset = strtolower(mb_preferred_mime_name(mb_detect_encoding($result)));
|
||||
|
||||
if(strtolower(substr($result, 0, 14)) === '<!doctype html')
|
||||
$response->setTypeHTML($charset);
|
||||
elseif(strtolower(substr($result, 0, 5)) === '<?xml')
|
||||
if(strtolower(substr($result, 0, 5)) === '<?xml')
|
||||
$response->setTypeXML($charset);
|
||||
else
|
||||
$response->setTypePlain($charset);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self::output($response->toResponse());
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue