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.
This commit is contained in:
Chris Boden 2012-06-01 23:07:25 -04:00
parent 0caa6e814b
commit 8e0175494d
5 changed files with 24 additions and 21 deletions

View File

@ -44,7 +44,7 @@ class HandshakeNegotiator {
public function onData(WsConnection $conn, $data) { public function onData(WsConnection $conn, $data) {
$conn->WebSocket->handshakeBuffer .= $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)); return new Response(413, array('X-Powered-By' => \Ratchet\VERSION));
} }
@ -59,7 +59,7 @@ class HandshakeNegotiator {
} }
// TODO: confirm message is buffered // 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 // Update VersionInterface to check for this, ::canHandshake() maybe
// return if can't, continue buffering // return if can't, continue buffering

View File

@ -68,7 +68,7 @@ class Hixie76 implements VersionInterface {
} }
public function generateKeyNumber($key) { public function generateKeyNumber($key) {
if (0 === mb_substr_count($key, ' ', 'ASCII')) { if (0 === substr_count($key, ' ')) {
return ''; return '';
} }

View File

@ -48,7 +48,7 @@ class Frame implements FrameInterface {
$buf = (string)$buf; $buf = (string)$buf;
$this->_data .= $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'); 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]; 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"); 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'); 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'); 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) { if ($length_check <= 125) {
$this->_pay_len_def = $length_check; $this->_pay_len_def = $length_check;
return $this->getPayloadLength(); return $this->getPayloadLength();
} }
@ -162,10 +163,11 @@ class Frame implements FrameInterface {
$strings = array(); $strings = array();
for ($i = 2; $i < $byte_length + 1; $i++) { 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)); $this->_pay_len_def = bindec(vsprintf(str_repeat('%08b', $byte_length - 1), $strings));
return $this->getPayloadLength(); return $this->getPayloadLength();
} }
@ -184,14 +186,14 @@ class Frame implements FrameInterface {
throw new \UnderflowException('Not enough data buffered to calculate the masking key'); 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} * {@inheritdoc}
*/ */
public function getPayloadStartingByte() { 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(); $start = $this->getPayloadStartingByte();
for ($i = 0; $i < $length; $i++) { 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 { } 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 // 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'); throw new \UnexpectedValueException('Payload length does not match expected length');
} }

View File

@ -34,7 +34,7 @@ class HandshakeVerifier {
* @return bool * @return bool
*/ */
public function verifyMethod($val) { public function verifyMethod($val) {
return ('get' === mb_strtolower($val, 'ASCII')); return ('get' === strtolower($val));
} }
/** /**
@ -59,7 +59,7 @@ class HandshakeVerifier {
return false; return false;
} }
return mb_check_encoding($val, 'ASCII'); return mb_check_encoding($val, 'US-ASCII');
} }
/** /**
@ -78,7 +78,7 @@ class HandshakeVerifier {
* @return bool * @return bool
*/ */
public function verifyUpgradeRequest($val) { public function verifyUpgradeRequest($val) {
return ('websocket' === mb_strtolower($val, 'ASCII')); return ('websocket' === strtolower($val));
} }
/** /**
@ -87,13 +87,12 @@ class HandshakeVerifier {
* @return bool * @return bool
*/ */
public function verifyConnection($val) { public function verifyConnection($val) {
$val = mb_strtolower($val, 'ASCII'); $val = strtolower($val);
if ('upgrade' === $val) { if ('upgrade' === $val) {
return true; return true;
} }
// todo change this to mb_eregi_replace
$vals = explode(',', str_replace(', ', ',', $val)); $vals = explode(',', str_replace(', ', ',', $val));
return (false !== array_search('upgrade', $vals)); return (false !== array_search('upgrade', $vals));
@ -104,9 +103,10 @@ class HandshakeVerifier {
* @param string|null * @param string|null
* @return bool * @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 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) { public function verifyKey($val) {
return (16 === mb_strlen(base64_decode((string)$val), '8bit')); return (16 === strlen(base64_decode((string)$val)));
} }
/** /**

View File

@ -166,6 +166,6 @@ class WsServer implements MessageComponentInterface {
} }
} }
return mb_substr($string, 0, -1, 'ASCII'); return substr($string, 0, -1);
} }
} }