Guzzle Typecasting

Updated all WebSocket protocol version to accept a Guzzle Request object
This commit is contained in:
Chris Boden 2012-01-06 16:43:02 -05:00
parent 08fa8a948f
commit e20a52dacc
5 changed files with 23 additions and 33 deletions

View File

@ -216,16 +216,14 @@ class App implements ApplicationInterface, ConfiguratorInterface {
* @todo Verify the first line of the HTTP header as per page 16 of RFC 6455
*/
protected function getVersion(RequestInterface $request) {
$headers = $request->getHeaders();
foreach ($this->_versions as $name => $instance) {
if (null !== $instance) {
if ($instance::isProtocol($headers)) {
if ($instance::isProtocol($request)) {
return $instance;
}
} else {
$ns = __NAMESPACE__ . "\\Version\\{$name}";
if ($ns::isProtocol($headers)) {
if ($ns::isProtocol($request)) {
$this->_versions[$name] = new $ns;
return $this->_versions[$name];
}

View File

@ -1,5 +1,6 @@
<?php
namespace Ratchet\Application\WebSocket\Version;
use Guzzle\Http\Message\RequestInterface;
/**
* FOR THE LOVE OF BEER, PLEASE PLEASE PLEASE DON'T allow the use of this in your application!
@ -14,15 +15,17 @@ namespace Ratchet\Application\WebSocket\Version;
* @link http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol-76
*/
class Hixie76 implements VersionInterface {
public static function isProtocol($headers) {
return isset($headers['Sec-WebSocket-Key2']);
public static function isProtocol(RequestInterface $request) {
return !(null === $request->getHeader('Sec-WebSocket-Key2'));
}
/**
* @param string
* @return string
*/
public function handshake($message) {
public function handshake(RequestInterface $request) {
$message = $request->getRawHeaders() . $request->getResponse()->getBody(true);
$buffer = $message;
$resource = $host = $origin = $key1 = $key2 = $protocol = $code = $handshake = null;

View File

@ -1,18 +1,14 @@
<?php
namespace Ratchet\Application\WebSocket\Version;
use Guzzle\Http\Message\RequestInterface;
/**
* @todo Note: Even though this is the "legacy" HyBi version, it's using the RFC Message and Frame classes - change if needed
*/
class HyBi10 extends RFC6455 {
public static function isProtocol($headers) {
if (isset($headers['Sec-WebSocket-Version'])) {
if ((int)$headers['Sec-WebSocket-Version'] >= 6 && (int)$headers['Sec-WebSocket-Version'] < 13) {
return true;
}
}
return false;
public static function isProtocol(RequestInterface $request) {
$version = (int)$request->getHeader('Sec-WebSocket-Version', -1);
return ($version >= 6 && $version < 13);
}
/**

View File

@ -21,14 +21,9 @@ class RFC6455 implements VersionInterface {
/**
* @todo Change the request to be a Guzzle RequestInterface
*/
public static function isProtocol($headers) {
if (isset($headers['Sec-WebSocket-Version'])) {
if ((int)$headers['Sec-WebSocket-Version'] == 13) {
return true;
}
}
return false;
public static function isProtocol(RequestInterface $request) {
$version = (int)$request->getHeader('Sec-WebSocket-Version', -1);
return (13 === $version);
}
/**
@ -36,11 +31,8 @@ class RFC6455 implements VersionInterface {
* I kept this as an array and combined in App for future considerations...easier to add a subprotol as a key value than edit a string
* @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($message) {
$headers = $message->getHeaders();
$key = $this->sign($headers['Sec-WebSocket-Key']);
if (true !== $this->_verifier->verifyAll($message)) {
public function handshake(RequestInterface $request) {
if (true !== $this->_verifier->verifyAll($request)) {
throw new \InvalidArgumentException('Invalid HTTP header');
}
@ -48,7 +40,7 @@ class RFC6455 implements VersionInterface {
'' => 'HTTP/1.1 101 Switching Protocols'
, 'Upgrade' => 'websocket'
, 'Connection' => 'Upgrade'
, 'Sec-WebSocket-Accept' => $this->sign($headers['Sec-WebSocket-Key'])
, 'Sec-WebSocket-Accept' => $this->sign($request->getHeader('Sec-WebSocket-Key'))
// , 'Sec-WebSocket-Protocol' => ''
);
}

View File

@ -1,5 +1,6 @@
<?php
namespace Ratchet\Application\WebSocket\Version;
use Guzzle\Http\Message\RequestInterface;
/**
* Despite the version iterations of WebInterface the actions they go through are similar
@ -10,21 +11,21 @@ namespace Ratchet\Application\WebSocket\Version;
interface VersionInterface {
/**
* Given an HTTP header, determine if this version should handle the protocol
* @param array
* @param Guzzle\Http\Message\RequestInterface
* @return bool
* @throws UnderflowException If the protocol thinks the headers are still fragmented
*/
static function isProtocol($headers);
static function isProtocol(RequestInterface $request);
/**
* Perform the handshake and return the response headers
* @param string
* @param Guzzle\Http\Message\RequestInterface
* @return array|string
* @throws InvalidArgumentException If the HTTP handshake is mal-formed
* @throws UnderflowException If the message hasn't finished buffering (not yet implemented, theoretically will only happen with Hixie version)
* @todo Change param to accept a Guzzle RequestInterface object
*/
function handshake($message);
function handshake(RequestInterface $request);
/**
* @return MessageInterface