Skip to content

Commit c6d2206

Browse files
refactor: improve code readability and remove duplication
1 parent 15eee60 commit c6d2206

4 files changed

Lines changed: 50 additions & 48 deletions

File tree

DebugProbe.AspNetCore/Handlers/DebugProbeHttpClientHandler.cs

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,7 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
5050
/// </summary>
5151
private async Task CaptureRequest(HttpRequestMessage request, HttpResponseMessage? response, Exception? exception, long durationMs)
5252
{
53-
var context = _httpContextAccessor.HttpContext;
54-
55-
if (context == null)
56-
{
57-
return;
58-
}
59-
60-
if (!context.Items.TryGetValue("DebugProbeEntry", out var value))
61-
{
62-
return;
63-
}
64-
65-
if (value is not DebugEntry entry)
53+
if (!TryGetActiveEntry(out var entry))
6654
{
6755
return;
6856
}
@@ -88,34 +76,42 @@ private async Task CaptureRequest(HttpRequestMessage request, HttpResponseMessag
8876
ResponseHeaders = response != null ? response.Headers.ToDictionary(x => x.Key, x => RedactionUtils.RedactHeader(x.Key, string.Join(", ", x.Value), _options)) : []
8977
};
9078

91-
if (request.Content != null)
92-
{
93-
var contentType = request.Content.Headers.ContentType?.MediaType;
79+
outgoing.RequestBody = await CaptureBodyAsync(request.Content);
9480

95-
if (HttpContentUtils.IsTextContent(contentType))
96-
{
97-
var body = await request.Content.ReadAsStringAsync();
81+
outgoing.ResponseBody = await CaptureBodyAsync(response?.Content);
9882

99-
outgoing.RequestBody = JsonUtils.Format(RedactionUtils.RedactJsonFields(
100-
HttpContentUtils.Trim(body, _options.MaxBodyCaptureSizeBytes),
101-
_options));
102-
}
103-
}
83+
entry.OutgoingRequests.Add(outgoing);
84+
}
85+
86+
private bool TryGetActiveEntry(out DebugEntry entry)
87+
{
88+
entry = null!;
89+
90+
var context = _httpContextAccessor.HttpContext;
10491

105-
if (response?.Content != null)
92+
if (context == null ||
93+
!context.Items.TryGetValue("DebugProbeEntry", out var value) ||
94+
value is not DebugEntry activeEntry)
10695
{
107-
var contentType = response.Content.Headers.ContentType?.MediaType;
96+
return false;
97+
}
10898

109-
if (HttpContentUtils.IsTextContent(contentType))
110-
{
111-
var body = await response.Content.ReadAsStringAsync();
99+
entry = activeEntry;
100+
return true;
101+
}
112102

113-
outgoing.ResponseBody = JsonUtils.Format(RedactionUtils.RedactJsonFields(
114-
HttpContentUtils.Trim(body, _options.MaxBodyCaptureSizeBytes),
115-
_options));
116-
}
103+
private async Task<string> CaptureBodyAsync(HttpContent? content)
104+
{
105+
if (content == null ||
106+
!HttpContentUtils.IsTextContent(content.Headers.ContentType?.MediaType))
107+
{
108+
return string.Empty;
117109
}
118110

119-
entry.OutgoingRequests.Add(outgoing);
111+
var body = await content.ReadAsStringAsync();
112+
113+
return JsonUtils.Format(RedactionUtils.RedactJsonFields(
114+
HttpContentUtils.Trim(body, _options.MaxBodyCaptureSizeBytes),
115+
_options));
120116
}
121117
}

DebugProbe.AspNetCore/Internal/Utils/RedactionUtils.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ private static string RedactQuery(string query, DebugProbeOptions options)
9898
continue;
9999
}
100100

101-
parts[i] = equalsIndex >= 0? $"{name}={options.RedactionText}" : $"{name}={options.RedactionText}";
101+
parts[i] = $"{name}={options.RedactionText}";
102102
}
103103

104104
return string.Join("&", parts);

DebugProbe.AspNetCore/Middleware/DebugProbeMiddleware.cs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,7 @@ public DebugProbeMiddleware(RequestDelegate next, DebugProbeOptions options)
5757
/// </summary>
5858
public async Task Invoke(HttpContext context, DebugEntryStore store)
5959
{
60-
var path = context.Request.Path.Value ?? string.Empty;
61-
62-
var ignorePaths = DefaultIgnorePaths
63-
.Concat(_options.IgnorePaths)
64-
.Distinct(StringComparer.OrdinalIgnoreCase);
65-
66-
var ignored = ignorePaths.Any(x =>
67-
path.StartsWith(x, StringComparison.OrdinalIgnoreCase));
68-
69-
if (ignored)
60+
if (IsIgnoredPath(context.Request.Path))
7061
{
7162
await _next(context);
7263
return;
@@ -116,7 +107,9 @@ public async Task Invoke(HttpContext context, DebugEntryStore store)
116107

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

119-
var responseBody = exception ? HttpContentUtils.Trim(exceptionResponseBody, maxBodySize) : CaptureResponseBody(context, responseCapture, maxBodySize);
110+
var responseBody = exception
111+
? HttpContentUtils.Trim(exceptionResponseBody, maxBodySize)
112+
: CaptureResponseBody(context, responseCapture, maxBodySize);
120113

121114
entry.Method = context.Request.Method;
122115

@@ -162,6 +155,16 @@ public async Task Invoke(HttpContext context, DebugEntryStore store)
162155
}
163156
}
164157

158+
private bool IsIgnoredPath(PathString requestPath)
159+
{
160+
var path = requestPath.Value ?? string.Empty;
161+
162+
return DefaultIgnorePaths
163+
.Concat(_options.IgnorePaths)
164+
.Distinct(StringComparer.OrdinalIgnoreCase)
165+
.Any(ignorePath => path.StartsWith(ignorePath, StringComparison.OrdinalIgnoreCase));
166+
}
167+
165168
private static async Task<string> CaptureRequestBodyAsync(HttpContext context, int maxBodySize)
166169
{
167170
if (!HasBody(context.Request))

DebugProbe.AspNetCore/Storage/DebugEntryStore.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,11 @@ public DebugEntryStore(DebugProbeOptions options)
4040
public void Add(DebugEntry entry)
4141
{
4242
_queue.Enqueue(entry);
43+
4344
while (_queue.Count > _limit)
45+
{
4446
_queue.TryDequeue(out _);
47+
}
4548
}
4649

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

62-
private string GetDateFormat()
65+
private static string GetDateFormat()
6366
{
6467
var shortDatePattern = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern;
6568
var index = shortDatePattern.LastIndexOf('y');
6669
var dataFormat = index >= 0 ? shortDatePattern[..(index + 1)] : shortDatePattern;
6770

6871
return dataFormat;
6972
}
70-
}
73+
}

0 commit comments

Comments
 (0)