-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFrameCodec.php
More file actions
154 lines (117 loc) · 4.68 KB
/
Copy pathFrameCodec.php
File metadata and controls
154 lines (117 loc) · 4.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
declare(strict_types=1);
namespace Micilini\PhpSockets\Protocol;
use InvalidArgumentException;
final readonly class FrameCodec
{
public function __construct(private int $maxPayloadBytes = 65536)
{
if ($this->maxPayloadBytes < 1) {
throw new InvalidArgumentException('Maximum payload size must be greater than zero.');
}
}
public function decode(string $data, bool $fromClient = true): Frame
{
if (strlen($data) < 2) {
throw new ProtocolException('Incomplete WebSocket frame header.');
}
$firstByte = ord($data[0]);
$secondByte = ord($data[1]);
$fin = ($firstByte & 0x80) === 0x80;
$reservedBits = $firstByte & 0x70;
if ($reservedBits !== 0) {
throw new ProtocolException('Reserved WebSocket frame bits are not supported.');
}
$opcode = Opcode::tryFrom($firstByte & 0x0F);
if (!$opcode instanceof Opcode) {
throw new ProtocolException('Unsupported WebSocket opcode.');
}
$masked = ($secondByte & 0x80) === 0x80;
if ($fromClient && !$masked) {
throw new ProtocolException('Client WebSocket frames must be masked.');
}
$payloadLength = $secondByte & 0x7F;
$offset = 2;
if ($payloadLength === 126) {
$this->assertAvailableBytes($data, $offset, 2);
$lengthParts = unpack('nlength', substr($data, $offset, 2));
if ($lengthParts === false) {
throw new ProtocolException('Invalid WebSocket payload length.');
}
$payloadLength = (int) $lengthParts['length'];
$offset += 2;
} elseif ($payloadLength === 127) {
$this->assertAvailableBytes($data, $offset, 8);
$parts = unpack('Nhigh/Nlow', substr($data, $offset, 8));
if ($parts === false) {
throw new ProtocolException('Invalid WebSocket payload length.');
}
if ((int) $parts['high'] !== 0) {
throw new ProtocolException('WebSocket payload length is too large.');
}
$payloadLength = (int) $parts['low'];
$offset += 8;
}
if ($payloadLength > $this->maxPayloadBytes) {
throw new ProtocolException('WebSocket payload exceeds the configured maximum size.');
}
if ($opcode->isControl()) {
if (!$fin) {
throw new ProtocolException('Control frames must not be fragmented.');
}
if ($payloadLength > 125) {
throw new ProtocolException('Control frame payload cannot be larger than 125 bytes.');
}
}
$maskingKey = '';
if ($masked) {
$this->assertAvailableBytes($data, $offset, 4);
$maskingKey = substr($data, $offset, 4);
$offset += 4;
}
$this->assertAvailableBytes($data, $offset, $payloadLength);
$payload = substr($data, $offset, $payloadLength);
if ($masked) {
$payload = self::applyMask($payload, $maskingKey);
}
return new Frame($fin, $opcode, $payload, $masked);
}
public function encode(Frame $frame, bool $mask = false): string
{
$payload = $frame->payload;
$payloadLength = strlen($payload);
if ($payloadLength > $this->maxPayloadBytes) {
throw new ProtocolException('WebSocket payload exceeds the configured maximum size.');
}
$firstByte = ($frame->fin ? 0x80 : 0x00) | $frame->opcode->value;
$header = chr($firstByte);
$maskBit = $mask ? 0x80 : 0x00;
if ($payloadLength <= 125) {
$header .= chr($maskBit | $payloadLength);
} elseif ($payloadLength <= 65535) {
$header .= chr($maskBit | 126) . pack('n', $payloadLength);
} else {
$header .= chr($maskBit | 127) . pack('NN', 0, $payloadLength);
}
if (!$mask) {
return $header . $payload;
}
$maskingKey = random_bytes(4);
return $header . $maskingKey . self::applyMask($payload, $maskingKey);
}
private function assertAvailableBytes(string $data, int $offset, int $neededBytes): void
{
if (strlen($data) < $offset + $neededBytes) {
throw new ProtocolException('Incomplete WebSocket frame payload.');
}
}
private static function applyMask(string $payload, string $maskingKey): string
{
$result = '';
$payloadLength = strlen($payload);
for ($index = 0; $index < $payloadLength; $index++) {
$result .= $payload[$index] ^ $maskingKey[$index % 4];
}
return $result;
}
}