Use mbstring if available

HHVM seems to have mbstring loaded/enabled by default
This commit is contained in:
Chris Boden 2016-01-27 19:59:58 -05:00
parent 04a7b41d5a
commit f6bf0ca07c

View File

@ -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
*/