36
36
import java .util .List ;
37
37
import java .util .Map ;
38
38
import org .glassfish .grizzly .Connection ;
39
+ import org .glassfish .grizzly .Grizzly ;
40
+ import org .glassfish .grizzly .attributes .Attribute ;
39
41
import org .glassfish .grizzly .filterchain .BaseFilter ;
40
42
import org .glassfish .grizzly .filterchain .FilterChainContext ;
41
43
import org .glassfish .grizzly .filterchain .FilterChainEvent ;
61
63
final class AsyncHttpClientFilter extends BaseFilter {
62
64
private final static Logger LOGGER = LoggerFactory .getLogger (AsyncHttpClientFilter .class );
63
65
66
+ private final static Attribute <Boolean > USED_CONNECTION =
67
+ Grizzly .DEFAULT_ATTRIBUTE_BUILDER .createAttribute (
68
+ AsyncHttpClientFilter .class .getName () + ".used-connection" );
69
+
64
70
// Lazy NTLM instance holder
65
71
private static class NTLM_INSTANCE_HOLDER {
66
72
private final static NTLMEngine ntlmEngine = new NTLMEngine ();
@@ -104,6 +110,13 @@ public NextAction handleEvent(final FilterChainContext ctx, final FilterChainEve
104
110
105
111
private boolean sendAsGrizzlyRequest (final HttpTransactionContext httpTxCtx , final FilterChainContext ctx ) throws IOException {
106
112
final Connection connection = ctx .getConnection ();
113
+
114
+ final boolean isUsedConnection = Boolean .TRUE .equals (USED_CONNECTION .get (connection ));
115
+ if (!isUsedConnection ) {
116
+ USED_CONNECTION .set (connection , Boolean .TRUE );
117
+ }
118
+
119
+
107
120
final Request ahcRequest = httpTxCtx .getAhcRequest ();
108
121
if (isUpgradeRequest (httpTxCtx .getAsyncHandler ()) && isWSRequest (httpTxCtx .requestUri )) {
109
122
httpTxCtx .isWSRequest = true ;
@@ -120,7 +133,7 @@ private boolean sendAsGrizzlyRequest(final HttpTransactionContext httpTxCtx, fin
120
133
// once the tunnel is established, sendAsGrizzlyRequest will
121
134
// be called again and we'll finally send the request over the tunnel
122
135
return establishConnectTunnel (proxy , httpTxCtx , uri , ctx );
123
- }
136
+ }
124
137
final HttpRequestPacket .Builder builder = HttpRequestPacket .builder ().protocol (Protocol .HTTP_1_1 ).method (method );
125
138
126
139
if (useProxy && !((secure || httpTxCtx .isWSRequest ) &&
@@ -164,9 +177,14 @@ private boolean sendAsGrizzlyRequest(final HttpTransactionContext httpTxCtx, fin
164
177
addHostHeaderIfNeeded (ahcRequest , uri , requestPacket );
165
178
addServiceHeaders (requestPacket );
166
179
addAcceptHeaders (requestPacket );
167
- addAuthorizationHeader (connection , getRealm (ahcRequest ), requestPacket );
180
+
181
+ final Realm realm = getRealm (ahcRequest );
182
+ addAuthorizationHeader (ahcRequest , requestPacket , realm ,
183
+ uri , proxy , isUsedConnection );
184
+
168
185
if (useProxy ) {
169
- addProxyHeaders (proxy , requestPacket );
186
+ addProxyHeaders (ahcRequest , requestPacket , realm , proxy ,
187
+ isUsedConnection , false );
170
188
}
171
189
172
190
ctx .notifyDownstream (new SSLSwitchingEvent (connection , secure ,
@@ -193,8 +211,10 @@ private boolean establishConnectTunnel(final ProxyServer proxy,
193
211
final Request request = httpCtx .getAhcRequest ();
194
212
addHostHeaderIfNeeded (request , uri , requestPacket );
195
213
addServiceHeaders (requestPacket );
196
- addAuthorizationHeader (connection , getRealm (request ), requestPacket );
197
- addProxyHeaders (proxy , requestPacket );
214
+
215
+ final Realm realm = getRealm (request );
216
+ addAuthorizationHeader (request , requestPacket , realm , uri , proxy , false );
217
+ addProxyHeaders (request , requestPacket , realm , proxy , false , true );
198
218
199
219
// turn off SSL, because CONNECT will be sent in plain mode
200
220
ctx .notifyDownstream (new SSLSwitchingEvent (connection , false ));
@@ -265,22 +285,57 @@ private boolean isPayloadAllowed(final Method method) {
265
285
return method .getPayloadExpectation () != Method .PayloadExpectation .NOT_ALLOWED ;
266
286
}
267
287
268
- private void addAuthorizationHeader (final Connection c , final Realm realm , final HttpRequestPacket requestPacket ) {
269
- if (realm != null && realm .getUsePreemptiveAuth ()) {
270
- final String authHeaderValue = generateAuthHeader (c , realm );
271
- if (authHeaderValue != null ) {
272
- requestPacket .addHeader (Header .Authorization , authHeaderValue );
288
+ private void addAuthorizationHeader (final Request req ,
289
+ final HttpRequestPacket requestPacket ,
290
+ final Realm realm ,
291
+ final Uri uri , ProxyServer proxy ,
292
+ final boolean isUsedConnection ) throws IOException {
293
+
294
+ if (!isUsedConnection ) {
295
+ final String conAuth =
296
+ AuthenticatorUtils .perConnectionAuthorizationHeader (
297
+ req , uri , proxy , realm );
298
+ if (conAuth != null ) {
299
+ requestPacket .addHeader (Header .Authorization , conAuth );
273
300
}
274
301
}
302
+
303
+ final String reqAuth = AuthenticatorUtils .perRequestAuthorizationHeader (
304
+ req , uri , realm );
305
+ if (reqAuth != null ) {
306
+ requestPacket .addHeader (Header .Authorization , reqAuth );
307
+ }
275
308
}
276
309
277
- private void addProxyHeaders (final ProxyServer proxy ,
278
- final HttpRequestPacket requestPacket ) {
310
+ private void addProxyHeaders (
311
+ final Request req ,
312
+ final HttpRequestPacket requestPacket ,
313
+ final Realm realm ,
314
+ final ProxyServer proxy ,
315
+ final boolean isUsedConnection ,
316
+ final boolean isConnect ) throws IOException {
279
317
280
318
setKeepAliveForHeader (Header .ProxyConnection , requestPacket );
319
+ setProxyAuthorizationHeader (req , requestPacket , proxy , realm ,
320
+ isUsedConnection , isConnect );
321
+ }
322
+
323
+ private void setProxyAuthorizationHeader (final Request req , final HttpRequestPacket requestPacket , final ProxyServer proxy , final Realm realm , final boolean isUsedConnection , final boolean isConnect ) throws IOException {
324
+ final String reqAuth = AuthenticatorUtils .perRequestProxyAuthorizationHeader (
325
+ req , realm , proxy , isConnect );
326
+
327
+ if (reqAuth != null ) {
328
+ requestPacket .setHeader (Header .ProxyAuthorization , reqAuth );
329
+ return ;
330
+ }
281
331
282
- if (proxy .getPrincipal () != null ) {
283
- requestPacket .setHeader (Header .ProxyAuthorization , AuthenticatorUtils .computeBasicAuthentication (proxy ));
332
+ if (!isUsedConnection ) {
333
+ final String conAuth =
334
+ AuthenticatorUtils .perConnectionProxyAuthorizationHeader (
335
+ req , proxy , isConnect );
336
+ if (conAuth != null ) {
337
+ requestPacket .setHeader (Header .ProxyAuthorization , conAuth );
338
+ }
284
339
}
285
340
}
286
341
0 commit comments