Browse Source

Introduce `AbpRequestSizeLimitMiddleware `

Resolve #12348
pull/12358/head
maliming 5 years ago
parent
commit
f708553321
No known key found for this signature in database GPG Key ID: 96224957E51C89E
  1. 8
      framework/src/Volo.Abp.AspNetCore/Microsoft/AspNetCore/Builder/AbpApplicationBuilderExtensions.cs
  2. 63
      framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/RequestSizeLimit/AbpRequestSizeLimitMiddleware.cs
  3. 19
      framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/RequestSizeLimit/RequestSizeLimit.cs

8
framework/src/Volo.Abp.AspNetCore/Microsoft/AspNetCore/Builder/AbpApplicationBuilderExtensions.cs

@ -7,6 +7,7 @@ using Microsoft.Extensions.Hosting;
using Volo.Abp; using Volo.Abp;
using Volo.Abp.AspNetCore.Auditing; using Volo.Abp.AspNetCore.Auditing;
using Volo.Abp.AspNetCore.ExceptionHandling; using Volo.Abp.AspNetCore.ExceptionHandling;
using Volo.Abp.AspNetCore.RequestSizeLimit;
using Volo.Abp.AspNetCore.Security; using Volo.Abp.AspNetCore.Security;
using Volo.Abp.AspNetCore.Security.Claims; using Volo.Abp.AspNetCore.Security.Claims;
using Volo.Abp.AspNetCore.Tracing; using Volo.Abp.AspNetCore.Tracing;
@ -111,4 +112,11 @@ public static class AbpApplicationBuilderExtensions
{ {
return app.UseMiddleware<AbpSecurityHeadersMiddleware>(); return app.UseMiddleware<AbpSecurityHeadersMiddleware>();
} }
public static IApplicationBuilder UseAbpRequestSizeLimit(this IApplicationBuilder app)
{
return app
.UseAbpExceptionHandling()
.UseMiddleware<AbpRequestSizeLimitMiddleware>();
}
} }

63
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<AbpRequestSizeLimitMiddleware> _logger;
public AbpRequestSizeLimitMiddleware(ILogger<AbpRequestSizeLimitMiddleware> 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<AbpRequestSizeLimitAttribute>();
if (attribute != null)
{
var maxRequestBodySizeFeature = context.Features.Get<IHttpMaxRequestBodySizeFeature>();
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);
}
}

19
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;
}
}
Loading…
Cancel
Save