From 0326ac99bdff72decdf965764df81adc8694107b Mon Sep 17 00:00:00 2001 From: Chris Boden Date: Sat, 2 Jun 2012 21:11:29 -0400 Subject: [PATCH] [WebSocket] Frame overflow --- Version/RFC6455.php | 79 +-------------------------------------- Version/RFC6455/Frame.php | 24 +++++++++++- 2 files changed, 25 insertions(+), 78 deletions(-) diff --git a/Version/RFC6455.php b/Version/RFC6455.php index 59cbdde..c01613b 100644 --- a/Version/RFC6455.php +++ b/Version/RFC6455.php @@ -6,6 +6,7 @@ use Guzzle\Http\Message\Response; /** * @link http://tools.ietf.org/html/rfc6455 + * @todo Unicode: return mb_convert_encoding(pack("N",$u), mb_internal_encoding(), 'UCS-4BE'); */ class RFC6455 implements VersionInterface { const GUID = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11'; @@ -65,88 +66,12 @@ class RFC6455 implements VersionInterface { } /** - * Thanks to @lemmingzshadow for the code on decoding a HyBi-10 frame - * @link https://github.com/lemmingzshadow/php-websocket - * @todo look into what happens when false is returned here * @todo This is needed when a client is created - needs re-write as missing parts of protocol * @param string * @return string */ public function frame($message, $mask = true) { -return RFC6455\Frame::create($message)->data; - - $payload = $message; - $type = 'text'; - $masked = $mask; - - $frameHead = array(); - $frame = ''; - $payloadLength = strlen($payload); - - switch($type) { - case 'text': - // first byte indicates FIN, Text-Frame (10000001): - $frameHead[0] = 129; - break; - - case 'close': - // first byte indicates FIN, Close Frame(10001000): - $frameHead[0] = 136; - break; - - case 'ping': - // first byte indicates FIN, Ping frame (10001001): - $frameHead[0] = 137; - break; - - case 'pong': - // first byte indicates FIN, Pong frame (10001010): - $frameHead[0] = 138; - break; - } - - // set mask and payload length (using 1, 3 or 9 bytes) - if($payloadLength > 65535) { - $payloadLengthBin = str_split(sprintf('%064b', $payloadLength), 8); - $frameHead[1] = ($masked === true) ? 255 : 127; - for($i = 0; $i < 8; $i++) { - $frameHead[$i+2] = bindec($payloadLengthBin[$i]); - } - // most significant bit MUST be 0 (return false if to much data) - if($frameHead[2] > 127) { - return false; - } - } elseif($payloadLength > 125) { - $payloadLengthBin = str_split(sprintf('%016b', $payloadLength), 8); - $frameHead[1] = ($masked === true) ? 254 : 126; - $frameHead[2] = bindec($payloadLengthBin[0]); - $frameHead[3] = bindec($payloadLengthBin[1]); - } else { - $frameHead[1] = ($masked === true) ? $payloadLength + 128 : $payloadLength; - } - - // convert frame-head to string: - foreach(array_keys($frameHead) as $i) { - $frameHead[$i] = chr($frameHead[$i]); - } if($masked === true) { - // generate a random mask: - $mask = array(); - for($i = 0; $i < 4; $i++) - { - $mask[$i] = chr(rand(0, 255)); - } - - $frameHead = array_merge($frameHead, $mask); - } - $frame = implode('', $frameHead); - - // append payload to frame: - $framePayload = array(); - for($i = 0; $i < $payloadLength; $i++) { - $frame .= ($masked === true) ? $payload[$i] ^ $mask[$i % 4] : $payload[$i]; - } - - return $frame; + return RFC6455\Frame::create($message)->data; } /** diff --git a/Version/RFC6455/Frame.php b/Version/RFC6455/Frame.php index 5a7a7ff..af8e961 100644 --- a/Version/RFC6455/Frame.php +++ b/Version/RFC6455/Frame.php @@ -72,6 +72,7 @@ class Frame implements FrameInterface { } /** + * Encode the fake binary string to send over the wire * @param string of 1's and 0's * @return string */ @@ -110,7 +111,7 @@ class Frame implements FrameInterface { public function addBuffer($buf) { $buf = (string)$buf; - $this->data .= $buf; + $this->data .= $buf; $this->_bytes_rec += strlen($buf); } @@ -290,4 +291,25 @@ class Frame implements FrameInterface { return $payload; } + /** + * Sometimes clients will concatinate more than one frame over the wire + * This method will take the extra bytes off the end and return them + * @todo Consider returning new Frame + * @return string + */ + public function extractOverflow() { + if ($this->isCoalesced()) { + $endPoint = $this->getPayloadLength(); + $endPoint += $this->getPayloadStartingByte(); + + if ($this->_bytes_rec > $endPoint) { + $overflow = substr($this->data, $endPoint); + $this->data = substr($this->data, 0, $endPoint); + + return $overflow; + } + } + + return ''; + } } \ No newline at end of file