From 1a5aeaf5a47f505af2aed674fbac3ef71db6a427 Mon Sep 17 00:00:00 2001 From: Chris Boden Date: Tue, 15 Mar 2016 19:46:31 -0400 Subject: [PATCH 1/2] Added support for reason to close frame Also doing strict type checks on integers Add Autobahn badge --- README.md | 13 +++++++------ src/Messaging/Frame.php | 2 +- src/Messaging/MessageBuffer.php | 30 +++++++++++++++--------------- 3 files changed, 23 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 73aac8b..a23b491 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,13 @@ # RFC6455 - The WebSocket Protocol [![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. -It contains components for both server and client side handshake and messaging protocol negotation. +This library a protocol handler for the RFC6455 specification. +It contains components for both server and client side handshake and messaging protocol negotation. -Aspects that are left open to interpertation in the specification are also left open in this library. -It is up to the implementation to determine how those interpertations are to be dealt with. +Aspects that are left open to interpertation in the specification are also left open in this library. +It is up to the implementation to determine how those interpertations are to be dealt with. -This library is independent, framework agnostic, and does not deal with any I/O. -HTTP upgrade negotiation integration points are handled with PSR-7 interfaces. +This library is independent, framework agnostic, and does not deal with any I/O. +HTTP upgrade negotiation integration points are handled with PSR-7 interfaces. diff --git a/src/Messaging/Frame.php b/src/Messaging/Frame.php index ead5544..40f9eb2 100644 --- a/src/Messaging/Frame.php +++ b/src/Messaging/Frame.php @@ -182,7 +182,7 @@ class Frame implements FrameInterface { throw call_user_func($this->ufeg, 'Not enough bytes received to determine reserved bit'); } - return 16 == ($this->firstByte & 16); + return 16 === ($this->firstByte & 16); } /** diff --git a/src/Messaging/MessageBuffer.php b/src/Messaging/MessageBuffer.php index fc304b1..673142f 100644 --- a/src/Messaging/MessageBuffer.php +++ b/src/Messaging/MessageBuffer.php @@ -93,7 +93,7 @@ class MessageBuffer { if ($this->messageBuffer->isCoalesced()) { $msgCheck = $this->checkMessage($this->messageBuffer); if (true !== $msgCheck) { - $onControl($this->newCloseFrame($msgCheck)); + $onControl($this->newCloseFrame($msgCheck, 'Ratchet detected an invalid UTF-8 payload')); } else { $onMessage($this->messageBuffer); } @@ -116,18 +116,18 @@ class MessageBuffer { false !== $frame->getRsv2() || 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()) { - return $this->newCloseFrame(Frame::CLOSE_PROTOCOL); + return $this->newCloseFrame(Frame::CLOSE_PROTOCOL, 'Ratchet detected an incorrect frame mask'); } $opcode = $frame->getOpcode(); if ($opcode > 2) { 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) { @@ -140,8 +140,8 @@ class MessageBuffer { return $this->newCloseFrame(Frame::CLOSE_NORMAL); } - if (strlen($bin) == 1) { - return $this->newCloseFrame(Frame::CLOSE_PROTOCOL); + if (strlen($bin) === 1) { + return $this->newCloseFrame(Frame::CLOSE_PROTOCOL, 'Ratchet detected an invalid close code'); } if (strlen($bin) >= 2) { @@ -150,11 +150,11 @@ class MessageBuffer { $checker = $this->closeFrameChecker; 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))) { - 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 $this->newCloseFrame(Frame::CLOSE_NORMAL); @@ -163,19 +163,19 @@ class MessageBuffer { case Frame::OP_PONG: break; default: - return $this->newCloseFrame(Frame::CLOSE_PROTOCOL); + return $this->newCloseFrame(Frame::CLOSE_PROTOCOL, 'Ratchet detected an invalid OP code'); break; } return $frame; } - if (Frame::OP_CONTINUE == $frame->getOpcode() && 0 == count($this->messageBuffer)) { - return $this->newCloseFrame(Frame::CLOSE_PROTOCOL); + if (Frame::OP_CONTINUE === $frame->getOpcode() && 0 === count($this->messageBuffer)) { + 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()) { - return $this->newCloseFrame(Frame::CLOSE_PROTOCOL); + if (count($this->messageBuffer) > 0 && Frame::OP_CONTINUE !== $frame->getOpcode()) { + return $this->newCloseFrame(Frame::CLOSE_PROTOCOL, 'Ratchet detected invalid OP code when expecting continue frame'); } return $frame; @@ -221,7 +221,7 @@ class MessageBuffer { return new Frame($payload, $final, $opcode, $this->exceptionFactory); } - public function newCloseFrame($code) { - return $this->newFrame(pack('n', $code), true, Frame::OP_CLOSE); + public function newCloseFrame($code, $reason = '') { + return $this->newFrame(pack('n', $code) . $reason, true, Frame::OP_CLOSE); } } From 276c19cb3d89f6cc94ee9447e1c0d5ae28046009 Mon Sep 17 00:00:00 2001 From: Chris Boden Date: Tue, 15 Mar 2016 19:47:16 -0400 Subject: [PATCH 2/2] Bad markdown --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a23b491..7c09148 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # RFC6455 - The WebSocket Protocol [![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)] +![Autobahn Testsuite](https://img.shields.io/badge/Autobahn-passing-brightgreen.svg) This library a protocol handler for the RFC6455 specification. It contains components for both server and client side handshake and messaging protocol negotation.