Skip to content

Commit bfef2b3

Browse files
committed
Port 68c9e7a on master
1 parent f5f9b4b commit bfef2b3

File tree

6 files changed

+178
-168
lines changed

6 files changed

+178
-168
lines changed

api/src/main/java/org/asynchttpclient/netty/request/NettyRequestFactoryBase.java

Lines changed: 0 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,13 @@
1313
*/
1414
package org.asynchttpclient.netty.request;
1515

16-
import static org.asynchttpclient.ntlm.NtlmUtils.getNTLM;
1716
import static org.asynchttpclient.util.AsyncHttpProviderUtils.getAuthority;
1817
import static org.asynchttpclient.util.AsyncHttpProviderUtils.getNonEmptyPath;
19-
import static org.asynchttpclient.util.AuthenticatorUtils.computeBasicAuthentication;
20-
import static org.asynchttpclient.util.AuthenticatorUtils.computeDigestAuthentication;
2118
import static org.asynchttpclient.util.HttpUtils.useProxyConnect;
2219
import static org.asynchttpclient.util.MiscUtils.isNonEmpty;
2320

24-
import java.util.List;
25-
2621
import org.asynchttpclient.AsyncHttpClientConfig;
27-
import org.asynchttpclient.Realm;
28-
import org.asynchttpclient.Realm.AuthScheme;
29-
import org.asynchttpclient.Request;
30-
import org.asynchttpclient.ntlm.NtlmEngine;
3122
import org.asynchttpclient.proxy.ProxyServer;
32-
import org.asynchttpclient.spnego.SpnegoEngine;
3323
import org.asynchttpclient.uri.Uri;
3424

3525
public abstract class NettyRequestFactoryBase {
@@ -40,29 +30,6 @@ public NettyRequestFactoryBase(AsyncHttpClientConfig config) {
4030
this.config = config;
4131
}
4232

43-
protected abstract List<String> getProxyAuthorizationHeader(Request request);
44-
45-
protected String firstRequestOnlyProxyAuthorizationHeader(Request request, ProxyServer proxyServer, boolean connect) {
46-
String proxyAuthorization = null;
47-
48-
if (connect) {
49-
List<String> auth = getProxyAuthorizationHeader(request);
50-
String ntlmHeader = getNTLM(auth);
51-
if (ntlmHeader != null) {
52-
proxyAuthorization = ntlmHeader;
53-
}
54-
55-
} else if (proxyServer != null && proxyServer.getPrincipal() != null && isNonEmpty(proxyServer.getNtlmDomain())) {
56-
List<String> auth = getProxyAuthorizationHeader(request);
57-
if (getNTLM(auth) == null) {
58-
String msg = NtlmEngine.INSTANCE.generateType1Msg();
59-
proxyAuthorization = "NTLM " + msg;
60-
}
61-
}
62-
63-
return proxyAuthorization;
64-
}
65-
6633
protected String requestUri(Uri uri, ProxyServer proxyServer, boolean connect) {
6734
if (connect)
6835
return getAuthority(uri);
@@ -79,95 +46,6 @@ else if (proxyServer != null && !useProxyConnect(uri))
7946
}
8047
}
8148

