| | | 1 | | using System.Diagnostics; |
| | | 2 | | using AsiBackbone.AspNetCore.DependencyInjection; |
| | | 3 | | using AsiBackbone.Core; |
| | | 4 | | using Microsoft.AspNetCore.Http; |
| | | 5 | | using Microsoft.AspNetCore.Routing; |
| | | 6 | | using Microsoft.Extensions.Options; |
| | | 7 | | using Microsoft.Extensions.Primitives; |
| | | 8 | | |
| | | 9 | | namespace AsiBackbone.AspNetCore.Correlation; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// Resolves safe request correlation data from the current ASP.NET Core HTTP context. |
| | | 13 | | /// </summary> |
| | | 14 | | /// <remarks> |
| | | 15 | | /// Client-supplied correlation identifiers are accepted only when their trimmed value is printable and does not exceed |
| | | 16 | | /// <see cref="AsiBackboneIdentifierLimits.MaximumLength" /> characters. Invalid header values are ignored so the resolv |
| | | 17 | | /// can continue to another configured value or use the existing host-generated fallback. |
| | | 18 | | /// </remarks> |
| | | 19 | | public sealed class HttpContextAsiBackboneRequestCorrelationResolver : IAsiBackboneHttpRequestCorrelationResolver |
| | | 20 | | { |
| | | 21 | | private readonly IHttpContextAccessor httpContextAccessor; |
| | | 22 | | private readonly AsiBackboneAspNetCoreOptions options; |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Initializes a new instance of the <see cref="HttpContextAsiBackboneRequestCorrelationResolver" /> class. |
| | | 26 | | /// </summary> |
| | | 27 | | /// <param name="httpContextAccessor">The ASP.NET Core HTTP context accessor.</param> |
| | | 28 | | /// <param name="options">The request correlation options.</param> |
| | 48 | 29 | | public HttpContextAsiBackboneRequestCorrelationResolver( |
| | 48 | 30 | | IHttpContextAccessor httpContextAccessor, |
| | 48 | 31 | | IOptions<AsiBackboneAspNetCoreOptions> options) |
| | | 32 | | { |
| | 48 | 33 | | this.httpContextAccessor = httpContextAccessor ?? throw new ArgumentNullException(nameof(httpContextAccessor)); |
| | 48 | 34 | | this.options = options?.Value ?? throw new ArgumentNullException(nameof(options)); |
| | 48 | 35 | | this.options.Validate(); |
| | 48 | 36 | | } |
| | | 37 | | |
| | | 38 | | /// <inheritdoc /> |
| | | 39 | | public AsiBackboneHttpRequestCorrelation ResolveRequestCorrelation() |
| | | 40 | | { |
| | 43 | 41 | | HttpContext? httpContext = httpContextAccessor.HttpContext; |
| | | 42 | | |
| | 43 | 43 | | return httpContext is null |
| | 43 | 44 | | ? new AsiBackboneHttpRequestCorrelation(traceId: Activity.Current?.Id) |
| | 43 | 45 | | : new AsiBackboneHttpRequestCorrelation( |
| | 43 | 46 | | ResolveCorrelationId(httpContext), |
| | 43 | 47 | | ResolveTraceId(httpContext), |
| | 43 | 48 | | ResolveMetadata(httpContext)); |
| | | 49 | | } |
| | | 50 | | |
| | | 51 | | private string? ResolveCorrelationId(HttpContext httpContext) |
| | | 52 | | { |
| | 315 | 53 | | foreach (string headerName in options.CorrelationIdHeaderNames) |
| | | 54 | | { |
| | 118 | 55 | | if (string.IsNullOrWhiteSpace(headerName)) |
| | | 56 | | { |
| | | 57 | | continue; |
| | | 58 | | } |
| | | 59 | | |
| | 117 | 60 | | if (!httpContext.Request.Headers.TryGetValue(headerName, out StringValues values)) |
| | | 61 | | { |
| | | 62 | | continue; |
| | | 63 | | } |
| | | 64 | | |
| | 53 | 65 | | foreach (string? candidate in values) |
| | | 66 | | { |
| | 15 | 67 | | string? normalizedCandidate = NormalizeClientCorrelationId(candidate); |
| | 15 | 68 | | if (normalizedCandidate is not null) |
| | | 69 | | { |
| | 5 | 70 | | return normalizedCandidate; |
| | | 71 | | } |
| | | 72 | | } |
| | | 73 | | } |
| | | 74 | | |
| | 37 | 75 | | return options.UseHttpContextTraceIdentifierAsCorrelationId |
| | 37 | 76 | | ? httpContext.TraceIdentifier |
| | 37 | 77 | | : null; |
| | 5 | 78 | | } |
| | | 79 | | |
| | | 80 | | private static string? NormalizeClientCorrelationId(string? value) |
| | | 81 | | { |
| | 15 | 82 | | if (string.IsNullOrWhiteSpace(value)) |
| | | 83 | | { |
| | 2 | 84 | | return null; |
| | | 85 | | } |
| | | 86 | | |
| | 13 | 87 | | string normalizedValue = value.Trim(); |
| | 13 | 88 | | if (normalizedValue.Length > AsiBackboneIdentifierLimits.MaximumLength) |
| | | 89 | | { |
| | 1 | 90 | | return null; |
| | | 91 | | } |
| | | 92 | | |
| | 567 | 93 | | foreach (char character in normalizedValue) |
| | | 94 | | { |
| | 275 | 95 | | if (char.IsControl(character)) |
| | | 96 | | { |
| | 7 | 97 | | return null; |
| | | 98 | | } |
| | | 99 | | } |
| | | 100 | | |
| | 5 | 101 | | return normalizedValue; |
| | | 102 | | } |
| | | 103 | | |
| | | 104 | | private static string? ResolveTraceId(HttpContext httpContext) |
| | | 105 | | { |
| | 42 | 106 | | return Activity.Current?.Id ?? httpContext.TraceIdentifier; |
| | | 107 | | } |
| | | 108 | | |
| | | 109 | | private Dictionary<string, string> ResolveMetadata(HttpContext httpContext) |
| | | 110 | | { |
| | 42 | 111 | | Dictionary<string, string> metadata = new(StringComparer.Ordinal); |
| | | 112 | | |
| | 42 | 113 | | if (options.IncludeRequestMethod && !string.IsNullOrWhiteSpace(httpContext.Request.Method)) |
| | | 114 | | { |
| | 2 | 115 | | metadata[AsiBackboneHttpRequestMetadataKeys.Method] = httpContext.Request.Method.Trim(); |
| | | 116 | | } |
| | | 117 | | |
| | 42 | 118 | | if (options.IncludeRequestPath && httpContext.Request.Path.HasValue) |
| | | 119 | | { |
| | 1 | 120 | | metadata[AsiBackboneHttpRequestMetadataKeys.Path] = httpContext.Request.Path.Value; |
| | | 121 | | } |
| | | 122 | | |
| | 42 | 123 | | Endpoint? endpoint = httpContext.GetEndpoint(); |
| | 42 | 124 | | var routeEndpoint = endpoint as RouteEndpoint; |
| | | 125 | | |
| | 42 | 126 | | if (options.IncludeEndpointDisplayName && !string.IsNullOrWhiteSpace(endpoint?.DisplayName)) |
| | | 127 | | { |
| | 4 | 128 | | metadata[AsiBackboneHttpRequestMetadataKeys.EndpointDisplayName] = endpoint.DisplayName.Trim(); |
| | | 129 | | } |
| | | 130 | | |
| | 42 | 131 | | if (options.IncludeRoutePattern && !string.IsNullOrWhiteSpace(routeEndpoint?.RoutePattern.RawText)) |
| | | 132 | | { |
| | 2 | 133 | | metadata[AsiBackboneHttpRequestMetadataKeys.RoutePattern] = routeEndpoint.RoutePattern.RawText.Trim(); |
| | | 134 | | } |
| | | 135 | | |
| | 42 | 136 | | if (options.IncludeRouteValues) |
| | | 137 | | { |
| | 90 | 138 | | foreach (KeyValuePair<string, object?> routeValue in httpContext.Request.RouteValues) |
| | | 139 | | { |
| | 4 | 140 | | if (routeValue.Value is null || string.IsNullOrWhiteSpace(routeValue.Key)) |
| | | 141 | | { |
| | | 142 | | continue; |
| | | 143 | | } |
| | | 144 | | |
| | 2 | 145 | | metadata[$"{AsiBackboneHttpRequestMetadataKeys.RouteValuePrefix}{routeValue.Key.Trim()}"] = |
| | 2 | 146 | | routeValue.Value.ToString()?.Trim() ?? string.Empty; |
| | | 147 | | } |
| | | 148 | | } |
| | | 149 | | |
| | 42 | 150 | | return metadata; |
| | | 151 | | } |
| | | 152 | | } |