diff --git a/src/Microsoft.AspNetCore.Http.Abstractions/Internal/ParsingHelpers.cs b/src/Microsoft.AspNetCore.Http.Abstractions/Internal/ParsingHelpers.cs index fb6af992..1e06ce6e 100644 --- a/src/Microsoft.AspNetCore.Http.Abstractions/Internal/ParsingHelpers.cs +++ b/src/Microsoft.AspNetCore.Http.Abstractions/Internal/ParsingHelpers.cs @@ -26,9 +26,13 @@ private static IEnumerable GetHeaderSplitImplementation(StringValues val { foreach (var segment in new HeaderSegmentCollection(values)) { - if (segment.Data.HasValue) + if (!StringSegment.IsNullOrEmpty(segment.Data)) { - yield return DeQuote(segment.Data.Value); + var value = DeQuote(segment.Data.Value); + if (!string.IsNullOrEmpty(value)) + { + yield return value; + } } } } diff --git a/test/Microsoft.AspNetCore.Http.Tests/HeaderDictionaryTests.cs b/test/Microsoft.AspNetCore.Http.Tests/HeaderDictionaryTests.cs index 40c94df9..f9cd2488 100644 --- a/test/Microsoft.AspNetCore.Http.Tests/HeaderDictionaryTests.cs +++ b/test/Microsoft.AspNetCore.Http.Tests/HeaderDictionaryTests.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; +using System.Linq; using Microsoft.Extensions.Primitives; using Xunit; @@ -15,7 +16,9 @@ public class HeaderDictionaryTests new[] { "Value1", "Value2", "Value3", "Value4" }, new[] { "Value1", "", "Value3", "Value4" }, new[] { "Value1", "", "", "Value4" }, - new[] { "", "", "", "" } + new[] { "Value1", "", null, "Value4" }, + new[] { "", "", "", "" }, + new[] { "", null, "", null }, }; [Fact] @@ -37,7 +40,7 @@ public void PropertiesAreAccessible() [Theory] [MemberData(nameof(HeaderSegmentData))] - public void EmptyHeaderSegmentsAreParsable(IEnumerable segments) + public void EmptyHeaderSegmentsAreIgnored(IEnumerable segments) { var header = string.Join(",", segments); @@ -48,8 +51,22 @@ public void EmptyHeaderSegmentsAreParsable(IEnumerable segments) }); var result = headers.GetCommaSeparatedValues("Header1"); + var expectedResult = segments.Where(s => !string.IsNullOrEmpty(s)); - Assert.Equal(segments, result); + Assert.Equal(expectedResult, result); + } + + [Fact] + public void EmtpyQuotedHeaderSegmentsAreIgnored() + { + var headers = new HeaderDictionary( + new Dictionary(StringComparer.OrdinalIgnoreCase) + { + { "Header1", "Value1,\"\",,Value2" }, + }); + + var result = headers.GetCommaSeparatedValues("Header1"); + Assert.Equal(new[] { "Value1", "Value2" }, result); } } } \ No newline at end of file