Fix HttpHeaders.Remove to handle well-known header names#127165
Fix HttpHeaders.Remove to handle well-known header names#127165
Conversation
…owing for disallowed/invalid header names Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/d0ce6ca6-9f52-4275-b304-1ea2502913cc Co-authored-by: MihaZupan <25307628+MihaZupan@users.noreply.github.com>
…ders and fix trailing newline Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/d0ce6ca6-9f52-4275-b304-1ea2502913cc Co-authored-by: MihaZupan <25307628+MihaZupan@users.noreply.github.com>
Co-authored-by: Miha Zupan <mihazupan.zupan1@gmail.com>
There was a problem hiding this comment.
Pull request overview
This PR adjusts System.Net.Http.Headers.HttpHeaders behavior so that Contains(string) and Remove(string) return false (instead of throwing) when given invalid header names or well-known header names that are disallowed for a particular header collection (e.g., content headers on request/response headers).
Changes:
- Update
HttpHeaders.Contains(string)andHttpHeaders.Remove(string)to rely onTryGetHeaderDescriptorand returnfalseon invalid/disallowed names. - Update existing unit tests that previously expected exceptions for invalid names to now expect
false. - Add new unit tests ensuring
Contains/Removereturnfalsefor disallowed well-known headers across request/response/content header collections.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| src/libraries/System.Net.Http/src/System/Net/Http/Headers/HttpHeaders.cs | Changes Contains(string)/Remove(string) to use TryGetHeaderDescriptor, avoiding exceptions for invalid/disallowed names. |
| src/libraries/System.Net.Http/tests/UnitTests/Headers/HttpHeadersTest.cs | Updates exception-based tests to validate false return values for null/empty/invalid names; renames a mismatched test. |
| src/libraries/System.Net.Http/tests/UnitTests/Headers/HttpRequestHeadersTest.cs | Adds coverage for disallowed content headers on request headers returning false from Contains/Remove. |
| src/libraries/System.Net.Http/tests/UnitTests/Headers/HttpResponseHeadersTest.cs | Adds coverage for disallowed content headers on response headers returning false from Contains/Remove. |
| src/libraries/System.Net.Http/tests/UnitTests/Headers/HttpContentHeadersTest.cs | Adds coverage for disallowed request headers on content headers returning false from Contains/Remove. |
🤖 Copilot Code Review — PR #127165Note This review was generated by Copilot and reflects analysis from multiple model families (Claude, GPT, System.Net domain reviewer). Holistic AssessmentMotivation: The change aligns Approach: The implementation is clean — switching from Summary: Detailed Findings❌ Null argument must still throw
|
| Method | Null/empty | Invalid name | Disallowed type |
|---|---|---|---|
Add(string, ...) |
throws | throws | throws |
GetValues(string) |
throws | throws | throws |
TryAddWithoutValidation(string, ...) |
false |
false |
false |
TryGetValues(string, ...) |
false |
false |
false |
Contains(string) 🔄 |
false |
false |
false |
Remove(string) 🔄 |
false |
false |
false |
The bool-returning methods are now uniformly non-throwing (except Add/GetValues), which is a coherent split. The null concern is the main sticking point.
Models contributing: Claude Opus 4.6 (primary), GPT-5.3-Codex, System.Net domain reviewer (Claude Haiku 4.5)
Generated by Code Review for issue #127165 · ◷
Instead of throwing for header names that don't belong in this collection, return false.
This lets the caller fall back to the other header collection without knowing upfront which collection it should have picked.
To workaround this, YARP currently has a hardcoded list of our internal implementation detail of which headers are considered "content": https://github.com/dotnet/yarp/blob/3a6fd9f54b2ebc5de82a3e55bda2c6ada7bab4f6/src/ReverseProxy/Forwarder/RequestUtilities.cs#L96-L111
Closes #61968