Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,31 @@ jobs:
name: nuget-packages
path: ./artifacts/*.nupkg
retention-days: 30

native-aot:
name: Native AOT (${{ matrix.framework }})
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
framework: [net8.0, net9.0, net10.0, net11.0]

steps:
- name: Checkout code
uses: actions/checkout@v6

- name: Setup .NET
uses: actions/setup-dotnet@v5
with:
dotnet-version: ${{ inputs.dotnet-version }}
global-json-file: ./global.json

- name: Install Native AOT prerequisites
run: sudo apt-get update && sudo apt-get install -y clang zlib1g-dev

# The libraries are rooted completely: every trimming or AOT warning fails the publish.
- name: Publish smoke test with Native AOT
run: dotnet publish tests/HttpClientHints.NativeAot.Smoke --configuration Release --framework ${{ matrix.framework }} --runtime linux-x64 --output ./artifacts/aot

- name: Run smoke test
run: ./artifacts/aot/MyCSharp.HttpClientHints.NativeAot.Smoke
6 changes: 3 additions & 3 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@
<RepositoryUrl>https://github.com/mycsharp/HttpClientHints</RepositoryUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageTags>UserAgent, User Agent, Client Hints, Browser, Client, Detector, Detection, Console, ASP, Desktop, Mobile</PackageTags>
<PackageIcon>mycsharpde-logo-nuget.png</PackageIcon>
<PackageIcon>httpclienthints-logo.png</PackageIcon>
</PropertyGroup>

<!-- Ensure the repository root res/mycsharpde-logo-nuget.png is packed into NuGet packages -->
<!-- Ensure the repository root res/httpclienthints-logo.png is packed into NuGet packages -->
<ItemGroup>
<None Include="$(MSBuildThisFileDirectory)res\mycsharpde-logo-nuget.png" Pack="true" PackagePath="" />
<None Include="$(MSBuildThisFileDirectory)res\httpclienthints-logo.png" Pack="true" PackagePath="" />
</ItemGroup>

<PropertyGroup Condition="'$(IsBenchmarkProject)' == 'true'">
Expand Down
9 changes: 8 additions & 1 deletion MyCSharp.HttpClientHints.sln
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "_", "_", "{E23F6AE5-FF68-4E
Justfile = Justfile
LICENSE = LICENSE
NuGet.config = NuGet.config
README.md = README.md
readme.md = readme.md
SECURITY.md = SECURITY.md
version.json = version.json
EndProjectSection
Expand All @@ -39,6 +39,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "perf", "perf", "{02EA681E-C
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HttpClientHints.Benchmarks", "perf\HttpClientHints.Benchmarks\HttpClientHints.Benchmarks.csproj", "{F4088F7A-51EC-4111-BBDB-CA36596B3F77}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HttpClientHints.NativeAot.Smoke", "tests\HttpClientHints.NativeAot.Smoke\HttpClientHints.NativeAot.Smoke.csproj", "{5DFAF474-48E3-45AE-A97C-F73910C57A05}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -69,6 +71,10 @@ Global
{F4088F7A-51EC-4111-BBDB-CA36596B3F77}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F4088F7A-51EC-4111-BBDB-CA36596B3F77}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F4088F7A-51EC-4111-BBDB-CA36596B3F77}.Release|Any CPU.Build.0 = Release|Any CPU
{5DFAF474-48E3-45AE-A97C-F73910C57A05}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5DFAF474-48E3-45AE-A97C-F73910C57A05}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5DFAF474-48E3-45AE-A97C-F73910C57A05}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5DFAF474-48E3-45AE-A97C-F73910C57A05}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -80,6 +86,7 @@ Global
{E60AE9AF-09E5-4FE0-A005-5F697599E508} = {3CF449E7-2707-44A2-BACC-6F9A5C8DBA63}
{2F2815DE-1730-4645-B6CF-13599BBA3B2C} = {DD28B2F1-1CF8-4070-A997-83DE356EA88C}
{F4088F7A-51EC-4111-BBDB-CA36596B3F77} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8}
{5DFAF474-48E3-45AE-A97C-F73910C57A05} = {3CF449E7-2707-44A2-BACC-6F9A5C8DBA63}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {B0EDDFE8-3921-4DCE-8524-12765744AE12}
Expand Down
64 changes: 53 additions & 11 deletions README.md → readme.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,45 @@
<div align="center">

<img src="res/httpclienthints-logo.png" alt="HttpClientHints logo: the myCSharp.de raccoon in a browser window, reading the Sec-CH-UA-Platform Client Hint with a magnifying glass" width="180" />

# MyCSharp.HttpClientHints

Reading HTTP Client Hints with .NET
**Reading HTTP Client Hints with .NET**

Requests Client Hints with `Accept-CH` and reads the browser, platform, architecture, model and mobile hints in ASP.NET Core.

[![Build](https://img.shields.io/github/actions/workflow/status/mycsharp/HttpClientHints/main-build.yml?branch=main&logo=githubactions&logoColor=white&label=build)](https://github.com/mycsharp/HttpClientHints/actions/workflows/main-build.yml)
[![Coverage: 100% line and branch coverage, enforced in CI](https://img.shields.io/badge/coverage-100%25-brightgreen)](#build-and-test)
[![NuGet](https://img.shields.io/nuget/v/MyCSharp.HttpClientHints?logo=nuget&label=nuget&color=004880)](https://nubrowse.com/packages/MyCSharp.HttpClientHints)
[![NuGet downloads](https://img.shields.io/nuget/dt/MyCSharp.HttpClientHints?logo=nuget&label=downloads&color=004880)](https://nubrowse.com/packages/MyCSharp.HttpClientHints)
[![License: MIT](https://img.shields.io/github/license/mycsharp/HttpClientHints?color=blue)](LICENSE)

[![.NET 8 | 9 | 10 | 11](https://img.shields.io/badge/.NET-8%20%7C%209%20%7C%2010%20%7C%2011-512BD4?logo=dotnet&logoColor=white)](https://dotnet.microsoft.com/)
[![Native AOT compatible](https://img.shields.io/badge/Native%20AOT-compatible-512BD4?logo=dotnet&logoColor=white)](#native-aot-and-trimming)
[![Trimming compatible](https://img.shields.io/badge/trimming-compatible-512BD4?logo=dotnet&logoColor=white)](#native-aot-and-trimming)
[![Strong-named assemblies](https://img.shields.io/badge/strong--named-yes-blue)](https://learn.microsoft.com/dotnet/standard/assembly/strong-named)
[![SourceLink enabled](https://img.shields.io/badge/SourceLink-enabled-blue)](https://learn.microsoft.com/dotnet/standard/library-guidance/sourcelink)

[![GitHub stars](https://img.shields.io/github/stars/mycsharp/HttpClientHints?logo=github)](https://github.com/mycsharp/HttpClientHints/stargazers)
[![Last commit](https://img.shields.io/github/last-commit/mycsharp/HttpClientHints/main?logo=github)](https://github.com/mycsharp/HttpClientHints/commits/main)
[![Open issues](https://img.shields.io/github/issues/mycsharp/HttpClientHints?logo=github)](https://github.com/mycsharp/HttpClientHints/issues)

</div>

## NuGet

| NuGet | Install |
|-|-|
| [![MyCSharp.HttpClientHints](https://img.shields.io/nuget/v/MyCSharp.HttpClientHints.svg?logo=nuget&label=MyCSharp.HttpClientHints)](https://www.nuget.org/packages/MyCSharp.HttpClientHints) | `dotnet add package MyCSharp.HttpClientHints` |
| [![MyCSharp.HttpClientHints.AspNetCore](https://img.shields.io/nuget/v/MyCSharp.HttpClientHints.AspNetCore.svg?logo=nuget&label=MyCSharp.HttpClientHints.AspNetCore)](https://www.nuget.org/packages/MyCSharp.HttpClientHints.AspNetCore) | `dotnet add package MyCSharp.HttpClientHints.AspNetCore` |
| Package | Version | Downloads | Install |
| --- | --- | --- | --- |
| **[MyCSharp.HttpClientHints](https://nubrowse.com/packages/MyCSharp.HttpClientHints)**<br><sub>`HttpClientHints` record and header decoding</sub> | [![NuGet](https://img.shields.io/nuget/v/MyCSharp.HttpClientHints?logo=nuget&label=nuget&color=004880)](https://nubrowse.com/packages/MyCSharp.HttpClientHints) | [![Downloads](https://img.shields.io/nuget/dt/MyCSharp.HttpClientHints?label=downloads&color=004880)](https://nubrowse.com/packages/MyCSharp.HttpClientHints) | `dotnet add package MyCSharp.HttpClientHints` |
| **[MyCSharp.HttpClientHints.AspNetCore](https://nubrowse.com/packages/MyCSharp.HttpClientHints.AspNetCore)**<br><sub>`Accept-CH` middleware and `HttpContext` integration</sub> | [![NuGet](https://img.shields.io/nuget/v/MyCSharp.HttpClientHints.AspNetCore?logo=nuget&label=nuget&color=004880)](https://nubrowse.com/packages/MyCSharp.HttpClientHints.AspNetCore) | [![Downloads](https://img.shields.io/nuget/dt/MyCSharp.HttpClientHints.AspNetCore?label=downloads&color=004880)](https://nubrowse.com/packages/MyCSharp.HttpClientHints.AspNetCore) | `dotnet add package MyCSharp.HttpClientHints.AspNetCore` |

The NuGet packages target .NET 8, 9, 10, and 11. Building the solution requires the .NET 11 SDK. ASP.NET Core applications only need `MyCSharp.HttpClientHints.AspNetCore`, which references the core package.

## Client Hints

HTTP client hints are intended to completely replace the user agents (see [mycsharp/HttpUserAgentParser](https://github.com/mycsharp/HttpUserAgentParser)) of the browser in the long term and pay more attention to data protection. We are currently in a phase in which client hints are generally regarded as experimental and therefore do not represent a complete alternative.
User-Agent Client Hints are intended to replace the `User-Agent` header (see [mycsharp/HttpUserAgentParser](https://github.com/mycsharp/HttpUserAgentParser)) in the long term and expose less information by default. Not every browser sends them yet, so they are not a complete alternative to the `User-Agent` header today.

Furthermore, in contrast to the User-Agent header, not all client hints are provided automatically, but only on request after the first response from the client.
Unlike the `User-Agent` header, browsers send most Client Hints only after the server requests them with the `Accept-CH` response header, and only over HTTPS. A few hints, such as `Sec-CH-UA`, `Sec-CH-UA-Mobile`, and `Sec-CH-UA-Platform`, are sent with every request.

## Usage

Expand Down Expand Up @@ -74,7 +99,7 @@ Since potentially all client hints are optional, all properties are potentially

## HttpContext feature

Since 3.3, you can register `IHttpClientHintsFeature` in `HttpContext.Features` with a separate middleware. Add it only if you want to read the Client Hints through the feature:
A separate middleware registers `IHttpClientHintsFeature` in `HttpContext.Features`. Add it only if you want to read the Client Hints through the feature:

```csharp
app.UseHttpClientHints();
Expand Down Expand Up @@ -103,7 +128,7 @@ Without `UseHttpClientHintsFeature()`, requests have no additional cost, and `Fe

## Decoded values

Browsers send `Sec-CH-UA-Platform`, `Sec-CH-UA-Platform-Version`, `Sec-CH-UA-Arch`, and `Sec-CH-UA-Model` as quoted strings, for example `Sec-CH-UA-Platform: "Windows"`. `Platform`, `PlatformVersion`, `Architecture`, and `Model` return these header values as sent, including the quotes. Since 3.2, the matching `Decoded*` properties return the value without quotes and escapes.
Browsers send `Sec-CH-UA-Platform`, `Sec-CH-UA-Platform-Version`, `Sec-CH-UA-Arch`, and `Sec-CH-UA-Model` as quoted strings, for example `Sec-CH-UA-Platform: "Windows"`. `Platform`, `PlatformVersion`, `Architecture`, and `Model` return these header values as sent, including the quotes. The matching `Decoded*` properties return the value without quotes and escapes.

| Request header | Property | Decoded property |
| --- | --- | --- |
Expand Down Expand Up @@ -165,9 +190,26 @@ Like the platform, `Sec-CH-UA-Bitness` is sent as a quoted string such as `"64"`
string? decodedBitness = HttpClientHintsInterpreter.DecodeStructuredString(bitness); // 64
```

## Native AOT and trimming

Both packages are annotated with `IsAotCompatible`, so the trimming and AOT analyzers check them on every build. The Native AOT smoke test (`tests/HttpClientHints.NativeAot.Smoke`) is published and run with .NET 8, 9, 10, and 11 in every CI build of `main` and of pull requests:

```shell
dotnet publish tests/HttpClientHints.NativeAot.Smoke -c Release -f net11.0 -r linux-x64
```

The app roots both library assemblies, so the trimmer and the AOT compiler analyze all of their code, and the publish fails on any trimming or AOT warning. At runtime it checks:

- decoding of quoted header values and of `Sec-CH-UA-Mobile`
- the `Accept-CH` and `Accept-CH-Lifetime` response headers of `UseHttpClientHints()`
- `GetClientHints()` and `GetClientHintsView()` on `HttpContext` and `IHeaderDictionary`
- `IHttpClientHintsFeature` registered by `UseHttpClientHintsFeature()`

Publishing with Native AOT requires the [platform toolchain](https://learn.microsoft.com/dotnet/core/deploying/native-aot/#prerequisites): `clang` and `zlib1g-dev` on Linux, the Visual Studio workload "Desktop development with C++" on Windows.

## Build and test

Use the .NET 11 SDK to build the solution. Both NuGet packages target .NET 8, 9, 10, and 11. The .NET 11 build uses `Microsoft.Extensions.Primitives` from the shared runtime. Install the corresponding runtimes to run the full test suite.
Use the .NET 11 SDK to build the solution. The .NET 11 build uses `Microsoft.Extensions.Primitives` from the shared runtime. Install the corresponding runtimes to run the full test suite.

```shell
dotnet build -c Release
Expand Down Expand Up @@ -219,7 +261,7 @@ Reading header values from the view does not allocate, and a snapshot allocates

### Middleware

The middleware benchmarks run pipelines built with `ApplicationBuilder`. They reuse one `DefaultHttpContext`, and the last handler completes synchronously. `UseHttpClientHints()` sets `Accept-CH` and `Accept-CH-Lifetime` and does not allocate in this setup. If a later middleware completes asynchronously, it allocates its async state machine, as in earlier versions. `UseHttpClientHintsFeature()` allocates the 24-byte feature and returns the task of the next middleware directly. Registering the feature changes the revision of the feature collection, so `HttpContext` looks up the features it has cached again. Other servers can allocate more for the registration, see [HttpContext feature](#httpcontext-feature).
The middleware benchmarks run pipelines built with `ApplicationBuilder`. They reuse one `DefaultHttpContext`, and the last handler completes synchronously. `UseHttpClientHints()` sets `Accept-CH` and `Accept-CH-Lifetime` and does not allocate in this setup. If a later middleware completes asynchronously, it allocates its async state machine. `UseHttpClientHintsFeature()` allocates the 24-byte feature and returns the task of the next middleware directly. Registering the feature changes the revision of the feature collection, so `HttpContext` looks up the features it has cached again. Other servers can allocate more for the registration, see [HttpContext feature](#httpcontext-feature).

| Method | Mean | Error | StdDev | Gen0 | Allocated |
|------------------------------------- |---------:|---------:|---------:|-------:|----------:|
Expand Down
Binary file added res/httpclienthints-logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
<Description>HTTP Client Hints Extensions for ASP.NET Core</Description>
</PropertyGroup>

<PropertyGroup Label="Native AOT and trimming">
<IsAotCompatible>true</IsAotCompatible>
</PropertyGroup>

<PropertyGroup Label="Package">
<IsPackable>true</IsPackable>
<PackageReadmeFile>readme.md</PackageReadmeFile>
Expand Down
59 changes: 54 additions & 5 deletions src/HttpClientHints.AspNetCore/readme.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,58 @@
# MyCSharp.HttpClientHints
# MyCSharp.HttpClientHints.AspNetCore

Read HTTP Client Hints with .NET
ASP.NET Core integration for MyCSharp.HttpClientHints.

Since 3.2, `HttpClientHints` and `HttpClientHintsView` have `DecodedPlatform`, `DecodedPlatformVersion`, `DecodedArchitecture`, and `DecodedModel` properties that return the header values without quotes. [Decoded values](https://github.com/mycsharp/HttpClientHints#decoded-values)

Since 3.3, the optional `app.UseHttpClientHintsFeature()` middleware registers `IHttpClientHintsFeature` in `HttpContext.Features`. [HttpContext feature](https://github.com/mycsharp/HttpClientHints#httpcontext-feature)
- Middleware that requests Client Hints with the `Accept-CH` response header
- `GetClientHints()` and `GetClientHintsView()` on `HttpContext` and `IHeaderDictionary`
- Optional `IHttpClientHintsFeature` in `HttpContext.Features`
- Trimming and Native AOT compatible

Repository:
https://github.com/mycsharp/HttpClientHints

## Install

```bash
dotnet add package MyCSharp.HttpClientHints.AspNetCore
```

## Quick start

Configure the hints to request and register the middleware:

```csharp
builder.Services.AddHttpClientHints(options =>
{
options.Platform = true;
options.Mobile = true;
options.Lifetime = TimeSpan.FromDays(31);
});

app.UseHttpClientHints();
```

Read the hints of a request:

```csharp
HttpClientHints hints = httpContext.GetClientHints(); // reads the headers once per request
HttpClientHintsView view = httpContext.GetClientHintsView(); // reads the headers on every access, does not allocate
string? platform = view.DecodedPlatform; // Windows instead of "Windows"
bool? isMobile = view.Mobile;
```

All properties are nullable, because the client may not send a hint.

## Decoded values

`HttpClientHints` and `HttpClientHintsView` have `DecodedPlatform`, `DecodedPlatformVersion`, `DecodedArchitecture`, and `DecodedModel` properties that return the header values without quotes. [Decoded values](https://github.com/mycsharp/HttpClientHints#decoded-values)

## HttpContext feature

The optional `app.UseHttpClientHintsFeature()` middleware registers `IHttpClientHintsFeature` in `HttpContext.Features`. [HttpContext feature](https://github.com/mycsharp/HttpClientHints#httpcontext-feature)

```csharp
app.UseHttpClientHintsFeature();

IHttpClientHintsFeature? feature = httpContext.Features.Get<IHttpClientHintsFeature>();
bool? isMobile = feature?.View.Mobile;
```
4 changes: 4 additions & 0 deletions src/HttpClientHints/HttpClientHints.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
<Description>Reads HTTP Client Hints to identify Browsers, Platforms and Bots.</Description>
</PropertyGroup>

<PropertyGroup Label="Native AOT and trimming">
<IsAotCompatible>true</IsAotCompatible>
</PropertyGroup>

<PropertyGroup Label="Package">
<IsPackable>true</IsPackable>
<PackageReadmeFile>readme.md</PackageReadmeFile>
Expand Down
27 changes: 25 additions & 2 deletions src/HttpClientHints/readme.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,30 @@
# MyCSharp.HttpClientHints

Read HTTP Client Hints with .NET
Read HTTP Client Hints with .NET.

Since 3.2, the `HttpClientHints` record has `DecodedPlatform`, `DecodedPlatformVersion`, `DecodedArchitecture`, and `DecodedModel` properties that return the header values without quotes, and `HttpClientHintsInterpreter.DecodeStructuredString` decodes other quoted header values. [Decoded values](https://github.com/mycsharp/HttpClientHints#decoded-values)
- `HttpClientHints` record with the values of the `Sec-CH-UA` request headers and access to all received headers
- `Decoded*` properties return quoted header values such as `"Windows"` without quotes and escapes
- `HttpClientHintsInterpreter` interprets `Sec-CH-UA-Mobile` and decodes other quoted header values ([RFC 9651 § 4.2.5](https://www.rfc-editor.org/rfc/rfc9651.html#section-4.2.5))
- Trimming and Native AOT compatible

Repository:
https://github.com/mycsharp/HttpClientHints

## Install

```bash
dotnet add package MyCSharp.HttpClientHints
```

For ASP.NET Core, install [MyCSharp.HttpClientHints.AspNetCore](https://nubrowse.com/packages/MyCSharp.HttpClientHints.AspNetCore) instead. It requests the hints with `Accept-CH` and reads them from the `HttpContext`.

## Decoded values

The `HttpClientHints` record has `DecodedPlatform`, `DecodedPlatformVersion`, `DecodedArchitecture`, and `DecodedModel` properties that return the header values without quotes, and `HttpClientHintsInterpreter.DecodeStructuredString` decodes other quoted header values.

```csharp
string? bitness = HttpClientHintsInterpreter.DecodeStructuredString("\"64\""); // 64
bool? isMobile = HttpClientHintsInterpreter.IsMobile("?1"); // true
```

[Decoded values](https://github.com/mycsharp/HttpClientHints#decoded-values)
Loading