Improve HTTP header parser recovery performance - #68306
Merged
Merged
Conversation
Avoid revisiting malformed input that header element parsers have already inspected. Add recovery regression tests and BenchmarkDotNet coverage for the affected list parser paths. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR improves the performance of forgiving (non-strict) HTTP header list parsing by ensuring element parsers report how much malformed input they already inspected, allowing the shared recovery loop to skip past invalid spans in O(1) rather than repeatedly re-scanning the same characters. It also adds targeted regression tests and microbenchmarks to validate both correctness (recovery of subsequent valid values) and performance at realistic worst-case input sizes.
Changes:
- Propagate “consumed malformed span” lengths from additional failure paths in
BaseHeaderParser<T>,MediaTypeHeaderValue,SetCookieHeaderValue, andStringWithQualityHeaderValue. - Add regression tests covering recovery of a valid value after malformed separators, media types (missing
// subtype), Set-Cookie (missing=), and invalid quality suffixes. - Add BenchmarkDotNet coverage for these scenarios up to 32,768-character inputs.
Reviewed changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| src/Http/Http/perf/Microbenchmarks/HeaderParserListBenchmarks.cs | Adds microbenchmarks for key malformed-list recovery scenarios across increasing input sizes. |
| src/Http/Headers/test/StringWithQualityHeaderValueTest.cs | Adds regression test ensuring invalid quality suffix recovery preserves following valid value. |
| src/Http/Headers/test/SetCookieHeaderValueTest.cs | Adds regression test ensuring missing = recovery preserves following valid Set-Cookie. |
| src/Http/Headers/test/MediaTypeHeaderValueTest.cs | Adds regression tests for recovery after missing / and missing subtype patterns. |
| src/Http/Headers/test/EntityTagHeaderValueTest.cs | Adds regression coverage validating separator-skipping recovery avoids repeated parsing work. |
| src/Http/Headers/src/StringWithQualityHeaderValue.cs | Reports consumed malformed span when quality parsing fails after ;. |
| src/Http/Headers/src/SetCookieHeaderValue.cs | Reports consumed malformed span when cookie name is missing =. |
| src/Http/Headers/src/MediaTypeHeaderValue.cs | Reports consumed malformed span for missing / and missing subtype cases via null mediaType signaling. |
| src/Http/Headers/src/BaseHeaderParser.cs | Reports consumed malformed span when list separator/whitespace was scanned before element parsing returns 0. |
💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.
cincuranet
approved these changes
Aug 11, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #68305
Summary
Validation
dotnet test .\src\Http\Headers\test\Microsoft.Net.Http.Headers.Tests.csproj -c Release --no-restoredotnet build .\src\Http\Http\perf\Microbenchmarks\Microsoft.AspNetCore.Http.Microbenchmarks.csproj -c Release --no-restore -p:UseIisNativeAssets=falseHeaderParserListBenchmarks