hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1180120 - in /httpcomponents/httpclient/trunk: RELEASE_NOTES.txt httpclient/src/main/java/org/apache/http/impl/client/DefaultRequestDirector.java httpclient/src/test/java/org/apache/http/impl/client/TestClientAuthentication.java
Date Fri, 07 Oct 2011 17:49:42 GMT
Author: olegk
Date: Fri Oct  7 17:49:42 2011
New Revision: 1180120

URL: http://svn.apache.org/viewvc?rev=1180120&view=rev
Log:
HTTPCLIENT-1131: HttpClient to authenticate preemptively using BASIC scheme if a userinfo
attribute is specified in the request URI

Modified:
    httpcomponents/httpclient/trunk/RELEASE_NOTES.txt
    httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/DefaultRequestDirector.java
    httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestClientAuthentication.java

Modified: httpcomponents/httpclient/trunk/RELEASE_NOTES.txt
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/RELEASE_NOTES.txt?rev=1180120&r1=1180119&r2=1180120&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/RELEASE_NOTES.txt (original)
+++ httpcomponents/httpclient/trunk/RELEASE_NOTES.txt Fri Oct  7 17:49:42 2011
@@ -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 >
 

Modified: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/DefaultRequestDirector.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/DefaultRequestDirector.java?rev=1180120&r1=1180119&r2=1180120&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/DefaultRequestDirector.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/DefaultRequestDirector.java
Fri Oct  7 17:49:42 2011
@@ -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.Http
 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 impl
                     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();
 

Modified: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestClientAuthentication.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestClientAuthentication.java?rev=1180120&r1=1180119&r2=1180120&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestClientAuthentication.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestClientAuthentication.java
Fri Oct  7 17:49:42 2011
@@ -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 ex
         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);
+    }
+
 }



Mime
View raw message