Skip to content

Commit 23fc6f6

Browse files
committed
Drain JDK HTTP client response body in all cases
Prior to this commit, when using the `SimpleClientHttpRequestFactory` as a driver for `RestTemplate`, the HTTP response body would only be drained if there was an attempt to read it in the first place. This commit ensures that, even if there's no attempt at reading the response body, it is properly drained when the response is closed to make sure that the connection is released in a proper state and can be put back in the connection pool for reuse. Issue: SPR-17181
1 parent 432cdd7 commit 23fc6f6

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

spring-web/src/main/java/org/springframework/http/client/SimpleClientHttpResponse.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,15 @@ public InputStream getBody() throws IOException {
9191

9292
@Override
9393
public void close() {
94-
if (this.responseStream != null) {
95-
try {
96-
StreamUtils.drain(this.responseStream);
97-
this.responseStream.close();
98-
}
99-
catch (Exception ex) {
100-
// ignore
94+
try {
95+
if (this.responseStream == null) {
96+
getBody();
10197
}
98+
StreamUtils.drain(this.responseStream);
99+
this.responseStream.close();
100+
}
101+
catch (Exception ex) {
102+
// ignore
102103
}
103104
}
104105

spring-web/src/test/java/org/springframework/http/client/AbstractHttpRequestFactoryTestCase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public void multipleWrites() throws Exception {
135135

136136
@Test(expected = UnsupportedOperationException.class)
137137
public void headersAfterExecute() throws Exception {
138-
ClientHttpRequest request = factory.createRequest(new URI(baseUrl + "/echo"), HttpMethod.POST);
138+
ClientHttpRequest request = factory.createRequest(new URI(baseUrl + "/status/ok"), HttpMethod.POST);
139139

140140
request.getHeaders().add("MyHeader", "value");
141141
byte[] body = "Hello World".getBytes("UTF-8");

spring-web/src/test/java/org/springframework/http/client/SimpleClientHttpResponseTests.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,18 @@ public void shouldNotDrainWhenErrorStreamClosed() throws Exception {
106106
verify(is).close();
107107
}
108108

109+
@Test // SPR-17181
110+
public void shouldDrainResponseEvenIfResponseNotRead() throws Exception {
111+
TestByteArrayInputStream is = new TestByteArrayInputStream("SpringSpring".getBytes(StandardCharsets.UTF_8));
112+
given(this.connection.getErrorStream()).willReturn(null);
113+
given(this.connection.getInputStream()).willReturn(is);
114+
115+
this.response.close();
116+
assertThat(is.available(), is(0));
117+
assertTrue(is.isClosed());
118+
verify(this.connection, never()).disconnect();
119+
}
120+
109121

110122
private static class TestByteArrayInputStream extends ByteArrayInputStream {
111123

0 commit comments

Comments
 (0)