From 417c11670afdbafe666a1ecf9aab2977b63dba33 Mon Sep 17 00:00:00 2001 From: Christopher Vagnetoft Date: Thu, 22 Feb 2024 00:15:22 +0100 Subject: [PATCH] More code cleanup --- src/Debug/HexDumpTrait.php | 28 ++++++++++++++++++++++++ src/WebSocketCodec.php | 42 +++++++++++------------------------- src/WebSocketConnection.php | 4 ++-- tests/WebSocketCodecTest.php | 16 +++++++------- 4 files changed, 50 insertions(+), 40 deletions(-) create mode 100644 src/Debug/HexDumpTrait.php diff --git a/src/Debug/HexDumpTrait.php b/src/Debug/HexDumpTrait.php new file mode 100644 index 0000000..6c5066d --- /dev/null +++ b/src/Debug/HexDumpTrait.php @@ -0,0 +1,28 @@ + true, @@ -56,10 +49,13 @@ class WebSocketCodec $size1 = ($len >> 8) & 0xFF; $size2 = $len & 0xFF; $size3 = null; + $size4 = null; } else { $size0 = $len; $size1 = null; + $size2 = null; $size3 = null; + $size4 = null; } $encoded .= chr(($frame['final']?0x80:0x00) @@ -152,38 +148,24 @@ class WebSocketCodec return $decoded; } + /** + * Masking is reversible, and simply xors a repeated 4-byte key with the data. + * + * @param string $payload The unmasked (or masked) input + * @param string $mask The mask to use (4 bytes) + * @return string The masked (or unmasked) output + */ private function mask(string $payload, string $mask): string { $payloadData = array_map("ord", str_split($payload,1)); $maskData = array_map("ord", str_split($mask,1)); - //printf("Mask: %02x %02x %02x %02x\n", ...$maskData); - $unmasked = []; for ($n = 0; $n < count($payloadData); $n++) { $unmasked[] = $payloadData[$n] ^ $maskData[$n % 4]; } + return join("", array_map("chr", $unmasked)); } - private function hexdump($data): void - { - printf("%4d .\n", strlen($data)); - $rows = str_split($data, 16); - $offs = 0; - foreach ($rows as $row) { - $h = []; $a = []; - for ($n = 0; $n < 16; $n++) { - if ($n < strlen($row)) { - $h[] = sprintf("%02x%s", ord($row[$n]), ($n==7)?" ":" "); - $a[] = sprintf("%s%s", (ctype_print($row[$n])?$row[$n]:"."), ($n==7)?" ":""); - } else { - $h[] = (($n==7)?" ":" "); - $a[] = (($n==7)?" ":" "); - } - } - printf("%04x | %s | %s\n", 16 * $offs++, join("", $h), join("", $a)); - } - } - } \ No newline at end of file diff --git a/src/WebSocketConnection.php b/src/WebSocketConnection.php index a9be681..efac912 100644 --- a/src/WebSocketConnection.php +++ b/src/WebSocketConnection.php @@ -178,12 +178,12 @@ class WebSocketConnection implements WebSocketInterface public function pause() { - return $this->inStream->pause(); + $this->inStream->pause(); } public function resume() { - return $this->inStream->resume(); + $this->inStream->resume(); } public function isWritable() diff --git a/tests/WebSocketCodecTest.php b/tests/WebSocketCodecTest.php index 3a433d2..1c7db00 100644 --- a/tests/WebSocketCodecTest.php +++ b/tests/WebSocketCodecTest.php @@ -13,18 +13,18 @@ class WebSocketCodecTest extends \PHPUnit\Framework\TestCase $codec = new WebSocketCodec(); $msg = $codec->encode([ - 'opcode'=>WebSocketCodec::OP_PING, + 'opcode'=>WebSocketConnection::OP_PING, 'payload'=>"ping" ]); $this->assertEquals("\x89\x04ping", $msg); $msg = $codec->encode([ - 'opcode'=>WebSocketCodec::OP_FRAME_TEXT, + 'opcode'=>WebSocketConnection::OP_FRAME_TEXT, 'payload'=>"abcdefgh"]); $this->assertEquals("\x81\x08abcdefgh", $msg); $msg = $codec->encode([ - 'opcode'=>WebSocketCodec::OP_FRAME_TEXT, + 'opcode'=>WebSocketConnection::OP_FRAME_TEXT, 'payload'=>"abcdefgh", 'masked'=>true, 'mask'=>"\x00\x00\x00\x00" @@ -32,7 +32,7 @@ class WebSocketCodecTest extends \PHPUnit\Framework\TestCase $this->assertEquals("\x81\x88\x00\x00\x00\x00abcdefgh", $msg); $msg = $codec->encode([ - 'opcode'=>WebSocketCodec::OP_FRAME_TEXT, + 'opcode'=>WebSocketConnection::OP_FRAME_TEXT, 'payload'=>"abcdefgh", 'masked'=>true, 'mask'=>"\x00\xFF\x00\xFF" @@ -47,7 +47,7 @@ class WebSocketCodecTest extends \PHPUnit\Framework\TestCase $msg = $codec->decode("\x89\x04ping"); $this->assertEquals([ - 'opcode'=>WebSocketCodec::OP_PING, + 'opcode'=>WebSocketConnection::OP_PING, 'payload'=>"ping", 'final'=>true, 'rsv1'=>false, @@ -59,7 +59,7 @@ class WebSocketCodecTest extends \PHPUnit\Framework\TestCase $msg = $codec->decode("\x81\x08abcdefgh"); $this->assertEquals([ - 'opcode'=>WebSocketCodec::OP_FRAME_TEXT, + 'opcode'=>WebSocketConnection::OP_FRAME_TEXT, 'payload'=>"abcdefgh", 'final'=>true, 'rsv1'=>false, @@ -71,7 +71,7 @@ class WebSocketCodecTest extends \PHPUnit\Framework\TestCase $msg = $codec->decode("\x81\x88\x00\x00\x00\x00abcdefgh"); $this->assertEquals([ - 'opcode'=>WebSocketCodec::OP_FRAME_TEXT, + 'opcode'=>WebSocketConnection::OP_FRAME_TEXT, 'payload'=>"abcdefgh", 'final'=>true, 'rsv1'=>false, @@ -84,7 +84,7 @@ class WebSocketCodecTest extends \PHPUnit\Framework\TestCase $msg = $codec->decode("\x81\x88\x00\xFF\x00\xFFa\x9dc\x9be\x99g\x97"); $this->assertEquals([ - 'opcode'=>WebSocketCodec::OP_FRAME_TEXT, + 'opcode'=>WebSocketConnection::OP_FRAME_TEXT, 'payload'=>"abcdefgh", 'final'=>true, 'rsv1'=>false,