Skip to content

Commit 1def343

Browse files
author
Martijn Croonen
committed
Allow more than 1 protocol version (handshake only)
1 parent cb3af02 commit 1def343

File tree

1 file changed

+37
-3
lines changed

1 file changed

+37
-3
lines changed

App/Net/WebSocketConnection.php

+37-3
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,20 @@ class WebSocketConnection extends Socket
158158
*/
159159
public static $aInConnectingState = array();
160160

161+
/**
162+
* The protocol versions that this code is able to handle
163+
*
164+
* @var array contains protocol versions that this code is able to handle
165+
*/
166+
protected $m_aAcceptedVersions = array(13);
167+
168+
/**
169+
* The protocol version in use on this connection
170+
*
171+
* @var array reference to the array containing all connections in the CONNECTION state
172+
*/
173+
public static $sProtocolVersion = 0;
174+
161175
/**
162176
* @var boolean True if the opening handshake from the client has been read
163177
*/
@@ -390,10 +404,30 @@ protected function parseHandshake()
390404

391405
/**
392406
* The Sec-WebSocket-Version is a non-optional part of the opening handshake and should
393-
* have the value 8. We don't support older versions of the protocol */
394-
if ($this -> getHeader('Sec-WebSocket-Version') != '13') {
395-
$this -> write('HTTP/1.1 400 Bad Request' . "\r\n\r\n");
407+
* have a value from $m_aAcceptedVersions. We don't support all versions of the protocol
408+
* First build an array of versions accepted by the client*/
409+
$aClientVersions = explode(',', $this -> getHeader('Sec-WebSocket-Version'));
410+
411+
/* Trim values */
412+
array_map('trim', $aClientVersions);
413+
414+
/* Get in common values. The protocols that appear first in the $m_aAcceptedVersions array
415+
* have the preference. */
416+
$aInCommomVersions = array_intersect($this -> m_aAcceptedVersions, $aClientVersions);
417+
418+
/* Look for a match */
419+
if (count($aInCommomVersions) > 0)
420+
{
421+
$sProtocolVersion = $aInCommomVersions[0];
422+
}
423+
else
424+
{
425+
/* Close the connection if none is supported and include the accepted versions
426+
* in the message so the client can retry with on of those. */
427+
$this -> write('HTTP/1.1 426 Upgrade Required' . "\r\nSec-WebSocket-Version: " .
428+
implode(', ', $this -> m_aAcceptedVersions) . "\r\n\r\n");
396429
$this -> close(1002);
430+
397431
return false;
398432
}
399433

0 commit comments

Comments
 (0)