Skip to content

Commit

Permalink
Fix HttpHeaders Keys case sensitivity (#50606)
Browse files Browse the repository at this point in the history
  • Loading branch information
WeihanLi committed Sep 11, 2023
1 parent 6f668d8 commit 4365561
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Servers/Kestrel/Core/src/Internal/Http/HttpHeaders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ protected static void ThrowDuplicateKeyException()

bool ICollection<KeyValuePair<string, StringValues>>.IsReadOnly => _isReadOnly;

ICollection<string> IDictionary<string, StringValues>.Keys => ((IDictionary<string, StringValues>)this).Select(pair => pair.Key).ToList();
ICollection<string> IDictionary<string, StringValues>.Keys => ((IDictionary<string, StringValues>)this).Select(pair => pair.Key).ToHashSet(StringComparer.OrdinalIgnoreCase);

ICollection<StringValues> IDictionary<string, StringValues>.Values => ((IDictionary<string, StringValues>)this).Select(pair => pair.Value).ToList();

Expand Down
9 changes: 9 additions & 0 deletions src/Servers/Kestrel/Core/test/HttpHeadersTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http;
using Microsoft.Extensions.Primitives;
using Microsoft.Net.Http.Headers;
Expand Down Expand Up @@ -298,4 +299,12 @@ private static void InvalidContentLengthsRejectedImpl(HttpHeaders httpHeaders)
Assert.Null(httpHeaders.ContentLength);
Assert.False(httpHeaders.ContentLength.HasValue);
}

[Fact]
public void KeysCompareShouldBeCaseInsensitive()
{
var httpHeaders = (IHeaderDictionary)new HttpRequestHeaders();
httpHeaders["Cache-Control"] = "no-cache";
Assert.True(httpHeaders.Keys.Contains("cache-control"));
}
}

0 comments on commit 4365561

Please sign in to comment.