From 8e0175494de31a2beddb1b5ef495073cd6995a26 Mon Sep 17 00:00:00 2001 From: Chris Boden Date: Fri, 1 Jun 2012 23:07:25 -0400 Subject: [PATCH] Removed most of the mbstring calls Moving forward we're going to assume `mbstring.func_overload` is off. For that reason we're not going to call `mb_` functions when checking byte level strings. --- HandshakeNegotiator.php | 4 ++-- Version/Hixie76.php | 2 +- Version/RFC6455/Frame.php | 25 ++++++++++++++----------- Version/RFC6455/HandshakeVerifier.php | 12 ++++++------ WsServer.php | 2 +- 5 files changed, 24 insertions(+), 21 deletions(-) diff --git a/HandshakeNegotiator.php b/HandshakeNegotiator.php index 3f19bf1..0cdef10 100644 --- a/HandshakeNegotiator.php +++ b/HandshakeNegotiator.php @@ -44,7 +44,7 @@ class HandshakeNegotiator { public function onData(WsConnection $conn, $data) { $conn->WebSocket->handshakeBuffer .= $data; - if (mb_strlen($conn->WebSocket->handshakeBuffer, '8bit') >= (int)$this->maxSize) { + if (strlen($conn->WebSocket->handshakeBuffer) >= (int)$this->maxSize) { return new Response(413, array('X-Powered-By' => \Ratchet\VERSION)); } @@ -59,7 +59,7 @@ class HandshakeNegotiator { } // TODO: confirm message is buffered - // Hixie requires the body to complete the handshake (6 characters long) + // Hixie requires the body to complete the handshake (6 characters long) - is that 6 ASCII or UTF-8 characters? // Update VersionInterface to check for this, ::canHandshake() maybe // return if can't, continue buffering diff --git a/Version/Hixie76.php b/Version/Hixie76.php index 6f91435..4601b51 100644 --- a/Version/Hixie76.php +++ b/Version/Hixie76.php @@ -68,7 +68,7 @@ class Hixie76 implements VersionInterface { } public function generateKeyNumber($key) { - if (0 === mb_substr_count($key, ' ', 'ASCII')) { + if (0 === substr_count($key, ' ')) { return ''; } diff --git a/Version/RFC6455/Frame.php b/Version/RFC6455/Frame.php index 4314b95..8a17d6a 100644 --- a/Version/RFC6455/Frame.php +++ b/Version/RFC6455/Frame.php @@ -48,7 +48,7 @@ class Frame implements FrameInterface { $buf = (string)$buf; $this->_data .= $buf; - $this->_bytes_rec += mb_strlen($buf, '8bit'); + $this->_bytes_rec += strlen($buf); } /** @@ -59,7 +59,7 @@ class Frame implements FrameInterface { throw new \UnderflowException('Not enough bytes received to determine if this is the final frame in message'); } - $fbb = sprintf('%08b', ord(mb_substr($this->_data, 0, 1, '8bit'))); + $fbb = sprintf('%08b', ord(substr($this->_data, 0, 1))); return (boolean)(int)$fbb[0]; } @@ -71,7 +71,7 @@ class Frame implements FrameInterface { throw new \UnderflowException("Not enough bytes received ({$this->_bytes_rec}) to determine if mask is set"); } - return (boolean)bindec(mb_substr(sprintf('%08b', ord(mb_substr($this->_data, 1, 1, '8bit'))), 0, 1, '8bit')); + return (boolean)bindec(substr(sprintf('%08b', ord(substr($this->_data, 1, 1))), 0, 1)); } /** @@ -82,7 +82,7 @@ class Frame implements FrameInterface { throw new \UnderflowException('Not enough bytes received to determine opcode'); } - return bindec(mb_substr(sprintf('%08b', ord(mb_substr($this->_data, 0, 1, '8bit'))), 4, 4, '8bit')); + return bindec(substr(sprintf('%08b', ord(substr($this->_data, 0, 1))), 4, 4)); } /** @@ -95,7 +95,7 @@ class Frame implements FrameInterface { throw new \UnderflowException('Not enough bytes received'); } - return ord(mb_substr($this->_data, 1, 1, '8bit')) & 127; + return ord(substr($this->_data, 1, 1)) & 127; } /** @@ -152,6 +152,7 @@ class Frame implements FrameInterface { if ($length_check <= 125) { $this->_pay_len_def = $length_check; + return $this->getPayloadLength(); } @@ -162,10 +163,11 @@ class Frame implements FrameInterface { $strings = array(); for ($i = 2; $i < $byte_length + 1; $i++) { - $strings[] = ord(mb_substr($this->_data, $i, 1, '8bit')); + $strings[] = ord(substr($this->_data, $i, 1)); } $this->_pay_len_def = bindec(vsprintf(str_repeat('%08b', $byte_length - 1), $strings)); + return $this->getPayloadLength(); } @@ -184,14 +186,14 @@ class Frame implements FrameInterface { throw new \UnderflowException('Not enough data buffered to calculate the masking key'); } - return mb_substr($this->_data, $start, $length, '8bit'); + return substr($this->_data, $start, $length); } /** * {@inheritdoc} */ public function getPayloadStartingByte() { - return 1 + $this->getNumPayloadBytes() + mb_strlen($this->getMaskingKey(), '8bit'); + return 1 + $this->getNumPayloadBytes() + strlen($this->getMaskingKey()); } /** @@ -210,13 +212,14 @@ class Frame implements FrameInterface { $start = $this->getPayloadStartingByte(); for ($i = 0; $i < $length; $i++) { - $payload .= mb_substr($this->_data, $i + $start, 1, '8bit') ^ mb_substr($mask, $i % 4, 1, '8bit'); + // Double check the RFC - is the masking byte level or character level? + $payload .= substr($this->_data, $i + $start, 1) ^ substr($mask, $i % 4, 1); } } else { - $payload = mb_substr($this->_data, $start, $this->getPayloadLength(), '8bit'); + $payload = substr($this->_data, $start, $this->getPayloadLength()); } - if (mb_strlen($payload, '8bit') !== $length) { + if (strlen($payload) !== $length) { // Is this possible? isCoalesced() math _should_ ensure if there is mal-formed data, it would return false throw new \UnexpectedValueException('Payload length does not match expected length'); } diff --git a/Version/RFC6455/HandshakeVerifier.php b/Version/RFC6455/HandshakeVerifier.php index e9172ef..93d7928 100644 --- a/Version/RFC6455/HandshakeVerifier.php +++ b/Version/RFC6455/HandshakeVerifier.php @@ -34,7 +34,7 @@ class HandshakeVerifier { * @return bool */ public function verifyMethod($val) { - return ('get' === mb_strtolower($val, 'ASCII')); + return ('get' === strtolower($val)); } /** @@ -59,7 +59,7 @@ class HandshakeVerifier { return false; } - return mb_check_encoding($val, 'ASCII'); + return mb_check_encoding($val, 'US-ASCII'); } /** @@ -78,7 +78,7 @@ class HandshakeVerifier { * @return bool */ public function verifyUpgradeRequest($val) { - return ('websocket' === mb_strtolower($val, 'ASCII')); + return ('websocket' === strtolower($val)); } /** @@ -87,13 +87,12 @@ class HandshakeVerifier { * @return bool */ public function verifyConnection($val) { - $val = mb_strtolower($val, 'ASCII'); + $val = strtolower($val); if ('upgrade' === $val) { return true; } - // todo change this to mb_eregi_replace $vals = explode(',', str_replace(', ', ',', $val)); return (false !== array_search('upgrade', $vals)); @@ -104,9 +103,10 @@ class HandshakeVerifier { * @param string|null * @return bool * @todo The spec says we don't need to base64_decode - can I just check if the length is 24 and not decode? + * @todo Check the spec to see what the encoding of the key could be */ public function verifyKey($val) { - return (16 === mb_strlen(base64_decode((string)$val), '8bit')); + return (16 === strlen(base64_decode((string)$val))); } /** diff --git a/WsServer.php b/WsServer.php index 40c3973..d1e0f2b 100644 --- a/WsServer.php +++ b/WsServer.php @@ -166,6 +166,6 @@ class WsServer implements MessageComponentInterface { } } - return mb_substr($string, 0, -1, 'ASCII'); + return substr($string, 0, -1); } } \ No newline at end of file