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); }
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); }
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); } }
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); } }
public async Task PostEvaluateAsync(ThrottleContext throttleContext) { if (throttleContext == null) { throw new ArgumentNullException(nameof(throttleContext)); } foreach (var handler in _handlers) { await handler.PostHandleRequirementAsync(throttleContext); } }
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(); }
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; } }
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); }
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);
public abstract Task HandleAsync(ThrottleContext throttleContext, TExclusion exclusion);
public abstract long GetIncrementValue(ThrottleContext throttleContext, TRequirement requirement);
public virtual Task PostHandleRequirementAsync(ThrottleContext throttleContext, TRequirement requirement) { return(Constants.CompletedTask); }
public abstract Task HandleRequirementAsync(ThrottleContext throttleContext, TRequirement requirement);