From f70855332147ada20619f9b9a3d2bed85e616733 Mon Sep 17 00:00:00 2001 From: maliming Date: Tue, 26 Apr 2022 10:25:06 +0800 Subject: [PATCH] Introduce `AbpRequestSizeLimitMiddleware ` Resolve #12348 --- .../AbpApplicationBuilderExtensions.cs | 8 +++ .../AbpRequestSizeLimitMiddleware.cs | 63 +++++++++++++++++++ .../RequestSizeLimit/RequestSizeLimit.cs | 19 ++++++ 3 files changed, 90 insertions(+) create mode 100644 framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/RequestSizeLimit/AbpRequestSizeLimitMiddleware.cs create mode 100644 framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/RequestSizeLimit/RequestSizeLimit.cs diff --git a/framework/src/Volo.Abp.AspNetCore/Microsoft/AspNetCore/Builder/AbpApplicationBuilderExtensions.cs b/framework/src/Volo.Abp.AspNetCore/Microsoft/AspNetCore/Builder/AbpApplicationBuilderExtensions.cs index 8ef3ec8d99..0e745e9669 100644 --- a/framework/src/Volo.Abp.AspNetCore/Microsoft/AspNetCore/Builder/AbpApplicationBuilderExtensions.cs +++ b/framework/src/Volo.Abp.AspNetCore/Microsoft/AspNetCore/Builder/AbpApplicationBuilderExtensions.cs @@ -7,6 +7,7 @@ using Microsoft.Extensions.Hosting; using Volo.Abp; using Volo.Abp.AspNetCore.Auditing; using Volo.Abp.AspNetCore.ExceptionHandling; +using Volo.Abp.AspNetCore.RequestSizeLimit; using Volo.Abp.AspNetCore.Security; using Volo.Abp.AspNetCore.Security.Claims; using Volo.Abp.AspNetCore.Tracing; @@ -111,4 +112,11 @@ public static class AbpApplicationBuilderExtensions { return app.UseMiddleware(); } + + public static IApplicationBuilder UseAbpRequestSizeLimit(this IApplicationBuilder app) + { + return app + .UseAbpExceptionHandling() + .UseMiddleware(); + } } diff --git a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/RequestSizeLimit/AbpRequestSizeLimitMiddleware.cs b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/RequestSizeLimit/AbpRequestSizeLimitMiddleware.cs new file mode 100644 index 0000000000..77e99599c4 --- /dev/null +++ b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/RequestSizeLimit/AbpRequestSizeLimitMiddleware.cs @@ -0,0 +1,63 @@ +using System.Globalization; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Http.Features; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging; +using Volo.Abp.DependencyInjection; + +namespace Volo.Abp.AspNetCore.RequestSizeLimit; + +public class AbpRequestSizeLimitMiddleware : IMiddleware, ITransientDependency +{ + private readonly ILogger _logger; + + public AbpRequestSizeLimitMiddleware(ILogger logger) + { + _logger = logger; + } + + public async Task InvokeAsync(HttpContext context, RequestDelegate next) + { + var endpoint = context.GetEndpoint(); + if (endpoint != null) + { + if (!endpoint.Metadata.Any(x => x is RequestSizeLimitAttribute || x is DisableRequestSizeLimitAttribute)) + { + var attribute = endpoint.Metadata.GetMetadata(); + if (attribute != null) + { + var maxRequestBodySizeFeature = context.Features.Get(); + if (maxRequestBodySizeFeature == null) + { + _logger.LogInformation("A request body size limit could not be applied. This server does not support the IHttpRequestBodySizeFeature."); + } + else if (maxRequestBodySizeFeature.IsReadOnly) + { + _logger.LogInformation("A request body size limit could not be applied. The IHttpRequestBodySizeFeature for the server is read-only."); + } + else + { + _logger.LogInformation($"The maximum request body size has been set to {attribute.GetBytes().ToString(CultureInfo.InvariantCulture)}."); + maxRequestBodySizeFeature.MaxRequestBodySize = attribute.GetBytes(); + } + } + else + { + _logger.LogInformation($"AbpRequestSizeLimitAttribute does not exist in endpoint, Skipping."); + } + } + else + { + _logger.LogInformation($"Endpoint already exists IRequestSizePolicy, Skipping."); + } + } + else + { + _logger.LogInformation($"Endpoint is null, Skipping."); + } + + await next(context); + } +} diff --git a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/RequestSizeLimit/RequestSizeLimit.cs b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/RequestSizeLimit/RequestSizeLimit.cs new file mode 100644 index 0000000000..5d6db19220 --- /dev/null +++ b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/RequestSizeLimit/RequestSizeLimit.cs @@ -0,0 +1,19 @@ +using System; + +namespace Volo.Abp.AspNetCore.RequestSizeLimit; + +[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)] +public class AbpRequestSizeLimitAttribute : Attribute +{ + private readonly long _bytes; + + public AbpRequestSizeLimitAttribute(long bytes) + { + _bytes = bytes; + } + + public long GetBytes() + { + return _bytes; + } +}