public virtual void SetCredentials(AuthScope authscope, Credentials credentials)
 {
     @internal.SetCredentials(authscope, credentials);
 }
예제 #2
0
        /// <exception cref="System.IO.IOException"></exception>
        /// <exception cref="Apache.Http.HttpException"></exception>
        public virtual Socket Tunnel(HttpHost proxy, HttpHost target, Credentials credentials
                                     )
        {
            Args.NotNull(proxy, "Proxy host");
            Args.NotNull(target, "Target host");
            Args.NotNull(credentials, "Credentials");
            HttpHost host = target;

            if (host.GetPort() <= 0)
            {
                host = new HttpHost(host.GetHostName(), 80, host.GetSchemeName());
            }
            HttpRoute route = new HttpRoute(host, this.requestConfig.GetLocalAddress(), proxy
                                            , false, RouteInfo.TunnelType.Tunnelled, RouteInfo.LayerType.Plain);
            ManagedHttpClientConnection conn = this.connFactory.Create(route, this.connectionConfig
                                                                       );
            HttpContext  context = new BasicHttpContext();
            HttpResponse response;
            IHttpRequest connect = new BasicHttpRequest("CONNECT", host.ToHostString(), HttpVersion
                                                        .Http11);
            BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();

            credsProvider.SetCredentials(new AuthScope(proxy), credentials);
            // Populate the execution context
            context.SetAttribute(HttpCoreContext.HttpTargetHost, target);
            context.SetAttribute(HttpCoreContext.HttpConnection, conn);
            context.SetAttribute(HttpCoreContext.HttpRequest, connect);
            context.SetAttribute(HttpClientContext.HttpRoute, route);
            context.SetAttribute(HttpClientContext.ProxyAuthState, this.proxyAuthState);
            context.SetAttribute(HttpClientContext.CredsProvider, credsProvider);
            context.SetAttribute(HttpClientContext.AuthschemeRegistry, this.authSchemeRegistry
                                 );
            context.SetAttribute(HttpClientContext.RequestConfig, this.requestConfig);
            this.requestExec.PreProcess(connect, this.httpProcessor, context);
            for (; ;)
            {
                if (!conn.IsOpen())
                {
                    Socket socket = Sharpen.Extensions.CreateSocket(proxy.GetHostName(), proxy.GetPort
                                                                        ());
                    conn.Bind(socket);
                }
                this.authenticator.GenerateAuthResponse(connect, this.proxyAuthState, context);
                response = this.requestExec.Execute(connect, conn, context);
                int status = response.GetStatusLine().GetStatusCode();
                if (status < 200)
                {
                    throw new HttpException("Unexpected response to CONNECT request: " + response.GetStatusLine
                                                ());
                }
                if (this.authenticator.IsAuthenticationRequested(proxy, response, this.proxyAuthStrategy
                                                                 , this.proxyAuthState, context))
                {
                    if (this.authenticator.HandleAuthChallenge(proxy, response, this.proxyAuthStrategy
                                                               , this.proxyAuthState, context))
                    {
                        // Retry request
                        if (this.reuseStrategy.KeepAlive(response, context))
                        {
                            // Consume response content
                            HttpEntity entity = response.GetEntity();
                            EntityUtils.Consume(entity);
                        }
                        else
                        {
                            conn.Close();
                        }
                        // discard previous auth header
                        connect.RemoveHeaders(AUTH.ProxyAuthResp);
                    }
                    else
                    {
                        break;
                    }
                }
                else
                {
                    break;
                }
            }
            int status_1 = response.GetStatusLine().GetStatusCode();

            if (status_1 > 299)
            {
                // Buffer response content
                HttpEntity entity = response.GetEntity();
                if (entity != null)
                {
                    response.SetEntity(new BufferedHttpEntity(entity));
                }
                conn.Close();
                throw new TunnelRefusedException("CONNECT refused by proxy: " + response.GetStatusLine
                                                     (), response);
            }
            return(conn.GetSocket());
        }