More code cleanup

This commit is contained in:
Chris 2024-02-22 00:15:22 +01:00
parent 70e353bd0c
commit 417c11670a
4 changed files with 50 additions and 40 deletions

View File

@ -0,0 +1,28 @@
<?php
namespace NoccyLabs\React\WebSocket\Debug;
trait HexDumpTrait
{
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));
}
}
}

View File

@ -17,19 +17,12 @@ use React\Stream\WritableStreamInterface;
class WebSocketCodec class WebSocketCodec
{ {
const OP_CONTINUATION = 0x0;
const OP_FRAME_TEXT = 0x1;
const OP_FRAME_BINARY = 0x2;
const OP_CLOSE = 0x8;
const OP_PING = 0x9;
const OP_PONG = 0xA;
public function encode(array $frame): string public function encode(array $frame): string
{ {
// Encoded frame // Encoded frame
$encoded = null; $encoded = null;
// Unpack frame with defaults // Re-unpack frame with defaults
$frame = [ $frame = [
...[ ...[
'final' => true, 'final' => true,
@ -56,10 +49,13 @@ class WebSocketCodec
$size1 = ($len >> 8) & 0xFF; $size1 = ($len >> 8) & 0xFF;
$size2 = $len & 0xFF; $size2 = $len & 0xFF;
$size3 = null; $size3 = null;
$size4 = null;
} else { } else {
$size0 = $len; $size0 = $len;
$size1 = null; $size1 = null;
$size2 = null;
$size3 = null; $size3 = null;
$size4 = null;
} }
$encoded .= chr(($frame['final']?0x80:0x00) $encoded .= chr(($frame['final']?0x80:0x00)
@ -152,38 +148,24 @@ class WebSocketCodec
return $decoded; 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 private function mask(string $payload, string $mask): string
{ {
$payloadData = array_map("ord", str_split($payload,1)); $payloadData = array_map("ord", str_split($payload,1));
$maskData = array_map("ord", str_split($mask,1)); $maskData = array_map("ord", str_split($mask,1));
//printf("Mask: %02x %02x %02x %02x\n", ...$maskData);
$unmasked = []; $unmasked = [];
for ($n = 0; $n < count($payloadData); $n++) { for ($n = 0; $n < count($payloadData); $n++) {
$unmasked[] = $payloadData[$n] ^ $maskData[$n % 4]; $unmasked[] = $payloadData[$n] ^ $maskData[$n % 4];
} }
return join("", array_map("chr", $unmasked)); 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));
}
}
} }

View File

@ -178,12 +178,12 @@ class WebSocketConnection implements WebSocketInterface
public function pause() public function pause()
{ {
return $this->inStream->pause(); $this->inStream->pause();
} }
public function resume() public function resume()
{ {
return $this->inStream->resume(); $this->inStream->resume();
} }
public function isWritable() public function isWritable()

View File

@ -13,18 +13,18 @@ class WebSocketCodecTest extends \PHPUnit\Framework\TestCase
$codec = new WebSocketCodec(); $codec = new WebSocketCodec();
$msg = $codec->encode([ $msg = $codec->encode([
'opcode'=>WebSocketCodec::OP_PING, 'opcode'=>WebSocketConnection::OP_PING,
'payload'=>"ping" 'payload'=>"ping"
]); ]);
$this->assertEquals("\x89\x04ping", $msg); $this->assertEquals("\x89\x04ping", $msg);
$msg = $codec->encode([ $msg = $codec->encode([
'opcode'=>WebSocketCodec::OP_FRAME_TEXT, 'opcode'=>WebSocketConnection::OP_FRAME_TEXT,
'payload'=>"abcdefgh"]); 'payload'=>"abcdefgh"]);
$this->assertEquals("\x81\x08abcdefgh", $msg); $this->assertEquals("\x81\x08abcdefgh", $msg);
$msg = $codec->encode([ $msg = $codec->encode([
'opcode'=>WebSocketCodec::OP_FRAME_TEXT, 'opcode'=>WebSocketConnection::OP_FRAME_TEXT,
'payload'=>"abcdefgh", 'payload'=>"abcdefgh",
'masked'=>true, 'masked'=>true,
'mask'=>"\x00\x00\x00\x00" 'mask'=>"\x00\x00\x00\x00"
@ -32,7 +32,7 @@ class WebSocketCodecTest extends \PHPUnit\Framework\TestCase
$this->assertEquals("\x81\x88\x00\x00\x00\x00abcdefgh", $msg); $this->assertEquals("\x81\x88\x00\x00\x00\x00abcdefgh", $msg);
$msg = $codec->encode([ $msg = $codec->encode([
'opcode'=>WebSocketCodec::OP_FRAME_TEXT, 'opcode'=>WebSocketConnection::OP_FRAME_TEXT,
'payload'=>"abcdefgh", 'payload'=>"abcdefgh",
'masked'=>true, 'masked'=>true,
'mask'=>"\x00\xFF\x00\xFF" 'mask'=>"\x00\xFF\x00\xFF"
@ -47,7 +47,7 @@ class WebSocketCodecTest extends \PHPUnit\Framework\TestCase
$msg = $codec->decode("\x89\x04ping"); $msg = $codec->decode("\x89\x04ping");
$this->assertEquals([ $this->assertEquals([
'opcode'=>WebSocketCodec::OP_PING, 'opcode'=>WebSocketConnection::OP_PING,
'payload'=>"ping", 'payload'=>"ping",
'final'=>true, 'final'=>true,
'rsv1'=>false, 'rsv1'=>false,
@ -59,7 +59,7 @@ class WebSocketCodecTest extends \PHPUnit\Framework\TestCase
$msg = $codec->decode("\x81\x08abcdefgh"); $msg = $codec->decode("\x81\x08abcdefgh");
$this->assertEquals([ $this->assertEquals([
'opcode'=>WebSocketCodec::OP_FRAME_TEXT, 'opcode'=>WebSocketConnection::OP_FRAME_TEXT,
'payload'=>"abcdefgh", 'payload'=>"abcdefgh",
'final'=>true, 'final'=>true,
'rsv1'=>false, 'rsv1'=>false,
@ -71,7 +71,7 @@ class WebSocketCodecTest extends \PHPUnit\Framework\TestCase
$msg = $codec->decode("\x81\x88\x00\x00\x00\x00abcdefgh"); $msg = $codec->decode("\x81\x88\x00\x00\x00\x00abcdefgh");
$this->assertEquals([ $this->assertEquals([
'opcode'=>WebSocketCodec::OP_FRAME_TEXT, 'opcode'=>WebSocketConnection::OP_FRAME_TEXT,
'payload'=>"abcdefgh", 'payload'=>"abcdefgh",
'final'=>true, 'final'=>true,
'rsv1'=>false, '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"); $msg = $codec->decode("\x81\x88\x00\xFF\x00\xFFa\x9dc\x9be\x99g\x97");
$this->assertEquals([ $this->assertEquals([
'opcode'=>WebSocketCodec::OP_FRAME_TEXT, 'opcode'=>WebSocketConnection::OP_FRAME_TEXT,
'payload'=>"abcdefgh", 'payload'=>"abcdefgh",
'final'=>true, 'final'=>true,
'rsv1'=>false, 'rsv1'=>false,