fix: address code quality review findings for batch 1
- SubjectsCollide: split tokens once upfront instead of O(n²) TokenAt calls - NatsHeaderParser: manual digit accumulation avoids string allocation and overflow - NatsHeaders: use IReadOnlyDictionary for Headers, immutable Invalid sentinel - PermissionLruCache: add missing Count property
This commit is contained in:
@@ -1,20 +1,26 @@
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Text;
|
||||
|
||||
namespace NATS.Server.Protocol;
|
||||
|
||||
public readonly struct NatsHeaders
|
||||
public readonly struct NatsHeaders()
|
||||
{
|
||||
public int Status { get; init; }
|
||||
public string Description { get; init; }
|
||||
public Dictionary<string, string[]> Headers { get; init; }
|
||||
public string Description { get; init; } = string.Empty;
|
||||
public IReadOnlyDictionary<string, string[]> Headers { get; init; } = ReadOnlyDictionary<string, string[]>.Empty;
|
||||
|
||||
public static readonly NatsHeaders Invalid = new() { Status = -1, Description = string.Empty, Headers = new() };
|
||||
public static readonly NatsHeaders Invalid = new()
|
||||
{
|
||||
Status = -1,
|
||||
Description = string.Empty,
|
||||
Headers = ReadOnlyDictionary<string, string[]>.Empty,
|
||||
};
|
||||
}
|
||||
|
||||
public static class NatsHeaderParser
|
||||
{
|
||||
private static readonly byte[] CrLf = "\r\n"u8.ToArray();
|
||||
private static readonly byte[] Prefix = "NATS/1.0"u8.ToArray();
|
||||
private static ReadOnlySpan<byte> CrLf => "\r\n"u8;
|
||||
private static ReadOnlySpan<byte> Prefix => "NATS/1.0"u8;
|
||||
|
||||
public static NatsHeaders Parse(ReadOnlySpan<byte> data)
|
||||
{
|
||||
@@ -46,9 +52,10 @@ public static class NatsHeaderParser
|
||||
while (si < statusLine.Length && statusLine[si] >= (byte)'0' && statusLine[si] <= (byte)'9')
|
||||
si++;
|
||||
|
||||
if (si > numStart)
|
||||
if (si > numStart && si - numStart <= 5) // max 5 digits to avoid overflow
|
||||
{
|
||||
status = int.Parse(Encoding.ASCII.GetString(statusLine[numStart..si]));
|
||||
for (int idx = numStart; idx < si; idx++)
|
||||
status = status * 10 + (statusLine[idx] - '0');
|
||||
|
||||
while (si < statusLine.Length && statusLine[si] == (byte)' ')
|
||||
si++;
|
||||
|
||||
Reference in New Issue
Block a user