Skip to content

Commit 5039846

Browse files
velokdavisk6austinpio
authored
Fix for #1286 (#1287)
* Fix for #1286 When feign is done with the response, also invoke close on http response (if closeable) * Update httpclient/src/main/java/feign/httpclient/ApacheHttpClient.java Co-authored-by: Austin Pio <austinpioj@gmail.com> * Update ApacheHttpClient.java * Use finally block to close http5 client * Use finally block to close http5 client Co-authored-by: Kevin Davis <kdavisk6@gmail.com> Co-authored-by: Austin Pio <austinpioj@gmail.com>
1 parent bcaa87c commit 5039846

File tree

2 files changed

+34
-11
lines changed

2 files changed

+34
-11
lines changed

hc5/src/main/java/feign/hc5/ApacheHttp5Client.java

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,34 @@
1616
import static feign.Util.UTF_8;
1717
import static feign.Util.enumForName;
1818

19-
import feign.*;
20-
import java.io.*;
19+
import feign.Client;
20+
import feign.Request;
21+
import feign.Response;
22+
import feign.Util;
23+
import java.io.IOException;
24+
import java.io.InputStream;
25+
import java.io.InputStreamReader;
26+
import java.io.Reader;
2127
import java.net.URI;
2228
import java.net.URISyntaxException;
2329
import java.nio.charset.Charset;
24-
import java.util.*;
30+
import java.util.ArrayList;
31+
import java.util.Collection;
32+
import java.util.HashMap;
33+
import java.util.List;
34+
import java.util.Map;
2535
import org.apache.hc.client5.http.classic.HttpClient;
2636
import org.apache.hc.client5.http.config.Configurable;
2737
import org.apache.hc.client5.http.config.RequestConfig;
2838
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
2939
import org.apache.hc.client5.http.protocol.HttpClientContext;
30-
import org.apache.hc.core5.http.*;
40+
import org.apache.hc.core5.http.ClassicHttpRequest;
41+
import org.apache.hc.core5.http.ClassicHttpResponse;
42+
import org.apache.hc.core5.http.ContentType;
43+
import org.apache.hc.core5.http.Header;
44+
import org.apache.hc.core5.http.HttpEntity;
45+
import org.apache.hc.core5.http.HttpHost;
46+
import org.apache.hc.core5.http.NameValuePair;
3147
import org.apache.hc.core5.http.io.entity.ByteArrayEntity;
3248
import org.apache.hc.core5.http.io.entity.EntityUtils;
3349
import org.apache.hc.core5.http.io.entity.StringEntity;
@@ -174,14 +190,14 @@ Response toFeignResponse(ClassicHttpResponse httpResponse, Request request) thro
174190

175191
final String reason = httpResponse.getReasonPhrase();
176192

177-
final Map<String, Collection<String>> headers = new HashMap<String, Collection<String>>();
193+
final Map<String, Collection<String>> headers = new HashMap<>();
178194
for (final Header header : httpResponse.getHeaders()) {
179195
final String name = header.getName();
180196
final String value = header.getValue();
181197

182198
Collection<String> headerValues = headers.get(name);
183199
if (headerValues == null) {
184-
headerValues = new ArrayList<String>();
200+
headerValues = new ArrayList<>();
185201
headers.put(name, headerValues);
186202
}
187203
headerValues.add(value);
@@ -222,7 +238,6 @@ public InputStream asInputStream() throws IOException {
222238
return entity.getContent();
223239
}
224240

225-
@SuppressWarnings("deprecation")
226241
@Override
227242
public Reader asReader() throws IOException {
228243
return new InputStreamReader(asInputStream(), UTF_8);
@@ -236,7 +251,11 @@ public Reader asReader(Charset charset) throws IOException {
236251

237252
@Override
238253
public void close() throws IOException {
239-
EntityUtils.consume(entity);
254+
try {
255+
EntityUtils.consume(entity);
256+
} finally {
257+
httpResponse.close();
258+
}
240259
}
241260
};
242261
}

httpclient/src/main/java/feign/httpclient/ApacheHttpClient.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@
3838
import org.apache.http.StatusLine;
3939
import org.apache.http.client.HttpClient;
4040
import org.apache.http.client.config.RequestConfig;
41-
import org.apache.http.client.methods.Configurable;
42-
import org.apache.http.client.methods.HttpUriRequest;
43-
import org.apache.http.client.methods.RequestBuilder;
41+
import org.apache.http.client.methods.*;
4442
import org.apache.http.client.utils.URIBuilder;
4543
import org.apache.http.client.utils.URLEncodedUtils;
4644
import org.apache.http.entity.ByteArrayEntity;
@@ -234,6 +232,12 @@ public Reader asReader(Charset charset) throws IOException {
234232
@Override
235233
public void close() throws IOException {
236234
EntityUtils.consume(entity);
235+
try {
236+
EntityUtils.consume(entity);
237+
} finally {
238+
if (httpResponse instanceof CloseableHttpResponse)
239+
((CloseableHttpResponse) httpResponse).close();
240+
}
237241
}
238242
};
239243
}

0 commit comments

Comments
 (0)