use of org.asynchttpclient.Request in project async-http-client by AsyncHttpClient.
the class Unauthorized401Interceptor method exitAfterHandling401.
public boolean exitAfterHandling401(final Channel channel, final NettyResponseFuture<?> future, HttpResponse response, final Request request, Realm realm, HttpRequest httpRequest) {
if (realm == null) {
LOGGER.debug("Can't handle 401 as there's no realm");
return false;
}
if (future.isAndSetInAuth(true)) {
LOGGER.info("Can't handle 401 as auth was already performed");
return false;
}
List<String> wwwAuthHeaders = response.headers().getAll(WWW_AUTHENTICATE);
if (wwwAuthHeaders.isEmpty()) {
LOGGER.info("Can't handle 401 as response doesn't contain WWW-Authenticate headers");
return false;
}
// FIXME what's this???
future.setChannelState(ChannelState.NEW);
HttpHeaders requestHeaders = new DefaultHttpHeaders().add(request.getHeaders());
switch(realm.getScheme()) {
case BASIC:
if (getHeaderWithPrefix(wwwAuthHeaders, "Basic") == null) {
LOGGER.info("Can't handle 401 with Basic realm as WWW-Authenticate headers don't match");
return false;
}
if (realm.isUsePreemptiveAuth()) {
// FIXME do we need this, as future.getAndSetAuth
// was tested above?
// auth was already performed, most likely auth
// failed
LOGGER.info("Can't handle 401 with Basic realm as auth was preemptive and already performed");
return false;
}
// FIXME do we want to update the realm, or directly
// set the header?
Realm newBasicRealm = realm(realm).setUsePreemptiveAuth(true).build();
future.setRealm(newBasicRealm);
break;
case DIGEST:
String digestHeader = getHeaderWithPrefix(wwwAuthHeaders, "Digest");
if (digestHeader == null) {
LOGGER.info("Can't handle 401 with Digest realm as WWW-Authenticate headers don't match");
return false;
}
Realm newDigestRealm = realm(realm).setUri(request.getUri()).setMethodName(request.getMethod()).setUsePreemptiveAuth(true).parseWWWAuthenticateHeader(digestHeader).build();
future.setRealm(newDigestRealm);
break;
case NTLM:
String ntlmHeader = getHeaderWithPrefix(wwwAuthHeaders, "NTLM");
if (ntlmHeader == null) {
LOGGER.info("Can't handle 401 with NTLM realm as WWW-Authenticate headers don't match");
return false;
}
ntlmChallenge(ntlmHeader, requestHeaders, realm, future);
Realm newNtlmRealm = realm(realm).setUsePreemptiveAuth(true).build();
future.setRealm(newNtlmRealm);
break;
case KERBEROS:
case SPNEGO:
if (getHeaderWithPrefix(wwwAuthHeaders, NEGOTIATE) == null) {
LOGGER.info("Can't handle 401 with Kerberos or Spnego realm as WWW-Authenticate headers don't match");
return false;
}
try {
kerberosChallenge(realm, request, requestHeaders);
} catch (SpnegoEngineException e) {
// FIXME
String ntlmHeader2 = getHeaderWithPrefix(wwwAuthHeaders, "NTLM");
if (ntlmHeader2 != null) {
LOGGER.warn("Kerberos/Spnego auth failed, proceeding with NTLM");
ntlmChallenge(ntlmHeader2, requestHeaders, realm, future);
Realm newNtlmRealm2 = realm(realm).setScheme(AuthScheme.NTLM).setUsePreemptiveAuth(true).build();
future.setRealm(newNtlmRealm2);
} else {
requestSender.abort(channel, future, e);
return false;
}
}
break;
default:
throw new IllegalStateException("Invalid Authentication scheme " + realm.getScheme());
}
final Request nextRequest = future.getCurrentRequest().toBuilder().setHeaders(requestHeaders).build();
LOGGER.debug("Sending authentication to {}", request.getUri());
if (future.isKeepAlive() && !HttpUtil.isTransferEncodingChunked(httpRequest) && !HttpUtil.isTransferEncodingChunked(response)) {
future.setReuseChannel(true);
requestSender.drainChannelAndExecuteNextRequest(channel, future, nextRequest);
} else {
channelManager.closeChannel(channel);
requestSender.sendNextRequest(nextRequest, future);
}
return true;
}
use of org.asynchttpclient.Request in project async-http-client by AsyncHttpClient.
the class HttpUtilsTest method testDefaultFollowRedirect.
@Test
public void testDefaultFollowRedirect() {
Request request = Dsl.get("http://stackoverflow.com/questions/1057564").setVirtualHost("example.com").build();
DefaultAsyncHttpClientConfig config = new DefaultAsyncHttpClientConfig.Builder().build();
boolean followRedirect = HttpUtils.followRedirect(config, request);
assertFalse(followRedirect, "Default value of redirect should be false");
}
use of org.asynchttpclient.Request in project async-http-client by AsyncHttpClient.
the class HttpUtilsTest method testGetFollowRedirectInConfig.
@Test
public void testGetFollowRedirectInConfig() {
Request request = Dsl.get("http://stackoverflow.com/questions/1057564").build();
DefaultAsyncHttpClientConfig config = new DefaultAsyncHttpClientConfig.Builder().setFollowRedirect(true).build();
boolean followRedirect = HttpUtils.followRedirect(config, request);
assertTrue(followRedirect, "Follow redirect should be equal to value specified in config when not specified in request");
}
use of org.asynchttpclient.Request in project async-http-client by AsyncHttpClient.
the class HttpUtilsTest method testGetFollowRedirectPriorityGivenToRequest.
@Test
public void testGetFollowRedirectPriorityGivenToRequest() {
Request request = Dsl.get("http://stackoverflow.com/questions/1057564").setFollowRedirect(false).build();
DefaultAsyncHttpClientConfig config = new DefaultAsyncHttpClientConfig.Builder().setFollowRedirect(true).build();
boolean followRedirect = HttpUtils.followRedirect(config, request);
assertFalse(followRedirect, "Follow redirect value set in request should be given priority");
}
use of org.asynchttpclient.Request in project async-http-client by AsyncHttpClient.
the class HttpUtilsTest method testGetFollowRedirectInRequest.
@Test
public void testGetFollowRedirectInRequest() {
Request request = Dsl.get("http://stackoverflow.com/questions/1057564").setFollowRedirect(true).build();
DefaultAsyncHttpClientConfig config = new DefaultAsyncHttpClientConfig.Builder().build();
boolean followRedirect = HttpUtils.followRedirect(config, request);
assertTrue(followRedirect, "Follow redirect must be true as set in the request");
}
Aggregations