1 | <?php |
||
11 | * |
||
12 | * TODO: Support unmasked frames. |
||
13 | */ |
||
14 | class FrameReader { |
||
15 | |||
16 | /** |
||
17 | * https://tools.ietf.org/html/rfc6455#section-5.2 |
||
18 | */ |
||
19 | protected const REGEXP = |
||
20 | //op((char )|(short )|(bigint ))(mask ) |
||
21 | '/^.([\x80-\xfd]|\xfe(?<n>..)|\xff(?<J>.{8}))(?<mask>.{4})/s'; |
||
22 | |||
23 | /** |
||
24 | * Peer read buffer. |
||
25 | * |
||
26 | * @var string |
||
27 | */ |
||
28 | protected $buffer = ''; |
||
29 | |||
30 | /** |
||
31 | * @var WebSocketClient |
||
32 | */ |
||
33 | protected $client; |
||
34 | |||
35 | /** |
||
36 | * Frame header buffer. |
||
37 | * |
||
38 | * @var null|array |
||
39 | */ |
||
40 | protected $header; |
||
41 | |||
42 | /** |
||
43 | * Maximum inbound per-frame payload length (fragment). |
||
44 | * |
||
45 | * Must be greater than or equal to `125` |
||
46 | * |
||
47 | * Defaults to 128 KiB. |
||
48 | * |
||
49 | * https://tools.ietf.org/html/rfc6455#section-5.2 |
||
50 | * |
||
51 | * @var int |
||
52 | */ |
||
53 | protected $maxLength = 128 * 1024; |
||
54 | |||
55 | /** |
||
56 | * RSV bit mask claimed by extensions. |
||
57 | * |
||
58 | * @var int |
||
59 | */ |
||
60 | protected $rsv = 0; |
||
61 | |||
62 | /** |
||
63 | * @param WebSocketClient $client |
||
64 | */ |
||
65 | public function __construct (WebSocketClient $client) { |
||
66 | $this->client = $client; |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * Reads and returns a single pending frame from the buffer, or nothing. |
||
71 | * |
||
72 | * @return null|Frame |
||
73 | * @throws WebSocketError |
||
74 | */ |
||
75 | protected function getFrame (): ?Frame { |
||
76 | // wait for the header |
||
77 | if (!$this->header ??= $this->getFrame_header()) { |
||
|
|||
78 | return null; |
||
79 | } |
||
80 | |||
81 | // wait for the whole frame |
||
82 | $length = $this->header['length']; |
||
83 | if (strlen($this->buffer) < $length) { |
||
84 | return null; |
||
85 | } |
||
86 | |||
87 | // extract the payload |
||
88 | $payload = substr($this->buffer, 0, $length); |
||
89 | |||
90 | // chop the buffer |
||
91 | $this->buffer = substr($this->buffer, $length); |
||
92 | |||
93 | // unmask the payload |
||
94 | $mask = $this->header['mask']; |
||
95 | for ($i = 0; $i < $length; $i++) { |
||
96 | $payload[$i] = chr(ord($payload[$i]) ^ $mask[$i % 4]); |
||
97 | } |
||
98 | |||
99 | // construct the frame instance |
||
100 | $frame = $this->newFrame($payload); |
||
101 | |||
102 | // destroy the header buffer |
||
103 | $this->header = null; |
||
104 | |||
105 | // return the frame |
||
106 | return $frame; |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * https://tools.ietf.org/html/rfc6455#section-5.2 |
||
111 | * @return null|array |
||
112 | */ |
||
113 | protected function getFrame_header (): ?array { |
||
114 | if (!preg_match(self::REGEXP, $this->buffer, $match)) { |
||
115 | if (strlen($this->buffer) >= 14) { // max head room |
||
116 | throw new WebSocketError(Frame::CLOSE_PROTOCOL_ERROR, 'Bad frame.'); |
||
117 | } |
||
118 | return null; |
||
119 | } |
||
120 | |||
121 | // unpack the first two bytes |
||
122 | [, $b0, $b1] = unpack('C2', $match[0]); // 1-based indices |
||
123 | |||
124 | // convert the second byte into an unpack() format, or the actual length (sans the MASK bit). |
||
125 | // the unpack() format is also used as the length's named-group in the regexp match. |
||
126 | $len = [0xfe => 'n', 0xff => 'J'][$b1] ?? ($b1 & Frame::LEN); |
||
127 | |||
128 | // fill the header buffer |
||
129 | $header = [ |
||
130 | 'final' => $final = $b0 & Frame::FIN, |
||
131 | 'rsv' => $rsv = $b0 & Frame::RSV123, |
||
132 | 'opCode' => $opCode = $b0 & Frame::OP, |
||
133 | 'length' => $length = is_int($len) ? $len : unpack($len, $match[$len])[1], |
||
134 | 'mask' => array_values(unpack('C*', $match['mask'])), |
||
135 | ]; |
||
136 | |||
137 | // chop the peer buffer |
||
138 | $this->buffer = substr($this->buffer, strlen($match[0])); |
||
139 | |||
140 | // validate |
||
141 | if ($badRsv = $rsv & ~$this->rsv) { |
||
142 | $badRsv = str_pad(base_convert($badRsv >> 4, 10, 2), 3, '0', STR_PAD_LEFT); |
||
143 | throw new WebSocketError(Frame::CLOSE_PROTOCOL_ERROR, "Received unknown RSV bits: 0b{$badRsv}"); |
||
144 | } |
||
145 | elseif ($opCode >= Frame::OP_CLOSE) { |
||
146 | if ($opCode > Frame::OP_PONG) { |
||
147 | throw new WebSocketError(Frame::CLOSE_PROTOCOL_ERROR, "Received unsupported control frame ({$opCode})"); |
||
148 | } |
||
149 | elseif (!$final) { |
||
150 | throw new WebSocketError(Frame::CLOSE_PROTOCOL_ERROR, "Received fragmented control frame ({$opCode})"); |
||
151 | } |
||
152 | } |
||
225 | } |