Skip to content

Commit b947bfe

Browse files
committed
Support of HTTP persistent connections for JDK client
Prior to this commit, HTTP clients relying on the JDK HTTP client would not properly reuse existing TCP connections (i.e. HTTP 1.1 persisten connection). The SimpleClientHttpResponse would close the actual connection once the response is handled. As explained in the JDK documentation (http://docs.oracle.com/javase/8/docs/technotes/guides/net/http-keepalive.html) HTTP clients should do the following to allow resource reuse: * consume the whole HTTP response content * close the response inputstream once done This commit makes sure that the response content is totally drained and then the stream closed (and not the connection). Issue: SPR-14040
1 parent 3910350 commit b947bfe

File tree

3 files changed

+162
-4
lines changed

3 files changed

+162
-4
lines changed

spring-core/src/main/java/org/springframework/util/StreamUtils.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -131,6 +131,25 @@ public static int copy(InputStream in, OutputStream out) throws IOException {
131131
return byteCount;
132132
}
133133

134+
/**
135+
* Drain the remaining content of the given InputStream.
136+
* Leaves the InputStream open when done.
137+
* @param in the InputStream to drain
138+
* @return the number of bytes read
139+
* @throws IOException in case of I/O errors
140+
* @since 4.3.0
141+
*/
142+
public static int drain(InputStream in) throws IOException {
143+
Assert.notNull(in, "No InputStream specified");
144+
byte[] buffer = new byte[BUFFER_SIZE];
145+
int bytesRead = -1;
146+
int byteCount = 0;
147+
while ((bytesRead = in.read(buffer)) != -1) {
148+
byteCount += bytesRead;
149+
}
150+
return byteCount;
151+
}
152+
134153
/**
135154
* Return an efficient empty {@link InputStream}.
136155
* @return a {@link ByteArrayInputStream} based on an empty byte array

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@
2121
import java.net.HttpURLConnection;
2222

2323
import org.springframework.http.HttpHeaders;
24+
import org.springframework.util.StreamUtils;
2425
import org.springframework.util.StringUtils;
2526

2627
/**
@@ -29,6 +30,7 @@
2930
* {@link SimpleStreamingClientHttpRequest#execute()}.
3031
*
3132
* @author Arjen Poutsma
33+
* @author Brian Clozel
3234
* @since 3.0
3335
*/
3436
final class SimpleClientHttpResponse extends AbstractClientHttpResponse {
@@ -37,6 +39,8 @@ final class SimpleClientHttpResponse extends AbstractClientHttpResponse {
3739

3840
private HttpHeaders headers;
3941

42+
private InputStream responseStream;
43+
4044

4145
SimpleClientHttpResponse(HttpURLConnection connection) {
4246
this.connection = connection;
@@ -78,12 +82,19 @@ public HttpHeaders getHeaders() {
7882
@Override
7983
public InputStream getBody() throws IOException {
8084
InputStream errorStream = this.connection.getErrorStream();
81-
return (errorStream != null ? errorStream : this.connection.getInputStream());
85+
this.responseStream = (errorStream != null ? errorStream : this.connection.getInputStream());
86+
return this.responseStream;
8287
}
8388

8489
@Override
8590
public void close() {
86-
this.connection.disconnect();
91+
if (this.responseStream != null) {
92+
try {
93+
StreamUtils.drain(this.responseStream);
94+
this.responseStream.close();
95+
}
96+
catch (IOException e) { }
97+
}
8798
}
8899

89100
}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/*
2+
* Copyright 2002-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.http.client;
18+
19+
import static org.hamcrest.MatcherAssert.*;
20+
import static org.hamcrest.Matchers.*;
21+
import static org.junit.Assert.assertTrue;
22+
import static org.mockito.BDDMockito.*;
23+
import static org.mockito.Mockito.mock;
24+
import static org.mockito.Mockito.never;
25+
import static org.mockito.Mockito.verify;
26+
27+
import java.io.ByteArrayInputStream;
28+
import java.io.IOException;
29+
import java.io.InputStream;
30+
import java.net.HttpURLConnection;
31+
import java.nio.charset.Charset;
32+
33+
import org.junit.Before;
34+
import org.junit.Test;
35+
36+
import org.springframework.util.StreamUtils;
37+
38+
/**
39+
* @author Brian Clozel
40+
*/
41+
public class SimpleClientHttpResponseTests {
42+
43+
private final Charset UTF8 = Charset.forName("UTF-8");
44+
45+
private SimpleClientHttpResponse response;
46+
47+
private HttpURLConnection connection;
48+
49+
@Before
50+
public void setup() throws Exception {
51+
this.connection = mock(HttpURLConnection.class);
52+
this.response = new SimpleClientHttpResponse(this.connection);
53+
}
54+
55+
// SPR-14040
56+
@Test
57+
public void shouldNotCloseConnectionWhenResponseClosed() throws Exception {
58+
TestByteArrayInputStream is = new TestByteArrayInputStream("Spring".getBytes(UTF8));
59+
given(this.connection.getErrorStream()).willReturn(null);
60+
given(this.connection.getInputStream()).willReturn(is);
61+
62+
InputStream responseStream = this.response.getBody();
63+
assertThat(StreamUtils.copyToString(responseStream, UTF8), is("Spring"));
64+
65+
this.response.close();
66+
assertTrue(is.isClosed());
67+
verify(this.connection, never()).disconnect();
68+
}
69+
70+
// SPR-14040
71+
@Test
72+
public void shouldDrainStreamWhenResponseClosed() throws Exception {
73+
byte[] buf = new byte[6];
74+
TestByteArrayInputStream is = new TestByteArrayInputStream("SpringSpring".getBytes(UTF8));
75+
given(this.connection.getErrorStream()).willReturn(null);
76+
given(this.connection.getInputStream()).willReturn(is);
77+
78+
InputStream responseStream = this.response.getBody();
79+
responseStream.read(buf);
80+
assertThat(new String(buf, UTF8), is("Spring"));
81+
assertThat(is.available(), is(6));
82+
83+
this.response.close();
84+
assertThat(is.available(), is(0));
85+
assertTrue(is.isClosed());
86+
verify(this.connection, never()).disconnect();
87+
}
88+
89+
// SPR-14040
90+
@Test
91+
public void shouldDrainErrorStreamWhenResponseClosed() throws Exception {
92+
byte[] buf = new byte[6];
93+
TestByteArrayInputStream is = new TestByteArrayInputStream("SpringSpring".getBytes(UTF8));
94+
given(this.connection.getErrorStream()).willReturn(is);
95+
96+
InputStream responseStream = this.response.getBody();
97+
responseStream.read(buf);
98+
assertThat(new String(buf, UTF8), is("Spring"));
99+
assertThat(is.available(), is(6));
100+
101+
this.response.close();
102+
assertThat(is.available(), is(0));
103+
assertTrue(is.isClosed());
104+
verify(this.connection, never()).disconnect();
105+
}
106+
107+
108+
class TestByteArrayInputStream extends ByteArrayInputStream {
109+
110+
private boolean closed;
111+
112+
public TestByteArrayInputStream(byte[] buf) {
113+
super(buf);
114+
this.closed = false;
115+
}
116+
117+
public boolean isClosed() {
118+
return closed;
119+
}
120+
121+
@Override
122+
public void close() throws IOException {
123+
super.close();
124+
this.closed = true;
125+
}
126+
}
127+
128+
}

0 commit comments

Comments
 (0)