Skip to content

Commit 7fe2fb6

Browse files
author
Stephane Landelle
committed
Drop InputStreamBodyGenerator marking strategy, close AsyncHttpClient#760
1 parent 6bc05ae commit 7fe2fb6

File tree

2 files changed

+37
-68
lines changed

2 files changed

+37
-68
lines changed

src/main/java/com/ning/http/client/generators/InputStreamBodyGenerator.java

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,6 @@ public class InputStreamBodyGenerator implements BodyGenerator {
4040

4141
public InputStreamBodyGenerator(InputStream inputStream) {
4242
this.inputStream = inputStream;
43-
44-
if (inputStream.markSupported()) {
45-
inputStream.mark(0);
46-
} else {
47-
logger.info("inputStream.markSupported() not supported. Some features will not work.");
48-
}
4943
}
5044

5145
public InputStream getInputStream() {
@@ -97,12 +91,8 @@ public long read(ByteBuffer buffer) throws IOException {
9791

9892
buffer.put(END_PADDING);
9993

100-
10194
return buffer.position();
10295
} else {
103-
if (inputStream.markSupported()) {
104-
inputStream.reset();
105-
}
10696
eof = false;
10797
}
10898
return -1;
@@ -118,14 +108,8 @@ public long read(ByteBuffer buffer) throws IOException {
118108
buffer.put(chunk, 0, read);
119109
// Was missing the final chunk \r\n.
120110
buffer.put(END_PADDING);
121-
} else {
122-
if (read > 0) {
123-
buffer.put(chunk, 0, read);
124-
} else {
125-
if (inputStream.markSupported()) {
126-
inputStream.reset();
127-
}
128-
}
111+
} else if (read > 0) {
112+
buffer.put(chunk, 0, read);
129113
}
130114
return read;
131115
}

src/test/java/com/ning/http/client/async/ChunkingTest.java

Lines changed: 35 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,16 @@
1818
import com.ning.http.client.RequestBuilder;
1919
import com.ning.http.client.Response;
2020
import com.ning.http.client.generators.InputStreamBodyGenerator;
21+
2122
import org.testng.annotations.Test;
2223

2324
import java.io.BufferedInputStream;
2425
import java.io.ByteArrayOutputStream;
2526
import java.io.File;
2627
import java.io.FileInputStream;
28+
import java.io.IOException;
2729
import java.io.InputStream;
30+
import java.net.URISyntaxException;
2831
import java.net.URL;
2932
import java.util.Random;
3033

@@ -67,11 +70,21 @@ abstract public class ChunkingTest extends AbstractBasicTest {
6770
* Tests that the custom chunked stream result in success and content returned that is unchunked
6871
*/
6972
@Test()
70-
public void testCustomChunking() throws Throwable {
71-
doTest(true);
73+
public void testBufferLargerThanFile() throws Throwable {
74+
doTest(new BufferedInputStream(new FileInputStream(getTestFile()), 400000));
7275
}
7376

74-
private void doTest(boolean customChunkedInputStream) throws Exception {
77+
@Test()
78+
public void testBufferSmallThanFile() throws Throwable {
79+
doTest(new BufferedInputStream(new FileInputStream(getTestFile())));
80+
}
81+
82+
@Test()
83+
public void testDirectFile() throws Throwable {
84+
doTest(new FileInputStream(getTestFile()));
85+
}
86+
87+
public void doTest(InputStream is) throws Throwable {
7588
AsyncHttpClientConfig.Builder bc = new AsyncHttpClientConfig.Builder()//
7689
.setAllowPoolingConnections(true)//
7790
.setMaxConnectionsPerHost(1)//
@@ -84,44 +97,27 @@ private void doTest(boolean customChunkedInputStream) throws Exception {
8497
try {
8598
RequestBuilder builder = new RequestBuilder("POST");
8699
builder.setUrl(getTargetUrl());
87-
if (customChunkedInputStream) {
88-
// made buff in stream big enough to mark.
89-
builder.setBody(new InputStreamBodyGenerator(new BufferedInputStream(new FileInputStream(getTestFile()), 400000)));
100+
// made buff in stream big enough to mark.
101+
builder.setBody(new InputStreamBodyGenerator(is));
102+
103+
ListenableFuture<Response> response = client.executeRequest(builder.build());
104+
Response res = response.get();
105+
assertNotNull(res.getResponseBodyAsStream());
106+
if (500 == res.getStatusCode()) {
107+
assertEquals(res.getStatusCode(), 500, "Should have 500 status code");
108+
assertTrue(res.getHeader("X-Exception").contains("invalid.chunk.length"), "Should have failed due to chunking");
109+
fail("HARD Failing the test due to provided InputStreamBodyGenerator, chunking incorrectly:" + res.getHeader("X-Exception"));
90110
} else {
91-
// made buff in stream big enough to mark.
92-
builder.setBody(new InputStreamBodyGenerator(new BufferedInputStream(new FileInputStream(getTestFile()), 400000)));
93-
}
94-
com.ning.http.client.Request r = builder.build();
95-
Response res = null;
96-
97-
try {
98-
ListenableFuture<Response> response = client.executeRequest(r);
99-
res = response.get();
100-
assertNotNull(res.getResponseBodyAsStream());
101-
if (500 == res.getStatusCode()) {
102-
System.out.println("==============");
103-
System.out.println("500 response from call");
104-
System.out.println("Headers:" + res.getHeaders());
105-
System.out.println("==============");
106-
System.out.flush();
107-
assertEquals(res.getStatusCode(), 500, "Should have 500 status code");
108-
assertTrue(res.getHeader("X-Exception").contains("invalid.chunk.length"), "Should have failed due to chunking");
109-
fail("HARD Failing the test due to provided InputStreamBodyGenerator, chunking incorrectly:" + res.getHeader("X-Exception"));
110-
} else {
111-
assertEquals(readInputStreamToBytes(res.getResponseBodyAsStream()), LARGE_IMAGE_BYTES);
112-
}
113-
} catch (Exception e) {
114-
115-
fail("Exception Thrown:" + e.getMessage());
111+
assertEquals(readInputStreamToBytes(res.getResponseBodyAsStream()), LARGE_IMAGE_BYTES);
116112
}
117113
} finally {
118114
if (client != null)
119115
client.close();
120116
}
121117
}
122-
123-
private byte[] readInputStreamToBytes(InputStream stream) {
124-
byte[] data = new byte[0];
118+
119+
120+
private byte[] readInputStreamToBytes(InputStream stream) throws IOException {
125121
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
126122
try {
127123
int nRead;
@@ -130,32 +126,21 @@ private byte[] readInputStreamToBytes(InputStream stream) {
130126
while ((nRead = stream.read(tmp, 0, tmp.length)) != -1) {
131127
buffer.write(tmp, 0, nRead);
132128
}
129+
133130
buffer.flush();
134-
data = buffer.toByteArray();
135-
} catch (Exception e) {
131+
return buffer.toByteArray();
136132

137133
} finally {
138134
try {
139135
stream.close();
140136
} catch (Exception e2) {
141137
}
142-
return data;
143138
}
144139
}
145140

146-
private static File getTestFile() {
141+
private static File getTestFile() throws URISyntaxException {
147142
String testResource1 = "300k.png";
148-
149-
File testResource1File = null;
150-
try {
151-
ClassLoader cl = ChunkingTest.class.getClassLoader();
152-
URL url = cl.getResource(testResource1);
153-
testResource1File = new File(url.toURI());
154-
} catch (Throwable e) {
155-
// TODO Auto-generated catch block
156-
fail("unable to find " + testResource1);
157-
}
158-
159-
return testResource1File;
143+
URL url = ChunkingTest.class.getClassLoader().getResource(testResource1);
144+
return new File(url.toURI());
160145
}
161146
}

0 commit comments

Comments
 (0)