private boolean needAuthentication(final InternalState state) throws HttpException {
final HttpClientContext localContext = state.getLocalContext();
final CredentialsProvider credsProvider = localContext.getCredentialsProvider();
if (credsProvider != null) {
final HttpRoute route = state.getRoute();
final HttpResponse currentResponse = state.getCurrentResponse();
HttpHost target = localContext.getTargetHost();
if (target == null) {
target = route.getTargetHost();
}
if (target.getPort() < 0) {
target = new HttpHost(
target.getHostName(),
route.getTargetHost().getPort(),
target.getSchemeName());
}
final AuthState targetAuthState = localContext.getTargetAuthState();
final AuthState proxyAuthState = localContext.getProxyAuthState();
final boolean targetAuthRequested = this.authenticator.isAuthenticationRequested(
target, currentResponse, this.targetAuthStrategy, targetAuthState, localContext);
HttpHost proxy = route.getProxyHost();
// if proxy is not set use target host instead
if (proxy == null) {
proxy = route.getTargetHost();
}
final boolean proxyAuthRequested = this.authenticator.isAuthenticationRequested(
proxy, currentResponse, this.proxyAuthStrategy, proxyAuthState, localContext);
if (targetAuthRequested) {