Search in sources :

Example 91 with SessionInputBuffer

use of org.apache.hc.core5.http.io.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();
}
Also used : InterruptedIOException(java.io.InterruptedIOException) SessionInputBuffer(org.apache.hc.core5.http.io.SessionInputBuffer) Test(org.junit.jupiter.api.Test)

Example 92 with SessionInputBuffer

use of org.apache.hc.core5.http.io.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();
}
Also used : SessionInputBuffer(org.apache.hc.core5.http.io.SessionInputBuffer) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.jupiter.api.Test)

Example 93 with SessionInputBuffer

use of org.apache.hc.core5.http.io.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());
}
Also used : SessionInputBuffer(org.apache.hc.core5.http.io.SessionInputBuffer) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.jupiter.api.Test)

Example 94 with SessionInputBuffer

use of org.apache.hc.core5.http.io.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();
}
Also used : InterruptedIOException(java.io.InterruptedIOException) SessionInputBuffer(org.apache.hc.core5.http.io.SessionInputBuffer) Test(org.junit.jupiter.api.Test)

Example 95 with SessionInputBuffer

use of org.apache.hc.core5.http.io.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));
}
Also used : SessionInputBuffer(org.apache.hc.core5.http.io.SessionInputBuffer) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)123 SessionInputBuffer (org.apache.hc.core5.http.io.SessionInputBuffer)62 ReadableByteChannel (java.nio.channels.ReadableByteChannel)61 SessionInputBuffer (org.apache.hc.core5.http.nio.SessionInputBuffer)61 ByteArrayInputStream (java.io.ByteArrayInputStream)58 BasicHttpTransportMetrics (org.apache.hc.core5.http.impl.BasicHttpTransportMetrics)47 ReadableByteChannelMock (org.apache.hc.core5.http.ReadableByteChannelMock)46 ByteBuffer (java.nio.ByteBuffer)30 CharArrayBuffer (org.apache.hc.core5.util.CharArrayBuffer)25 ByteArrayOutputStream (java.io.ByteArrayOutputStream)22 Header (org.apache.hc.core5.http.Header)13 RandomAccessFile (java.io.RandomAccessFile)12 FileChannel (java.nio.channels.FileChannel)12 SessionOutputBuffer (org.apache.hc.core5.http.io.SessionOutputBuffer)10 InputStream (java.io.InputStream)7 WritableByteChannel (java.nio.channels.WritableByteChannel)6 CharsetDecoder (java.nio.charset.CharsetDecoder)6 InterruptedIOException (java.io.InterruptedIOException)4 ClassicHttpRequest (org.apache.hc.core5.http.ClassicHttpRequest)4 ClassicHttpResponse (org.apache.hc.core5.http.ClassicHttpResponse)4