Skip to content

Commit 0ea2063

Browse files
authored
Update Ws.php
1 parent 18ffa66 commit 0ea2063

File tree

1 file changed

+26
-15
lines changed

1 file changed

+26
-15
lines changed

Protocols/Ws.php

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
11
<?php
2+
/**
3+
* This file is part of workerman.
4+
*
5+
* Licensed under The MIT License
6+
* For full copyright and license information, please see the MIT-LICENSE.txt
7+
* Redistributions of files must retain the above copyright notice.
8+
*
9+
* @author walkor<[email protected]>
10+
* @copyright walkor<[email protected]>
11+
* @link http://www.workerman.net/
12+
* @license http://www.opensource.org/licenses/mit-license.php MIT License
13+
*/
214
namespace Workerman\Protocols;
315

416
use Workerman\Worker;
@@ -10,13 +22,6 @@
1022
*/
1123
class Ws
1224
{
13-
/**
14-
* Minimum head length of websocket protocol.
15-
*
16-
* @var int
17-
*/
18-
const MIN_HEAD_LEN = 2;
19-
2025
/**
2126
* Websocket blob type.
2227
*
@@ -49,7 +54,7 @@ public static function input($buffer, $connection)
4954
return self::dealHandshake($buffer, $connection);
5055
}
5156
$recv_len = strlen($buffer);
52-
if ($recv_len < self::MIN_HEAD_LEN) {
57+
if ($recv_len < 2) {
5358
return 0;
5459
}
5560
// Buffer websocket frame data.
@@ -60,10 +65,14 @@ public static function input($buffer, $connection)
6065
return 0;
6166
}
6267
} else {
63-
$data_len = ord($buffer[1]) & 127;
68+
6469
$firstbyte = ord($buffer[0]);
70+
$secondbyte = ord($buffer[1]);
71+
$data_len = $secondbyte & 127;
6572
$is_fin_frame = $firstbyte >> 7;
73+
$masked = $secondbyte >> 7;
6674
$opcode = $firstbyte & 0xf;
75+
6776
switch ($opcode) {
6877
case 0x0:
6978
break;
@@ -110,9 +119,10 @@ public static function input($buffer, $connection)
110119
}
111120
// Consume data from receive buffer.
112121
if (!$data_len) {
113-
$connection->consumeRecvBuffer(self::MIN_HEAD_LEN);
114-
if ($recv_len > self::MIN_HEAD_LEN) {
115-
return self::input(substr($buffer, self::MIN_HEAD_LEN), $connection);
122+
$head_len = $masked ? 6 : 2;
123+
$connection->consumeRecvBuffer($head_len);
124+
if ($recv_len > $head_len) {
125+
return self::input(substr($buffer, $head_len), $connection);
116126
}
117127
return 0;
118128
}
@@ -133,9 +143,10 @@ public static function input($buffer, $connection)
133143
}
134144
// Consume data from receive buffer.
135145
if (!$data_len) {
136-
$connection->consumeRecvBuffer(self::MIN_HEAD_LEN);
137-
if ($recv_len > self::MIN_HEAD_LEN) {
138-
return self::input(substr($buffer, self::MIN_HEAD_LEN), $connection);
146+
$head_len = $masked ? 6 : 2;
147+
$connection->consumeRecvBuffer($head_len);
148+
if ($recv_len > $head_len) {
149+
return self::input(substr($buffer, $head_len), $connection);
139150
}
140151
return 0;
141152
}

0 commit comments

Comments
 (0)