Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ private async Task InvokeCore(HttpContext context, Stream decompressionStream)
context.GetEndpoint()?.Metadata?.GetMetadata<IRequestSizeLimitMetadata>()?.MaxRequestBodySize
?? context.Features.Get<IHttpMaxRequestBodySizeFeature>()?.MaxRequestBodySize;

context.Request.Body = new SizeLimitedStream(decompressionStream, sizeLimit);
context.Request.Body = new SizeLimitedStream(decompressionStream, sizeLimit, (long totalBytesRead, long sizeLimit) => throw new BadHttpRequestException(
$"The decompressed request body is larger than the request body size limit {sizeLimit}.",
StatusCodes.Status413PayloadTooLarge));
await _next(context);
}
finally
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -499,8 +499,8 @@ public async Task Endpoint_HasRequestSizeLimit_UsedForRequest(bool exceedsLimit)
if (exceedsLimit)
{
Assert.NotNull(exception);
Assert.IsAssignableFrom<InvalidOperationException>(exception);
Assert.Equal("The maximum number of bytes have been read.", exception.Message);
Assert.IsAssignableFrom<BadHttpRequestException>(exception);
Assert.Equal(StatusCodes.Status413PayloadTooLarge, ((BadHttpRequestException)exception).StatusCode);
}
else
{
Expand Down Expand Up @@ -583,8 +583,8 @@ public async Task Feature_HasRequestSizeLimit_UsedForRequest(bool exceedsLimit)
if (exceedsLimit)
{
Assert.NotNull(exception);
Assert.IsAssignableFrom<InvalidOperationException>(exception);
Assert.Equal("The maximum number of bytes have been read.", exception.Message);
Assert.IsAssignableFrom<BadHttpRequestException>(exception);
Assert.Equal(StatusCodes.Status413PayloadTooLarge, ((BadHttpRequestException)exception).StatusCode);
}
else
{
Expand Down
23 changes: 19 additions & 4 deletions src/Shared/SizeLimitedStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@
{
private readonly Stream _innerStream;
private readonly long? _sizeLimit;

private readonly Action<long, long>? _handleSizeLimit;

Check failure on line 8 in src/Shared/SizeLimitedStream.cs

View check run for this annotation

Azure Pipelines / aspnetcore-quarantined-pr (Tests: Ubuntu x64)

src/Shared/SizeLimitedStream.cs#L8

src/Shared/SizeLimitedStream.cs(8,40): error CS8632: (NETCORE_ENGINEERING_TELEMETRY=Build) The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check failure on line 8 in src/Shared/SizeLimitedStream.cs

View check run for this annotation

Azure Pipelines / aspnetcore-quarantined-pr (Tests: Ubuntu x64)

src/Shared/SizeLimitedStream.cs#L8

src/Shared/SizeLimitedStream.cs(8,40): error CS8632: (NETCORE_ENGINEERING_TELEMETRY=Build) The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check failure on line 8 in src/Shared/SizeLimitedStream.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Test: Ubuntu x64)

src/Shared/SizeLimitedStream.cs#L8

src/Shared/SizeLimitedStream.cs(8,40): error CS8632: (NETCORE_ENGINEERING_TELEMETRY=Build) The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check failure on line 8 in src/Shared/SizeLimitedStream.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Test: Ubuntu x64)

src/Shared/SizeLimitedStream.cs#L8

src/Shared/SizeLimitedStream.cs(8,40): error CS8632: (NETCORE_ENGINEERING_TELEMETRY=Build) The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check failure on line 8 in src/Shared/SizeLimitedStream.cs

View check run for this annotation

Azure Pipelines / aspnetcore-quarantined-pr (Tests: macOS)

src/Shared/SizeLimitedStream.cs#L8

src/Shared/SizeLimitedStream.cs(8,40): error CS8632: (NETCORE_ENGINEERING_TELEMETRY=Build) The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check failure on line 8 in src/Shared/SizeLimitedStream.cs

View check run for this annotation

Azure Pipelines / aspnetcore-quarantined-pr (Tests: macOS)

src/Shared/SizeLimitedStream.cs#L8

src/Shared/SizeLimitedStream.cs(8,40): error CS8632: (NETCORE_ENGINEERING_TELEMETRY=Build) The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check failure on line 8 in src/Shared/SizeLimitedStream.cs

View check run for this annotation

Azure Pipelines / aspnetcore-quarantined-pr

src/Shared/SizeLimitedStream.cs#L8

src/Shared/SizeLimitedStream.cs(8,40): error CS8632: (NETCORE_ENGINEERING_TELEMETRY=Build) The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check failure on line 8 in src/Shared/SizeLimitedStream.cs

View check run for this annotation

Azure Pipelines / aspnetcore-quarantined-pr

src/Shared/SizeLimitedStream.cs#L8

src/Shared/SizeLimitedStream.cs(8,40): error CS8632: (NETCORE_ENGINEERING_TELEMETRY=Build) The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
private long _totalBytesRead;

public SizeLimitedStream(Stream innerStream, long? sizeLimit)
public SizeLimitedStream(Stream innerStream, long? sizeLimit, Action<long, long>? handleSizeLimit = null)

Check failure on line 11 in src/Shared/SizeLimitedStream.cs

View check run for this annotation

Azure Pipelines / aspnetcore-quarantined-pr (Tests: Ubuntu x64)

src/Shared/SizeLimitedStream.cs#L11

src/Shared/SizeLimitedStream.cs(11,85): error CS8632: (NETCORE_ENGINEERING_TELEMETRY=Build) The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check failure on line 11 in src/Shared/SizeLimitedStream.cs

View check run for this annotation

Azure Pipelines / aspnetcore-quarantined-pr (Tests: Ubuntu x64)

src/Shared/SizeLimitedStream.cs#L11

src/Shared/SizeLimitedStream.cs(11,85): error CS8632: (NETCORE_ENGINEERING_TELEMETRY=Build) The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check failure on line 11 in src/Shared/SizeLimitedStream.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Test: Ubuntu x64)

