Exemplo n.º 1
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");
            IHttpRequest original = request.GetOriginal();
            URI          uri      = null;

            if (original is IHttpUriRequest)
            {
                uri = ((IHttpUriRequest)original).GetURI();
            }
            else
            {
                string uriString = original.GetRequestLine().GetUri();
                try
                {
                    uri = URI.Create(uriString);
                }
                catch (ArgumentException ex)
                {
                    if (this.log.IsDebugEnabled())
                    {
                        this.log.Debug("Unable to parse '" + uriString + "' as a valid URI; " + "request URI and Host header may be inconsistent"
                                       , ex);
                    }
                }
            }
            request.SetURI(uri);
            // Re-write request URI if needed
            RewriteRequestURI(request, route);
            HttpParams @params     = request.GetParams();
            HttpHost   virtualHost = (HttpHost)@params.GetParameter(ClientPNames.VirtualHost);

            // HTTPCLIENT-1092 - add the port if necessary
            if (virtualHost != null && virtualHost.GetPort() == -1)
            {
                int port = route.GetTargetHost().GetPort();
                if (port != -1)
                {
                    virtualHost = new HttpHost(virtualHost.GetHostName(), port, virtualHost.GetSchemeName
                                                   ());
                }
                if (this.log.IsDebugEnabled())
                {
                    this.log.Debug("Using virtual host" + virtualHost);
                }
            }
            HttpHost target = null;

            if (virtualHost != null)
            {
                target = virtualHost;
            }
            else
            {
                if (uri != null && uri.IsAbsolute() && uri.GetHost() != null)
                {
                    target = new HttpHost(uri.GetHost(), uri.GetPort(), uri.GetScheme());
                }
            }
            if (target == null)
            {
                target = route.GetTargetHost();
            }
            // Get user info from the URI
            if (uri != null)
            {
                string userinfo = uri.GetUserInfo();
                if (userinfo != null)
                {
                    CredentialsProvider credsProvider = context.GetCredentialsProvider();
                    if (credsProvider == null)
                    {
                        credsProvider = new BasicCredentialsProvider();
                        context.SetCredentialsProvider(credsProvider);
                    }
                    credsProvider.SetCredentials(new AuthScope(target), new UsernamePasswordCredentials
                                                     (userinfo));
                }
            }
            // Run request protocol interceptors
            context.SetAttribute(HttpClientContext.HttpTargetHost, target);
            context.SetAttribute(HttpClientContext.HttpRoute, route);
            context.SetAttribute(HttpClientContext.HttpRequest, request);
            this.httpProcessor.Process(request, context);
            CloseableHttpResponse response = this.requestExecutor.Execute(route, request, context
                                                                          , execAware);

            try
            {
                // Run response protocol interceptors
                context.SetAttribute(HttpClientContext.HttpResponse, response);
                this.httpProcessor.Process(response, context);
                return(response);
            }
            catch (RuntimeException ex)
            {
                response.Close();
                throw;
            }
            catch (IOException ex)
            {
                response.Close();
                throw;
            }
            catch (HttpException ex)
            {
                response.Close();
                throw;
            }
        }
        /// <exception cref="Apache.Http.Auth.MalformedChallengeException"></exception>
        public virtual Queue <AuthOption> Select(IDictionary <string, Header> challenges, HttpHost
                                                 authhost, HttpResponse response, HttpContext context)
        {
            Args.NotNull(challenges, "Map of auth challenges");
            Args.NotNull(authhost, "Host");
            Args.NotNull(response, "HTTP response");
            Args.NotNull(context, "HTTP context");
            HttpClientContext clientContext = ((HttpClientContext)HttpClientContext.Adapt(context
                                                                                          ));
            Queue <AuthOption>          options  = new List <AuthOption>();
            Lookup <AuthSchemeProvider> registry = clientContext.GetAuthSchemeRegistry();

            if (registry == null)
            {
                this.log.Debug("Auth scheme registry not set in the context");
                return(options);
            }
            CredentialsProvider credsProvider = clientContext.GetCredentialsProvider();

            if (credsProvider == null)
            {
                this.log.Debug("Credentials provider not set in the context");
                return(options);
            }
            RequestConfig        config    = clientContext.GetRequestConfig();
            ICollection <string> authPrefs = GetPreferredAuthSchemes(config);

            if (authPrefs == null)
            {
                authPrefs = DefaultSchemePriority;
            }
            if (this.log.IsDebugEnabled())
            {
                this.log.Debug("Authentication schemes in the order of preference: " + authPrefs);
            }
            foreach (string id in authPrefs)
            {
                Header challenge = challenges.Get(id.ToLower(CultureInfo.InvariantCulture));
                if (challenge != null)
                {
                    AuthSchemeProvider authSchemeProvider = registry.Lookup(id);
                    if (authSchemeProvider == null)
                    {
                        if (this.log.IsWarnEnabled())
                        {
                            this.log.Warn("Authentication scheme " + id + " not supported");
                        }
                        // Try again
                        continue;
                    }
                    AuthScheme authScheme = authSchemeProvider.Create(context);
                    authScheme.ProcessChallenge(challenge);
                    AuthScope authScope = new AuthScope(authhost.GetHostName(), authhost.GetPort(), authScheme
                                                        .GetRealm(), authScheme.GetSchemeName());
                    Credentials credentials = credsProvider.GetCredentials(authScope);
                    if (credentials != null)
                    {
                        options.AddItem(new AuthOption(authScheme, credentials));
                    }
                }
                else
                {
                    if (this.log.IsDebugEnabled())
                    {
                        this.log.Debug("Challenge for " + id + " authentication scheme not available");
                    }
                }
            }
            // Try again
            return(options);
        }