public function testUnframeMatchesPreFraming() { $string = 'Hello World!'; $framed = $this->version->newFrame($string)->getContents(); $frame = new Frame(); $frame->addBuffer($framed); $this->assertEquals($string, $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->getContents() . $frame2->getContents()); $this->assertEquals($frame1->getContents(), $cat->getContents()); $this->assertEquals($string1, $cat->getPayload()); $uncat = new Frame(); $uncat->addBuffer($cat->extractOverflow()); $this->assertEquals($string1, $cat->getPayload()); $this->assertEquals($string2, $uncat->getPayload()); }
/** * Parse received data * * @param $response */ private function parseData($response) { if (!$this->connected && isset($response['Sec-Websocket-Accept'])) { if (base64_encode(pack('H*', sha1($this->key . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11'))) === $response['Sec-Websocket-Accept']) { $this->connected = true; } } if ($this->connected && !empty($response['content'])) { $content = trim($response['content']); $frame = new Frame(); $frame->addBuffer($content); $content = $frame->getPayload(); $content = utf8_encode($content); $data = json_decode($content, true); if (json_last_error() === JSON_ERROR_NONE) { unset($response['status']); unset($response['content']); $this->receiveData($data, $response); } else { echo 'JSON decode error [#' . json_last_error() . ']'; } $overflow = $frame->extractOverflow(); if ($overflow) { $this->parseData(array('content' => $overflow)); } } }
/** * There was a frame boundary issue when the first 3 bytes of a frame with a payload greater than * 126 was added to the frame buffer and then Frame::getPayloadLength was called. It would cause the frame * to set the payload length to 126 and then not recalculate it once the full length information was available. * * This is fixed by setting the defPayLen back to -1 before the underflow exception is thrown. * * @covers Ratchet\WebSocket\Version\RFC6455\Frame::getPayloadLength * @covers Ratchet\WebSocket\Version\RFC6455\Frame::extractOverflow */ public function testFrameDeliveredOneByteAtATime() { $startHeader = "~"; // header for a text frame of 256 - non-final $framePayload = str_repeat("*", 256); $rawOverflow = "xyz"; $rawFrame = $startHeader . $framePayload . $rawOverflow; $frame = new Frame(); $payloadLen = 256; for ($i = 0; $i < strlen($rawFrame); $i++) { $frame->addBuffer($rawFrame[$i]); try { // payloadLen will $payloadLen = $frame->getPayloadLength(); } catch (\UnderflowException $e) { if ($i > 2) { // we should get an underflow on 0,1,2 $this->fail("Underflow exception when the frame length should be available"); } } if ($payloadLen !== 256) { $this->fail("Payload length of " . $payloadLen . " should have been 256."); } } // make sure the overflow is good $this->assertEquals($rawOverflow, $frame->extractOverflow()); }