Merge branch 'close-reason'

This commit is contained in:
Chris Boden 2016-03-16 08:32:52 -04:00
commit 055b3eca13
3 changed files with 23 additions and 22 deletions

View File

@ -1,6 +1,7 @@
# RFC6455 - The WebSocket Protocol # RFC6455 - The WebSocket Protocol
[![Build Status](https://travis-ci.org/ratchetphp/RFC6455.svg?branch=master)](https://travis-ci.org/ratchetphp/RFC6455) [![Build Status](https://travis-ci.org/ratchetphp/RFC6455.svg?branch=master)](https://travis-ci.org/ratchetphp/RFC6455)
![Autobahn Testsuite](https://img.shields.io/badge/Autobahn-passing-brightgreen.svg)
This library a protocol handler for the RFC6455 specification. This library a protocol handler for the RFC6455 specification.
It contains components for both server and client side handshake and messaging protocol negotation. It contains components for both server and client side handshake and messaging protocol negotation.

View File

@ -182,7 +182,7 @@ class Frame implements FrameInterface {
throw call_user_func($this->ufeg, 'Not enough bytes received to determine reserved bit'); throw call_user_func($this->ufeg, 'Not enough bytes received to determine reserved bit');
} }
return 16 == ($this->firstByte & 16); return 16 === ($this->firstByte & 16);
} }
/** /**

View File

@ -93,7 +93,7 @@ class MessageBuffer {
if ($this->messageBuffer->isCoalesced()) { if ($this->messageBuffer->isCoalesced()) {
$msgCheck = $this->checkMessage($this->messageBuffer); $msgCheck = $this->checkMessage($this->messageBuffer);
if (true !== $msgCheck) { if (true !== $msgCheck) {
$onControl($this->newCloseFrame($msgCheck)); $onControl($this->newCloseFrame($msgCheck, 'Ratchet detected an invalid UTF-8 payload'));
} else { } else {
$onMessage($this->messageBuffer); $onMessage($this->messageBuffer);
} }
@ -116,18 +116,18 @@ class MessageBuffer {
false !== $frame->getRsv2() || false !== $frame->getRsv2() ||
false !== $frame->getRsv3() false !== $frame->getRsv3()
) { ) {
return $this->newCloseFrame(Frame::CLOSE_PROTOCOL); return $this->newCloseFrame(Frame::CLOSE_PROTOCOL, 'Ratchet detected an invalid reserve code');
} }
if ($this->checkForMask && !$frame->isMasked()) { if ($this->checkForMask && !$frame->isMasked()) {
return $this->newCloseFrame(Frame::CLOSE_PROTOCOL); return $this->newCloseFrame(Frame::CLOSE_PROTOCOL, 'Ratchet detected an incorrect frame mask');
} }
$opcode = $frame->getOpcode(); $opcode = $frame->getOpcode();
if ($opcode > 2) { if ($opcode > 2) {
if ($frame->getPayloadLength() > 125 || !$frame->isFinal()) { if ($frame->getPayloadLength() > 125 || !$frame->isFinal()) {
return $this->newCloseFrame(Frame::CLOSE_PROTOCOL); return $this->newCloseFrame(Frame::CLOSE_PROTOCOL, 'Ratchet detected a mismatch between final bit and indicated payload length');
} }
switch ($opcode) { switch ($opcode) {
@ -140,8 +140,8 @@ class MessageBuffer {
return $this->newCloseFrame(Frame::CLOSE_NORMAL); return $this->newCloseFrame(Frame::CLOSE_NORMAL);
} }
if (strlen($bin) == 1) { if (strlen($bin) === 1) {
return $this->newCloseFrame(Frame::CLOSE_PROTOCOL); return $this->newCloseFrame(Frame::CLOSE_PROTOCOL, 'Ratchet detected an invalid close code');
} }
if (strlen($bin) >= 2) { if (strlen($bin) >= 2) {
@ -150,11 +150,11 @@ class MessageBuffer {
$checker = $this->closeFrameChecker; $checker = $this->closeFrameChecker;
if (!$checker($closeCode)) { if (!$checker($closeCode)) {
return $this->newCloseFrame(Frame::CLOSE_PROTOCOL); return $this->newCloseFrame(Frame::CLOSE_PROTOCOL, 'Ratchet detected an invalid close code');
} }
if (!$this->checkUtf8(substr($bin, 2))) { if (!$this->checkUtf8(substr($bin, 2))) {
return $this->newCloseFrame(Frame::CLOSE_BAD_PAYLOAD); return $this->newCloseFrame(Frame::CLOSE_BAD_PAYLOAD, 'Ratchet detected an invalid UTF-8 payload in the close reason');
} }
return $frame; return $frame;
@ -163,19 +163,19 @@ class MessageBuffer {
case Frame::OP_PONG: case Frame::OP_PONG:
break; break;
default: default:
return $this->newCloseFrame(Frame::CLOSE_PROTOCOL); return $this->newCloseFrame(Frame::CLOSE_PROTOCOL, 'Ratchet detected an invalid OP code');
break; break;
} }
return $frame; return $frame;
} }
if (Frame::OP_CONTINUE == $frame->getOpcode() && 0 == count($this->messageBuffer)) { if (Frame::OP_CONTINUE === $frame->getOpcode() && 0 === count($this->messageBuffer)) {
return $this->newCloseFrame(Frame::CLOSE_PROTOCOL); return $this->newCloseFrame(Frame::CLOSE_PROTOCOL, 'Ratchet detected the first frame of a message was a continue');
} }
if (count($this->messageBuffer) > 0 && Frame::OP_CONTINUE != $frame->getOpcode()) { if (count($this->messageBuffer) > 0 && Frame::OP_CONTINUE !== $frame->getOpcode()) {
return $this->newCloseFrame(Frame::CLOSE_PROTOCOL); return $this->newCloseFrame(Frame::CLOSE_PROTOCOL, 'Ratchet detected invalid OP code when expecting continue frame');
} }
return $frame; return $frame;
@ -221,7 +221,7 @@ class MessageBuffer {
return new Frame($payload, $final, $opcode, $this->exceptionFactory); return new Frame($payload, $final, $opcode, $this->exceptionFactory);
} }
public function newCloseFrame($code) { public function newCloseFrame($code, $reason = '') {
return $this->newFrame(pack('n', $code), true, Frame::OP_CLOSE); return $this->newFrame(pack('n', $code) . $reason, true, Frame::OP_CLOSE);
} }
} }