Ejemplo n.º 1
0
 /**
  * Build the instance of the handshake from the client data buffer
  *
  * @return bool Whether a complete handshake request has been received
  */
 public function readClientHandshake(Buffer $buffer)
 {
     if (!$buffer->indexOf('/\\r?\\n\\r?\\n/', 0, Buffer::NEEDLE_REGEX) !== false) {
         return false;
     }
     $this->complete = true;
     $headers = $buffer->readLine('/\\r?\\n\\r?\\n/', Buffer::NEEDLE_REGEX);
     $this->request->parseString($headers);
     if ($this->request->getMethod() !== 'GET') {
         throw new \RangeException('Invalid request - method must be GET');
     }
     if ($this->request->getHttpVersion() < 1.1) {
         throw new \RangeException('Invalid request - request protocol version must be >=1.1');
     }
     $length = $this->request->getHeader('content-length');
     if ($length && current($length)) {
         throw new \RangeException('Invalid request - GET requests cannot contain a non-empty entity-body');
     }
     if (!$this->request->getHeader('host')) {
         throw new \RangeException('Invalid request - missing Host: header');
     }
     if (!($key = $this->request->getHeader('sec-websocket-key'))) {
         throw new \RangeException('Invalid request - missing Sec-Websocket-Key: header');
     }
     $key = current($key);
     $this->response->addHeader('Sec-WebSocket-Accept', $this->buildSignature($key));
     return true;
 }
 /**
  * Construct a frame from the current pending frame header and a data buffer
  *
  * @param \WebSocketServer\Socket\Buffer $buffer The data buffer
  *
  * @return \WebSocketServer\Socket\Frame The constructed frame
  */
 private function makeFrame(Buffer $buffer)
 {
     $data = $buffer->read($this->pendingFrameHeader['length']);
     if ($this->pendingFrameHeader['masked']) {
         $data = $this->unmaskData($data, $this->pendingFrameHeader['maskingKey']);
     }
     $frame = $this->frameFactory->create($this->pendingFrameHeader['fin'], $this->pendingFrameHeader['rsv'], $this->pendingFrameHeader['opcode'], $data);
     unset($this->pendingFrameHeader);
     return $frame;
 }