HTTPCLIENT-1131: HttpClient to authenticate preemptively using BASIC scheme if a userinfo attribute is specified in the request URI

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1180120 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2011-10-07 17:49:42 +00:00
parent 430c3d530e
commit 162f3d6d99
3 changed files with 45 additions and 0 deletions

View File

@ -1,5 +1,9 @@
Changes since 4.1.2
* [HTTPCLIENT-1131] HttpClient to authenticate preemptively using BASIC scheme if a userinfo
attribute is specified in the request URI.
Contributed by Oleg Kalnichevski <olegk at apache.org>
* [HTTPCLIENT-1116] ResponseCachingPolicy uses integers for sizes
Contributed by Greg Bowyer <gbowyer at fastmail.co.uk >

View File

@ -47,9 +47,12 @@ import org.apache.http.ProtocolVersion;
import org.apache.http.annotation.NotThreadSafe;
import org.apache.http.auth.AuthProtocolState;
import org.apache.http.auth.AuthScheme;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.AuthState;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.AuthenticationHandler;
import org.apache.http.client.AuthenticationStrategy;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.HttpRequestRetryHandler;
import org.apache.http.client.NonRepeatableRequestException;
import org.apache.http.client.RedirectException;
@ -74,6 +77,7 @@ import org.apache.http.conn.routing.HttpRouteDirector;
import org.apache.http.conn.routing.HttpRoutePlanner;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.conn.ConnectionShutdownException;
import org.apache.http.message.BasicHttpRequest;
import org.apache.http.params.HttpConnectionParams;
@ -478,6 +482,12 @@ public class DefaultRequestDirector implements RequestDirector {
break;
}
String userinfo = wrapper.getURI().getUserInfo();
if (userinfo != null) {
targetAuthState.setAuthScheme(new BasicScheme());
targetAuthState.setCredentials(new UsernamePasswordCredentials(userinfo));
}
// Reset headers on the request wrapper
wrapper.resetHeaders();

View File

@ -30,6 +30,7 @@ import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpException;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
@ -396,4 +397,34 @@ public class TestClientAuthentication extends BasicServerTestBase {
Assert.assertEquals(1, authStrategy.getCount());
}
@Test
public void testAuthenticationUserinfoInRequestSuccess() throws Exception {
this.localServer.register("*", new AuthHandler());
this.localServer.start();
HttpHost target = getServerHttp();
HttpGet httpget = new HttpGet("http://test:test@" + target.toHostString() + "/");
HttpResponse response = this.httpclient.execute(getServerHttp(), httpget);
HttpEntity entity = response.getEntity();
Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
Assert.assertNotNull(entity);
EntityUtils.consume(entity);
}
@Test
public void testAuthenticationUserinfoInRequestFailure() throws Exception {
this.localServer.register("*", new AuthHandler());
this.localServer.start();
HttpHost target = getServerHttp();
HttpGet httpget = new HttpGet("http://test:all-wrong@" + target.toHostString() + "/");
HttpResponse response = this.httpclient.execute(getServerHttp(), httpget);
HttpEntity entity = response.getEntity();
Assert.assertEquals(HttpStatus.SC_UNAUTHORIZED, response.getStatusLine().getStatusCode());
Assert.assertNotNull(entity);
EntityUtils.consume(entity);
}
}