/// <exception cref="Apache.Http.Client.ClientProtocolException"></exception>
        private static HttpHost DetermineTarget(IHttpUriRequest request)
        {
            // A null target may be acceptable if there is a default target.
            // Otherwise, the null target is detected in the director.
            HttpHost target     = null;
            URI      requestURI = request.GetURI();

            if (requestURI.IsAbsolute())
            {
                target = URIUtils.ExtractHost(requestURI);
                if (target == null)
                {
                    throw new ClientProtocolException("URI does not specify a valid host name: " + requestURI
                                                      );
                }
            }
            return(target);
        }
Exemplo n.º 2
0
        /// <exception cref="System.IO.IOException"></exception>
        /// <exception cref="Apache.Http.HttpException"></exception>
        public virtual CloseableHttpResponse Execute(HttpRoute route, HttpRequestWrapper
                                                     request, HttpClientContext context, HttpExecutionAware execAware)
        {
            Args.NotNull(route, "HTTP route");
            Args.NotNull(request, "HTTP request");
            Args.NotNull(context, "HTTP context");
            IList <URI> redirectLocations = context.GetRedirectLocations();

            if (redirectLocations != null)
            {
                redirectLocations.Clear();
            }
            RequestConfig      config         = context.GetRequestConfig();
            int                maxRedirects   = config.GetMaxRedirects() > 0 ? config.GetMaxRedirects() : 50;
            HttpRoute          currentRoute   = route;
            HttpRequestWrapper currentRequest = request;

            for (int redirectCount = 0; ;)
            {
                CloseableHttpResponse response = requestExecutor.Execute(currentRoute, currentRequest
                                                                         , context, execAware);
                try
                {
                    if (config.IsRedirectsEnabled() && this.redirectStrategy.IsRedirected(currentRequest
                                                                                          , response, context))
                    {
                        if (redirectCount >= maxRedirects)
                        {
                            throw new RedirectException("Maximum redirects (" + maxRedirects + ") exceeded");
                        }
                        redirectCount++;
                        IHttpRequest redirect = this.redirectStrategy.GetRedirect(currentRequest, response
                                                                                  , context);
                        if (!redirect.HeaderIterator().HasNext())
                        {
                            IHttpRequest original = request.GetOriginal();
                            redirect.SetHeaders(original.GetAllHeaders());
                        }
                        currentRequest = HttpRequestWrapper.Wrap(redirect);
                        if (currentRequest is HttpEntityEnclosingRequest)
                        {
                            Proxies.EnhanceEntity((HttpEntityEnclosingRequest)currentRequest);
                        }
                        URI      uri       = currentRequest.GetURI();
                        HttpHost newTarget = URIUtils.ExtractHost(uri);
                        if (newTarget == null)
                        {
                            throw new ProtocolException("Redirect URI does not specify a valid host name: " +
                                                        uri);
                        }
                        // Reset virtual host and auth states if redirecting to another host
                        if (!currentRoute.GetTargetHost().Equals(newTarget))
                        {
                            AuthState targetAuthState = context.GetTargetAuthState();
                            if (targetAuthState != null)
                            {
                                this.log.Debug("Resetting target auth state");
                                targetAuthState.Reset();
                            }
                            AuthState proxyAuthState = context.GetProxyAuthState();
                            if (proxyAuthState != null)
                            {
                                AuthScheme authScheme = proxyAuthState.GetAuthScheme();
                                if (authScheme != null && authScheme.IsConnectionBased())
                                {
                                    this.log.Debug("Resetting proxy auth state");
                                    proxyAuthState.Reset();
                                }
                            }
                        }
                        currentRoute = this.routePlanner.DetermineRoute(newTarget, currentRequest, context
                                                                        );
                        if (this.log.IsDebugEnabled())
                        {
                            this.log.Debug("Redirecting to '" + uri + "' via " + currentRoute);
                        }
                        EntityUtils.Consume(response.GetEntity());
                        response.Close();
                    }
                    else
                    {
                        return(response);
                    }
                }
                catch (RuntimeException ex)
                {
                    response.Close();
                    throw;
                }
                catch (IOException ex)
                {
                    response.Close();
                    throw;
                }
                catch (HttpException ex)
                {
                    // Protocol exception related to a direct.
                    // The underlying connection may still be salvaged.
                    try
                    {
                        EntityUtils.Consume(response.GetEntity());
                    }
                    catch (IOException ioex)
                    {
                        this.log.Debug("I/O error while releasing connection", ioex);
                    }
                    finally
                    {
                        response.Close();
                    }
                    throw;
                }
            }
        }