[WebSocket] Fixed failing Hixie handshake bug

refs #80
This commit is contained in:
Chris Boden 2013-03-31 11:20:00 -04:00
parent f905e3ad1f
commit 7933d26269
3 changed files with 31 additions and 19 deletions

View File

@ -7,12 +7,10 @@ class RequestFactory extends GuzzleRequestFactory {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function create($method, $url, $headers = null, $body = null) { public function create($method, $url, $headers = null, $body = '') {
$c = $this->entityEnclosingRequestClass; $c = $this->entityEnclosingRequestClass;
$request = new $c($method, $url, $headers); $request = new $c($method, $url, $headers);
if ($body) { $request->setBody(EntityBody::factory($body));
$request->setBody(EntityBody::factory($body));
}
return $request; return $request;
} }

View File

@ -36,9 +36,15 @@ class Hixie76 implements VersionInterface {
/** /**
* @param \Guzzle\Http\Message\RequestInterface $request * @param \Guzzle\Http\Message\RequestInterface $request
* @return \Guzzle\Http\Message\Response * @return \Guzzle\Http\Message\Response
* @throws \UnderflowException If there hasn't been enough data received
*/ */
public function handshake(RequestInterface $request) { public function handshake(RequestInterface $request) {
$body = $this->sign($request->getHeader('Sec-WebSocket-Key1', true), $request->getHeader('Sec-WebSocket-Key2', true), (string)$request->getBody()); $body = substr($request->getBody(), 0, 8);
if (8 !== strlen($body)) {
throw new \UnderflowException("Not enough data received to issue challenge response");
}
$challenge = $this->sign($request->getHeader('Sec-WebSocket-Key1', true), $request->getHeader('Sec-WebSocket-Key2', true), $body);
$headers = array( $headers = array(
'Upgrade' => 'WebSocket' 'Upgrade' => 'WebSocket'
@ -47,7 +53,7 @@ class Hixie76 implements VersionInterface {
, 'Sec-WebSocket-Location' => 'ws://' . $request->getHeader('Host', true) . $request->getPath() , 'Sec-WebSocket-Location' => 'ws://' . $request->getHeader('Host', true) . $request->getPath()
); );
$response = new Response(101, $headers, $body); $response = new Response(101, $headers, $challenge);
$response->setStatus(101, 'WebSocket Protocol Handshake'); $response->setStatus(101, 'WebSocket Protocol Handshake');
return $response; return $response;

View File

@ -89,26 +89,34 @@ 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) {
try { if (isset($from->WebSocket->request)) {
if (null === ($request = $this->reqParser->onMessage($from, $msg))) { $from->WebSocket->request->getBody()->write($msg);
return; } else {
try {
if (null === ($request = $this->reqParser->onMessage($from, $msg))) {
return;
}
} catch (\OverflowException $oe) {
return $this->close($from, 413);
} }
} catch (\OverflowException $oe) {
return $this->close($from, 413); if (!$this->versioner->isVersionEnabled($request)) {
return $this->close($from);
}
$from->WebSocket->request = $request;
$from->WebSocket->version = $this->versioner->getVersion($request);
} }
if (!$this->versioner->isVersionEnabled($request)) { try {
return $this->close($from); $response = $from->WebSocket->version->handshake($from->WebSocket->request);
} catch (\UnderflowException $e) {
return;
} }
$from->WebSocket->request = $request;
$from->WebSocket->version = $this->versioner->getVersion($request);
$response = $from->WebSocket->version->handshake($request);
$response->setHeader('X-Powered-By', \Ratchet\VERSION); $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($from->WebSocket->request->getTokenizedHeader('Sec-WebSocket-Protocol', ',')))) {
$response->setHeader('Sec-WebSocket-Protocol', $agreedSubProtocols); $response->setHeader('Sec-WebSocket-Protocol', $agreedSubProtocols);
} }