-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
HTTP Headers are enumerated in the wrong order when added via a mix of parsed and unparsed APIs #63831
Comments
Tagging subscribers to this area: @dotnet/ncl Issue DetailsRepro 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:
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.
|
Do we define/require a specific order today? Is this more about a new constraint we'd like to enable? |
Not that this answers the question but just for a reference RFC 2616 says:
|
Ordering of header values is significant for many multi-valued headers. In general we need to preserve the ordering as specified when adding these values. We do preserve this if you only add via parsed APIs, or only add via TryAddWithoutValidation. It's the mixing of these that leads to trouble. There are really two issues here: First: If you do Add and then TryAddWithoutValidation, then enumerate, you'll see the header that was added second get enumerated before the header that was added first. It could be argued that mixing Add and TryAddWithoutValidation like this will not guarantee ordering across the two; for example, we could say that headers added via TryAddWithoutValidation always come first. However, headers added with TryAddWithoutValidation get transformed into parsed headers whenever anything forces parsing, and thus parsing causes the headers to be reordered. That's the second issue, as shown above: forcing the headers to be parsed should not change their order. |
Triage: Should be fairly simple to change the order in which we enumerate. Not critical, but we should be closer to RFC with the fix. |
Repro code:
Output:
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.
The text was updated successfully, but these errors were encountered: