| | | 1 | | using System.Globalization; |
| | | 2 | | using ArturRios.Heimdall.Command.Services; |
| | | 3 | | |
| | | 4 | | namespace ArturRios.Heimdall.WebApi.Security; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// <see cref="IRetryAfterAdvisor" /> that sets the current response's <c>Retry-After</c> header, |
| | | 8 | | /// in whole seconds and at least one — the form the rate limiter's own rejections use. |
| | | 9 | | /// </summary> |
| | 90 | 10 | | public class HttpResponseRetryAfterAdvisor(IHttpContextAccessor httpContextAccessor) : IRetryAfterAdvisor |
| | | 11 | | { |
| | | 12 | | public void Advise(TimeSpan wait) |
| | | 13 | | { |
| | 24 | 14 | | if (httpContextAccessor.HttpContext is not { Response.HasStarted: false } httpContext) |
| | | 15 | | { |
| | 0 | 16 | | return; |
| | | 17 | | } |
| | | 18 | | |
| | 24 | 19 | | var seconds = Math.Max(1, (long)Math.Ceiling(wait.TotalSeconds)); |
| | | 20 | | |
| | 24 | 21 | | httpContext.Response.Headers.RetryAfter = seconds.ToString(CultureInfo.InvariantCulture); |
| | 24 | 22 | | } |
| | | 23 | | } |