Browse Source

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
pull/1929/head
Brian Clozel 6 years ago
parent
commit
23fc6f6b1d
  1. 15
      spring-web/src/main/java/org/springframework/http/client/SimpleClientHttpResponse.java
  2. 2
      spring-web/src/test/java/org/springframework/http/client/AbstractHttpRequestFactoryTestCase.java
  3. 12
      spring-web/src/test/java/org/springframework/http/client/SimpleClientHttpResponseTests.java

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

@ -91,14 +91,15 @@ final class SimpleClientHttpResponse extends AbstractClientHttpResponse { @@ -91,14 +91,15 @@ final class SimpleClientHttpResponse extends AbstractClientHttpResponse {
@Override
public void close() {
if (this.responseStream != null) {
try {
StreamUtils.drain(this.responseStream);
this.responseStream.close();
}
catch (Exception ex) {
// ignore
try {
if (this.responseStream == null) {
getBody();
}
StreamUtils.drain(this.responseStream);
this.responseStream.close();
}
catch (Exception ex) {
// ignore
}
}

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

@ -135,7 +135,7 @@ public abstract class AbstractHttpRequestFactoryTestCase extends AbstractMockWeb @@ -135,7 +135,7 @@ public abstract class AbstractHttpRequestFactoryTestCase extends AbstractMockWeb
@Test(expected = UnsupportedOperationException.class)
public void headersAfterExecute() throws Exception {
ClientHttpRequest request = factory.createRequest(new URI(baseUrl + "/echo"), HttpMethod.POST);
ClientHttpRequest request = factory.createRequest(new URI(baseUrl + "/status/ok"), HttpMethod.POST);
request.getHeaders().add("MyHeader", "value");
byte[] body = "Hello World".getBytes("UTF-8");

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

@ -106,6 +106,18 @@ public class SimpleClientHttpResponseTests { @@ -106,6 +106,18 @@ public class SimpleClientHttpResponseTests {
verify(is).close();
}
@Test // SPR-17181
public void shouldDrainResponseEvenIfResponseNotRead() throws Exception {
TestByteArrayInputStream is = new TestByteArrayInputStream("SpringSpring".getBytes(StandardCharsets.UTF_8));
given(this.connection.getErrorStream()).willReturn(null);
given(this.connection.getInputStream()).willReturn(is);
this.response.close();
assertThat(is.available(), is(0));
assertTrue(is.isClosed());
verify(this.connection, never()).disconnect();
}
private static class TestByteArrayInputStream extends ByteArrayInputStream {

Loading…
Cancel
Save