Skip to content

Is a Null Header Value a Set header value? #4754

@benaadams

Description

@benaadams

Setting a Header to null

  • doesn't change its count (stays at zero)
  • doesn't change the Response output (header is not output)

However it will cause an error if a real value is added after:

app.Run(async (context) =>
{
    var resposne = context.Response;

    // Prints zero
    Console.WriteLine(resposne.Headers["warning"].Count);

    // Does not change response
    resposne.Headers.Add("warning", new StringValues((string)null));

    // Prints zero
    Console.WriteLine(resposne.Headers["warning"].Count);

    // Throws: System.ArgumentException: An item with the same key has already been added.
    resposne.Headers.Add("warning", new StringValues("Hello"));

    await context.Response.WriteAsync("Hello World!");
});

If you can't smell it, see it, taste it, does it exist?

Should it not throw, but set the value? (as no value is set)

From aspnet/KestrelHttpServer#2347 (which was treating it as not set for perf reasons; i.e. don't expend extra cycles tracking nothing for no effect other than to just to throw an error on a second add)

/cc @halter73 @davidfowl

Metadata

Metadata

Labels

affected-very-fewThis issue impacts very few customersarea-networkingIncludes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractionsbugThis issue describes a behavior which is not expected - a bug.feature-kestrelhelp wantedUp for grabs. We would accept a PR to help resolve this issueseverity-nice-to-haveThis label is used by an internal tool

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions