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 * @todo Verify the first line of the HTTP header as per page 16 of RFC 6455
*/ */
protected function getVersion(RequestInterface $request) { protected function getVersion(RequestInterface $request) {
$headers = $request->getHeaders();
foreach ($this->_versions as $name => $instance) { foreach ($this->_versions as $name => $instance) {
if (null !== $instance) { if (null !== $instance) {
if ($instance::isProtocol($headers)) { if ($instance::isProtocol($request)) {
return $instance; return $instance;
} }
} else { } else {
$ns = __NAMESPACE__ . "\\Version\\{$name}"; $ns = __NAMESPACE__ . "\\Version\\{$name}";
if ($ns::isProtocol($headers)) { if ($ns::isProtocol($request)) {
$this->_versions[$name] = new $ns; $this->_versions[$name] = new $ns;
return $this->_versions[$name]; return $this->_versions[$name];
} }

View File

@ -1,5 +1,6 @@
<?php <?php
namespace Ratchet\Application\WebSocket\Version; 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! * 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 * @link http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol-76
*/ */
class Hixie76 implements VersionInterface { class Hixie76 implements VersionInterface {
public static function isProtocol($headers) { public static function isProtocol(RequestInterface $request) {
return isset($headers['Sec-WebSocket-Key2']); return !(null === $request->getHeader('Sec-WebSocket-Key2'));
} }
/** /**
* @param string * @param string
* @return string * @return string
*/ */
public function handshake($message) { public function handshake(RequestInterface $request) {
$message = $request->getRawHeaders() . $request->getResponse()->getBody(true);
$buffer = $message; $buffer = $message;
$resource = $host = $origin = $key1 = $key2 = $protocol = $code = $handshake = null; $resource = $host = $origin = $key1 = $key2 = $protocol = $code = $handshake = null;

View File

@ -1,18 +1,14 @@
<?php <?php
namespace Ratchet\Application\WebSocket\Version; 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 * @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 { class HyBi10 extends RFC6455 {
public static function isProtocol($headers) { public static function isProtocol(RequestInterface $request) {
if (isset($headers['Sec-WebSocket-Version'])) { $version = (int)$request->getHeader('Sec-WebSocket-Version', -1);
if ((int)$headers['Sec-WebSocket-Version'] >= 6 && (int)$headers['Sec-WebSocket-Version'] < 13) { return ($version >= 6 && $version < 13);
return true;
}
}
return false;
} }
/** /**

View File

@ -21,14 +21,9 @@ class RFC6455 implements VersionInterface {
/** /**
* @todo Change the request to be a Guzzle RequestInterface * @todo Change the request to be a Guzzle RequestInterface
*/ */
public static function isProtocol($headers) { public static function isProtocol(RequestInterface $request) {
if (isset($headers['Sec-WebSocket-Version'])) { $version = (int)$request->getHeader('Sec-WebSocket-Version', -1);
if ((int)$headers['Sec-WebSocket-Version'] == 13) { return (13 === $version);
return true;
}
}
return false;
} }
/** /**
@ -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 * 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? * @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) { public function handshake(RequestInterface $request) {
$headers = $message->getHeaders(); if (true !== $this->_verifier->verifyAll($request)) {
$key = $this->sign($headers['Sec-WebSocket-Key']);
if (true !== $this->_verifier->verifyAll($message)) {
throw new \InvalidArgumentException('Invalid HTTP header'); throw new \InvalidArgumentException('Invalid HTTP header');
} }
@ -48,7 +40,7 @@ class RFC6455 implements VersionInterface {
'' => 'HTTP/1.1 101 Switching Protocols' '' => 'HTTP/1.1 101 Switching Protocols'
, 'Upgrade' => 'websocket' , 'Upgrade' => 'websocket'
, 'Connection' => 'Upgrade' , 'Connection' => 'Upgrade'
, 'Sec-WebSocket-Accept' => $this->sign($headers['Sec-WebSocket-Key']) , 'Sec-WebSocket-Accept' => $this->sign($request->getHeader('Sec-WebSocket-Key'))
// , 'Sec-WebSocket-Protocol' => '' // , 'Sec-WebSocket-Protocol' => ''
); );
} }

View File

@ -1,5 +1,6 @@
<?php <?php
namespace Ratchet\Application\WebSocket\Version; namespace Ratchet\Application\WebSocket\Version;
use Guzzle\Http\Message\RequestInterface;
/** /**
* Despite the version iterations of WebInterface the actions they go through are similar * Despite the version iterations of WebInterface the actions they go through are similar
@ -10,21 +11,21 @@ namespace Ratchet\Application\WebSocket\Version;
interface VersionInterface { interface VersionInterface {
/** /**
* 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 array * @param Guzzle\Http\Message\RequestInterface
* @return bool * @return bool
* @throws UnderflowException If the protocol thinks the headers are still fragmented * @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 * Perform the handshake and return the response headers
* @param string * @param Guzzle\Http\Message\RequestInterface
* @return array|string * @return array|string
* @throws InvalidArgumentException If the HTTP handshake is mal-formed * @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) * @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 * @todo Change param to accept a Guzzle RequestInterface object
*/ */
function handshake($message); function handshake(RequestInterface $request);
/** /**
* @return MessageInterface * @return MessageInterface