Skip to content

Commit d2a85aa

Browse files
author
Stephane Landelle
committed
Merge pull request AsyncHttpClient#514 from elakito/1.8.x
511: 1.8.x GrizzlyAsyncHttpProvider not honoring usePreemtiveAuth
2 parents 6901b9f + 9ad209b commit d2a85aa

File tree

2 files changed

+65
-3
lines changed

2 files changed

+65
-3
lines changed

src/main/java/com/ning/http/client/providers/grizzly/GrizzlyAsyncHttpProvider.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@
134134
import com.ning.http.client.filter.FilterContext;
135135
import com.ning.http.client.filter.ResponseFilter;
136136
import com.ning.http.client.listener.TransferCompletionHandler;
137+
import com.ning.http.client.ntlm.NTLMEngine;
137138
import com.ning.http.client.websocket.WebSocket;
138139
import com.ning.http.client.websocket.WebSocketByteListener;
139140
import com.ning.http.client.websocket.WebSocketCloseCodeReasonListener;
@@ -164,6 +165,7 @@ public class GrizzlyAsyncHttpProvider implements AsyncHttpProvider {
164165
}
165166
private static final Attribute<HttpTransactionContext> REQUEST_STATE_ATTR =
166167
Grizzly.DEFAULT_ATTRIBUTE_BUILDER.createAttribute(HttpTransactionContext.class.getName());
168+
private final static NTLMEngine ntlmEngine = new NTLMEngine();
167169

168170
private final BodyHandlerFactory bodyHandlerFactory = new BodyHandlerFactory();
169171

@@ -905,6 +907,7 @@ private boolean sendAsGrizzlyRequest(final Request request,
905907
}
906908
addHeaders(request, requestPacket);
907909
addCookies(request, requestPacket);
910+
addAuthorizationHeader(request, requestPacket);
908911

909912
if (useProxy) {
910913
if (!requestPacket.getHeaders().contains(Header.ProxyConnection)) {
@@ -926,6 +929,36 @@ private boolean sendAsGrizzlyRequest(final Request request,
926929

927930
}
928931

932+
private void addAuthorizationHeader(final Request request, final HttpRequestPacket requestPacket) {
933+
Realm realm = request.getRealm();
934+
if (realm == null) {
935+
realm = config.getRealm();
936+
}
937+
if (realm != null && realm.getUsePreemptiveAuth()) {
938+
final String authHeaderValue = generateAuthHeader(realm);
939+
if (authHeaderValue != null) {
940+
requestPacket.addHeader(Header.Authorization, authHeaderValue);
941+
}
942+
}
943+
}
944+
945+
private String generateAuthHeader(final Realm realm) {
946+
try {
947+
switch (realm.getAuthScheme()) {
948+
case BASIC:
949+
return AuthenticatorUtils.computeBasicAuthentication(realm);
950+
case DIGEST:
951+
return AuthenticatorUtils.computeDigestAuthentication(realm);
952+
case NTLM:
953+
return ntlmEngine.generateType1Msg("NTLM " + realm.getNtlmDomain(), realm.getNtlmHost());
954+
default:
955+
return null;
956+
}
957+
} catch (Exception e) {
958+
throw new RuntimeException(e);
959+
}
960+
}
961+
929962

930963
private boolean isUpgradeRequest(final AsyncHandler handler) {
931964
return (handler instanceof UpgradeHandler);

src/test/java/com/ning/http/client/async/BasicAuthTest.java

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.ning.http.client.HttpResponseHeaders;
2323
import com.ning.http.client.HttpResponseStatus;
2424
import com.ning.http.client.Realm;
25+
import com.ning.http.client.Realm.AuthScheme;
2526
import com.ning.http.client.Response;
2627
import com.ning.http.client.generators.InputStreamBodyGenerator;
2728

@@ -72,6 +73,8 @@ public abstract class BasicAuthTest extends AbstractBasicTest {
7273
protected final static String admin = "admin";
7374

7475
private Server server2;
76+
private Server serverNoAuth;
77+
private int portNoAuth;
7578

7679
@BeforeClass(alwaysRun = true)
7780
@Override
@@ -201,6 +204,24 @@ private void stopSecondServer() throws Exception {
201204
server2.stop();
202205
}
203206

207+
private void setUpServerNoAuth() throws Exception {
208+
serverNoAuth = new Server();
209+
portNoAuth = findFreePort();
210+
211+
Connector listener = new SelectChannelConnector();
212+
listener.setHost("127.0.0.1");
213+
listener.setPort(portNoAuth);
214+
215+
serverNoAuth.addConnector(listener);
216+
217+
serverNoAuth.setHandler(new SimpleHandler());
218+
serverNoAuth.start();
219+
}
220+
221+
private void stopServerNoAuth() throws Exception {
222+
serverNoAuth.stop();
223+
}
224+
204225
private class RedirectHandler extends AbstractHandler {
205226

206227
public void handle(String s, Request r, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
@@ -231,7 +252,7 @@ public void handle(String s, Request r, HttpServletRequest request, HttpServletR
231252

232253
private class SimpleHandler extends AbstractHandler {
233254
public void handle(String s, Request r, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
234-
255+
235256
if (request.getHeader("X-401") != null) {
236257
response.setStatus(401);
237258
response.getOutputStream().flush();
@@ -307,6 +328,10 @@ protected String getTargetUrl2() {
307328
return "http://127.0.0.1:" + port2 + "/uff";
308329
}
309330

331+
protected String getTargetUrlNoAuth() {
332+
return "http://127.0.0.1:" + portNoAuth + "/";
333+
}
334+
310335
@Test(groups = { "standalone", "default_provider" })
311336
public void basic401Test() throws IOException, ExecutionException, TimeoutException, InterruptedException {
312337
AsyncHttpClient client = getAsyncHttpClient(null);
@@ -351,10 +376,13 @@ public Integer onCompleted() throws Exception {
351376
}
352377

353378
@Test(groups = { "standalone", "default_provider" })
354-
public void basicAuthTestPreemtiveTest() throws IOException, ExecutionException, TimeoutException, InterruptedException {
379+
public void basicAuthTestPreemtiveTest() throws Exception, ExecutionException, TimeoutException, InterruptedException {
355380
AsyncHttpClient client = getAsyncHttpClient(null);
356381
try {
357-
AsyncHttpClient.BoundRequestBuilder r = client.prepareGet(getTargetUrl()).setRealm((new Realm.RealmBuilder()).setPrincipal(user).setPassword(admin).setUsePreemptiveAuth(true).build());
382+
setUpServerNoAuth();
383+
384+
AsyncHttpClient.BoundRequestBuilder r = client.prepareGet(getTargetUrlNoAuth())
385+
.setRealm((new Realm.RealmBuilder()).setScheme(AuthScheme.BASIC).setPrincipal(user).setPassword(admin).setUsePreemptiveAuth(true).build());
358386

359387
Future<Response> f = r.execute();
360388
Response resp = f.get(3, TimeUnit.SECONDS);
@@ -363,6 +391,7 @@ public void basicAuthTestPreemtiveTest() throws IOException, ExecutionException,
363391
assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK);
364392
} finally {
365393
client.close();
394+
stopServerNoAuth();
366395
}
367396
}
368397

0 commit comments

Comments
 (0)