dotnet / dotnet/aspnetcore

IHeaderDictionary.Append not adding headers on empty values

Open
#13,085 8 comments 2 reactions 0 assignees View on GitHub
affected-very-few area-networking severity-nice-to-have task
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 10h
Merged PRs (30d)
281

Description

### Describe the bug
This is probably by design, but it does lead to surprising behaviour

### To Reproduce
Steps to reproduce the behavior:
1. Using ASP.NET Core `3.0.0-preview9.19412.6`
2. Run
```csharp
var headers = new Microsoft.AspNetCore.Http.HeaderDictionary();

headers.Add("header1", "");
headers.Append("header2", "");
headers.Append("header3", new string[] { "", "" });

headers.ContainsKey("header1"); // true
headers.ContainsKey("header2"); // false
headers.ContainsKey("header3"); // true
```

When I wire the above to a kestrel app:
```
$ curl -ki https://127.0.0.1:5001/headers
HTTP/2 200
date: Tue, 13 Aug 2019 08:14:13 GMT
server: Kestrel
header1:
header3:
header3:
```

### Expected behavior

`"header2"` should be populated with a `StringValue` instance containing a single empty string. This expectation comes from the behaviour `HeaderDictionary.Add` and the fact that asp.net core is supporting empty header values. Also, appending empty values is not idempotent in general, e.g.
```csharp
var headers = new Microsoft.AspNetCore.Http.HeaderDictionary();

headers.Add("header1", "");
headers.Append("header1", "");
```
The behaviour seems to be caused by [this line](https://github.com/aspnet/AspNetCore/blob/4ef204e13b88c0734e0e94a1cc4c0ef05f40849e/src/Http/Http.Abstractions/src/Internal/ParsingHelpers.cs#L105).

Contributor guide

Open the contributing guide

Research direction

Start with Internal/ParsingHelpers.cs at the linked line and inspect how HeaderDictionary.Append handles empty string values. Reproduce the examples against ASP.NET Core 3.0.0-preview9.19412.6, then verify that appending an empty value creates the expected header entry without changing existing empty-value behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
api, backend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.