Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions src/Glyph11/Validation/BodyFramingDetector.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Runtime.CompilerServices;
using Glyph11.Parser;
using Glyph11.Protocol;

Expand Down Expand Up @@ -100,6 +101,7 @@ public static BodyFramingResult DetectResponseBodyFraming(
/// (<see cref="ContentLengthFields.Absent"/> when there are none).
/// </summary>
/// <exception cref="HttpParseException">The Content-Length fields disagree.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static long ScanFramingFields(KeyValueList headers, out bool hasChunkedTE)
{
long contentLength = ContentLengthFields.Absent;
Expand Down Expand Up @@ -135,14 +137,13 @@ internal static ReadOnlySpan<byte> TrimOws(ReadOnlySpan<byte> value)
return value[start..end];
}

internal static long ParseContentLengthDigits(ReadOnlySpan<byte> value)
/// <summary>
/// One decimal length with no list or whitespace around it — <see cref="ContentLengthFields.Fold"/>
/// splits and trims each field first: <c>-1</c> if it is empty or not all digits,
/// <see cref="long.MaxValue"/> if a long cannot hold it.
/// </summary>
internal static long ParseDecimalDigits(ReadOnlySpan<byte> 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
Expand Down
2 changes: 1 addition & 1 deletion src/Glyph11/Validation/ContentLengthFields.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ internal static void Fold(ref long length, ReadOnlySpan<byte> fieldValue)
if (!element.IsEmpty)
{
sawElement = true;
Merge(ref length, BodyFramingDetector.ParseContentLengthDigits(element));
Merge(ref length, BodyFramingDetector.ParseDecimalDigits(element));
}

if (comma < 0)
Expand Down
Loading