82-
protected String systematicProxyAuthorizationHeader(Request request, ProxyServer proxyServer, Realm realm, boolean connect) {
83-
84-
String proxyAuthorization = null;
85-
86-
if (!connect && proxyServer != null && proxyServer.getPrincipal() != null && proxyServer.getScheme() == AuthScheme.BASIC) {
87-
proxyAuthorization = computeBasicAuthentication(proxyServer);
88-
} else if (realm != null && realm.getUsePreemptiveAuth() && realm.isTargetProxy()) {
89-
90-
switch (realm.getScheme()) {
91-
case BASIC:
92-
proxyAuthorization = computeBasicAuthentication(realm);
93-
break;
94-
case DIGEST:
95-
if (isNonEmpty(realm.getNonce()))
96-
proxyAuthorization = computeDigestAuthentication(realm);
97-
break;
98-
case NTLM:
99-
case KERBEROS:
100-
case SPNEGO:
101-
// NTLM, KERBEROS and SPNEGO are only set on the first request,
102-
// see firstRequestOnlyAuthorizationHeader
103-
case NONE:
104-
break;
105-
default:
106-
throw new IllegalStateException("Invalid Authentication " + realm);
107-
}
108-
}
109-
110-
return proxyAuthorization;
111-
}
112-
113-
protected String firstRequestOnlyAuthorizationHeader(Request request, ProxyServer proxyServer, Realm realm) {
114-
String authorizationHeader = null;
115-
116-
if (realm != null && realm.getUsePreemptiveAuth()) {
117-
switch (realm.getScheme()) {
118-
case NTLM:
119-
String msg = NtlmEngine.INSTANCE.generateType1Msg();
120-
authorizationHeader = "NTLM " + msg;
121-
break;
122-
case KERBEROS:
123-
case SPNEGO:
124-
String host;
125-
if (proxyServer != null)
126-
host = proxyServer.getHost();
127-
else if (request.getVirtualHost() != null)
128-
host = request.getVirtualHost();
129-
else
130-
host = request.getUri().getHost();
131-
132-
authorizationHeader = "Negotiate " + SpnegoEngine.instance().generateToken(host);
133-
break;
134-
default:
135-
break;
136-
}
137-
}
138-
139-
return authorizationHeader;
140-
}
141-
142-
protected String systematicAuthorizationHeader(Request request, Realm realm) {
143-
144-
String authorizationHeader = null;
145-
146-
if (realm != null && realm.getUsePreemptiveAuth()) {
147-
148-
switch (realm.getScheme()) {
149-
case BASIC:
150-
authorizationHeader = computeBasicAuthentication(realm);
151-
break;
152-
case DIGEST:
153-
if (isNonEmpty(realm.getNonce()))
154-
authorizationHeader = computeDigestAuthentication(realm);
155-
break;
156-
case NTLM:
157-
case KERBEROS:
158-
case SPNEGO:
159-
// NTLM, KERBEROS and SPNEGO are only set on the first request,
160-
// see firstRequestOnlyAuthorizationHeader
161-
case NONE:
162-
break;
163-
default:
164-
throw new IllegalStateException("Invalid Authentication " + realm);
165-
}
166-
}
167-
168-
return authorizationHeader;
169-
}
170-
17149
protected String connectionHeader(boolean allowConnectionPooling, boolean http11) {
17250
if (allowConnectionPooling)
17351
return "keep-alive";

api/src/main/java/org/asynchttpclient/util/AuthenticatorUtils.java

Lines changed: 124 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,25 @@
1313
package org.asynchttpclient.util;
1414

1515
import static java.nio.charset.StandardCharsets.ISO_8859_1;
16+
import static org.asynchttpclient.ntlm.NtlmUtils.getNTLM;
1617
import static org.asynchttpclient.util.AsyncHttpProviderUtils.getNonEmptyPath;
1718
import static org.asynchttpclient.util.MiscUtils.isNonEmpty;
1819

1920
import java.nio.charset.Charset;
21+
import java.util.List;
2022

2123
import org.asynchttpclient.Realm;
24+
import org.asynchttpclient.Request;
25+
import org.asynchttpclient.Realm.AuthScheme;
26+
import org.asynchttpclient.ntlm.NtlmEngine;
2227
import org.asynchttpclient.proxy.ProxyServer;
28+
import org.asynchttpclient.spnego.SpnegoEngine;
2329
import org.asynchttpclient.uri.Uri;
2430

2531
public final class AuthenticatorUtils {
2632

33+
private static final String PROXY_AUTHORIZATION_HEADER = "Proxy-Authorization";
34+
2735
public static String computeBasicAuthentication(Realm realm) {
2836
return computeBasicAuthentication(realm.getPrincipal(), realm.getPassword(), realm.getCharset());
2937
}
@@ -40,7 +48,7 @@ private static String computeBasicAuthentication(String principal, String passwo
4048
public static String computeRealmURI(Realm realm) {
4149
return computeRealmURI(realm.getUri(), realm.isUseAbsoluteURI(), realm.isOmitQuery());
4250
}
43-
51+
4452
public static String computeRealmURI(Uri uri, boolean useAbsoluteURI, boolean omitQuery) {
4553
if (useAbsoluteURI) {
4654
return omitQuery && MiscUtils.isNonEmpty(uri.getQuery()) ? uri.withNewQuery(null).toUrl() : uri.toUrl();
@@ -50,7 +58,7 @@ public static String computeRealmURI(Uri uri, boolean useAbsoluteURI, boolean om
5058
}
5159
}
5260

53-
public static String computeDigestAuthentication(Realm realm) {
61+
private static String computeDigestAuthentication(Realm realm) {
5462

5563
StringBuilder builder = new StringBuilder().append("Digest ");
5664
append(builder, "username", realm.getPrincipal(), true);
@@ -86,4 +94,118 @@ private static StringBuilder append(StringBuilder builder, String name, String v
8694

8795
return builder.append(", ");
8896
}
97+
98+
private static List<String> getProxyAuthorizationHeader(Request request) {
99+
return request.getHeaders().get(PROXY_AUTHORIZATION_HEADER);
100+
}
101+
102+
public static String perConnectionProxyAuthorizationHeader(Request request, ProxyServer proxyServer, boolean connect) {
103+
String proxyAuthorization = null;
104+
105+
if (connect) {
106+
List<String> auth = getProxyAuthorizationHeader(request);
107+
String ntlmHeader = getNTLM(auth);
108+
if (ntlmHeader != null) {
109+
proxyAuthorization = ntlmHeader;
110+
}
111+
112+
} else if (proxyServer != null && proxyServer.getPrincipal() != null && isNonEmpty(proxyServer.getNtlmDomain())) {
113+
List<String> auth = getProxyAuthorizationHeader(request);
114+
if (getNTLM(auth) == null) {
115+
String msg = NtlmEngine.INSTANCE.generateType1Msg();
116+
proxyAuthorization = "NTLM " + msg;
117+
}
118+
}
119+
120+
return proxyAuthorization;
121+
}
122+
123+
public static String perRequestProxyAuthorizationHeader(Request request, ProxyServer proxyServer, Realm realm, boolean connect) {
124+
125+
String proxyAuthorization = null;
126+
127+
if (!connect && proxyServer != null && proxyServer.getPrincipal() != null && proxyServer.getScheme() == AuthScheme.BASIC) {
128+
proxyAuthorization = computeBasicAuthentication(proxyServer);
129+
} else if (realm != null && realm.getUsePreemptiveAuth() && realm.isTargetProxy()) {
130+
131+
switch (realm.getScheme()) {
132+
case BASIC:
133+
proxyAuthorization = computeBasicAuthentication(realm);
134+
break;
135+
case DIGEST:
136+
if (isNonEmpty(realm.getNonce()))
137+
proxyAuthorization = computeDigestAuthentication(realm);
138+
break;
139+
case NTLM:
140+
case KERBEROS:
141+
case SPNEGO:
142+
// NTLM, KERBEROS and SPNEGO are only set on the first request,
143+
// see firstRequestOnlyAuthorizationHeader
144+
case NONE:
145+
break;
146+
default:
147+
throw new IllegalStateException("Invalid Authentication " + realm);
148+
}
149+
}
150+
151+
return proxyAuthorization;
152+
}
153+
154+
public static String perConnectionAuthorizationHeader(Request request, ProxyServer proxyServer, Realm realm) {
155+
String authorizationHeader = null;
156+
157+
if (realm != null && realm.getUsePreemptiveAuth()) {
158+
switch (realm.getScheme()) {
159+
case NTLM:
160+
String msg = NtlmEngine.INSTANCE.generateType1Msg();
161+
authorizationHeader = "NTLM " + msg;
162+
break;
163+
case KERBEROS:
164+
case SPNEGO:
165+
String host;
166+
if (proxyServer != null)
167+
host = proxyServer.getHost();
168+
else if (request.getVirtualHost() != null)
169+
host = request.getVirtualHost();
170+
else
171+
host = request.getUri().getHost();
172+
173+
authorizationHeader = "Negotiate " + SpnegoEngine.instance().generateToken(host);
174+
break;
175+
default:
176+
break;
177+
}
178+
}
179+
180+
return authorizationHeader;
181+
}
182+
183+
public static String perRequestAuthorizationHeader(Request request, Realm realm) {
184+
185+
String authorizationHeader = null;
186+
187+
if (realm != null && realm.getUsePreemptiveAuth()) {
188+
189+
switch (realm.getScheme()) {
190+
case BASIC:
191+
authorizationHeader = computeBasicAuthentication(realm);
192+
break;
193+
case DIGEST:
194+
if (isNonEmpty(realm.getNonce()))
195+
authorizationHeader = computeDigestAuthentication(realm);
196+
break;
197+
case NTLM:
198+
case KERBEROS:
199+
case SPNEGO:
200+
// NTLM, KERBEROS and SPNEGO are only set on the first request,
201+
// see firstRequestOnlyAuthorizationHeader
202+
case NONE:
203+
break;
204+
default:
205+
throw new IllegalStateException("Invalid Authentication " + realm);
206+
}
207+
}
208+
209+
return authorizationHeader;
210+
}
89211
}

providers/netty3/src/main/java/org/asynchttpclient/netty/request/NettyRequestFactory.java

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,27 @@
1616
import static org.asynchttpclient.util.AsyncHttpProviderUtils.DEFAULT_CHARSET;
1717
import static org.asynchttpclient.util.AsyncHttpProviderUtils.hostHeader;
1818
import static org.asynchttpclient.util.AsyncHttpProviderUtils.urlEncodeFormParams;
19+
import static org.asynchttpclient.util.AuthenticatorUtils.perRequestAuthorizationHeader;
20+
import static org.asynchttpclient.util.AuthenticatorUtils.perRequestProxyAuthorizationHeader;
1921
import static org.asynchttpclient.util.HttpUtils.isSecure;
2022
import static org.asynchttpclient.util.HttpUtils.isWebSocket;
2123
import static org.asynchttpclient.util.MiscUtils.isNonEmpty;
2224
import static org.asynchttpclient.ws.WebSocketUtils.getKey;
23-
import static org.jboss.netty.handler.codec.http.HttpHeaders.Names.*;
25+
import static org.jboss.netty.handler.codec.http.HttpHeaders.Names.ACCEPT;
26+
import static org.jboss.netty.handler.codec.http.HttpHeaders.Names.ACCEPT_ENCODING;
27+
import static org.jboss.netty.handler.codec.http.HttpHeaders.Names.AUTHORIZATION;
28+
import static org.jboss.netty.handler.codec.http.HttpHeaders.Names.CONNECTION;
29+
import static org.jboss.netty.handler.codec.http.HttpHeaders.Names.CONTENT_LENGTH;
30+
import static org.jboss.netty.handler.codec.http.HttpHeaders.Names.CONTENT_TYPE;
31+
import static org.jboss.netty.handler.codec.http.HttpHeaders.Names.COOKIE;
32+
import static org.jboss.netty.handler.codec.http.HttpHeaders.Names.HOST;
33+
import static org.jboss.netty.handler.codec.http.HttpHeaders.Names.ORIGIN;
34+
import static org.jboss.netty.handler.codec.http.HttpHeaders.Names.PROXY_AUTHORIZATION;
35+
import static org.jboss.netty.handler.codec.http.HttpHeaders.Names.SEC_WEBSOCKET_KEY;
36+
import static org.jboss.netty.handler.codec.http.HttpHeaders.Names.SEC_WEBSOCKET_VERSION;
37+
import static org.jboss.netty.handler.codec.http.HttpHeaders.Names.TRANSFER_ENCODING;
38+
import static org.jboss.netty.handler.codec.http.HttpHeaders.Names.UPGRADE;
39+
import static org.jboss.netty.handler.codec.http.HttpHeaders.Names.USER_AGENT;
2440

2541
import java.nio.charset.Charset;
2642
import java.util.List;
@@ -60,10 +76,6 @@ public NettyRequestFactory(AsyncHttpClientConfig config) {
6076
super(config);
6177
}
6278

63-
protected List<String> getProxyAuthorizationHeader(Request request) {
64-
return request.getHeaders().get(PROXY_AUTHORIZATION);
65-
}
66-
6779
private NettyBody body(Request request, boolean connect) {
6880
NettyBody nettyBody = null;
6981
if (!connect) {
@@ -75,7 +87,7 @@ private NettyBody body(Request request, boolean connect) {
7587

7688
else if (request.getCompositeByteData() != null)
7789
nettyBody = new NettyCompositeByteArrayBody(request.getCompositeByteData());
78-
90+
7991
else if (request.getStringData() != null)
8092
nettyBody = new NettyByteBufferBody(StringUtils.charSequence2ByteBuffer(request.getStringData(), bodyCharset));
8193

@@ -101,8 +113,7 @@ else if (request.getFile() != null)
101113

102114
else if (request.getBodyGenerator() instanceof FileBodyGenerator) {
103115
FileBodyGenerator fileBodyGenerator = (FileBodyGenerator) request.getBodyGenerator();
104-
nettyBody = new NettyFileBody(fileBodyGenerator.getFile(), fileBodyGenerator.getRegionSeek(),
105-
fileBodyGenerator.getRegionLength(), config);
116+
nettyBody = new NettyFileBody(fileBodyGenerator.getFile(), fileBodyGenerator.getRegionSeek(), fileBodyGenerator.getRegionLength(), config);
106117

107118
} else if (request.getBodyGenerator() instanceof InputStreamBodyGenerator)
108119
nettyBody = new NettyInputStreamBody(InputStreamBodyGenerator.class.cast(request.getBodyGenerator()).getInputStream(), config);
@@ -119,12 +130,12 @@ public void addAuthorizationHeader(HttpHeaders headers, String authorizationHead
119130
// don't override authorization but append
120131
headers.add(AUTHORIZATION, authorizationHeader);
121132
}
122-
133+
123134
public void setProxyAuthorizationHeader(HttpHeaders headers, String proxyAuthorizationHeader) {
124135
if (proxyAuthorizationHeader != null)
125136
headers.set(PROXY_AUTHORIZATION, proxyAuthorizationHeader);
126137
}
127-
138+
128139
public NettyRequest newNettyRequest(Request request, boolean forceConnect, ProxyServer proxyServer) {
129140

130141
Uri uri = request.getUri();
@@ -199,9 +210,9 @@ public NettyRequest newNettyRequest(Request request, boolean forceConnect, Proxy
199210
Realm realm = request.getRealm() != null ? request.getRealm() : config.getRealm();
200211

201212
// don't override authorization but append
202-
addAuthorizationHeader(headers, systematicAuthorizationHeader(request, realm));
213+
addAuthorizationHeader(headers, perRequestAuthorizationHeader(request, realm));
203214

204-
setProxyAuthorizationHeader(headers, systematicProxyAuthorizationHeader(request, proxyServer, realm, connect));
215+
setProxyAuthorizationHeader(headers, perRequestProxyAuthorizationHeader(request, proxyServer, realm, connect));
205216

206217
// Add default accept headers
207218
if (!headers.contains(ACCEPT))

0 commit comments

Comments
 (0)