From 979e5da0d1e9d6658744ba90ec261fc430b9516f Mon Sep 17 00:00:00 2001 From: Diogo Martins Date: Tue, 3 Nov 2026 19:46:54 +0000 Subject: [PATCH] Drop the second comma search from Content-Length folding ContentLengthFields.Fold splits each Content-Length field at its commas and trims every element before it parses it, so ParseContentLengthDigits never sees a comma. It still searched for one and trimmed again, left over from when it was handed the whole field value. Fold is its only caller. Rename it ParseDecimalDigits and drop the comma handling; the saturating digit loop is unchanged. Inline ScanFramingFields, the scan both framing helpers share. --- src/Glyph11/Validation/BodyFramingDetector.cs | 15 ++++++++------- src/Glyph11/Validation/ContentLengthFields.cs | 2 +- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/Glyph11/Validation/BodyFramingDetector.cs b/src/Glyph11/Validation/BodyFramingDetector.cs index a0f2357..dc9ad34 100644 --- a/src/Glyph11/Validation/BodyFramingDetector.cs +++ b/src/Glyph11/Validation/BodyFramingDetector.cs @@ -1,3 +1,4 @@ +using System.Runtime.CompilerServices; using Glyph11.Parser; using Glyph11.Protocol; @@ -100,6 +101,7 @@ public static BodyFramingResult DetectResponseBodyFraming( /// ( when there are none). /// /// The Content-Length fields disagree. + [MethodImpl(MethodImplOptions.AggressiveInlining)] private static long ScanFramingFields(KeyValueList headers, out bool hasChunkedTE) { long contentLength = ContentLengthFields.Absent; @@ -135,14 +137,13 @@ internal static ReadOnlySpan TrimOws(ReadOnlySpan value) return value[start..end]; } - internal static long ParseContentLengthDigits(ReadOnlySpan value) + /// + /// One decimal length with no list or whitespace around it — + /// splits and trims each field first: -1 if it is empty or not all digits, + /// if a long cannot hold it. + /// + internal static long ParseDecimalDigits(ReadOnlySpan value) { - // Handle comma-separated: take first segment, without the OWS a list allows before its - // comma - "5 ,5" is 5 (RFC 9110 §5.6.1), exactly as the validator read it. - int comma = value.IndexOf((byte)','); - if (comma >= 0) - value = TrimOws(value[..comma]); - if (value.IsEmpty) return -1; const long Limit = long.MaxValue / 10; // any digit may follow a smaller value diff --git a/src/Glyph11/Validation/ContentLengthFields.cs b/src/Glyph11/Validation/ContentLengthFields.cs index 86a818f..6c48ac7 100644 --- a/src/Glyph11/Validation/ContentLengthFields.cs +++ b/src/Glyph11/Validation/ContentLengthFields.cs @@ -41,7 +41,7 @@ internal static void Fold(ref long length, ReadOnlySpan fieldValue) if (!element.IsEmpty) { sawElement = true; - Merge(ref length, BodyFramingDetector.ParseContentLengthDigits(element)); + Merge(ref length, BodyFramingDetector.ParseDecimalDigits(element)); } if (comma < 0)