Skip to content
Open
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
3 changes: 2 additions & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"sdk": {
"version": "10.0.201"
"version": "10.0.103",
"rollForward": "latestFeature"
}
}
40 changes: 38 additions & 2 deletions src/IdempotencyKey.AspNetCore/IdempotencyService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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<string>();
}

int nonNullCount = 0;
for (int i = 0; i < count; i++)
{
if (values[i] != null)
{
nonNullCount++;
}
}

if (nonNullCount == 0)
{
return Array.Empty<string>();
}

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);
Expand Down
Loading