Exemplo n.º 1
0
        private bool NeedAuthentication(AuthState targetAuthState, AuthState proxyAuthState
                                        , HttpRoute route, HttpResponse response, HttpClientContext context)
        {
            RequestConfig config = context.GetRequestConfig();

            if (config.IsAuthenticationEnabled())
            {
                HttpHost target = context.GetTargetHost();
                if (target == null)
                {
                    target = route.GetTargetHost();
                }
                if (target.GetPort() < 0)
                {
                    target = new HttpHost(target.GetHostName(), route.GetTargetHost().GetPort(), target
                                          .GetSchemeName());
                }
                if (this.authenticator.IsAuthenticationRequested(target, response, this.targetAuthStrategy
                                                                 , targetAuthState, context))
                {
                    return(this.authenticator.HandleAuthChallenge(target, response, this.targetAuthStrategy
                                                                  , targetAuthState, context));
                }
                HttpHost proxy = route.GetProxyHost();
                if (this.authenticator.IsAuthenticationRequested(proxy, response, this.proxyAuthStrategy
                                                                 , proxyAuthState, context))
                {
                    // if proxy is not set use target host instead
                    if (proxy == null)
                    {
                        proxy = route.GetTargetHost();
                    }
                    return(this.authenticator.HandleAuthChallenge(proxy, response, this.proxyAuthStrategy
                                                                  , proxyAuthState, context));
                }
            }
            return(false);
        }
        //end of switch
        /// <exception cref="Apache.Http.ProtocolException"></exception>
        public virtual URI GetLocationURI(IHttpRequest request, HttpResponse response, HttpContext
                                          context)
        {
            Args.NotNull(request, "HTTP request");
            Args.NotNull(response, "HTTP response");
            Args.NotNull(context, "HTTP context");
            HttpClientContext clientContext = ((HttpClientContext)HttpClientContext.Adapt(context
                                                                                          ));
            //get the location header to find out where to redirect to
            Header locationHeader = response.GetFirstHeader("location");

            if (locationHeader == null)
            {
                // got a redirect response, but no location header
                throw new ProtocolException("Received redirect response " + response.GetStatusLine
                                                () + " but no location header");
            }
            string location = locationHeader.GetValue();

            if (this.log.IsDebugEnabled())
            {
                this.log.Debug("Redirect requested to location '" + location + "'");
            }
            RequestConfig config = clientContext.GetRequestConfig();
            URI           uri    = CreateLocationURI(location);

            // rfc2616 demands the location value be a complete URI
            // Location       = "Location" ":" absoluteURI
            try
            {
                if (!uri.IsAbsolute())
                {
                    if (!config.IsRelativeRedirectsAllowed())
                    {
                        throw new ProtocolException("Relative redirect location '" + uri + "' not allowed"
                                                    );
                    }
                    // Adjust location URI
                    HttpHost target = clientContext.GetTargetHost();
                    Asserts.NotNull(target, "Target host");
                    URI requestURI         = new URI(request.GetRequestLine().GetUri());
                    URI absoluteRequestURI = URIUtils.RewriteURI(requestURI, target, false);
                    uri = URIUtils.Resolve(absoluteRequestURI, uri);
                }
            }
            catch (URISyntaxException ex)
            {
                throw new ProtocolException(ex.Message, ex);
            }
            RedirectLocations redirectLocations = (RedirectLocations)clientContext.GetAttribute
                                                      (HttpClientContext.RedirectLocations);

            if (redirectLocations == null)
            {
                redirectLocations = new RedirectLocations();
                context.SetAttribute(HttpClientContext.RedirectLocations, redirectLocations);
            }
            if (!config.IsCircularRedirectsAllowed())
            {
                if (redirectLocations.Contains(uri))
                {
                    throw new CircularRedirectException("Circular redirect to '" + uri + "'");
                }
            }
            redirectLocations.Add(uri);
            return(uri);
        }