Skip to content

feat: add decoded Client Hints values, enforce 90% coverage - #20

Merged
BenjaminAbt merged 3 commits into
mainfrom
feature/decode-structured-strings
Nov 21, 2026
Merged

BenjaminAbt merged 3 commits into
mainfrom
feature/decode-structured-strings

Conversation

@BenjaminAbt

@BenjaminAbt BenjaminAbt commented Nov 21, 2026 •

Copy link
Copy Markdown
Member

Summary

This PR does two things:

  1. It adds DecodedPlatform, DecodedPlatformVersion, DecodedArchitecture, and DecodedModel to HttpClientHints and HttpClientHintsView. Browsers send these Client Hints as quoted HTTP Structured Field strings, such as Sec-CH-UA-Platform: "Windows". The new properties return Windows instead of "Windows". They decode on every access and store nothing. Platform, PlatformVersion, Architecture, and Model keep returning the header values as sent, so the change is not breaking. The version goes from 3.1 to 3.2.
  2. It enforces a minimum of 90% line and branch coverage in CI. Coverage is currently 100% for both.

The first commit made Platform and the other three properties return decoded values and added Raw* properties for the original values, as a breaking 4.0 change. The second commit replaces that approach with the one described here. Please use "Squash and merge", so that the feat! commit message of the first commit does not end up in main.

Changes

Library