src/Shared/SizeLimitedStream.cs#L11

src/Shared/SizeLimitedStream.cs(11,85): error CS8632: (NETCORE_ENGINEERING_TELEMETRY=Build) The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check failure on line 11 in src/Shared/SizeLimitedStream.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Test: Ubuntu x64)

src/Shared/SizeLimitedStream.cs#L11

src/Shared/SizeLimitedStream.cs(11,85): error CS8632: (NETCORE_ENGINEERING_TELEMETRY=Build) The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check failure on line 11 in src/Shared/SizeLimitedStream.cs

View check run for this annotation

Azure Pipelines / aspnetcore-quarantined-pr (Tests: macOS)

src/Shared/SizeLimitedStream.cs#L11

src/Shared/SizeLimitedStream.cs(11,85): error CS8632: (NETCORE_ENGINEERING_TELEMETRY=Build) The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check failure on line 11 in src/Shared/SizeLimitedStream.cs

View check run for this annotation

Azure Pipelines / aspnetcore-quarantined-pr (Tests: macOS)

src/Shared/SizeLimitedStream.cs#L11

src/Shared/SizeLimitedStream.cs(11,85): error CS8632: (NETCORE_ENGINEERING_TELEMETRY=Build) The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check failure on line 11 in src/Shared/SizeLimitedStream.cs

View check run for this annotation

Azure Pipelines / aspnetcore-quarantined-pr

src/Shared/SizeLimitedStream.cs#L11

src/Shared/SizeLimitedStream.cs(11,85): error CS8632: (NETCORE_ENGINEERING_TELEMETRY=Build) The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check failure on line 11 in src/Shared/SizeLimitedStream.cs

View check run for this annotation

Azure Pipelines / aspnetcore-quarantined-pr

src/Shared/SizeLimitedStream.cs#L11

src/Shared/SizeLimitedStream.cs(11,85): error CS8632: (NETCORE_ENGINEERING_TELEMETRY=Build) The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
{
ArgumentNullException.ThrowIfNull(innerStream);

_innerStream = innerStream;
_sizeLimit = sizeLimit;
_handleSizeLimit = handleSizeLimit;
}

public override bool CanRead => _innerStream.CanRead;
Expand Down Expand Up @@ -48,7 +49,14 @@
_totalBytesRead += bytesRead;
if (_totalBytesRead > _sizeLimit)
{
throw new InvalidOperationException("The maximum number of bytes have been read.");
if (_handleSizeLimit != null)
{
_handleSizeLimit(_totalBytesRead, _sizeLimit.Value);
}
else
{
throw new InvalidOperationException("The maximum number of bytes have been read.");
}
}

return bytesRead;
Expand Down Expand Up @@ -81,7 +89,14 @@
_totalBytesRead += bytesRead;
if (_totalBytesRead > _sizeLimit)
{
throw new InvalidOperationException("The maximum number of bytes have been read.");
if (_handleSizeLimit != null)
{
_handleSizeLimit(_totalBytesRead, _sizeLimit.Value);
}
else
{
throw new InvalidOperationException("The maximum number of bytes have been read.");
}
}

return bytesRead;
Expand Down
Loading