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 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,17 +201,19 @@ class HttpRouter implements IRouter {
|
||||||
$response->setContent(new StringContent($result));
|
$response->setContent(new StringContent($result));
|
||||||
|
|
||||||
if(!$response->hasContentType()) {
|
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)));
|
$charset = strtolower(mb_preferred_mime_name(mb_detect_encoding($result)));
|
||||||
|
|
||||||
if(strtolower(substr($result, 0, 14)) === '<!doctype html')
|
if(strtolower(substr($result, 0, 5)) === '<?xml')
|
||||||
$response->setTypeHTML($charset);
|
|
||||||
elseif(strtolower(substr($result, 0, 5)) === '<?xml')
|
|
||||||
$response->setTypeXML($charset);
|
$response->setTypeXML($charset);
|
||||||
else
|
else
|
||||||
$response->setTypePlain($charset);
|
$response->setTypePlain($charset);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
self::output($response->toResponse());
|
self::output($response->toResponse());
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue