diff --git a/src/Messaging/Streaming/MessageStreamer.php b/src/Messaging/Streaming/MessageStreamer.php index e0ef3bd..3b999d9 100644 --- a/src/Messaging/Streaming/MessageStreamer.php +++ b/src/Messaging/Streaming/MessageStreamer.php @@ -158,7 +158,7 @@ class MessageStreamer { return $this->newCloseFrame(Frame::CLOSE_PROTOCOL); } - if (!preg_match('//u', substr($bin, 2))) { + if (!$this->checkUtf8(substr($bin, 2))) { return $this->newCloseFrame(Frame::CLOSE_BAD_PAYLOAD); } @@ -193,7 +193,7 @@ class MessageStreamer { */ public function checkMessage(MessageInterface $message) { if (!$message->isBinary()) { - if (!preg_match('//u', $message->getPayload())) { + if (!$this->checkUtf8($message->getPayload())) { return Frame::CLOSE_BAD_PAYLOAD; } } @@ -201,6 +201,14 @@ class MessageStreamer { return true; } + private function checkUtf8($string) { + if (extension_loaded('mbstring')) { + return mb_check_encoding($string, 'UTF-8'); + } + + return preg_match('//u', $string); + } + /** * @return \Ratchet\RFC6455\Messaging\Protocol\MessageInterface */