Skip to content

Commit 947f98c

Browse files
committed
fix: Read size on each request
1 parent e22d5aa commit 947f98c

File tree

4 files changed

+11
-9
lines changed

4 files changed

+11
-9
lines changed

src/Sentry.AspNetCore/ScopeExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ private static void SetBody(BaseScope scope, HttpContext context, SentryAspNetCo
104104
{
105105
return;
106106
}
107-
var dispatcher = new RequestBodyExtractionDispatcher(extractors, options, options.MaxRequestBodySize);
107+
var dispatcher = new RequestBodyExtractionDispatcher(extractors, options, () => options.MaxRequestBodySize);
108108

109109
var body = dispatcher.ExtractPayload(new HttpRequestAdapter(context.Request));
110110
if (body != null)

src/Sentry/Extensibility/RequestBodyExtractionDispatcher.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ namespace Sentry.Extensibility
66
public class RequestBodyExtractionDispatcher : IRequestPayloadExtractor
77
{
88
private readonly SentryOptions _options;
9-
private readonly RequestSize _size;
9+
private readonly Func<RequestSize> _sizeSwitch;
1010

1111
internal IEnumerable<IRequestPayloadExtractor> Extractors { get; }
1212

13-
public RequestBodyExtractionDispatcher(IEnumerable<IRequestPayloadExtractor> extractors, SentryOptions options, RequestSize size)
13+
public RequestBodyExtractionDispatcher(IEnumerable<IRequestPayloadExtractor> extractors, SentryOptions options, Func<RequestSize> sizeSwitch)
1414
{
1515
Extractors = extractors ?? throw new ArgumentNullException(nameof(extractors));
1616
_options = options ?? throw new ArgumentNullException(nameof(options));
17-
_size = size;
17+
_sizeSwitch = sizeSwitch ?? throw new ArgumentNullException(nameof(sizeSwitch));
1818
}
1919

2020
public object ExtractPayload(IHttpRequest request)
@@ -24,13 +24,15 @@ public object ExtractPayload(IHttpRequest request)
2424
return null;
2525
}
2626

27-
switch (_size)
27+
var size = _sizeSwitch();
28+
29+
switch (size)
2830
{
2931
case RequestSize.Small when request.ContentLength < 1_000:
3032
case RequestSize.Medium when request.ContentLength < 10_000:
3133
case RequestSize.Always:
3234
_options.DiagnosticLogger.LogDebug("Attempting to read request body of size: {0}, configured max: {1}.",
33-
request.ContentLength, _size);
35+
request.ContentLength, size);
3436

3537
foreach (var extractor in Extractors)
3638
{
@@ -52,7 +54,7 @@ public object ExtractPayload(IHttpRequest request)
5254
break;
5355
default:
5456
_options.DiagnosticLogger.LogWarning("Ignoring request with Size {0} and configuration RequestSize {1}",
55-
request.ContentLength, _size);
57+
request.ContentLength, size);
5658
break;
5759
}
5860

src/Sentry/SentryOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ public SentryOptions()
349349
new DefaultRequestPayloadExtractor()
350350
},
351351
this,
352-
MaxRequestBodySize)));
352+
() => MaxRequestBodySize)));
353353
#endif
354354
ExceptionProcessors
355355
= ImmutableList.Create<ISentryEventExceptionProcessor>(

test/Sentry.Tests/Extensibility/RequestBodyExtractionDispatcherTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public Fixture()
2424
Extractors = new[] { Extractor };
2525
}
2626

27-
public RequestBodyExtractionDispatcher GetSut() => new RequestBodyExtractionDispatcher(Extractors, SentryOptions, RequestSize);
27+
public RequestBodyExtractionDispatcher GetSut() => new RequestBodyExtractionDispatcher(Extractors, SentryOptions, () => RequestSize);
2828
}
2929

3030
private readonly Fixture _fixture = new Fixture();

0 commit comments

Comments
 (0)