private String getServicePrincipalName(final HttpContext context) { final String spn; if (this.servicePrincipalName != null) { spn = this.servicePrincipalName; } else { final HttpClientContext clientContext = HttpClientContext.adapt(context); final HttpHost target = clientContext.getTargetHost(); if (target != null) { spn = "HTTP/" + target.getHostName(); } else { final RouteInfo route = clientContext.getHttpRoute(); if (route != null) { spn = "HTTP/" + route.getTargetHost().getHostName(); } else { // Should not happen spn = null; } } } if (this.log.isDebugEnabled()) { this.log.debug("Using SPN: " + spn); } return spn; }
/** * A convenience method that optionally converts the original {@link java.net.URI} either * to a relative or an absolute form as required by the specified route. * * @param uri * original URI. * @throws URISyntaxException * If the resulting URI is invalid. * * @since 4.4 */ public static URI rewriteURIForRoute(final URI uri, final RouteInfo route) throws URISyntaxException { if (uri == null) { return null; } if (route.getProxyHost() != null && !route.isTunnelled()) { // Make sure the request URI is absolute if (!uri.isAbsolute()) { final HttpHost target = route.getTargetHost(); return rewriteURI(uri, target, true); } else { return rewriteURI(uri); } } else { // Make sure the request URI is relative if (uri.isAbsolute()) { return rewriteURI(uri, null, true); } else { return rewriteURI(uri); } } }
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException { Args.notNull(request, "HTTP request"); final String method = request.getRequestLine().getMethod(); if (method.equalsIgnoreCase("CONNECT")) { request.setHeader(PROXY_CONN_DIRECTIVE, HTTP.CONN_KEEP_ALIVE); return; } final HttpClientContext clientContext = HttpClientContext.adapt(context); // Obtain the client connection (required) final RouteInfo route = clientContext.getHttpRoute(); if (route == null) { if (Log.isLoggable(TAG, Log.DEBUG)) { Log.d(TAG, "Connection route not set in the context"); } return; } if (route.getHopCount() == 1 || route.isTunnelled()) { if (!request.containsHeader(HTTP.CONN_DIRECTIVE)) { request.addHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_KEEP_ALIVE); } } if (route.getHopCount() == 2 && !route.isTunnelled()) { if (!request.containsHeader(PROXY_CONN_DIRECTIVE)) { request.addHeader(PROXY_CONN_DIRECTIVE, HTTP.CONN_KEEP_ALIVE); } } }
@Override public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException { Args.notNull(request, "HTTP request"); final String method = request.getRequestLine().getMethod(); if (method.equalsIgnoreCase("CONNECT")) { request.setHeader(PROXY_CONN_DIRECTIVE, HTTP.CONN_KEEP_ALIVE); return; } final HttpClientContext clientContext = HttpClientContext.adapt(context); // Obtain the client connection (required) final RouteInfo route = clientContext.getHttpRoute(); if (route == null) { this.log.debug("Connection route not set in the context"); return; } if (route.getHopCount() == 1 || route.isTunnelled()) { if (!request.containsHeader(HTTP.CONN_DIRECTIVE)) { request.addHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_KEEP_ALIVE); } } if (route.getHopCount() == 2 && !route.isTunnelled()) { if (!request.containsHeader(PROXY_CONN_DIRECTIVE)) { request.addHeader(PROXY_CONN_DIRECTIVE, HTTP.CONN_KEEP_ALIVE); } } }
@Test(expected = HttpException.class) public void testEstablishRouteViaProxyTunnelMultipleHops() throws Exception { final AuthState authState = new AuthState(); final HttpHost proxy1 = new HttpHost("this", 8888); final HttpHost proxy2 = new HttpHost("that", 8888); final HttpRoute route = new HttpRoute(target, null, new HttpHost[] {proxy1, proxy2}, true, RouteInfo.TunnelType.TUNNELLED, RouteInfo.LayerType.LAYERED); final HttpClientContext context = new HttpClientContext(); final HttpRequestWrapper request = HttpRequestWrapper.wrap(new HttpGet("http://bar/test")); Mockito.when(managedConn.isOpen()).thenReturn(Boolean.TRUE); mainClientExec.establishRoute(authState, managedConn, route, request, context); }
@Override public void process(HttpRequest request, HttpContext context) throws HttpException, IOException { HttpClientContext clientContext = HttpClientContext.adapt(context); AuthState proxyAuthState = clientContext.getProxyAuthState(); // If there's no auth scheme available yet, try to initialize it preemptively if (proxyAuthState.getAuthScheme() == null) { CredentialsProvider credsProvider = clientContext.getCredentialsProvider(); RouteInfo route = clientContext.getHttpRoute(); if (route == null) { log.debug("No route found for {}", clientContext.getTargetHost()); return; } HttpHost proxyHost = route.getProxyHost(); if (proxyHost == null) { log.warn("No proxy host found in route {} for host {}", route, clientContext.getTargetHost()); return; } Credentials creds = credsProvider.getCredentials( new AuthScope(proxyHost.getHostName(), proxyHost.getPort())); if (creds == null) { log.info("No credentials found for proxy: " + proxyHost); return; } proxyAuthState.update(new BasicScheme(ChallengeState.PROXY), creds); } }
@Override public void process(HttpRequest request, HttpContext context) throws HttpException, IOException { HttpClientContext clientContext = HttpClientContext.adapt(context); AuthState proxyAuthState = clientContext.getProxyAuthState(); // If there's no auth scheme available yet, try to initialize it preemptively if (proxyAuthState.getAuthScheme() == null) { CredentialsProvider credsProvider = clientContext.getCredentialsProvider(); RouteInfo route = clientContext.getHttpRoute(); if (route == null) { if (log.isDebugEnabled()) { log.debug("No route found for {}", clientContext.getTargetHost()); } return; } HttpHost proxyHost = route.getProxyHost(); if (proxyHost == null) { log.warn("No proxy host found in route {} for host {}", route, clientContext.getTargetHost()); return; } Credentials creds = credsProvider.getCredentials( new AuthScope(proxyHost.getHostName(), proxyHost.getPort())); if (creds == null) { log.info("No credentials found for proxy: " + proxyHost); return; } proxyAuthState.update(new BasicScheme(ChallengeState.PROXY), creds); } }
public RouteInfo getHttpRoute() { return getAttribute(HTTP_ROUTE, HttpRoute.class); }