You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
HttpHeaders Remove will throw for some header names #61968
area-System.Net.HttpenhancementProduct code improvement that does NOT require public API changes/additionsin-prThere is an active PR which will close this issue when it is merged
The current implementation of Remove will throw if the header name we try to remove is well-known and not a part of the _allowedHeaderTypes.
For example, this means that calling content.Headers.Remove("Accept-Encoding") will result in an exception being thrown.
The result is that users must be aware of which headers are well-known and content-specific as defined in KnownHeaders.cs, which is just an implementation detail.
For example, YARP is forced to keep such a list: _contentHeaders.
We should consider changing the implementation of Remove to return false in case a descriptor is not available for a given header name:
This would match the pattern for how TryAddWithoutValidation is used - try on request.Headers and fallback to request.Content.Headers.
Risk
The current behavior of throwing is likely working to inform the user of the fact that header collections differ and not all headers are treated equally.
The scenario of removing a non-constant header name is less likely in regular applications.
Note: The same applies to HttpHeaders.Contains as well.
With this one there is at least the workaround of using NonValidated.Contains which uses TryGetHeaderDescriptor as suggested above.
HttpHeaders.Contains and Remove will also throw for invalid header names (invalid tokens). NonValidated.Contains can be used as a workaround there.
area-System.Net.HttpenhancementProduct code improvement that does NOT require public API changes/additionsin-prThere is an active PR which will close this issue when it is merged
Activity
stephentoub commentedon Nov 23, 2021
This seems reasonable. It's surprising and inconsistent that this throws.
karelz commentedon Nov 23, 2021
Triage:
Ultimately we need to decide if YARP is special case in this pattern or not.
MihaZupan commentedon Mar 28, 2022
Note: The same applies to
HttpHeaders.Containsas well.With this one there is at least the workaround of using
NonValidated.Containswhich usesTryGetHeaderDescriptoras suggested above.HttpHeaders.ContainsandRemovewill also throw for invalid header names (invalid tokens).NonValidated.Containscan be used as a workaround there.