-
-
Notifications
You must be signed in to change notification settings - Fork 230
Expand file tree
/
Copy pathRequestBodyExtractionDispatcher.cs
More file actions
83 lines (70 loc) · 2.78 KB
/
RequestBodyExtractionDispatcher.cs
File metadata and controls
83 lines (70 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using Sentry.Internal.Extensions;
namespace Sentry.Extensibility;
/// <summary>
/// Dispatches request body extractions if enabled and within limits.
/// </summary>
public class RequestBodyExtractionDispatcher : IRequestPayloadExtractor
{
private readonly SentryOptions _options;
private readonly Func<RequestSize> _sizeSwitch;
internal IEnumerable<IRequestPayloadExtractor> Extractors { get; }
/// <summary>
/// Creates a new instance of <see cref="RequestBodyExtractionDispatcher"/>.
/// </summary>
/// <param name="extractors">Extractors to use.</param>
/// <param name="options">Sentry Options.</param>
/// <param name="sizeSwitch">The max request size to capture.</param>
public RequestBodyExtractionDispatcher(IEnumerable<IRequestPayloadExtractor> extractors, SentryOptions options, Func<RequestSize> sizeSwitch)
{
ArgumentNullException.ThrowIfNull(extractors);
ArgumentNullException.ThrowIfNull(options);
ArgumentNullException.ThrowIfNull(sizeSwitch);
Extractors = extractors;
_options = options;
_sizeSwitch = sizeSwitch;
}
/// <summary>
/// Extract the payload using the provided extractors.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>A serializable representation of the payload.</returns>
public object? ExtractPayload(IHttpRequest request)
{
// Not to throw on code that ignores nullability warnings.
if (request.IsNull())
{
return null;
}
var size = _sizeSwitch();
switch (size)
{
case RequestSize.Small when request.ContentLength < 4_000:
case RequestSize.Medium when request.ContentLength < 10_000:
case RequestSize.Always:
_options.LogDebug("Attempting to read request body of size: {0}, configured max: {1}.",
request.ContentLength, size);
foreach (var extractor in Extractors)
{
var data = extractor.ExtractPayload(request);
if (data == null
|| data is string dataString
&& string.IsNullOrEmpty(dataString))
{
continue;
}
return data;
}
break;
// Request body extraction is opt-in
case RequestSize.None:
_options.LogDebug("Skipping request body extraction.");
return null;
}
if (request.ContentLength is not null)
{
_options.LogWarning("Ignoring request with Size {0} and configuration RequestSize {1}",
request.ContentLength, size);
}
return null;
}
}