HttpClientHintsInterpreter.DecodeStructuredString (MyCSharp.HttpClientHints, new)

  • Decodes a header value that is an sf-string (RFC 9651 § 4.2.5). It is public, like IsMobile, so that applications without ASP.NET Core can use it.

    Header value Result
    null null
    "Windows" Windows
    "" string.Empty
    "Model \"Pro\" \\ Edition" Model "Pro" \ Edition
    "Windows" (leading or trailing SP or HTAB) Windows
    Windows (unquoted) the same string instance
    "Windows (unterminated) null
    "a\qb" (escape other than \" or \\) null
    "Modèle", "a<TAB>b", "a<DEL>b" (character outside %x20-7E) null
    "Windows";a=1 (parameters) null
  • It allocates nothing for unquoted values and for a quoted empty value. It allocates a StringBuilder only if the value contains an escape.

HttpClientHints record (MyCSharp.HttpClientHints)

  • New computed properties DecodedPlatform, DecodedPlatformVersion, DecodedArchitecture, and DecodedModel. Each one calls DecodeStructuredString on the matching positional property.
  • The positional constructor and its parameters are unchanged. Their XML docs now state that the values include the quotes and point to the Decoded* properties.

HttpClientHintsView (MyCSharp.HttpClientHints.AspNetCore)

  • The same four Decoded* properties, computed from the header values on every access.
  • BuildSnapshot() is unchanged and still allocates only the record.
  • The struct summary no longer says "zero-allocation", because the Decoded* properties allocate the decoded string of a quoted value. The remarks state which reads allocate.

Version

  • version.json goes from 3.1 to 3.2.

Tests

  • DecodeStructuredString_ShouldReturnExpectedResult_ForGivenInput covers 21 header values: missing, empty, whitespace-only, unquoted, quoted empty, a typical quoted value, leading and trailing whitespace, inner whitespace, both escapes, the character range boundaries !#[]~, a lone ", an unterminated value, trailing text, a trailing backslash, an invalid escape, two strings, a tab, DEL, a non-ASCII character, and parameters.
  • DecodeStructuredString_ReturnsTheSameInstance_ForUnquotedValues checks that unquoted values are not copied.
  • New HttpClientHintsTests check that the record decodes all four properties, returns null for missing and malformed values, follows with expressions, and that reading a Decoded* property does not affect equality or the hash code.
  • The ASP.NET Core tests use real browser values ("Windows", "19.0.0", "x86", "") and check that the default properties keep the quotes and the Decoded* properties remove them, for the view, for the snapshot, and for the cached snapshot of an HttpContext.

CI and tooling

  • build-and-test.yml collects Cobertura coverage in the existing test run (--coverage --coverage-output-format cobertura). A new step, Check code coverage, fails the build when the merged line or branch coverage is below 90%. The Cobertura reports are uploaded with the TRX files.
  • New script eng/Assert-CodeCoverage.ps1 reads the merged Cobertura report that dotnet test writes and compares its line and branch rates with -Threshold (default 90). Microsoft.Testing.Extensions.CodeCoverage has no threshold option of its own.
  • just test-cov deletes old results, collects coverage for the default target framework, and runs the same check. just ci runs it as before.

Benchmarks

  • New benchmark ReadDecodedViewProperties reads the same four view properties as ReadViewProperties, but uses DecodedPlatform and DecodedArchitecture.

Documentation

  • The README has a new "Decoded values" section: a table that maps each header to its property and decoded property, a code example, the decoding rules, and a note that the Decoded* properties decode on every access.
  • "Usage" explains the difference between GetClientHints(), which returns a record that is cached for the request, and GetClientHintsView(), which reads the current headers without allocating. The new section refers to both types, and the README did not mention the view before.
  • "Customization" lists the headers that each option requests. The Additional example now requests Sec-CH-UA-Full-Version-List instead of Sec-CH-UA-Bitness. Architecture already requests Sec-CH-UA-Bitness, so the old example sent it twice in Accept-CH, and FullVersionList stayed null because nothing requested its header. The section also shows how to decode Sec-CH-UA-Bitness with DecodeStructuredString.
  • The NuGet table has an install column. Before, the table header had one column, so GitHub did not render the dotnet add package command. A typo is fixed ("experimental").
  • The package readmes of both NuGet packages mention the new properties and link to the "Decoded values" section.
  • The "Build and test" section describes the coverage requirement and how to check it locally.
  • The benchmark section shows the current results and explains which reads allocate.

Sample

  • The ASP.NET Core MVC sample shows each header value next to its decoded value, including Sec-CH-UA-Bitness decoded with DecodeStructuredString.
  • It reads the hints with HttpContext.GetClientHints() instead of building a new record from the headers.
  • It requests Sec-CH-UA-Full-Version-List instead of requesting Sec-CH-UA-Bitness a second time.
  • The empty "Last" and "Handle" columns from the project template and a stray " after Sec-CH-UA-Full-Version-List are removed.

Notes for reviewers

  • Why the default properties keep the header value: Decoding them by default would change their values without a compile error. Code such as Platform == "\"Windows\"" would silently evaluate to false. It would also make every snapshot more expensive, even when nobody reads the decoded values. The first commit measured 232 B instead of 88 B and 97 ns instead of 59 ns for a snapshot with all Client Hints.
  • Why the decoded values are not cached: A record's synthesized Equals and GetHashCode include all instance fields, including private ones. A cache field would make two otherwise equal snapshots unequal, depending on whether a decoded value was read. A with expression also copies all fields, so hints with { Platform = "\"Linux\"" } would return a stale cached Windows. Computed properties avoid both problems. A test covers each case.
  • ToString() output changes: The synthesized ToString() of a record prints all public properties, so it now also prints the four Decoded* values and decodes them for that call.
  • Discoverability: The obvious name Platform still returns the value with quotes. The XML docs of Platform and the other three properties point to the matching Decoded* property.
  • Parameters are not supported on purpose: "Windows";a=1 is a valid RFC 9651 Item, but the decoded value is null. Browsers do not send parameters on these headers, and supporting them would need a full parameter parser. The README and XML docs state this, and tests cover it.
  • More lenient than RFC 9651 in two places: Unquoted values pass through unchanged instead of yielding null, so non-compliant clients still produce a value. Leading and trailing HTAB is trimmed in addition to SP, matching HTTP's optional whitespace around field values.

Testing

  • dotnet build -c Release: 0 warnings, 0 errors, with TreatWarningsAsErrors enabled.
  • The CI test command with coverage, dotnet test -c Release --no-build --results-directory TestResults --results-directory-layout per-module -- --report-trx --coverage --coverage-output-format cobertura: 244 tests passed, 61 per target framework on net8.0, net9.0, net10.0, and net11.0 (37 in HttpClientHints.UnitTests, 24 in HttpClientHints.AspNetCore.UnitTests).
  • ./eng/Assert-CodeCoverage.ps1 -ResultsDirectory TestResults -Threshold 90 on that run: 100.00% line coverage and 100.00% branch coverage.
  • The same script on a synthetic Cobertura report with 85% line coverage exits with code 1.
  • just test-cov: 100.00% line and branch coverage on net11.0.
  • dotnet format --verify-no-changes: no changes.
  • The sample on net11.0, requested with curl and browser-like Client Hints: the page shows "Windows" next to Windows, "19.0.0" next to 19.0.0, "x86" next to x86, "64" next to 64, and "" next to an empty decoded model. The response sends Accept-CH: User-Agent, Sec-CH-UA, Sec-CH-UA-Platform, Sec-CH-UA-Platform-Version, Sec-CH-UA-Arch, Sec-CH-UA-Bitness, Sec-CH-UA-Model, Sec-CH-UA-Mobile, Sec-CH-UA-Full-Version-List, with each header once.
  • The CI run for the first commit already ran the new coverage step on the Linux runner and reported 100.00% line and branch coverage.

Benchmark

The "3.1" column is the benchmark output in the README on main, measured for #19. The "This PR" column was measured for this PR. Both runs used the same machine and settings: AMD Ryzen 9 9950X, Windows 10 x64, .NET SDK 11.0.100, .NET 11.0.0, BenchmarkDotNet 0.16.0-preview.2. The two versions were not measured side by side in one session.

Scenario 3.1, mean This PR, mean 3.1, allocated This PR, allocated
Read four properties from the view 18.69 ns 18.96 ns 0 B 0 B
Read four properties from the view, two of them decoded (new) – 36.65 ns – 72 B
Build a snapshot with all Client Hints 58.70 ns 57.35 ns 88 B 88 B
Build a snapshot with only User-Agent 51.31 ns 49.92 ns 88 B 88 B
Build and cache a snapshot on a cache miss 76.25 ns 71.12 ns 88 B 88 B
Retrieve a cached snapshot 6.17 ns 6.05 ns 0 B 0 B

This PR does not change the code that the existing five scenarios run, so their differences are variation between the two runs. In the new scenario, decoding "Windows" and "x86" adds about 18 ns and allocates the two decoded strings: 40 B for Windows and 32 B for x86 on x64. BenchmarkDotNet reported a bimodal distribution for ReadViewProperties in this run.

Reproduce

dotnet run -c Release --project perf/HttpClientHints.Benchmarks --framework net11.0 -- --filter '*' --iterationCount 15 --iterationTime 200 --launchCount 2 --warmupCount 6
Raw benchmark output, .NET 11, this PR
BenchmarkDotNet v0.16.0-preview.2, Windows 10 (10.0.19045.7846/22H2/2022Update)
AMD Ryzen 9 9950X 4.30GHz, 1 CPU, 32 logical and 16 physical cores
Memory: 61,64 GB Total, 31,05 GB Available
.NET SDK 11.0.100
  [Host]     : .NET 11.0.0 (11.0.0, 11.0.26.53018), X64 RyuJIT x86-64-v4
  Job-ZRYWOI : .NET 11.0.0 (11.0.0, 11.0.26.53018), X64 RyuJIT x86-64-v4

IterationCount=15  IterationTime=200ms  LaunchCount=2
WarmupCount=6
Method Mean Error StdDev Gen0 Allocated
ReadViewProperties 18.956 ns 0.3872 ns 0.5299 ns - -
ReadDecodedViewProperties 36.646 ns 0.5085 ns 0.6961 ns 0.0042 72 B
GetClientHintsFromHeaders 57.352 ns 0.4389 ns 0.6295 ns 0.0052 88 B
GetClientHintsFromHeadersWithoutHints 49.915 ns 0.7589 ns 1.1124 ns 0.0050 88 B
GetClientHintsFromContextUncached 71.118 ns 0.5539 ns 0.8119 ns 0.0049 88 B
GetClientHintsFromContextCached 6.052 ns 0.1073 ns 0.1538 ns - -

- Decode the RFC 9651 strings of Sec-CH-UA-Platform, Sec-CH-UA-Platform-Version,
  Sec-CH-UA-Arch, and Sec-CH-UA-Model in Platform, PlatformVersion,
  Architecture, and Model
- Add RawPlatform, RawPlatformVersion, RawArchitecture, and RawModel to
  HttpClientHintsView and the HttpClientHints record
- Bump the version to 4.0 and add an upgrade guide to the README
- Collect code coverage in CI and fail below 90% line or branch coverage
- Add a raw-read benchmark and update the README benchmark results
- Platform, PlatformVersion, Architecture, and Model return the header
  values as sent again, as in 3.x, so the change is no longer breaking
- Add DecodedPlatform, DecodedPlatformVersion, DecodedArchitecture, and
  DecodedModel to HttpClientHints and HttpClientHintsView; they decode on
  every access and store nothing
- Move the decoder to the public HttpClientHintsInterpreter.DecodeStructuredString
- Remove the Raw* properties and set the version to 3.2 instead of 4.0
- Replace the upgrade guide with a "Decoded values" README section
- Benchmark decoded reads with ReadDecodedViewProperties
@BenjaminAbt BenjaminAbt changed the title feat!: decode Client Hints structured strings, enforce 90% coverage feat: add decoded Client Hints values, enforce 90% coverage Nov 21, 2026
- README: explain GetClientHints and GetClientHintsView, list the headers
  each option requests, and use Sec-CH-UA-Full-Version-List as the
  Additional example instead of Sec-CH-UA-Bitness, which Architecture
  already requests
- README: show DecodeStructuredString for Sec-CH-UA-Bitness, show the
  install command for both packages, fix a typo
- Sample: show header values next to their decoded values, decode
  Sec-CH-UA-Bitness, request Sec-CH-UA-Full-Version-List, and remove the
  empty template columns
@BenjaminAbt
BenjaminAbt merged commit 2a62939 into main Nov 21, 2026
1 check passed
@BenjaminAbt
BenjaminAbt deleted the feature/decode-structured-strings branch November 21, 2026 15:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant