Ensure backend response is correctly closed in case of a HTTP, i/o or runtime error

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1420391 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2012-12-11 20:40:27 +00:00
parent f643326e9e
commit 278e06df47
1 changed files with 77 additions and 56 deletions

View File

@ -262,10 +262,10 @@ public class CachingExec implements ClientExecChain {
if (entry == null) {
log.debug("Cache miss");
return handleCacheMiss(route, request, context, execAware);
}
} else {
return handleCacheHit(route, request, context, execAware, entry);
}
}
private CloseableHttpResponse handleCacheHit(
final HttpRoute route,
@ -290,13 +290,11 @@ public class CachingExec implements ClientExecChain {
log.debug("Cache entry not usable; calling backend");
return callBackend(route, request, context, execAware);
}
if (context != null) {
context.setAttribute(ClientContext.ROUTE, route);
context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, target);
context.setAttribute(ExecutionContext.HTTP_REQUEST, request);
context.setAttribute(ExecutionContext.HTTP_RESPONSE, out);
context.setAttribute(ExecutionContext.HTTP_REQ_SENT, Boolean.TRUE);
}
return out;
}
@ -568,9 +566,17 @@ public class CachingExec implements ClientExecChain {
log.trace("Calling the backend");
CloseableHttpResponse backendResponse = backend.execute(route, request, context, execAware);
try {
backendResponse.addHeader("Via", generateViaHeader(backendResponse));
return handleBackendResponse(route, request, context, execAware,
requestDate, getCurrentDate(), backendResponse);
} catch (IOException ex) {
backendResponse.close();
throw ex;
} catch (RuntimeException ex) {
backendResponse.close();
throw ex;
}
}
private boolean revalidationResponseIsTooOld(HttpResponse backendResponse,
@ -604,6 +610,7 @@ public class CachingExec implements ClientExecChain {
Date requestDate = getCurrentDate();
CloseableHttpResponse backendResponse = backend.execute(
route, conditionalRequest, context, execAware);
try {
Date responseDate = getCurrentDate();
backendResponse.addHeader("Via", generateViaHeader(backendResponse));
@ -617,6 +624,8 @@ public class CachingExec implements ClientExecChain {
Header resultEtagHeader = backendResponse.getFirstHeader(HeaderConstants.ETAG);
if (resultEtagHeader == null) {
log.warn("304 response did not contain ETag");
EntityUtils.consume(backendResponse.getEntity());
backendResponse.close();
return callBackend(route, request, context, execAware);
}
@ -624,6 +633,8 @@ public class CachingExec implements ClientExecChain {
Variant matchingVariant = variants.get(resultEtag);
if (matchingVariant == null) {
log.debug("304 response did not contain ETag matching one sent in If-None-Match");
EntityUtils.consume(backendResponse.getEntity());
backendResponse.close();
return callBackend(route, request, context, execAware);
}
@ -631,6 +642,7 @@ public class CachingExec implements ClientExecChain {
if (revalidationResponseIsTooOld(backendResponse, matchedEntry)) {
EntityUtils.consume(backendResponse.getEntity());
backendResponse.close();
return retryRequestUnconditionally(route, request, context, execAware, matchedEntry);
}
@ -639,6 +651,7 @@ public class CachingExec implements ClientExecChain {
HttpCacheEntry responseEntry = getUpdatedVariantEntry(
route.getTargetHost(), conditionalRequest, requestDate, responseDate,
backendResponse, matchingVariant, matchedEntry);
backendResponse.close();
HttpResponse resp = responseGenerator.generateResponse(responseEntry);
tryToUpdateVariantMap(route.getTargetHost(), request, matchingVariant);
@ -646,11 +659,17 @@ public class CachingExec implements ClientExecChain {
if (shouldSendNotModifiedResponse(request, responseEntry)) {
return responseGenerator.generateNotModifiedResponse(responseEntry);
}
return resp;
} catch (IOException ex) {
backendResponse.close();
throw ex;
} catch (RuntimeException ex) {
backendResponse.close();
throw ex;
}
}
private HttpResponse retryRequestUnconditionally(
private CloseableHttpResponse retryRequestUnconditionally(
final HttpRoute route,
final HttpRequestWrapper request,
final HttpClientContext context,
@ -666,15 +685,17 @@ public class CachingExec implements ClientExecChain {
final HttpRequestWrapper conditionalRequest,
final Date requestDate,
final Date responseDate,
final HttpResponse backendResponse,
final CloseableHttpResponse backendResponse,
final Variant matchingVariant,
final HttpCacheEntry matchedEntry) {
final HttpCacheEntry matchedEntry) throws IOException {
HttpCacheEntry responseEntry = matchedEntry;
try {
responseEntry = responseCache.updateVariantCacheEntry(target, conditionalRequest,
matchedEntry, backendResponse, requestDate, responseDate, matchingVariant.getCacheKey());
} catch (IOException ioe) {
log.warn("Could not update cache entry", ioe);
} finally {
backendResponse.close();
}
return responseEntry;
}