Skip to content

HttpHeaders Remove will throw for some header names #61968

@MihaZupan

Description

@MihaZupan

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.

Metadata

Metadata

Labels

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

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