-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpClientHintsHttpContextExtensions.cs
More file actions
76 lines (62 loc) · 3.41 KB
/
HttpClientHintsHttpContextExtensions.cs
File metadata and controls
76 lines (62 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Copyright © https://myCSharp.de - all rights reserved
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Primitives;
namespace MyCSharp.HttpClientHints.AspNetCore;
/// <summary>
/// Extension methods for accessing HTTP Client Hints from the <see cref="HttpContext"/> and its headers.
/// </summary>
public static class HttpClientHintsHttpContextExtensions
{
/// <summary>
/// The cache key used to store the client hints in the HttpContext.Items dictionary.
/// </summary>
private const string ClientHintsCacheKey = "__HttpClientHints";
/// <summary>
/// Retrieves the <see cref="HttpClientHints"/> from the current HTTP context.
/// </summary>
/// <param name="context">The HTTP context containing the request headers.</param>
/// <returns>An instance of <see cref="HttpClientHints"/> populated with the relevant header values.</returns>
public static HttpClientHints GetClientHints(this HttpContext context)
{
// Check if client hints are already cached for this request
if (context.Items.TryGetValue(ClientHintsCacheKey, out object? cached) && cached is HttpClientHints hints)
{
return hints;
}
// Create and cache new client hints
HttpClientHints newHints = context.Request.Headers.GetClientHints();
context.Items[ClientHintsCacheKey] = newHints;
return newHints;
}
/// <summary>
/// Retrieves the <see cref="HttpClientHints"/> from the specified header dictionary.
/// </summary>
/// <param name="headers">The header dictionary containing the Client Hints headers.</param>
/// <returns>An instance of <see cref="HttpClientHints"/> populated with the relevant header values.</returns>
public static HttpClientHints GetClientHints(this IHeaderDictionary headers)
{
// User Agent
headers.TryGetValue("User-Agent", out StringValues userAgentValues);
string? userAgent = userAgentValues.Count > 0 ? userAgentValues[0] : null;
headers.TryGetValue("Sec-CH-UA", out StringValues uaValues);
string? ua = uaValues.Count > 0 ? uaValues[0] : null;
// Platform
headers.TryGetValue("Sec-CH-UA-Platform", out StringValues platformValues);
string? platform = platformValues.Count > 0 ? platformValues[0] : null;
headers.TryGetValue("Sec-CH-UA-Platform-Version", out StringValues platformVersionValues);
string? platformVersion = platformVersionValues.Count > 0 ? platformVersionValues[0] : null;
// Architecture
headers.TryGetValue("Sec-CH-UA-Arch", out StringValues architectureValues);
string? architecture = architectureValues.Count > 0 ? architectureValues[0] : null;
// Other
headers.TryGetValue("Sec-CH-UA-Full-Version-List", out StringValues fullVersionListValues);
string? fullVersionList = fullVersionListValues.Count > 0 ? fullVersionListValues[0] : null;
// Device
headers.TryGetValue("Sec-CH-UA-Model", out StringValues modelValues);
string? model = modelValues.Count > 0 ? modelValues[0] : null;
headers.TryGetValue("Sec-CH-UA-Mobile", out StringValues mobileValues);
bool? mobile = HttpClientHintsInterpreter.IsMobile(mobileValues.Count > 0 ? mobileValues[0] : null);
// Return the HttpClientHints record
return new(userAgent, platform, platformVersion, architecture, model, fullVersionList, ua, mobile, headers);
}
}