Skip to content
This repository was archived by the owner on May 28, 2018. It is now read-only.

Commit 1d14eb5

Browse files
author
alessandro.gherardi
committed
Patch for client connection leak when using digest authentication
1 parent 7d4c8b0 commit 1d14eb5

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

core-client/src/main/java/org/glassfish/jersey/client/authentication/BasicAuthenticator.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040

4141
package org.glassfish.jersey.client.authentication;
4242

43+
import java.io.IOException;
44+
4345
import javax.ws.rs.client.ClientRequestContext;
4446
import javax.ws.rs.client.ClientResponseContext;
4547
import javax.ws.rs.core.HttpHeaders;
@@ -113,7 +115,8 @@ public void filterRequest(ClientRequestContext request) throws RequestAuthentica
113115
* new request was done with digest authentication information and authentication was successful.
114116
* @throws ResponseAuthenticationException in case that basic credentials missing or are in invalid format
115117
*/
116-
public boolean filterResponseAndAuthenticate(ClientRequestContext request, ClientResponseContext response) {
118+
public boolean filterResponseAndAuthenticate(ClientRequestContext request, ClientResponseContext response)
119+
throws IOException {
117120
final String authenticate = response.getHeaders().getFirst(HttpHeaders.WWW_AUTHENTICATE);
118121
if (authenticate != null && authenticate.trim().toUpperCase().startsWith("BASIC")) {
119122
HttpAuthenticationFilter.Credentials credentials = HttpAuthenticationFilter

core-client/src/main/java/org/glassfish/jersey/client/authentication/HttpAuthenticationFilter.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,10 +292,20 @@ private void updateCache(ClientRequestContext request, boolean success, Type ope
292292
* @param newAuthorizationHeader {@code Authorization} header that should be added to the new request.
293293
* @return {@code true} is the authentication was successful ({@code true} if 401 response code was not returned;
294294
* {@code false} otherwise).
295+
* @throws IOException
295296
*/
296-
static boolean repeatRequest(ClientRequestContext request, ClientResponseContext response, String newAuthorizationHeader) {
297-
Client client = request.getClient();
297+
static boolean repeatRequest(ClientRequestContext request, ClientResponseContext response, String newAuthorizationHeader)
298+
throws IOException{
299+
// If the failed response has an entity stream, close it. We must do this to avoid leaking a connection
300+
// when we replace the entity stream of the failed response with that of the repeated response (see below).
301+
// Notice that by closing the entity stream before sending the repeated request we allow the connection allocated
302+
// to the failed request to be reused, if possible, for the repeated request.
303+
if (response.hasEntity()) {
304+
response.getEntityStream().close();
305+
response.setEntityStream(null);
306+
}
298307

308+
Client client = request.getClient();
299309
String method = request.getMethod();
300310
MediaType mediaType = request.getMediaType();
301311
URI lUri = request.getUri();

0 commit comments

Comments
 (0)