1
1
<?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
+ */
2
14
namespace Workerman \Protocols ;
3
15
4
16
use Workerman \Worker ;
10
22
*/
11
23
class Ws
12
24
{
13
- /**
14
- * Minimum head length of websocket protocol.
15
- *
16
- * @var int
17
- */
18
- const MIN_HEAD_LEN = 2 ;
19
-
20
25
/**
21
26
* Websocket blob type.
22
27
*
@@ -49,7 +54,7 @@ public static function input($buffer, $connection)
49
54
return self ::dealHandshake ($ buffer , $ connection );
50
55
}
51
56
$ recv_len = strlen ($ buffer );
52
- if ($ recv_len < self :: MIN_HEAD_LEN ) {
57
+ if ($ recv_len < 2 ) {
53
58
return 0 ;
54
59
}
55
60
// Buffer websocket frame data.
@@ -60,10 +65,14 @@ public static function input($buffer, $connection)
60
65
return 0 ;
61
66
}
62
67
} else {
63
- $ data_len = ord ( $ buffer [ 1 ]) & 127 ;
68
+
64
69
$ firstbyte = ord ($ buffer [0 ]);
70
+ $ secondbyte = ord ($ buffer [1 ]);
71
+ $ data_len = $ secondbyte & 127 ;
65
72
$ is_fin_frame = $ firstbyte >> 7 ;
73
+ $ masked = $ secondbyte >> 7 ;
66
74
$ opcode = $ firstbyte & 0xf ;
75
+
67
76
switch ($ opcode ) {
68
77
case 0x0 :
69
78
break ;
@@ -110,9 +119,10 @@ public static function input($buffer, $connection)
110
119
}
111
120
// Consume data from receive buffer.
112
121
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 );
116
126
}
117
127
return 0 ;
118
128
}
@@ -133,9 +143,10 @@ public static function input($buffer, $connection)
133
143
}
134
144
// Consume data from receive buffer.
135
145
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 );
139
150
}
140
151
return 0 ;
141
152
}
0 commit comments