use of org.apache.hc.core5.http.nio.SessionInputBuffer in project httpcomponents-core by apache.
the class TestChunkCoding method testResumeOnSocketTimeoutInChunk.
@Test
public void testResumeOnSocketTimeoutInChunk() throws IOException {
final String s = "5\000\r\000\n\00001234\r\n\0005\r\n56789\r\na\r\n0123456789\r\n\0000\r\n";
final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16);
final TimeoutByteArrayInputStream inputStream = new TimeoutByteArrayInputStream(s.getBytes(StandardCharsets.ISO_8859_1));
final ChunkedInputStream in = new ChunkedInputStream(inBuffer, inputStream);
final byte[] tmp = new byte[3];
int bytesRead = 0;
int timeouts = 0;
int i = 0;
while (i != -1) {
try {
i = in.read(tmp);
if (i > 0) {
bytesRead += i;
}
} catch (final InterruptedIOException ex) {
timeouts++;
}
}
Assertions.assertEquals(20, bytesRead);
Assertions.assertEquals(5, timeouts);
in.close();
}
use of org.apache.hc.core5.http.nio.SessionInputBuffer in project httpcomponents-core by apache.
the class TestChunkCoding method testCorruptChunkedInputStreamMissingCRLF.
// Missing \r\n at the end of the first chunk
@Test
public void testCorruptChunkedInputStreamMissingCRLF() throws IOException {
final String s = "5\r\n012345\r\n56789\r\n0\r\n";
final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16);
final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.ISO_8859_1));
final ChunkedInputStream in = new ChunkedInputStream(inBuffer, inputStream);
final byte[] buffer = new byte[300];
final ByteArrayOutputStream out = new ByteArrayOutputStream();
Assertions.assertThrows(MalformedChunkCodingException.class, () -> {
int len;
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
});
in.close();
}
use of org.apache.hc.core5.http.nio.SessionInputBuffer in project httpcomponents-core by apache.
the class TestChunkCoding method testChunkedInputStreamNoClosingChunk.
// Missing closing chunk
@Test
public void testChunkedInputStreamNoClosingChunk() throws IOException {
final String s = "5\r\n01234\r\n";
final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16);
final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.ISO_8859_1));
final ChunkedInputStream in = new ChunkedInputStream(inBuffer, inputStream);
final byte[] tmp = new byte[5];
Assertions.assertEquals(5, in.read(tmp));
Assertions.assertThrows(ConnectionClosedException.class, () -> in.read());
Assertions.assertThrows(ConnectionClosedException.class, () -> in.close());
}
use of org.apache.hc.core5.http.nio.SessionInputBuffer in project httpcomponents-core by apache.
the class TestChunkCoding method testResumeOnSocketTimeoutInData.
@Test
public void testResumeOnSocketTimeoutInData() throws IOException {
final String s = "5\r\n01234\r\n5\r\n5\0006789\r\na\r\n0123\000456789\r\n0\r\n";
final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16);
final TimeoutByteArrayInputStream inputStream = new TimeoutByteArrayInputStream(s.getBytes(StandardCharsets.ISO_8859_1));
final ChunkedInputStream in = new ChunkedInputStream(inBuffer, inputStream);
final byte[] tmp = new byte[3];
int bytesRead = 0;
int timeouts = 0;
int i = 0;
while (i != -1) {
try {
i = in.read(tmp);
if (i > 0) {
bytesRead += i;
}
} catch (final InterruptedIOException ex) {
timeouts++;
}
}
Assertions.assertEquals(20, bytesRead);
Assertions.assertEquals(2, timeouts);
in.close();
}
use of org.apache.hc.core5.http.nio.SessionInputBuffer in project httpcomponents-core by apache.
the class TestChunkCoding method testTooLongChunkHeader.
@Test
public void testTooLongChunkHeader() throws IOException {
final String s = "5; and some very looooong commend\r\n12345\r\n0\r\n";
final SessionInputBuffer inBuffer1 = new SessionInputBufferImpl(16);
final ByteArrayInputStream inputStream1 = new ByteArrayInputStream(s.getBytes(StandardCharsets.ISO_8859_1));
final ChunkedInputStream in1 = new ChunkedInputStream(inBuffer1, inputStream1);
final byte[] buffer = new byte[300];
Assertions.assertEquals(5, in1.read(buffer));
in1.close();
final SessionInputBuffer inBuffer2 = new SessionInputBufferImpl(16, 10);
final ByteArrayInputStream inputStream2 = new ByteArrayInputStream(s.getBytes(StandardCharsets.ISO_8859_1));
final ChunkedInputStream in2 = new ChunkedInputStream(inBuffer2, inputStream2);
Assertions.assertThrows(MessageConstraintException.class, () -> in2.read(buffer));
}
Aggregations