@@ -92,20 +92,11 @@ private String hostHeader(Request request, Uri uri) {
92
92
return request .getVirtualHost () != null || uri .getPort () == -1 ? host : host + ":" + uri .getPort ();
93
93
}
94
94
95
- private String authorizationHeader (Request request , Uri uri , ProxyServer proxyServer , Realm realm ) throws IOException {
96
-
95
+ public String firstRequestOnlyAuthorizationHeader (Request request , Uri uri , ProxyServer proxyServer , Realm realm ) throws IOException {
97
96
String authorizationHeader = null ;
98
97
99
98
if (realm != null && realm .getUsePreemptiveAuth ()) {
100
-
101
99
switch (realm .getAuthScheme ()) {
102
- case BASIC :
103
- authorizationHeader = computeBasicAuthentication (realm );
104
- break ;
105
- case DIGEST :
106
- if (isNonEmpty (realm .getNonce ()))
107
- authorizationHeader = computeDigestAuthentication (realm );
108
- break ;
109
100
case NTLM :
110
101
String domain ;
111
102
if (proxyServer != null && proxyServer .getNtlmDomain () != null ) {
@@ -122,7 +113,6 @@ private String authorizationHeader(Request request, Uri uri, ProxyServer proxySe
122
113
break ;
123
114
case KERBEROS :
124
115
case SPNEGO :
125
-
126
116
String host ;
127
117
if (proxyServer != null )
128
118
host = proxyServer .getHost ();
@@ -137,6 +127,32 @@ else if (request.getVirtualHost() != null)
137
127
throw new IOException (e );
138
128
}
139
129
break ;
130
+ default :
131
+ break ;
132
+ }
133
+ }
134
+
135
+ return authorizationHeader ;
136
+ }
137
+
138
+ private String systematicAuthorizationHeader (Request request , Uri uri , ProxyServer proxyServer , Realm realm ) {
139
+
140
+ String authorizationHeader = null ;
141
+
142
+ if (realm != null && realm .getUsePreemptiveAuth ()) {
143
+
144
+ switch (realm .getAuthScheme ()) {
145
+ case BASIC :
146
+ authorizationHeader = computeBasicAuthentication (realm );
147
+ break ;
148
+ case DIGEST :
149
+ if (isNonEmpty (realm .getNonce ()))
150
+ authorizationHeader = computeDigestAuthentication (realm );
151
+ break ;
152
+ case NTLM :
153
+ case KERBEROS :
154
+ case SPNEGO :
155
+ // NTLM, KERBEROS and SPNEGO are only set on the first request, see firstRequestOnlyAuthorizationHeader
140
156
case NONE :
141
157
break ;
142
158
default :
@@ -147,8 +163,7 @@ else if (request.getVirtualHost() != null)
147
163
return authorizationHeader ;
148
164
}
149
165
150
- private String proxyAuthorizationHeader (Request request , ProxyServer proxyServer , HttpMethod method ) throws IOException {
151
-
166
+ public String firstRequestOnlyProxyAuthorizationHeader (Request request , ProxyServer proxyServer , HttpMethod method ) throws IOException {
152
167
String proxyAuthorization = null ;
153
168
154
169
if (method == HttpMethod .CONNECT ) {
@@ -157,26 +172,31 @@ private String proxyAuthorizationHeader(Request request, ProxyServer proxyServer
157
172
proxyAuthorization = auth .get (0 );
158
173
}
159
174
160
- } else if (proxyServer != null && proxyServer .getPrincipal () != null ) {
161
- if (isNonEmpty (proxyServer .getNtlmDomain ())) {
162
- List <String > auth = request .getHeaders ().get (HttpHeaders .Names .PROXY_AUTHORIZATION );
163
- if (!isNTLM (auth )) {
164
- try {
165
- String msg = NTLMEngine .INSTANCE .generateType1Msg (proxyServer .getNtlmDomain (), proxyServer .getHost ());
166
- proxyAuthorization = "NTLM " + msg ;
167
- } catch (NTLMEngineException e ) {
168
- IOException ie = new IOException ();
169
- ie .initCause (e );
170
- throw ie ;
171
- }
175
+ } else if (proxyServer != null && proxyServer .getPrincipal () != null && isNonEmpty (proxyServer .getNtlmDomain ())) {
176
+ List <String > auth = request .getHeaders ().get (HttpHeaders .Names .PROXY_AUTHORIZATION );
177
+ if (!isNTLM (auth )) {
178
+ try {
179
+ String msg = NTLMEngine .INSTANCE .generateType1Msg (proxyServer .getNtlmDomain (), proxyServer .getHost ());
180
+ proxyAuthorization = "NTLM " + msg ;
181
+ } catch (NTLMEngineException e ) {
182
+ throw new IOException (e );
172
183
}
173
- } else {
174
- proxyAuthorization = computeBasicAuthentication (proxyServer );
175
184
}
176
185
}
177
186
178
187
return proxyAuthorization ;
179
188
}
189
+
190
+ private String systematicProxyAuthorizationHeader (Request request , ProxyServer proxyServer , HttpMethod method ) {
191
+
192
+ String proxyAuthorization = null ;
193
+
194
+ if (method != HttpMethod .CONNECT && proxyServer != null && proxyServer .getPrincipal () != null && !isNonEmpty (proxyServer .getNtlmDomain ())) {
195
+ proxyAuthorization = computeBasicAuthentication (proxyServer );
196
+ }
197
+
198
+ return proxyAuthorization ;
199
+ }
180
200
181
201
private byte [] computeBodyFromParams (List <Param > params , Charset bodyCharset ) {
182
202
@@ -235,6 +255,17 @@ else if (request.getBodyGenerator() != null)
235
255
return nettyBody ;
236
256
}
237
257
258
+ public void addAuthorizationHeader (HttpHeaders headers , String authorizationHeader ) {
259
+ if (authorizationHeader != null )
260
+ // don't override authorization but append
261
+ headers .add (HttpHeaders .Names .AUTHORIZATION , authorizationHeader );
262
+ }
263
+
264
+ public void setProxyAuthorizationHeader (HttpHeaders headers , String proxyAuthorizationHeader ) {
265
+ if (proxyAuthorizationHeader != null )
266
+ headers .set (HttpHeaders .Names .PROXY_AUTHORIZATION , proxyAuthorizationHeader );
267
+ }
268
+
238
269
public NettyRequest newNettyRequest (Request request , Uri uri , boolean forceConnect , ProxyServer proxyServer )
239
270
throws IOException {
240
271
@@ -301,14 +332,11 @@ public NettyRequest newNettyRequest(Request request, Uri uri, boolean forceConne
301
332
headers .set (HttpHeaders .Names .HOST , hostHeader (request , uri ));
302
333
303
334
Realm realm = request .getRealm () != null ? request .getRealm () : config .getRealm ();
304
- String authorizationHeader = authorizationHeader (request , uri , proxyServer , realm );
305
- if (authorizationHeader != null )
306
- // don't override authorization but append
307
- headers .add (HttpHeaders .Names .AUTHORIZATION , authorizationHeader );
308
335
309
- String proxyAuthorizationHeader = proxyAuthorizationHeader (request , proxyServer , method );
310
- if (proxyAuthorizationHeader != null )
311
- headers .set (HttpHeaders .Names .PROXY_AUTHORIZATION , proxyAuthorizationHeader );
336
+ // don't override authorization but append
337
+ addAuthorizationHeader (headers , systematicAuthorizationHeader (request , uri , proxyServer , realm ));
338
+
339
+ setProxyAuthorizationHeader (headers , systematicProxyAuthorizationHeader (request , proxyServer , method ));
312
340
313
341
// Add default accept headers
314
342
if (!headers .contains (HttpHeaders .Names .ACCEPT ))
0 commit comments