Ejemplo n.º 1
0
        /// <summary>
        /// Checks for known redirects and alters the request url if required, passes the request on to the next handler in the pipeline, then handles any redirect responses until a non-redirect response is received.
        /// </summary>
        /// <param name="request">The <see cref="HttpRequestMessage"/> to send.</param>
        /// <param name="cancellationToken">A <see cref="CancellationToken"/> that can be used to cancel the request.</param>
        /// <returns></returns>
        protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            request.RequestUri = _RedirectCache.GetFinalUri(request.RequestUri);
            cancellationToken.ThrowIfCancellationRequested();

            var result = await base.SendAsync(request, cancellationToken).ConfigureAwait(false);

            int redirects = 0;

            while (redirects < MaxRedirects)
            {
                cancellationToken.ThrowIfCancellationRequested();

                if (result.StatusCode.IsRedirect())
                {
                    if (String.Compare(request.Method.Method, "GET", StringComparison.OrdinalIgnoreCase) == 0 && result.Headers.Location != null)
                    {
                        if (result.StatusCode.IsPermanentRedirect())
                        {
                            _RedirectCache.AddOrUpdateRedirect(request.RequestUri, result.Headers.Location);
                        }

                        request.RequestUri = result.Headers.Location;
                        result             = await base.SendAsync(request, cancellationToken).ConfigureAwait(false);
                    }
                    redirects++;
                }
                else
                {
                    break;
                }
            }
            return(result);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Checks for known redirects and alters the request url if required, passes the request on to the next handler in the pipeline, then handles any redirect responses until a non-redirect response is received.
        /// </summary>
        /// <param name="request">The <see cref="HttpRequestMessage"/> to send.</param>
        /// <returns></returns>
        public IAsyncOperationWithProgress <HttpResponseMessage, HttpProgress> SendRequestAsync(HttpRequestMessage request)
        {
            return(AsyncInfo.Run <HttpResponseMessage, HttpProgress>(async(cancellationToken, progress) =>
            {
                request.RequestUri = _RedirectCache.GetFinalUri(request.RequestUri);
                cancellationToken.ThrowIfCancellationRequested();

                var result = await _InnerFilter.SendRequestAsync(request).AsTask().ConfigureAwait(false);

                int redirects = 0;
                while (redirects < MaxRedirects)
                {
                    if (result.StatusCode.IsRedirect())
                    {
                        if (String.Compare(request.Method.Method, "GET", StringComparison.OrdinalIgnoreCase) == 0 && result.Headers.Location != null)
                        {
                            if (result.StatusCode.IsPermanentRedirect())
                            {
                                _RedirectCache.AddOrUpdateRedirect(request.RequestUri, result.Headers.Location);
                            }

                            var request2 = new HttpRequestMessage(request.Method, result.Headers.Location);
                            WebHttpHeaderExtensions.CopyHeadersTo(request.Headers, request2.Headers);

                            result = await _InnerFilter.SendRequestAsync(request2).AsTask().ConfigureAwait(false);
                        }
                        redirects++;
                    }
                    else
                    {
                        break;
                    }

                    cancellationToken.ThrowIfCancellationRequested();
                }
                return result;
            }));
        }