Separating protocol parsing, message handling

This commit is contained in:
Chris Boden 2014-09-08 22:05:03 -04:00
parent 8884b40f00
commit b685f6c928
9 changed files with 50 additions and 56 deletions

View File

@ -4,6 +4,7 @@ use Ratchet\AbstractConnectionDecorator;
use Ratchet\RFC6455\Version\DataInterface; use Ratchet\RFC6455\Version\DataInterface;
/** /**
* @deprecated
* {@inheritdoc} * {@inheritdoc}
* @property \StdClass $WebSocket * @property \StdClass $WebSocket
*/ */

View File

@ -1,48 +1,43 @@
<?php <?php
namespace Ratchet\RFC6455\Version; namespace Ratchet\RFC6455\Handshake;
use Ratchet\ConnectionInterface;
use Ratchet\MessageInterface;
use Ratchet\WebSocket\Version\RFC6455\HandshakeVerifier;
use Ratchet\WebSocket\Version\RFC6455\Message;
use Ratchet\WebSocket\Version\RFC6455\Frame;
use Ratchet\WebSocket\Version\RFC6455\Connection;
use Ratchet\WebSocket\Encoding\ValidatorInterface;
use Ratchet\WebSocket\Encoding\Validator;
use Guzzle\Http\Message\RequestInterface; use Guzzle\Http\Message\RequestInterface;
use Guzzle\Http\Message\Response; use Guzzle\Http\Message\Response;
// TODO remove all these
use Ratchet\ConnectionInterface;
use Ratchet\MessageInterface;
use Ratchet\RFC6455\Encoding\ValidatorInterface;
use Ratchet\RFC6455\Message\Message;
use Ratchet\RFC6455\Message\Frame;
use Ratchet\RFC6455\Message\Connection;
/** /**
* The latest version of the WebSocket protocol * The latest version of the WebSocket protocol
* @link http://tools.ietf.org/html/rfc6455
* @todo Unicode: return mb_convert_encoding(pack("N",$u), mb_internal_encoding(), 'UCS-4BE'); * @todo Unicode: return mb_convert_encoding(pack("N",$u), mb_internal_encoding(), 'UCS-4BE');
*/ */
class RFC6455 implements VersionInterface { class Negotiator implements NegotiatorInterface {
const GUID = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11'; /**
* @var \Ratchet\RFC6455\Handshake\RequestVerifier
*/
private $verifier;
/** /**
* @var RFC6455\HandshakeVerifier * @var \Ratchet\RFC6455\Encoding\ValidatorInterface
*/ */
protected $_verifier; private $validator;
/** /**
* A lookup of the valid close codes that can be sent in a frame * A lookup of the valid close codes that can be sent in a frame
* @var array * @var array
* @deprecated
*/ */
private $closeCodes = array(); private $closeCodes = array();
/** public function __construct(ValidatorInterface $validator) {
* @var \Ratchet\WebSocket\Encoding\ValidatorInterface $this->verifier = new RequestVerifier;
*/
protected $validator;
public function __construct(ValidatorInterface $validator = null) {
$this->_verifier = new HandshakeVerifier;
$this->setCloseCodes(); $this->setCloseCodes();
if (null === $validator) {
$validator = new Validator;
}
$this->validator = $validator; $this->validator = $validator;
} }
@ -66,7 +61,7 @@ class RFC6455 implements VersionInterface {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function handshake(RequestInterface $request) { public function handshake(RequestInterface $request) {
if (true !== $this->_verifier->verifyAll($request)) { if (true !== $this->verifier->verifyAll($request)) {
return new Response(400); return new Response(400);
} }
@ -78,6 +73,7 @@ class RFC6455 implements VersionInterface {
} }
/** /**
* @deprecated
* @param \Ratchet\ConnectionInterface $conn * @param \Ratchet\ConnectionInterface $conn
* @param \Ratchet\MessageInterface $coalescedCallback * @param \Ratchet\MessageInterface $coalescedCallback
* @return \Ratchet\WebSocket\Version\RFC6455\Connection * @return \Ratchet\WebSocket\Version\RFC6455\Connection
@ -95,6 +91,7 @@ class RFC6455 implements VersionInterface {
} }
/** /**
* @deprecated - The logic belons somewhere else
* @param \Ratchet\WebSocket\Version\RFC6455\Connection $from * @param \Ratchet\WebSocket\Version\RFC6455\Connection $from
* @param string $data * @param string $data
*/ */
@ -121,6 +118,7 @@ class RFC6455 implements VersionInterface {
return $from->close($frame::CLOSE_PROTOCOL); return $from->close($frame::CLOSE_PROTOCOL);
} }
// This is server-side specific logic
if (!$frame->isMasked()) { if (!$frame->isMasked()) {
return $from->close($frame::CLOSE_PROTOCOL); return $from->close($frame::CLOSE_PROTOCOL);
} }
@ -208,6 +206,7 @@ class RFC6455 implements VersionInterface {
} }
/** /**
* @deprecated
* @return RFC6455\Message * @return RFC6455\Message
*/ */
public function newMessage() { public function newMessage() {
@ -215,6 +214,7 @@ class RFC6455 implements VersionInterface {
} }
/** /**
* @deprecated
* @param string|null $payload * @param string|null $payload
* @param bool|null $final * @param bool|null $final
* @param int|null $opcode * @param int|null $opcode
@ -235,6 +235,7 @@ class RFC6455 implements VersionInterface {
} }
/** /**
* @deprecated
* Determine if a close code is valid * Determine if a close code is valid
* @param int|string * @param int|string
* @return bool * @return bool
@ -252,6 +253,7 @@ class RFC6455 implements VersionInterface {
} }
/** /**
* @deprecated
* Creates a private lookup of valid, private close codes * Creates a private lookup of valid, private close codes
*/ */
protected function setCloseCodes() { protected function setCloseCodes() {

View File

@ -1,13 +1,16 @@
<?php <?php
namespace Ratchet\RFC6455\Version; namespace Ratchet\RFC6455\Handshake;
use Ratchet\MessageInterface; use Ratchet\MessageInterface;
use Ratchet\ConnectionInterface; use Ratchet\ConnectionInterface;
use Guzzle\Http\Message\RequestInterface; use Guzzle\Http\Message\RequestInterface;
/** /**
* A standard interface for interacting with the various version of the WebSocket protocol * A standard interface for interacting with the various version of the WebSocket protocol
* @todo Look in to extension support
*/ */
interface VersionInterface extends MessageInterface { interface NegotiatorInterface {
const GUID = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11';
/** /**
* Given an HTTP header, determine if this version should handle the protocol * Given an HTTP header, determine if this version should handle the protocol
* @param \Guzzle\Http\Message\RequestInterface $request * @param \Guzzle\Http\Message\RequestInterface $request
@ -31,27 +34,18 @@ interface VersionInterface extends MessageInterface {
function handshake(RequestInterface $request); function handshake(RequestInterface $request);
/** /**
* @param \Ratchet\ConnectionInterface $conn * Add supported protocols. If the request has any matching the response will include one
* @param \Ratchet\MessageInterface $coalescedCallback * @param string $id
* @return \Ratchet\ConnectionInterface
*/ */
function upgradeConnection(ConnectionInterface $conn, MessageInterface $coalescedCallback); function addSupportedSubProtocol($id);
/** /**
* @return MessageInterface * If enabled and support for a subprotocol has been added handshake
* will not upgrade if a match between request and supported subprotocols
* @param boolean $enable
* @todo Consider extending this interface and moving this there.
* The spec does says the server can fail for this reason, but
it is not a requirement. This is an implementation detail.
*/ */
//function newMessage(); function setStrictSubProtocolCheck($enable);
/**
* @return FrameInterface
*/
//function newFrame();
/**
* @param string
* @param bool
* @return string
* @todo Change to use other classes, this will be removed eventually
*/
//function frame($message, $mask = true);
} }

View File

@ -1,5 +1,5 @@
<?php <?php
namespace Ratchet\RFC6455\Version\RFC6455; namespace Ratchet\RFC6455\Handshake;
use Guzzle\Http\Message\RequestInterface; use Guzzle\Http\Message\RequestInterface;
/** /**
@ -7,7 +7,7 @@ use Guzzle\Http\Message\RequestInterface;
* Verification rules come from section 4.2.1 of the RFC6455 document * Verification rules come from section 4.2.1 of the RFC6455 document
* @todo Currently just returning invalid - should consider returning appropriate HTTP status code error #s * @todo Currently just returning invalid - should consider returning appropriate HTTP status code error #s
*/ */
class HandshakeVerifier { class RequestVerifier {
/** /**
* Given an array of the headers this method will run through all verification methods * Given an array of the headers this method will run through all verification methods
* @param \Guzzle\Http\Message\RequestInterface $request * @param \Guzzle\Http\Message\RequestInterface $request

View File

@ -1,5 +1,5 @@
<?php <?php
namespace Ratchet\RFC6455\Version; namespace Ratchet\RFC6455\Messaging\Protocol;
interface DataInterface { interface DataInterface {
/** /**

View File

@ -1,6 +1,5 @@
<?php <?php
namespace Ratchet\RFC6455\Version\RFC6455; namespace Ratchet\RFC6455\Messaging\Protocol;
use Ratchet\RFC6455\Version\FrameInterface;
class Frame implements FrameInterface { class Frame implements FrameInterface {
const OP_CONTINUE = 0; const OP_CONTINUE = 0;

View File

@ -1,5 +1,5 @@
<?php <?php
namespace Ratchet\RFC6455\Version; namespace Ratchet\RFC6455\Messaging\Protocol;
interface FrameInterface extends DataInterface { interface FrameInterface extends DataInterface {
/** /**

View File

@ -1,7 +1,5 @@
<?php <?php
namespace Ratchet\RFC6455\Version\RFC6455; namespace Ratchet\RFC6455\Messaging\Protocol;
use Ratchet\RFC6455\Version\MessageInterface;
use Ratchet\RFC6455\Version\FrameInterface;
class Message implements MessageInterface, \Countable { class Message implements MessageInterface, \Countable {
/** /**

View File

@ -1,5 +1,5 @@
<?php <?php
namespace Ratchet\RFC6455\Version; namespace Ratchet\RFC6455\Messaging\Protocol;
interface MessageInterface extends DataInterface { interface MessageInterface extends DataInterface {
/** /**