Skip to content
This repository was archived by the owner on Nov 20, 2018. It is now read-only.

Commit 07470d4

Browse files
committed
Ignore empty header values #722
1 parent dfd938e commit 07470d4

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

src/Microsoft.AspNetCore.Http.Abstractions/Internal/ParsingHelpers.cs

+6-2
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,13 @@ private static IEnumerable<string> GetHeaderSplitImplementation(StringValues val
2626
{
2727
foreach (var segment in new HeaderSegmentCollection(values))
2828
{
29-
if (segment.Data.HasValue)
29+
if (!StringSegment.IsNullOrEmpty(segment.Data))
3030
{
31-
yield return DeQuote(segment.Data.Value);
31+
var value = DeQuote(segment.Data.Value);
32+
if (!string.IsNullOrEmpty(value))
33+
{
34+
yield return value;
35+
}
3236
}
3337
}
3438
}

test/Microsoft.AspNetCore.Http.Tests/HeaderDictionaryTests.cs

+20-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using System.Linq;
67
using Microsoft.Extensions.Primitives;
78
using Xunit;
89

@@ -15,7 +16,9 @@ public class HeaderDictionaryTests
1516
new[] { "Value1", "Value2", "Value3", "Value4" },
1617
new[] { "Value1", "", "Value3", "Value4" },
1718
new[] { "Value1", "", "", "Value4" },
18-
new[] { "", "", "", "" }
19+
new[] { "Value1", "", null, "Value4" },
20+
new[] { "", "", "", "" },
21+
new[] { "", null, "", null },
1922
};
2023

2124
[Fact]
@@ -37,7 +40,7 @@ public void PropertiesAreAccessible()
3740

3841
[Theory]
3942
[MemberData(nameof(HeaderSegmentData))]
40-
public void EmptyHeaderSegmentsAreParsable(IEnumerable<string> segments)
43+
public void EmptyHeaderSegmentsAreIgnored(IEnumerable<string> segments)
4144
{
4245
var header = string.Join(",", segments);
4346

@@ -48,8 +51,22 @@ public void EmptyHeaderSegmentsAreParsable(IEnumerable<string> segments)
4851
});
4952

5053
var result = headers.GetCommaSeparatedValues("Header1");
54+
var expectedResult = segments.Where(s => !string.IsNullOrEmpty(s));
5155

52-
Assert.Equal(segments, result);
56+
Assert.Equal(expectedResult, result);
57+
}
58+
59+
[Fact]
60+
public void EmtpyQuotedHeaderSegmentsAreIgnored()
61+
{
62+
var headers = new HeaderDictionary(
63+
new Dictionary<string, StringValues>(StringComparer.OrdinalIgnoreCase)
64+
{
65+
{ "Header1", "Value1,\"\",,Value2" },
66+
});
67+
68+
var result = headers.GetCommaSeparatedValues("Header1");
69+
Assert.Equal(new[] { "Value1", "Value2" }, result);
5370
}
5471
}
5572
}

0 commit comments

Comments
 (0)