*/
private $response;
+ /**
+ * @var string
+ */
+ private $responseCharset = "UTF-8";
+
/**
* @var \DOMDocument
*/
*/
private function readBody(): void
{
+ $this->validateHeaders();
+
$this->body = "";
while (!$this->response->getBody()->eof()) {
$this->body .= $this->response->getBody()->read(8192);
}
$this->response->getBody()->close();
- if ($this->getCharset() !== 'UTF-8') {
+ if ($this->responseCharset !== 'UTF-8') {
try {
- $this->body = StringUtil::convertEncoding($this->getCharset(), 'UTF-8', $this->body);
+ $this->body = StringUtil::convertEncoding($this->responseCharset, 'UTF-8', $this->body);
} catch (Exception $e) {
throw new ParsingFailed(
"Could not parse body, due an invalid charset.",
}
}
- private function getCharset(): string
+ private function validateHeaders(): void
{
$headers = $this->response->getHeader('content-type');
if (\count($headers) !== 1) {
if ($contentType !== 'text/html') {
throw new ParsingFailed("Expected 'text/html' as the 'content-type'.");
}
+
$charset = null;
foreach ($pieces as $parameter) {
$parts = ArrayUtil::trim(\explode('=', $parameter, 2));
}
}
- if (!$charset) {
- $charset = 'UTF-8';
+ if ($charset) {
+ $this->responseCharset = \mb_strtoupper($charset);
}
-
- return \mb_strtoupper($charset);
}
/**