diff --git a/lib/Ratchet/Application/WebSocket/App.php b/lib/Ratchet/Application/WebSocket/App.php index 9feb7f8..edda0ef 100644 --- a/lib/Ratchet/Application/WebSocket/App.php +++ b/lib/Ratchet/Application/WebSocket/App.php @@ -18,6 +18,8 @@ use Guzzle\Http\Message\RequestFactory; * @todo Consider chaning this class to a State Pattern. If a WS App interface is passed use different state for additional methods used */ class App implements ApplicationInterface, ConfiguratorInterface { + const CRLF = "\r\n\r\n"; + /** * Decorated application * @var Ratchet\Application\ApplicationInterface @@ -87,6 +89,7 @@ class App implements ApplicationInterface, ConfiguratorInterface { } $headers = RequestFactory::fromMessage($from->WebSocket->headers); + $headers->setHeader('X-Body', $this->getBody($from->WebSocket->headers)); $from->WebSocket->version = $this->getVersion($headers); $from->WebSocket->headers = $headers; } @@ -236,10 +239,32 @@ class App implements ApplicationInterface, ConfiguratorInterface { /** * @param string * @return bool - * @todo Method is flawed, this CAN result in an error with the Hixie protocol + * @todo Abstract, some hard coding done for (stupid) Hixie protocol */ protected function isMessageComplete($message) { - return (boolean)strstr($message, "\r\n\r\n"); + $headers = (boolean)strstr($message, static::CRLF); + if (!$headers) { + + return false; + } + + if (strstr($message, 'Sec-WebSocket-Key2')) { + if (8 !== strlen($this->getBody($message))) { + return false; + } + } + + return true; + } + + /** + * @param string + * @return string + * @deprecated + * @todo Remove soon, this is a temp hack for Hixie + */ + protected function getBody($message) { + return substr($message, strpos($message, static::CRLF) + strlen(static::CRLF)); } /** diff --git a/lib/Ratchet/Application/WebSocket/Version/Hixie76.php b/lib/Ratchet/Application/WebSocket/Version/Hixie76.php index 5f98e3c..7a04958 100644 --- a/lib/Ratchet/Application/WebSocket/Version/Hixie76.php +++ b/lib/Ratchet/Application/WebSocket/Version/Hixie76.php @@ -24,7 +24,7 @@ class Hixie76 implements VersionInterface { * @return string */ public function handshake(RequestInterface $request) { - $message = $request->getRawHeaders() . $request->getResponse()->getBody(true); + $message = $request->getRawHeaders() . "\r\n\r\n" . $request->getHeader('X-Body'); $buffer = $message; $resource = $host = $origin = $key1 = $key2 = $protocol = $code = $handshake = null;