Skip to content

Commit 9a5f9ef

Browse files
author
Stephane Landelle
committed
Drop InputStreamBodyGenerator marking strategy, close AsyncHttpClient#760
1 parent 1abbe75 commit 9a5f9ef

File tree

2 files changed

+48
-84
lines changed

2 files changed

+48
-84
lines changed

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

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

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

5044
/**
@@ -96,9 +90,6 @@ public long read(ByteBuffer buffer) throws IOException {
9690

9791
return buffer.position();
9892
} else {
99-
if (inputStream.markSupported()) {
100-
inputStream.reset();
101-
}
10293
eof = false;
10394
}
10495
return -1;
@@ -114,14 +105,8 @@ public long read(ByteBuffer buffer) throws IOException {
114105
buffer.put(chunk, 0, read);
115106
// Was missing the final chunk \r\n.
116107
buffer.put(END_PADDING);
117-
} else {
118-
if (read > 0) {
119-
buffer.put(chunk, 0, read);
120-
} else {
121-
if (inputStream.markSupported()) {
122-
inputStream.reset();
123-
}
124-
}
108+
} else if (read > 0) {
109+
buffer.put(chunk, 0, read);
125110
}
126111
return read;
127112
}

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

Lines changed: 46 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,20 @@
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

31-
import static org.testng.Assert.assertNotNull;
32-
import static org.testng.AssertJUnit.assertEquals;
33-
import static org.testng.AssertJUnit.assertTrue;
34+
import static org.testng.Assert.*;
3435
import static org.testng.FileAssert.fail;
3536

3637
/**
@@ -69,64 +70,54 @@ abstract public class ChunkingTest extends AbstractBasicTest {
6970
* Tests that the custom chunked stream result in success and content returned that is unchunked
7071
*/
7172
@Test()
72-
public void testCustomChunking() throws Throwable {
73-
doTest(true);
73+
public void testBufferLargerThanFile() throws Throwable {
74+
doTest(new BufferedInputStream(new FileInputStream(getTestFile()), 400000));
7475
}
7576

76-
private void doTest(boolean customChunkedInputStream) throws Exception {
77-
AsyncHttpClient c = null;
78-
try {
79-
AsyncHttpClientConfig.Builder bc = new AsyncHttpClientConfig.Builder();
77+
@Test()
78+
public void testBufferSmallThanFile() throws Throwable {
79+
doTest(new BufferedInputStream(new FileInputStream(getTestFile())));
80+
}
8081

81-
bc.setAllowPoolingConnection(true);
82-
bc.setMaximumConnectionsPerHost(1);
83-
bc.setMaximumConnectionsTotal(1);
84-
bc.setConnectionTimeoutInMs(1000);
85-
bc.setRequestTimeoutInMs(1000);
86-
bc.setFollowRedirects(true);
82+
@Test()
83+
public void testDirectFile() throws Throwable {
84+
doTest(new FileInputStream(getTestFile()));
85+
}
8786

88-
c = getAsyncHttpClient(bc.build());
87+
public void doTest(InputStream is) throws Throwable {
88+
AsyncHttpClientConfig.Builder bc = new AsyncHttpClientConfig.Builder()//
89+
.setAllowPoolingConnection(true)//
90+
.setMaximumConnectionsPerHost(1)//
91+
.setMaximumConnectionsTotal(1)//
92+
.setConnectionTimeoutInMs(1000)//
93+
.setRequestTimeoutInMs(1000)//
94+
.setFollowRedirects(true);
8995

96+
AsyncHttpClient client = getAsyncHttpClient(bc.build());
97+
try {
9098
RequestBuilder builder = new RequestBuilder("POST");
9199
builder.setUrl(getTargetUrl());
92-
if (customChunkedInputStream) {
93-
// made buff in stream big enough to mark.
94-
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"));
95110
} else {
96-
// made buff in stream big enough to mark.
97-
builder.setBody(new InputStreamBodyGenerator(new BufferedInputStream(new FileInputStream(getTestFile()), 400000)));
98-
}
99-
com.ning.http.client.Request r = builder.build();
100-
Response res = null;
101-
102-
try {
103-
ListenableFuture<Response> response = c.executeRequest(r);
104-
res = response.get();
105-
assertNotNull(res.getResponseBodyAsStream());
106-
if (500 == res.getStatusCode()) {
107-
System.out.println("==============");
108-
System.out.println("500 response from call");
109-
System.out.println("Headers:" + res.getHeaders());
110-
System.out.println("==============");
111-
System.out.flush();
112-
assertEquals("Should have 500 status code", 500, res.getStatusCode());
113-
assertTrue("Should have failed due to chunking", res.getHeader("X-Exception").contains("invalid.chunk.length"));
114-
fail("HARD Failing the test due to provided InputStreamBodyGenerator, chunking incorrectly:" + res.getHeader("X-Exception"));
115-
} else {
116-
assertEquals(LARGE_IMAGE_BYTES, readInputStreamToBytes(res.getResponseBodyAsStream()));
117-
}
118-
} catch (Exception e) {
119-
120-
fail("Exception Thrown:" + e.getMessage());
111+
assertEquals(readInputStreamToBytes(res.getResponseBodyAsStream()), LARGE_IMAGE_BYTES);
121112
}
122113
} finally {
123-
if (c != null)
124-
c.close();
114+
if (client != null)
115+
client.close();
125116
}
126117
}
127-
128-
private byte[] readInputStreamToBytes(InputStream stream) {
129-
byte[] data = new byte[0];
118+
119+
120+
private byte[] readInputStreamToBytes(InputStream stream) throws IOException {
130121
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
131122
try {
132123
int nRead;
@@ -135,33 +126,21 @@ private byte[] readInputStreamToBytes(InputStream stream) {
135126
while ((nRead = stream.read(tmp, 0, tmp.length)) != -1) {
136127
buffer.write(tmp, 0, nRead);
137128
}
129+
138130
buffer.flush();
139-
data = buffer.toByteArray();
140-
} catch (Exception e) {
131+
return buffer.toByteArray();
141132

142133
} finally {
143134
try {
144135
stream.close();
145136
} catch (Exception e2) {
146137
}
147-
return data;
148138
}
149139
}
150140

151-
private static File getTestFile() {
141+
private static File getTestFile() throws URISyntaxException {
152142
String testResource1 = "300k.png";
153-
154-
File testResource1File = null;
155-
try {
156-
ClassLoader cl = ChunkingTest.class.getClassLoader();
157-
URL url = cl.getResource(testResource1);
158-
testResource1File = new File(url.toURI());
159-
} catch (Throwable e) {
160-
// TODO Auto-generated catch block
161-
fail("unable to find " + testResource1);
162-
}
163-
164-
return testResource1File;
143+
URL url = ChunkingTest.class.getClassLoader().getResource(testResource1);
144+
return new File(url.toURI());
165145
}
166-
167-
}
146+
}

0 commit comments

Comments
 (0)