Skip to content

Commit 6185c01

Browse files
author
feiyun0112
committed
413 status when size limit
1 parent b911290 commit 6185c01

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

src/Middleware/RequestDecompression/src/RequestDecompressionMiddleware.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ private async Task InvokeCore(HttpContext context, Stream decompressionStream)
6262
context.GetEndpoint()?.Metadata?.GetMetadata<IRequestSizeLimitMetadata>()?.MaxRequestBodySize
6363
?? context.Features.Get<IHttpMaxRequestBodySizeFeature>()?.MaxRequestBodySize;
6464

65-
context.Request.Body = new SizeLimitedStream(decompressionStream, sizeLimit);
65+
context.Request.Body = new SizeLimitedStream(decompressionStream, sizeLimit, (long totalBytesRead, long sizeLimit) => throw new BadHttpRequestException(
66+
$"The request's Content-Length {totalBytesRead} is larger than the request body size limit {sizeLimit}.",
67+
StatusCodes.Status413PayloadTooLarge));
6668
await _next(context);
6769
}
6870
finally

src/Middleware/RequestDecompression/test/RequestDecompressionMiddlewareTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -499,8 +499,8 @@ public async Task Endpoint_HasRequestSizeLimit_UsedForRequest(bool exceedsLimit)
499499
if (exceedsLimit)
500500
{
501501
Assert.NotNull(exception);
502-
Assert.IsAssignableFrom<InvalidOperationException>(exception);
503-
Assert.Equal("The maximum number of bytes have been read.", exception.Message);
502+
Assert.IsAssignableFrom<BadHttpRequestException>(exception);
503+
Assert.Equal(StatusCodes.Status413PayloadTooLarge, ((BadHttpRequestException)exception).StatusCode);
504504
}
505505
else
506506
{
@@ -583,8 +583,8 @@ public async Task Feature_HasRequestSizeLimit_UsedForRequest(bool exceedsLimit)
583583
if (exceedsLimit)
584584
{
585585
Assert.NotNull(exception);
586-
Assert.IsAssignableFrom<InvalidOperationException>(exception);
587-
Assert.Equal("The maximum number of bytes have been read.", exception.Message);
586+
Assert.IsAssignableFrom<BadHttpRequestException>(exception);
587+
Assert.Equal(StatusCodes.Status413PayloadTooLarge, ((BadHttpRequestException)exception).StatusCode);
588588
}
589589
else
590590
{

src/Shared/SizeLimitedStream.cs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@ internal sealed class SizeLimitedStream : Stream
55
{
66
private readonly Stream _innerStream;
77
private readonly long? _sizeLimit;
8-
8+
private readonly Action<long, long>? _handleSizeLimit;
99
private long _totalBytesRead;
1010

11-
public SizeLimitedStream(Stream innerStream, long? sizeLimit)
11+
public SizeLimitedStream(Stream innerStream, long? sizeLimit, Action<long, long>? handleSizeLimit = null)
1212
{
1313
ArgumentNullException.ThrowIfNull(innerStream);
1414

1515
_innerStream = innerStream;
1616
_sizeLimit = sizeLimit;
17+
_handleSizeLimit = handleSizeLimit;
1718
}
1819

1920
public override bool CanRead => _innerStream.CanRead;
@@ -48,7 +49,14 @@ public override int Read(byte[] buffer, int offset, int count)
4849
_totalBytesRead += bytesRead;
4950
if (_totalBytesRead > _sizeLimit)
5051
{
51-
throw new InvalidOperationException("The maximum number of bytes have been read.");
52+
if (_handleSizeLimit != null)
53+
{
54+
_handleSizeLimit(_totalBytesRead, _sizeLimit.Value);
55+
}
56+
else
57+
{
58+
throw new InvalidOperationException("The maximum number of bytes have been read.");
59+
}
5260
}
5361

5462
return bytesRead;
@@ -81,7 +89,14 @@ public override async ValueTask<int> ReadAsync(Memory<byte> buffer, Cancellation
8189
_totalBytesRead += bytesRead;
8290
if (_totalBytesRead > _sizeLimit)
8391
{
84-
throw new InvalidOperationException("The maximum number of bytes have been read.");
92+
if (_handleSizeLimit != null)
93+
{
94+
_handleSizeLimit(_totalBytesRead, _sizeLimit.Value);
95+
}
96+
else
97+
{
98+
throw new InvalidOperationException("The maximum number of bytes have been read.");
99+
}
85100
}
86101

87102
return bytesRead;

0 commit comments

Comments
 (0)