public override async Task HandleRequirementAsync(ThrottleContext throttleContext, TRequirement requirement)
        {
            if (throttleContext == null)
            {
                throw new ArgumentNullException(nameof(throttleContext));
            }

            if (requirement == null)
            {
                throw new ArgumentNullException(nameof(requirement));
            }

            var key = GetKey(throttleContext.HttpContext, requirement);

            if (key == null)
            {
                throttleContext.Skipped(requirement);
                return;
            }

            key = typeof(TRequirement) + key;
            var counter = await throttleContext.Store.IncrementAsync(key, requirement, 1);

            if (counter.LimitReached)
            {
                throttleContext.TooManyRequest(requirement, counter.Reset);
            }
            else
            {
                throttleContext.Succeed(requirement);
            }

            AddRateLimitHeaders(counter, throttleContext, requirement);
        }
예제 #2
0
        public virtual async Task <ThrottleContext> EvaluateAsync(HttpContext httpContext, ThrottleStrategy strategy)
        {
            if (httpContext == null)
            {
                throw new ArgumentNullException(nameof(httpContext));
            }

            if (strategy == null)
            {
                throw new ArgumentNullException(nameof(strategy));
            }

            var throttleContext = new ThrottleContext(httpContext, strategy, _store);

            foreach (var exclusion in _exclusionHandlers)
            {
                await exclusion.HandleExclusionAsync(throttleContext);
            }

            if (!throttleContext.HasAborted)
            {
                foreach (var handler in _handlers)
                {
                    await handler.HandleRequirementAsync(throttleContext);
                }
            }

            return(throttleContext);
        }
예제 #3
0
        public virtual async Task HandleExclusionAsync(ThrottleContext throttleContext)
        {
            if (throttleContext == null)
            {
                throw new ArgumentNullException(nameof(throttleContext));
            }

            foreach (var exclusion in throttleContext.Exclusions.OfType <TExclusion>())
            {
                await HandleAsync(throttleContext, exclusion);
            }
        }
예제 #4
0
        public virtual async Task HandleRequirementAsync(ThrottleContext throttleContext)
        {
            if (throttleContext == null)
            {
                throw new ArgumentNullException(nameof(throttleContext));
            }

            foreach (var requirement in throttleContext.Requirements.OfType <TRequirement>())
            {
                await HandleRequirementAsync(throttleContext, requirement);
            }
        }
예제 #5
0
        public async Task PostEvaluateAsync(ThrottleContext throttleContext)
        {
            if (throttleContext == null)
            {
                throw new ArgumentNullException(nameof(throttleContext));
            }

            foreach (var handler in _handlers)
            {
                await handler.PostHandleRequirementAsync(throttleContext);
            }
        }
예제 #6
0
        public override long GetIncrementValue(ThrottleContext throttleContext, TRequirement requirement)
        {
            if (throttleContext == null)
            {
                throw new ArgumentNullException(nameof(throttleContext));
            }

            if (requirement == null)
            {
                throw new ArgumentNullException(nameof(requirement));
            }

            return(throttleContext.ContentLengthTracker.ContentLength);
        }
        public override Task PostHandleRequirementAsync(ThrottleContext throttleContext, IPSessionRateLimitRequirement requirement)
        {
            if (throttleContext == null)
            {
                throw new ArgumentNullException(nameof(throttleContext));
            }

            if (requirement == null)
            {
                throw new ArgumentNullException(nameof(requirement));
            }

            return(base.PostHandleRequirementAsync(throttleContext, requirement));
        }
        public override void AddRateLimitHeaders(ThrottleCounter counter, ThrottleContext throttleContext, IPSessionRateLimitRequirement requirement)
        {
            if (counter == null)
            {
                throw new ArgumentNullException(nameof(counter));
            }

            if (throttleContext == null)
            {
                throw new ArgumentNullException(nameof(throttleContext));
            }

            if (requirement == null)
            {
                throw new ArgumentNullException(nameof(requirement));
            }

            throttleContext.ResponseHeaders["X-RateLimit-IPLimit"]     = requirement.MaxValue.ToString();
            throttleContext.ResponseHeaders["X-RateLimit-IPRemaining"] = counter.Remaining(requirement).ToString();
        }
예제 #9
0
        public static void TrackContentLength(this HttpResponse response, ThrottleContext throttleContext)
        {
            if (response == null)
            {
                throw new ArgumentNullException(nameof(response));
            }

            if (throttleContext == null)
            {
                throw new ArgumentNullException(nameof(throttleContext));
            }

            var body = response.Body as ContentLengthTrackingStream;

            if (body == null)
            {
                var tracker = new ContentLengthTracker();
                response.Body = new ContentLengthTrackingStream(response.Body, tracker);
                throttleContext.ContentLengthTracker = tracker;
            }
        }
예제 #10
0
        public override Task HandleAsync(ThrottleContext throttleContext, IPExclusion exclusion)
        {
            if (throttleContext == null)
            {
                throw new ArgumentNullException(nameof(throttleContext));
            }

            if (exclusion == null)
            {
                throw new ArgumentNullException(nameof(exclusion));
            }

            IHttpConnectionFeature connection = throttleContext.HttpContext.Features.Get <IHttpConnectionFeature>();

            if (exclusion.Whitelist.Contains(connection.RemoteIpAddress))
            {
                throttleContext.Abort(exclusion);
            }

            return(Constants.CompletedTask);
        }
예제 #11
0
        public override async Task PostHandleRequirementAsync(ThrottleContext throttleContext, TRequirement requirement)
        {
            if (throttleContext == null)
            {
                throw new ArgumentNullException(nameof(throttleContext));
            }

            if (requirement == null)
            {
                throw new ArgumentNullException(nameof(requirement));
            }

            var key = GetKey(throttleContext.HttpContext, requirement);

            if (key == null)
            {
                return;
            }

            key = typeof(TRequirement) + key;
            var incrementValue = GetIncrementValue(throttleContext, requirement);
            await throttleContext.Store.IncrementAsync(key, requirement, incrementValue, reachLimitAtMax : true);
        }
 public abstract void AddRateLimitHeaders(ThrottleCounter counter, ThrottleContext throttleContext, TRequirement requirement);
예제 #13
0
 public abstract Task HandleAsync(ThrottleContext throttleContext, TExclusion exclusion);
예제 #14
0
 public abstract long GetIncrementValue(ThrottleContext throttleContext, TRequirement requirement);
예제 #15
0
 public virtual Task PostHandleRequirementAsync(ThrottleContext throttleContext, TRequirement requirement)
 {
     return(Constants.CompletedTask);
 }
예제 #16
0
 public abstract Task HandleRequirementAsync(ThrottleContext throttleContext, TRequirement requirement);