[WebSocket] Cleanup
Removed some obsolete code Handshakes always returns a response
This commit is contained in:
parent
412f5c2d07
commit
f0a277cec9
@ -5,7 +5,6 @@ use Ratchet\ConnectionInterface;
|
|||||||
use Ratchet\WebSocket\Guzzle\Http\Message\RequestFactory;
|
use Ratchet\WebSocket\Guzzle\Http\Message\RequestFactory;
|
||||||
use Ratchet\WebSocket\Version\VersionInterface;
|
use Ratchet\WebSocket\Version\VersionInterface;
|
||||||
use Guzzle\Http\Message\RequestInterface;
|
use Guzzle\Http\Message\RequestInterface;
|
||||||
use Guzzle\Http\Message\Response;
|
|
||||||
|
|
||||||
class HttpRequestParser implements MessageInterface {
|
class HttpRequestParser implements MessageInterface {
|
||||||
const EOM = "\r\n\r\n";
|
const EOM = "\r\n\r\n";
|
||||||
@ -18,9 +17,9 @@ class HttpRequestParser implements MessageInterface {
|
|||||||
public $maxSize = 4096;
|
public $maxSize = 4096;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param StdClass
|
* @param Ratchet\ConnectionInterface
|
||||||
* @param string Data stream to buffer
|
* @param string Data stream to buffer
|
||||||
* @return Guzzle\Http\Message\Response|null Response object if it's done parsing, null if there's more to be buffered
|
* @return Guzzle\Http\Message\RequestInterface|null
|
||||||
* @throws OverflowException
|
* @throws OverflowException
|
||||||
*/
|
*/
|
||||||
public function onMessage(ConnectionInterface $context, $data) {
|
public function onMessage(ConnectionInterface $context, $data) {
|
||||||
@ -32,8 +31,6 @@ class HttpRequestParser implements MessageInterface {
|
|||||||
|
|
||||||
if (strlen($context->httpBuffer) > (int)$this->maxSize) {
|
if (strlen($context->httpBuffer) > (int)$this->maxSize) {
|
||||||
throw new \OverflowException("Maximum buffer size of {$this->maxSize} exceeded parsing HTTP header");
|
throw new \OverflowException("Maximum buffer size of {$this->maxSize} exceeded parsing HTTP header");
|
||||||
|
|
||||||
//return new Response(413, array('X-Powered-By' => \Ratchet\VERSION));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($this->isEom($context->httpBuffer)) {
|
if ($this->isEom($context->httpBuffer)) {
|
||||||
|
@ -43,23 +43,17 @@ class RFC6455 implements VersionInterface {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
* @todo Decide what to do on failure...currently throwing an exception and I think socket connection is closed. Should be sending 40x error - but from where?
|
|
||||||
*/
|
*/
|
||||||
public function handshake(RequestInterface $request) {
|
public function handshake(RequestInterface $request) {
|
||||||
if (true !== $this->_verifier->verifyAll($request)) {
|
if (true !== $this->_verifier->verifyAll($request)) {
|
||||||
// new header with 4xx error message
|
return new Response(400);
|
||||||
|
|
||||||
throw new \InvalidArgumentException('Invalid HTTP header');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$headers = array(
|
return new Response(101, array(
|
||||||
'Upgrade' => 'websocket'
|
'Upgrade' => 'websocket'
|
||||||
, 'Connection' => 'Upgrade'
|
, 'Connection' => 'Upgrade'
|
||||||
, 'Sec-WebSocket-Accept' => $this->sign($request->getHeader('Sec-WebSocket-Key'))
|
, 'Sec-WebSocket-Accept' => $this->sign($request->getHeader('Sec-WebSocket-Key'))
|
||||||
, 'X-Powered-By' => \Ratchet\VERSION
|
));
|
||||||
);
|
|
||||||
|
|
||||||
return new Response(101, $headers);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -112,8 +106,11 @@ class RFC6455 implements VersionInterface {
|
|||||||
if ($opcode > 2) {
|
if ($opcode > 2) {
|
||||||
switch ($opcode) {
|
switch ($opcode) {
|
||||||
case $frame::OP_CLOSE:
|
case $frame::OP_CLOSE:
|
||||||
|
$from->close($frame->getPayload());
|
||||||
|
/*
|
||||||
$from->send($frame->unMaskPayload());
|
$from->send($frame->unMaskPayload());
|
||||||
$from->getConnection()->close();
|
$from->getConnection()->close();
|
||||||
|
*/
|
||||||
// $from->send(Frame::create(Frame::CLOSE_NORMAL, true, Frame::OP_CLOSE));
|
// $from->send(Frame::create(Frame::CLOSE_NORMAL, true, Frame::OP_CLOSE));
|
||||||
|
|
||||||
return;
|
return;
|
||||||
|
@ -2,17 +2,12 @@
|
|||||||
namespace Ratchet\WebSocket\Version\RFC6455;
|
namespace Ratchet\WebSocket\Version\RFC6455;
|
||||||
use Ratchet\ConnectionInterface;
|
use Ratchet\ConnectionInterface;
|
||||||
use Ratchet\AbstractConnectionDecorator;
|
use Ratchet\AbstractConnectionDecorator;
|
||||||
use Ratchet\WebSocket\Version\VersionInterface;
|
|
||||||
use Ratchet\WebSocket\Version\FrameInterface;
|
use Ratchet\WebSocket\Version\FrameInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
class Connection extends AbstractConnectionDecorator {
|
class Connection extends AbstractConnectionDecorator {
|
||||||
public function __construct(ConnectionInterface $conn) {
|
|
||||||
parent::__construct($conn);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function send($msg) {
|
public function send($msg) {
|
||||||
if (!($msg instanceof FrameInterface)) {
|
if (!($msg instanceof FrameInterface)) {
|
||||||
$msg = new Frame($msg);
|
$msg = new Frame($msg);
|
||||||
|
29
WsServer.php
29
WsServer.php
@ -81,24 +81,21 @@ class WsServer implements MessageComponentInterface {
|
|||||||
*/
|
*/
|
||||||
public function onMessage(ConnectionInterface $from, $msg) {
|
public function onMessage(ConnectionInterface $from, $msg) {
|
||||||
if (true !== $from->WebSocket->established) {
|
if (true !== $from->WebSocket->established) {
|
||||||
if (null === ($request = $this->reqParser->onMessage($from, $msg))) {
|
try {
|
||||||
return;
|
if (null === ($request = $this->reqParser->onMessage($from, $msg))) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} catch (\OverflowException $oe) {
|
||||||
|
return $this->close($from, 413);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$this->versioner->isVersionEnabled($request)) {
|
if (!$this->versioner->isVersionEnabled($request)) {
|
||||||
$response = new Response(400, array(
|
return $this->close($from);
|
||||||
'Sec-WebSocket-Version' => $this->versioner->getSupportedVersionString()
|
|
||||||
, 'X-Powered-By' => \Ratchet\VERSION
|
|
||||||
));
|
|
||||||
|
|
||||||
$from->send((string)$response);
|
|
||||||
$from->close();
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$from->WebSocket->version = $this->versioner->getVersion($request);
|
$from->WebSocket->version = $this->versioner->getVersion($request);
|
||||||
$response = $from->WebSocket->version->handshake($request);
|
$response = $from->WebSocket->version->handshake($request);
|
||||||
|
$response->setHeader('X-Powered-By', \Ratchet\VERSION);
|
||||||
|
|
||||||
// This needs to be refactored later on, incorporated with routing
|
// This needs to be refactored later on, incorporated with routing
|
||||||
if ('' !== ($agreedSubProtocols = $this->getSubProtocolString($request->getTokenizedHeader('Sec-WebSocket-Protocol', ',')))) {
|
if ('' !== ($agreedSubProtocols = $this->getSubProtocolString($request->getTokenizedHeader('Sec-WebSocket-Protocol', ',')))) {
|
||||||
@ -150,6 +147,16 @@ class WsServer implements MessageComponentInterface {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function close(ConnectionInterface $conn, $code = 400) {
|
||||||
|
$response = new Response($code, array(
|
||||||
|
'Sec-WebSocket-Version' => $this->versioner->getSupportedVersionString()
|
||||||
|
, 'X-Powered-By' => \Ratchet\VERSION
|
||||||
|
));
|
||||||
|
|
||||||
|
$conn->send((string)$response);
|
||||||
|
$conn->close();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string
|
* @param string
|
||||||
* @return boolean
|
* @return boolean
|
||||||
|
Loading…
Reference in New Issue
Block a user