diff --git a/global.json b/global.json index ce67766..ed07ad8 100644 --- a/global.json +++ b/global.json @@ -1,5 +1,6 @@ { "sdk": { - "version": "10.0.201" + "version": "10.0.103", + "rollForward": "latestFeature" } } diff --git a/src/IdempotencyKey.AspNetCore/IdempotencyService.cs b/src/IdempotencyKey.AspNetCore/IdempotencyService.cs index c25a215..b79d009 100644 --- a/src/IdempotencyKey.AspNetCore/IdempotencyService.cs +++ b/src/IdempotencyKey.AspNetCore/IdempotencyService.cs @@ -159,7 +159,7 @@ public Task WriteValidationErrorAsync(HttpContext httpContext, string message) { if (httpContext.Request.Headers.TryGetValue(h, out var val)) { - selectedHeaders[h] = val.Where(x => x != null).Select(x => x!).ToArray(); + selectedHeaders[h] = FilterHeaderValues(val); } } @@ -248,7 +248,7 @@ public async Task ProcessResponseAndCompleteAsync(HttpContext httpContext, Memor { if (!IsUnsafeHeader(h.Key)) { - snapshot.Headers[h.Key] = h.Value.Where(x => x != null).Select(x => x!).ToArray(); + snapshot.Headers[h.Key] = FilterHeaderValues(h.Value); } } @@ -362,6 +362,42 @@ private static bool IsUnsafeHeader(string key) return !SafeReplayHeaders.Contains(key); } + private static string[] FilterHeaderValues(Microsoft.Extensions.Primitives.StringValues values) + { + var count = values.Count; + if (count == 0) + { + return Array.Empty(); + } + + int nonNullCount = 0; + for (int i = 0; i < count; i++) + { + if (values[i] != null) + { + nonNullCount++; + } + } + + if (nonNullCount == 0) + { + return Array.Empty(); + } + + var result = new string[nonNullCount]; + int index = 0; + for (int i = 0; i < count; i++) + { + var val = values[i]; + if (val != null) + { + result[index++] = val; + } + } + + return result; + } + private Task WriteErrorResponseAsync(HttpContext httpContext, IdempotencyErrorContext error) { return _options.ErrorResponseWriter(httpContext, error);