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