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
64 changes: 30 additions & 34 deletions DebugProbe.AspNetCore/Handlers/DebugProbeHttpClientHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,7 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
/// </summary>
private async Task CaptureRequest(HttpRequestMessage request, HttpResponseMessage? response, Exception? exception, long durationMs)
{
var context = _httpContextAccessor.HttpContext;

if (context == null)
{
return;
}

if (!context.Items.TryGetValue("DebugProbeEntry", out var value))
{
return;
}

if (value is not DebugEntry entry)
if (!TryGetActiveEntry(out var entry))
{
return;
}
Expand All @@ -88,34 +76,42 @@ private async Task CaptureRequest(HttpRequestMessage request, HttpResponseMessag
ResponseHeaders = response != null ? response.Headers.ToDictionary(x => x.Key, x => RedactionUtils.RedactHeader(x.Key, string.Join(", ", x.Value), _options)) : []
};

if (request.Content != null)
{
var contentType = request.Content.Headers.ContentType?.MediaType;
outgoing.RequestBody = await CaptureBodyAsync(request.Content);

if (HttpContentUtils.IsTextContent(contentType))
{
var body = await request.Content.ReadAsStringAsync();
outgoing.ResponseBody = await CaptureBodyAsync(response?.Content);

outgoing.RequestBody = JsonUtils.Format(RedactionUtils.RedactJsonFields(
HttpContentUtils.Trim(body, _options.MaxBodyCaptureSizeBytes),
_options));
}
}
entry.OutgoingRequests.Add(outgoing);
}

private bool TryGetActiveEntry(out DebugEntry entry)
{
entry = null!;

var context = _httpContextAccessor.HttpContext;

if (response?.Content != null)
if (context == null ||
!context.Items.TryGetValue("DebugProbeEntry", out var value) ||
value is not DebugEntry activeEntry)
{
var contentType = response.Content.Headers.ContentType?.MediaType;
return false;
}

if (HttpContentUtils.IsTextContent(contentType))
{
var body = await response.Content.ReadAsStringAsync();
entry = activeEntry;
return true;
}

outgoing.ResponseBody = JsonUtils.Format(RedactionUtils.RedactJsonFields(
HttpContentUtils.Trim(body, _options.MaxBodyCaptureSizeBytes),
_options));
}
private async Task<string> CaptureBodyAsync(HttpContent? content)
{
if (content == null ||
!HttpContentUtils.IsTextContent(content.Headers.ContentType?.MediaType))
{
return string.Empty;
}

entry.OutgoingRequests.Add(outgoing);
var body = await content.ReadAsStringAsync();

return JsonUtils.Format(RedactionUtils.RedactJsonFields(
HttpContentUtils.Trim(body, _options.MaxBodyCaptureSizeBytes),
_options));
}
}
2 changes: 1 addition & 1 deletion DebugProbe.AspNetCore/Internal/Utils/RedactionUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ private static string RedactQuery(string query, DebugProbeOptions options)
continue;
}

parts[i] = equalsIndex >= 0? $"{name}={options.RedactionText}" : $"{name}={options.RedactionText}";
parts[i] = $"{name}={options.RedactionText}";
}

return string.Join("&", parts);
Expand Down
25 changes: 14 additions & 11 deletions DebugProbe.AspNetCore/Middleware/DebugProbeMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,7 @@ public DebugProbeMiddleware(RequestDelegate next, DebugProbeOptions options)
/// </summary>
public async Task Invoke(HttpContext context, DebugEntryStore store)
{
var path = context.Request.Path.Value ?? string.Empty;

var ignorePaths = DefaultIgnorePaths
.Concat(_options.IgnorePaths)
.Distinct(StringComparer.OrdinalIgnoreCase);

var ignored = ignorePaths.Any(x =>
path.StartsWith(x, StringComparison.OrdinalIgnoreCase));

if (ignored)
if (IsIgnoredPath(context.Request.Path))
{
await _next(context);
return;
Expand Down Expand Up @@ -116,7 +107,9 @@ public async Task Invoke(HttpContext context, DebugEntryStore store)

var statusCode = exception && context.Response.StatusCode == 200 ? 500 : context.Response.StatusCode;

var responseBody = exception ? HttpContentUtils.Trim(exceptionResponseBody, maxBodySize) : CaptureResponseBody(context, responseCapture, maxBodySize);
var responseBody = exception
? HttpContentUtils.Trim(exceptionResponseBody, maxBodySize)
: CaptureResponseBody(context, responseCapture, maxBodySize);

entry.Method = context.Request.Method;

Expand Down Expand Up @@ -162,6 +155,16 @@ public async Task Invoke(HttpContext context, DebugEntryStore store)
}
}

private bool IsIgnoredPath(PathString requestPath)
{
var path = requestPath.Value ?? string.Empty;

return DefaultIgnorePaths
.Concat(_options.IgnorePaths)
.Distinct(StringComparer.OrdinalIgnoreCase)
.Any(ignorePath => path.StartsWith(ignorePath, StringComparison.OrdinalIgnoreCase));
}

private static async Task<string> CaptureRequestBodyAsync(HttpContext context, int maxBodySize)
{
if (!HasBody(context.Request))
Expand Down
7 changes: 5 additions & 2 deletions DebugProbe.AspNetCore/Storage/DebugEntryStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,11 @@ public DebugEntryStore(DebugProbeOptions options)
public void Add(DebugEntry entry)
{
_queue.Enqueue(entry);

while (_queue.Count > _limit)
{
_queue.TryDequeue(out _);
}
}

public List<DebugEntry> GetAll()
Expand All @@ -59,12 +62,12 @@ public void Clear()
while (_queue.TryDequeue(out _)) { }
}

private string GetDateFormat()
private static string GetDateFormat()
{
var shortDatePattern = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern;
var index = shortDatePattern.LastIndexOf('y');
var dataFormat = index >= 0 ? shortDatePattern[..(index + 1)] : shortDatePattern;

return dataFormat;
}
}
}
Loading