[WebSocket] Frame overflow
This commit is contained in:
parent
291bd5da5a
commit
7790ef39a1
@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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
|
||||
*/
|
||||
@ -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 '';
|
||||
}
|
||||
}
|
@ -241,8 +241,6 @@ class FrameTest extends \PHPUnit_Framework_TestCase {
|
||||
* @dataProvider UnframeMessageProvider
|
||||
*/
|
||||
public function testCheckPiecingTogetherMessage($msg, $encoded) {
|
||||
// return $this->markTestIncomplete('Ran out of time, had to attend to something else, come finish me!');
|
||||
|
||||
$framed = base64_decode($encoded);
|
||||
for ($i = 0, $len = strlen($framed);$i < $len; $i++) {
|
||||
$this->_frame->addBuffer(substr($framed, $i, 1));
|
||||
@ -265,6 +263,34 @@ class FrameTest extends \PHPUnit_Framework_TestCase {
|
||||
$this->assertEquals($pl, $frame->getPayload());
|
||||
}
|
||||
|
||||
public function testExtractOverflow() {
|
||||
$string1 = $this->generateRandomString();
|
||||
$frame1 = Frame::create($string1);
|
||||
|
||||
$string2 = $this->generateRandomString();
|
||||
$frame2 = Frame::create($string2);
|
||||
|
||||
$cat = new Frame;
|
||||
$cat->addBuffer($frame1->data . $frame2->data);
|
||||
|
||||
$this->assertEquals($string1, $cat->getPayload());
|
||||
|
||||
$uncat = new Frame;
|
||||
$uncat->addBuffer($cat->extractOverflow());
|
||||
|
||||
$this->assertEquals($string1, $cat->getPayload());
|
||||
$this->assertEquals($string2, $uncat->getPayload());
|
||||
}
|
||||
|
||||
public function testEmptyExtractOverflow() {
|
||||
$string = $this->generateRandomString();
|
||||
$frame = Frame::create($string);
|
||||
|
||||
$this->assertEquals($string, $frame->getPayload());
|
||||
$this->assertEquals('', $frame->extractOverflow());
|
||||
$this->assertEquals($string, $frame->getPayload());
|
||||
}
|
||||
|
||||
protected function generateRandomString($length = 10, $addSpaces = true, $addNumbers = true) {
|
||||
$characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"$%&/()=[]{}'; // ยง
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user