Skip to content

Commit

Permalink
make sure http client is closed ref #2114
Browse files Browse the repository at this point in the history
  • Loading branch information
ptrthomas committed Dec 10, 2022
1 parent 75c618a commit 1e7db1f
Showing 1 changed file with 5 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -268,11 +268,10 @@ public Response invoke(HttpRequest request) {
}
if (request.getHeaders() != null) {
request.getHeaders().forEach((k, vals) -> vals.forEach(v -> requestBuilder.addHeader(k, v)));
}
CloseableHttpClient client = clientBuilder.build();
}
CloseableHttpResponse httpResponse;
byte[] bytes;
try {
try (CloseableHttpClient client = clientBuilder.build()) {
httpResponse = client.execute(requestBuilder.build());
HttpEntity responseEntity = httpResponse.getEntity();
if (responseEntity == null || responseEntity.getContent() == null) {
Expand All @@ -282,13 +281,15 @@ public Response invoke(HttpRequest request) {
bytes = FileUtils.toBytes(is);
}
request.setEndTime(System.currentTimeMillis());
httpResponse.close();
} catch (Exception e) {
if (e instanceof ClientProtocolException && e.getCause() != null) { // better error message
throw new RuntimeException(e.getCause());
} else {
throw new RuntimeException(e);
}
}
int statusCode = httpResponse.getStatusLine().getStatusCode();
Map<String, List<String>> headers = toHeaders(httpResponse);
List<Cookie> storedCookies = cookieStore.getCookies();
Header[] requestCookieHeaders = httpResponse.getHeaders(HttpConstants.HDR_SET_COOKIE);
Expand Down Expand Up @@ -323,7 +324,7 @@ public Response invoke(HttpRequest request) {
}
headers.put(HttpConstants.HDR_SET_COOKIE, mergedCookieValues);
cookieStore.clear();
Response response = new Response(httpResponse.getStatusLine().getStatusCode(), headers, bytes);
Response response = new Response(statusCode, headers, bytes);
httpLogger.logResponse(getConfig(), request, response);
return response;
}
Expand Down

0 comments on commit 1e7db1f

Please sign in to comment.