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;
/**
* @deprecated
* {@inheritdoc}
* @property \StdClass $WebSocket
*/

View File

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

View File

@ -1,13 +1,16 @@
<?php
namespace Ratchet\RFC6455\Version;
namespace Ratchet\RFC6455\Handshake;
use Ratchet\MessageInterface;
use Ratchet\ConnectionInterface;
use Guzzle\Http\Message\RequestInterface;
/**
* 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
* @param \Guzzle\Http\Message\RequestInterface $request
@ -31,27 +34,18 @@ interface VersionInterface extends MessageInterface {
function handshake(RequestInterface $request);
/**
* @param \Ratchet\ConnectionInterface $conn
* @param \Ratchet\MessageInterface $coalescedCallback
* @return \Ratchet\ConnectionInterface
* Add supported protocols. If the request has any matching the response will include one
* @param string $id
*/
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();
/**
* @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);
function setStrictSubProtocolCheck($enable);
}

View File

@ -1,5 +1,5 @@
<?php
namespace Ratchet\RFC6455\Version\RFC6455;
namespace Ratchet\RFC6455\Handshake;
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
* @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
* @param \Guzzle\Http\Message\RequestInterface $request

View File

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

View File

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

View File

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

View File

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

View File

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