public async Task OnSent(HttpSentContext context)
 {
     foreach (var handler in GetHandlers(HandlerType.Sent))
     {
         await handler(context);
     }
 }
        public override async Task OnSent(HttpSentContext context)
        {
            if (!RedirectValidtor(context))
            {
                return;
            }

            var uri = context.Uri;

            var redirectCount = context.Items["RedirectCount"].As <int?>().GetValueOrDefault();

            if (redirectCount >= MaxAutoRedirects)
            {
                throw new MaximumAutoRedirectsException(context.Result, context.Request, redirectCount);
            }

            var newUri = GetRedirectUri(uri, context.Result);

            var ctx = new RedirectContext
            {
                StatusCode              = context.Result.StatusCode,
                RequestMessage          = context.Result.RequestMessage,
                RedirectUri             = newUri,
                CurrentUri              = uri,
                CurrentRedirectionCount = redirectCount
            };

            if (ctx.RedirectUri == null)
            {
                return;
            }

            OnRedirect?.Invoke(ctx);

            if (!ctx.ShouldRedirect)
            {
                return;
            }

            context.Builder.WithUri(ctx.RedirectUri);
            context.Items["RedirectCount"] = redirectCount + 1;

            // dispose of previous response
            ObjectHelpers.Dispose(context.Result);

            context.Result = await context.Builder.RecursiveResultAsync(context.Token);
        }
Пример #3
0
        public override async Task OnSent(HttpSentContext context)
        {
            if (!RetryValidator(context))
            {
                return;
            }

            var uri = context.Uri;

            var retryCount = context.Items["RetryCount"].As <int?>().GetValueOrDefault();

            if (retryCount >= MaxAutoRetries)
            {
                return;
            }

            var retryAfter = GetRetryAfter(context.Result);

            var ctx = new RetryContext
            {
                StatusCode        = context.Result.StatusCode,
                RequestMessage    = context.Result.RequestMessage,
                Uri               = uri,
                ShouldRetry       = retryAfter.HasValue,
                RetryAfter        = retryAfter ?? DefaultRetryAfter,
                CurrentRetryCount = retryCount
            };

            OnRetry?.Invoke(ctx);

            if (!ctx.ShouldRetry)
            {
                return;
            }

            if (ctx.RetryAfter > TimeSpan.Zero)
            {
                await Task.Delay(ctx.RetryAfter, context.Token);
            }

            context.Items["RetryCount"] = retryCount + 1;

            // dispose of previous response
            ObjectHelpers.Dispose(context.Result);

            context.Result = await context.Builder.RecursiveResultAsync(context.Token);
        }
Пример #4
0
        private static bool ShouldFollow(HttpSentContext context)
        {
            if (!context.IsSuccessfulResponse())
            {
                return(false);
            }

            if (!FollowedStatusCodes.Contains(context.Result.StatusCode))
            {
                return(false);
            }

            if (context.Result.Headers.Location == null)
            {
                return(false);
            }

            return(true);
        }
Пример #5
0
        public override async Task OnSent(HttpSentContext context)
        {
            if (!FollowValidtor(context))
            {
                return;
            }

            var uri = context.Uri;

            var locationUri = GetLocationUri(uri, context.Result);

            var ctx = new FollowLocationContext
            {
                StatusCode     = context.Result.StatusCode,
                RequestMessage = context.Result.RequestMessage,
                LocationUri    = locationUri,
                CurrentUri     = uri
            };

            if (ctx.LocationUri == null)
            {
                return;
            }

            OnFollow?.Invoke(ctx);

            if (!ctx.ShouldFollow)
            {
                return;
            }

            context.Builder.WithUri(ctx.LocationUri).AsGet().WithContent(string.Empty);

            // dispose of previous response
            ObjectHelpers.Dispose(context.Result);

            context.Result = await context.Builder.RecursiveResultAsync(context.Token);
        }
 private bool ShouldRedirect(HttpSentContext context)
 {
     return(RedirectStatusCodes.Contains(context.Result.StatusCode));
 }
Пример #7
0
 public virtual Task OnSent(HttpSentContext context)
 {
     return(Task.FromResult(true));
 }