@@ -158,6 +158,20 @@ class WebSocketConnection extends Socket
158
158
*/
159
159
public static $ aInConnectingState = array ();
160
160
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
+
161
175
/**
162
176
* @var boolean True if the opening handshake from the client has been read
163
177
*/
@@ -390,10 +404,30 @@ protected function parseHandshake()
390
404
391
405
/**
392
406
* 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" );
396
429
$ this -> close (1002 );
430
+
397
431
return false ;
398
432
}
399
433
0 commit comments