From b5a99a32ee46a045231a9bd5224b945667bc5033 Mon Sep 17 00:00:00 2001 From: crupest Date: Fri, 21 Aug 2020 22:49:48 +0800 Subject: ... --- Timeline/Filters/Header.cs | 69 ++++++++++++++++++++++++++++------------------ 1 file changed, 42 insertions(+), 27 deletions(-) (limited to 'Timeline/Filters') diff --git a/Timeline/Filters/Header.cs b/Timeline/Filters/Header.cs index 0db11faf..cc5ddd9f 100644 --- a/Timeline/Filters/Header.cs +++ b/Timeline/Filters/Header.cs @@ -4,45 +4,60 @@ using Timeline.Models.Http; namespace Timeline.Filters { - public class RequireContentTypeAttribute : ActionFilterAttribute + /// + /// Restrict max content length. + /// + public class MaxContentLengthFilter : IResourceFilter { - public override void OnActionExecuting(ActionExecutingContext context) + /// + /// + /// + /// Max length. + public MaxContentLengthFilter(long maxByteLength) { - if (context.HttpContext.Request.ContentType == null) - { - context.Result = new BadRequestObjectResult(ErrorResponse.Common.Header.ContentType_Missing()); - } + MaxByteLength = maxByteLength; } - } - - public class RequireContentLengthAttribute : ActionFilterAttribute - { - public RequireContentLengthAttribute() - : this(true) - { - } + /// + /// Max length. + /// + public long MaxByteLength { get; set; } - public RequireContentLengthAttribute(bool requireNonZero) + /// + public void OnResourceExecuted(ResourceExecutedContext context) { - RequireNonZero = requireNonZero; } - public bool RequireNonZero { get; set; } - - public override void OnActionExecuting(ActionExecutingContext context) + /// + public void OnResourceExecuting(ResourceExecutingContext context) { - if (context.HttpContext.Request.ContentLength == null) + var contentLength = context.HttpContext.Request.ContentLength; + if (contentLength != null && contentLength > MaxByteLength) { - context.Result = new BadRequestObjectResult(ErrorResponse.Common.Header.ContentLength_Missing()); - return; + context.Result = new BadRequestObjectResult(ErrorResponse.Common.Content.TooBig(MaxByteLength + "B")); } + } + } - if (RequireNonZero && context.HttpContext.Request.ContentLength.Value == 0) - { - context.Result = new BadRequestObjectResult(ErrorResponse.Common.Header.ContentLength_Zero()); - return; - } + /// + /// Restrict max content length. + /// + public class MaxContentLengthAttribute : TypeFilterAttribute + { + /// + /// + /// + /// Max length. + public MaxContentLengthAttribute(long maxByteLength) + : base(typeof(MaxContentLengthFilter)) + { + MaxByteLength = maxByteLength; + Arguments = new object[] { maxByteLength }; } + + /// + /// Max length. + /// + public long MaxByteLength { get; } } } -- cgit v1.2.3