Skip to content

HTTP Headers are enumerated in the wrong order when added via a mix of parsed and unparsed APIs #63831

Closed
@geoffkizer

Description

@geoffkizer

Repro code:

request.Headers.Add("Accept", "text/foo");
request.Headers.TryAddWithoutValidation("Accept", "text/bar");

Console.WriteLine("NonValidated before parsing:");
PrintHeadersNonValidated(request);

// Force parsing
_ = request.Headers.Accept.Count;

Console.WriteLine("NonValidated after parsing:");
PrintHeadersNonValidated(request);

static void PrintHeadersNonValidated(HttpRequestMessage request)
{
    foreach (var header in request.Headers.NonValidated)
    {
        Console.WriteLine($"  {header.Key}:");
        foreach (var value in header.Value)
        {
            Console.WriteLine($"    {value}");
        }
    }

    Console.WriteLine();
}

Output:

NonValidated before parsing (incorrect):
  Accept:
    text/bar
    text/foo

NonValidated after parsing (correct):
  Accept:
    text/foo
    text/bar

Note that this behavior is not limited to NonValidated -- it affects how the request headers are written to the wire when the request is sent.

Mixing typed Add with untyped TryAddWithoutValidation is not a common thing to do, so the impact here is small.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions