Search in sources :

Example 91 with HttpHost

use of org.apache.http.HttpHost in project platform_external_apache-http by android.

the class AbstractPoolEntry method layerProtocol.

// tunnelProxy
/**
     * Layers a protocol on top of an established tunnel.
     *
     * @param context   the context for layering
     * @param params    the parameters for layering
     *
     * @throws IOException  in case of a problem
     */
public void layerProtocol(HttpContext context, HttpParams params) throws IOException {
    //@@@ is context allowed to be null? depends on operator?
    if (params == null) {
        throw new IllegalArgumentException("Parameters must not be null.");
    }
    if ((this.tracker == null) || !this.tracker.isConnected()) {
        throw new IllegalStateException("Connection not open.");
    }
    if (!this.tracker.isTunnelled()) {
        //@@@ allow this?
        throw new IllegalStateException("Protocol layering without a tunnel not supported.");
    }
    if (this.tracker.isLayered()) {
        throw new IllegalStateException("Multiple protocol layering not supported.");
    }
    // - collect the arguments
    // - call the operator
    // - update the tracking data
    // In this order, we can be sure that only a successful
    // layering on top of the connection will be tracked.
    final HttpHost target = tracker.getTargetHost();
    connOperator.updateSecureConnection(this.connection, target, context, params);
    this.tracker.layerProtocol(this.connection.isSecure());
}
Also used : HttpHost(org.apache.http.HttpHost)

Example 92 with HttpHost

use of org.apache.http.HttpHost in project platform_external_apache-http by android.

the class DefaultRequestDirector method createTunnelToTarget.

// establishConnection
/**
     * Creates a tunnel to the target server.
     * The connection must be established to the (last) proxy.
     * A CONNECT request for tunnelling through the proxy will
     * be created and sent, the response received and checked.
     * This method does <i>not</i> update the connection with
     * information about the tunnel, that is left to the caller.
     *
     * @param route     the route to establish
     * @param context   the context for request execution
     *
     * @return  <code>true</code> if the tunnelled route is secure,
     *          <code>false</code> otherwise.
     *          The implementation here always returns <code>false</code>,
     *          but derived classes may override.
     *
     * @throws HttpException    in case of a problem
     * @throws IOException      in case of an IO problem
     */
protected boolean createTunnelToTarget(HttpRoute route, HttpContext context) throws HttpException, IOException {
    HttpHost proxy = route.getProxyHost();
    HttpHost target = route.getTargetHost();
    HttpResponse response = null;
    boolean done = false;
    while (!done) {
        done = true;
        if (!this.managedConn.isOpen()) {
            this.managedConn.open(route, context, this.params);
        }
        HttpRequest connect = createConnectRequest(route, context);
        String agent = HttpProtocolParams.getUserAgent(params);
        if (agent != null) {
            connect.addHeader(HTTP.USER_AGENT, agent);
        }
        connect.addHeader(HTTP.TARGET_HOST, target.toHostString());
        AuthScheme authScheme = this.proxyAuthState.getAuthScheme();
        AuthScope authScope = this.proxyAuthState.getAuthScope();
        Credentials creds = this.proxyAuthState.getCredentials();
        if (creds != null) {
            if (authScope != null || !authScheme.isConnectionBased()) {
                try {
                    connect.addHeader(authScheme.authenticate(creds, connect));
                } catch (AuthenticationException ex) {
                    if (this.log.isErrorEnabled()) {
                        this.log.error("Proxy authentication error: " + ex.getMessage());
                    }
                }
            }
        }
        response = requestExec.execute(connect, this.managedConn, context);
        int status = response.getStatusLine().getStatusCode();
        if (status < 200) {
            throw new HttpException("Unexpected response to CONNECT request: " + response.getStatusLine());
        }
        CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(ClientContext.CREDS_PROVIDER);
        if (credsProvider != null && HttpClientParams.isAuthenticating(params)) {
            if (this.proxyAuthHandler.isAuthenticationRequested(response, context)) {
                this.log.debug("Proxy requested authentication");
                Map<String, Header> challenges = this.proxyAuthHandler.getChallenges(response, context);
                try {
                    processChallenges(challenges, this.proxyAuthState, this.proxyAuthHandler, response, context);
                } catch (AuthenticationException ex) {
                    if (this.log.isWarnEnabled()) {
                        this.log.warn("Authentication error: " + ex.getMessage());
                        break;
                    }
                }
                updateAuthState(this.proxyAuthState, proxy, credsProvider);
                if (this.proxyAuthState.getCredentials() != null) {
                    done = false;
                    // Retry request
                    if (this.reuseStrategy.keepAlive(response, context)) {
                        this.log.debug("Connection kept alive");
                        // Consume response content
                        HttpEntity entity = response.getEntity();
                        if (entity != null) {
                            entity.consumeContent();
                        }
                    } else {
                        this.managedConn.close();
                    }
                }
            } else {
                // Reset proxy auth scope
                this.proxyAuthState.setAuthScope(null);
            }
        }
    }
    int status = response.getStatusLine().getStatusCode();
    if (status > 299) {
        // Buffer response content
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            response.setEntity(new BufferedHttpEntity(entity));
        }
        this.managedConn.close();
        throw new TunnelRefusedException("CONNECT refused by proxy: " + response.getStatusLine(), response);
    }
    this.managedConn.markReusable();
    // Leave it to derived classes, consider insecure by default here.
    return false;
}
Also used : HttpRequest(org.apache.http.HttpRequest) BasicHttpRequest(org.apache.http.message.BasicHttpRequest) AbortableHttpRequest(org.apache.http.client.methods.AbortableHttpRequest) HttpEntity(org.apache.http.HttpEntity) BufferedHttpEntity(org.apache.http.entity.BufferedHttpEntity) AuthenticationException(org.apache.http.auth.AuthenticationException) HttpResponse(org.apache.http.HttpResponse) CredentialsProvider(org.apache.http.client.CredentialsProvider) AuthScheme(org.apache.http.auth.AuthScheme) Header(org.apache.http.Header) BufferedHttpEntity(org.apache.http.entity.BufferedHttpEntity) HttpHost(org.apache.http.HttpHost) AuthScope(org.apache.http.auth.AuthScope) HttpException(org.apache.http.HttpException) Credentials(org.apache.http.auth.Credentials)

Example 93 with HttpHost

use of org.apache.http.HttpHost in project platform_external_apache-http by android.

the class AbstractHttpClient method determineTarget.

private HttpHost determineTarget(HttpUriRequest 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 = new HttpHost(requestURI.getHost(), requestURI.getPort(), requestURI.getScheme());
    }
    return target;
}
Also used : HttpHost(org.apache.http.HttpHost) URI(java.net.URI)

Example 94 with HttpHost

use of org.apache.http.HttpHost in project platform_external_apache-http by android.

the class HttpRoute method hashCode.

/**
     * Generates a hash code for this route.
     *
     * @return  the hash code
     */
@Override
public final int hashCode() {
    int hc = this.targetHost.hashCode();
    if (this.localAddress != null)
        hc ^= localAddress.hashCode();
    if (this.proxyChain != null) {
        hc ^= proxyChain.length;
        for (HttpHost aProxyChain : proxyChain) hc ^= aProxyChain.hashCode();
    }
    if (this.secure)
        hc ^= 0x11111111;
    hc ^= this.tunnelled.hashCode();
    hc ^= this.layered.hashCode();
    return hc;
}
Also used : HttpHost(org.apache.http.HttpHost)

Example 95 with HttpHost

use of org.apache.http.HttpHost in project platform_external_apache-http by android.

the class HttpRoute method getHopTarget.

// non-JavaDoc, see interface RouteInfo
public final HttpHost getHopTarget(int hop) {
    if (hop < 0)
        throw new IllegalArgumentException("Hop index must not be negative: " + hop);
    final int hopcount = getHopCount();
    if (hop >= hopcount)
        throw new IllegalArgumentException("Hop index " + hop + " exceeds route length " + hopcount);
    HttpHost result = null;
    if (hop < hopcount - 1)
        result = this.proxyChain[hop];
    else
        result = this.targetHost;
    return result;
}
Also used : HttpHost(org.apache.http.HttpHost)

Aggregations

HttpHost (org.apache.http.HttpHost)258 HttpResponse (org.apache.http.HttpResponse)50 IOException (java.io.IOException)44 URI (java.net.URI)35 Header (org.apache.http.Header)35 HttpEntity (org.apache.http.HttpEntity)32 AuthScope (org.apache.http.auth.AuthScope)31 HttpGet (org.apache.http.client.methods.HttpGet)29 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)28 CredentialsProvider (org.apache.http.client.CredentialsProvider)28 HttpRequest (org.apache.http.HttpRequest)27 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)25 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)23 BasicHttpResponse (org.apache.http.message.BasicHttpResponse)23 HttpParams (org.apache.http.params.HttpParams)23 Test (org.junit.Test)23 URISyntaxException (java.net.URISyntaxException)21 ProtocolVersion (org.apache.http.ProtocolVersion)21 BasicHttpRequest (org.apache.http.message.BasicHttpRequest)19 Before (org.junit.Before)18