Search in sources :

Example 36 with SessionInputBuffer

use of org.apache.hc.core5.http.io.SessionInputBuffer in project httpcomponents-core by apache.

the class TestChunkDecoder method testDecodingWithSmallBuffer.

@Test
public void testDecodingWithSmallBuffer() throws Exception {
    final String s1 = "5\r\n01234\r\n5\r\n5678";
    final String s2 = "9\r\n6\r\nabcdef\r\n0\r\n\r\n";
    final ReadableByteChannel channel = new ReadableByteChannelMock(new String[] { s1, s2 }, StandardCharsets.US_ASCII);
    final SessionInputBuffer inbuf = new SessionInputBufferImpl(1024, 256, 0, StandardCharsets.US_ASCII);
    final BasicHttpTransportMetrics metrics = new BasicHttpTransportMetrics();
    final ChunkDecoder decoder = new ChunkDecoder(channel, inbuf, metrics);
    final ByteBuffer dst = ByteBuffer.allocate(1024);
    final ByteBuffer tmp = ByteBuffer.allocate(4);
    int bytesRead = 0;
    while (dst.hasRemaining() && !decoder.isCompleted()) {
        final int i = decoder.read(tmp);
        if (i > 0) {
            bytesRead += i;
        }
        tmp.flip();
        dst.put(tmp);
        tmp.compact();
    }
    Assertions.assertEquals(16, bytesRead);
    Assertions.assertEquals("0123456789abcdef", CodecTestUtils.convert(dst));
    Assertions.assertTrue(decoder.isCompleted());
    dst.clear();
    bytesRead = decoder.read(dst);
    Assertions.assertEquals(-1, bytesRead);
    Assertions.assertTrue(decoder.isCompleted());
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) SessionInputBuffer(org.apache.hc.core5.http.nio.SessionInputBuffer) BasicHttpTransportMetrics(org.apache.hc.core5.http.impl.BasicHttpTransportMetrics) ReadableByteChannelMock(org.apache.hc.core5.http.ReadableByteChannelMock) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.jupiter.api.Test)

Example 37 with SessionInputBuffer

use of org.apache.hc.core5.http.io.SessionInputBuffer in project httpcomponents-core by apache.

the class TestChunkDecoder method testMissingLastCRLF.

@Test
public void testMissingLastCRLF() throws Exception {
    final String s = "10\r\n1234567890123456\r\n" + "5\r\n12345\r\n5\r\n12345";
    final ReadableByteChannel channel = new ReadableByteChannelMock(new String[] { s }, StandardCharsets.US_ASCII);
    final SessionInputBuffer inbuf = new SessionInputBufferImpl(1024, 256, 0, StandardCharsets.US_ASCII);
    final BasicHttpTransportMetrics metrics = new BasicHttpTransportMetrics();
    final ChunkDecoder decoder = new ChunkDecoder(channel, inbuf, metrics);
    final ByteBuffer dst = ByteBuffer.allocate(1024);
    Assertions.assertThrows(MalformedChunkCodingException.class, () -> {
        while (dst.hasRemaining() && !decoder.isCompleted()) {
            decoder.read(dst);
        }
    });
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) SessionInputBuffer(org.apache.hc.core5.http.nio.SessionInputBuffer) BasicHttpTransportMetrics(org.apache.hc.core5.http.impl.BasicHttpTransportMetrics) ReadableByteChannelMock(org.apache.hc.core5.http.ReadableByteChannelMock) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.jupiter.api.Test)

Example 38 with SessionInputBuffer

use of org.apache.hc.core5.http.io.SessionInputBuffer in project httpcomponents-core by apache.

the class TestChunkDecoder method testTooLongFoldedFooter.

