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:
-public bool Remove(string name) => Remove(GetHeaderDescriptor(name));
+public bool Remove(string name) => TryGetHeaderDescriptor(name, out HeaderDescriptor descriptor) && Remove(descriptor);
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.
The current implementation of
Removewill 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
Removeto returnfalsein case a descriptor is not available for a given header name:This would match the pattern for how
TryAddWithoutValidationis used - try onrequest.Headersand fallback torequest.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.