fix: use byte-length for close message truncation, add exception-safe disposal
- CreateCloseMessage now operates on UTF-8 byte length (matching Go's len(body) behavior) instead of character length, with proper UTF-8 boundary detection during truncation - WsCompression.Compress now uses try/finally for exception-safe disposal of DeflateStream and MemoryStream
This commit is contained in:
@@ -22,19 +22,24 @@ public static class WsCompression
|
||||
{
|
||||
var output = new MemoryStream();
|
||||
var deflate = new DeflateStream(output, CompressionLevel.Fastest, leaveOpen: true);
|
||||
deflate.Write(data);
|
||||
deflate.Flush();
|
||||
try
|
||||
{
|
||||
deflate.Write(data);
|
||||
deflate.Flush();
|
||||
|
||||
var compressed = output.ToArray();
|
||||
var compressed = output.ToArray();
|
||||
|
||||
deflate.Dispose();
|
||||
output.Dispose();
|
||||
// Remove trailing 4-byte sync marker (0x00 0x00 0xff 0xff) per RFC 7692
|
||||
if (compressed.Length >= 4)
|
||||
return compressed[..^4];
|
||||
|
||||
// Remove trailing 4-byte sync marker (0x00 0x00 0xff 0xff) per RFC 7692
|
||||
if (compressed.Length >= 4)
|
||||
return compressed[..^4];
|
||||
|
||||
return compressed;
|
||||
return compressed;
|
||||
}
|
||||
finally
|
||||
{
|
||||
deflate.Dispose();
|
||||
output.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in New Issue
Block a user