@Test
public void testTooLongFoldedFooter() throws Exception {
    final String s = "10\r\n1234567890123456\r\n" + "0\r\nFooter1: blah\r\n  blah\r\n  blah\r\n  blah\r\n  blah\r\n  blah\r\n  blah\r\n  blah\r\n\r\n";
    final ReadableByteChannel channel1 = new ReadableByteChannelMock(new String[] { s }, StandardCharsets.US_ASCII);
    final SessionInputBuffer inbuf1 = new SessionInputBufferImpl(1024, 256, 0, StandardCharsets.US_ASCII);
    final BasicHttpTransportMetrics metrics1 = new BasicHttpTransportMetrics();
    final ChunkDecoder decoder1 = new ChunkDecoder(channel1, inbuf1, metrics1);
    final ByteBuffer dst = ByteBuffer.allocate(1024);
    final int bytesRead = decoder1.read(dst);
    Assertions.assertEquals(16, bytesRead);
    Assertions.assertEquals("1234567890123456", CodecTestUtils.convert(dst));
    final List<? extends Header> trailers = decoder1.getTrailers();
    Assertions.assertNotNull(trailers);
    Assertions.assertEquals(1, trailers.size());
    final Http1Config http1Config = Http1Config.custom().setMaxLineLength(25).build();
    final ReadableByteChannel channel2 = new ReadableByteChannelMock(new String[] { s }, StandardCharsets.US_ASCII);
    final SessionInputBuffer inbuf2 = new SessionInputBufferImpl(1024, 256, 0, StandardCharsets.US_ASCII);
    final BasicHttpTransportMetrics metrics2 = new BasicHttpTransportMetrics();
    final ChunkDecoder decoder2 = new ChunkDecoder(channel2, inbuf2, http1Config, metrics2);
    dst.clear();
    Assertions.assertThrows(MessageConstraintException.class, () -> decoder2.read(dst));
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) SessionInputBuffer(org.apache.hc.core5.http.nio.SessionInputBuffer) BasicHttpTransportMetrics(org.apache.hc.core5.http.impl.BasicHttpTransportMetrics) ReadableByteChannelMock(org.apache.hc.core5.http.ReadableByteChannelMock) ByteBuffer(java.nio.ByteBuffer) Http1Config(org.apache.hc.core5.http.config.Http1Config) Test(org.junit.jupiter.api.Test)

Example 39 with SessionInputBuffer

use of org.apache.hc.core5.http.io.SessionInputBuffer in project httpcomponents-core by apache.

the class TestChunkDecoder method testMalformedChunkEndingDecoding.

@Test
public void testMalformedChunkEndingDecoding() throws Exception {
    final String s = "5\r\n01234\r\n5\r\n56789\r\r6\r\nabcdef\r\n0\r\n\r\n";
    final ReadableByteChannel channel = new ReadableByteChannelMock(new String[] { s }, StandardCharsets.US_ASCII);
    final SessionInputBuffer inbuf = new SessionInputBufferImpl(1024, 256, 0, StandardCharsets.US_ASCII);
    final BasicHttpTransportMetrics metrics = new BasicHttpTransportMetrics();
    final ChunkDecoder decoder = new ChunkDecoder(channel, inbuf, metrics);
    final ByteBuffer dst = ByteBuffer.allocate(1024);
    Assertions.assertThrows(MalformedChunkCodingException.class, () -> decoder.read(dst));
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) SessionInputBuffer(org.apache.hc.core5.http.nio.SessionInputBuffer) BasicHttpTransportMetrics(org.apache.hc.core5.http.impl.BasicHttpTransportMetrics) ReadableByteChannelMock(org.apache.hc.core5.http.ReadableByteChannelMock) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.jupiter.api.Test)

Example 40 with SessionInputBuffer

use of org.apache.hc.core5.http.io.SessionInputBuffer in project httpcomponents-core by apache.

the class TestChunkDecoder method testTooLongFooter.

@Test
public void testTooLongFooter() throws Exception {
    final String s = "10\r\n1234567890123456\r\n" + "0\r\nFooter1: looooooooooooooooooooooooooooooooooooooooooooooooooooooog\r\n\r\n";
    final ReadableByteChannel channel1 = new ReadableByteChannelMock(new String[] { s }, StandardCharsets.US_ASCII);
    final SessionInputBuffer inbuf1 = new SessionInputBufferImpl(1024, 256, 0);
    final BasicHttpTransportMetrics metrics1 = new BasicHttpTransportMetrics();
    final ChunkDecoder decoder1 = new ChunkDecoder(channel1, inbuf1, metrics1);
    final ByteBuffer dst = ByteBuffer.allocate(1024);
    final int bytesRead = decoder1.read(dst);
    Assertions.assertEquals(16, bytesRead);
    Assertions.assertEquals("1234567890123456", CodecTestUtils.convert(dst));
    final List<? extends Header> trailers = decoder1.getTrailers();
    Assertions.assertNotNull(trailers);
    Assertions.assertEquals(1, trailers.size());
    final ReadableByteChannel channel2 = new ReadableByteChannelMock(new String[] { s }, StandardCharsets.US_ASCII);
    final SessionInputBuffer inbuf2 = new SessionInputBufferImpl(1024, 256, 25, StandardCharsets.US_ASCII);
    final BasicHttpTransportMetrics metrics2 = new BasicHttpTransportMetrics();
    final ChunkDecoder decoder2 = new ChunkDecoder(channel2, inbuf2, metrics2);
    dst.clear();
    Assertions.assertThrows(MessageConstraintException.class, () -> decoder2.read(dst));
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) SessionInputBuffer(org.apache.hc.core5.http.nio.SessionInputBuffer) BasicHttpTransportMetrics(org.apache.hc.core5.http.impl.BasicHttpTransportMetrics) ReadableByteChannelMock(org.apache.hc.core5.http.ReadableByteChannelMock) ByteBuffer(java.nio.ByteBuffer) 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