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
18 changes: 12 additions & 6 deletions src/Http/Headers/src/HeaderUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Contracts;
using System.Globalization;
using System.Runtime.CompilerServices;
using Microsoft.Extensions.Primitives;

namespace Microsoft.Net.Http.Headers;
Expand Down Expand Up @@ -169,18 +170,23 @@ internal static int GetNextNonEmptyOrWhitespaceIndex(
current++; // skip delimiter.
current = current + HttpRuleParser.GetWhitespaceLength(input, current);

if (skipEmptyValues)
if (skipEmptyValues && (current < input.Length) && (input[current] == ','))
{
while ((current < input.Length) && (input[current] == ','))
{
current++; // skip delimiter.
current = current + HttpRuleParser.GetWhitespaceLength(input, current);
}
current = SkipEmptyValues(input, current);
}

return current;
}

// Rare: one or more consecutive empty values (e.g. "a,,b"). Kept out of line so that
// GetNextNonEmptyOrWhitespaceIndex stays small; long runs are skipped vectorized.
[MethodImpl(MethodImplOptions.NoInlining)]
private static int SkipEmptyValues(StringSegment input, int current)
{
Debug.Assert(input[current] == ',');
return current + HttpRuleParser.GetSeparatorsAndWhitespaceLength(input, current, ',');
}

private static int AdvanceCacheDirectiveIndex(int current, string headerValue)
{
// Skip until the next potential name
Expand Down
17 changes: 17 additions & 0 deletions src/Http/Headers/test/EntityTagHeaderValueTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,23 @@ public void ParseList_NullOrEmptyArray_ReturnsEmptyList()
Assert.Empty(result);
}

[Theory]
[InlineData(",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,")]
[InlineData(", , , ,\t,\t , ,,,, ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, ,")]
[InlineData(",,,,\r\n ,,,,\r\n\t,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,")]
public void TryParseStrictList_LongRunsOfEmptyValues_Skipped(string separators)
{
var input = "\"a\"" + separators + "\"b\"" + separators;
Assert.True(EntityTagHeaderValue.TryParseStrictList(new[] { input }, out var results));
Assert.Equal(new[] { new EntityTagHeaderValue("\"a\""), new EntityTagHeaderValue("\"b\"") }, results);
}

[Fact]
public void TryParseStrictList_EmptyValuesFollowedByCRWithoutFolding_Fails()
{
Assert.False(EntityTagHeaderValue.TryParseStrictList(new[] { "\"a\",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,\r\n\"b\"" }, out _));
}

[Fact]
public void TryParseList_NullOrEmptyArray_ReturnsFalse()
{
Expand Down
23 changes: 23 additions & 0 deletions src/Http/Http/perf/Microbenchmarks/HeaderParsingBenchmarks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using BenchmarkDotNet.Attributes;
using Microsoft.Net.Http.Headers;

namespace Microsoft.AspNetCore.Http;

public class HeaderParsingBenchmarks
{
private readonly string[] _accept = ["text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8"];
private readonly string[] _acceptEncoding = ["gzip, deflate, br, zstd"];
private readonly string[] _acceptLanguage = ["en-US,en;q=0.9,fr-FR;q=0.8,fr;q=0.7"];

[Benchmark]
public int ParseAccept() => MediaTypeHeaderValue.ParseList(_accept).Count;

[Benchmark]
public int ParseAcceptEncoding() => StringWithQualityHeaderValue.ParseList(_acceptEncoding).Count;

[Benchmark]
public int ParseAcceptLanguage() => StringWithQualityHeaderValue.ParseList(_acceptLanguage).Count;
}
30 changes: 30 additions & 0 deletions src/Shared/HttpRuleParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Buffers;
using System.Diagnostics.Contracts;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Text;
using Microsoft.Extensions.Primitives;

Expand Down Expand Up @@ -111,6 +112,35 @@ internal static int GetWhitespaceLength(StringSegment input, int startIndex)
return input.Length - startIndex;
}

// Returns the length of the run of separator characters and LWS at the start of input.
// Used to skip long runs of empty list elements (e.g. ",,,,") without a per-char loop.
[MethodImpl(MethodImplOptions.NoInlining)]
internal static int GetSeparatorsAndWhitespaceLength(StringSegment input, int startIndex, char separator)
{
var current = startIndex;
while (current < input.Length)
{
var index = input.AsSpan(current).IndexOfAnyExcept(separator, SP, Tab);
if (index < 0)
{
return input.Length - startIndex;
}

current += index;

// Handle obsolete line folding (CRLF followed by SP or HT).
var whitespaceLength = GetWhitespaceLength(input, current);
if (whitespaceLength == 0)
{
break;
}

current += whitespaceLength;
}

return current - startIndex;
}

internal static int GetNumberLength(StringSegment input, int startIndex, bool allowDecimal)
{
Contract.Requires((startIndex >= 0) && (startIndex < input.Length));
Expand Down
Loading