Search in sources :

Example 41 with SessionInputBuffer

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

the class TestChunkDecoder method testMalformedFooters.

@Test
public void testMalformedFooters() throws Exception {
    final String s = "10;key=\"value\"\r\n1234567890123456\r\n" + "5\r\n12345\r\n5\r\n12345\r\n0\r\nFooter1 abcde\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(IOException.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 42 with SessionInputBuffer

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

the class TestChunkDecoder method testIncompleteChunkDecoding.

@Test
public void testIncompleteChunkDecoding() throws Exception {
    final String[] chunks = { "10;", "key=\"value\"\r", "\n123456789012345", "6\r\n5\r\n12", "345\r\n6\r", "\nabcdef\r", "\n0\r\nFoot", "er1: abcde\r\nFooter2: f", "ghij\r\n\r\n" };
    final ReadableByteChannel channel = new ReadableByteChannelMock(chunks, StandardCharsets.US_ASCII);
    final SessionInputBuffer inbuf = new SessionInputBufferImpl(1024, 256, 0, StandardCharsets.US_ASCII);
    final BasicHttpTransportMetrics metrics = new BasicHttpTransportMetrics();
    final ByteBuffer dst = ByteBuffer.allocate(1024);
    final ChunkDecoder decoder = new ChunkDecoder(channel, inbuf, metrics);
    int bytesRead = 0;
    while (dst.hasRemaining() && !decoder.isCompleted()) {
        final int i = decoder.read(dst);
        if (i > 0) {
            bytesRead += i;
        }
    }
    Assertions.assertEquals(27, bytesRead);
    Assertions.assertEquals("123456789012345612345abcdef", CodecTestUtils.convert(dst));
    Assertions.assertTrue(decoder.isCompleted());
    final List<? extends Header> trailers = decoder.getTrailers();
    Assertions.assertEquals(2, trailers.size());
    Assertions.assertEquals("Footer1", trailers.get(0).getName());
    Assertions.assertEquals("abcde", trailers.get(0).getValue());
    Assertions.assertEquals("Footer2", trailers.get(1).getName());
    Assertions.assertEquals("fghij", trailers.get(1).getValue());
    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 43 with SessionInputBuffer

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

the class TestRequestParser method testBasicMessageParsingTooManyLeadingEmptyLines.

@Test
public void testBasicMessageParsingTooManyLeadingEmptyLines() throws Exception {
    final String s = "\r\n" + "\r\n" + "\r\n" + "GET / HTTP/1.1\r\n" + "Host: localhost\r\n" + "\r\n";
    final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.US_ASCII));
    final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16, StandardCharsets.US_ASCII.newDecoder());
    final DefaultHttpRequestParser parser = new DefaultHttpRequestParser(Http1Config.custom().setMaxEmptyLineCount(3).build());
    Assertions.assertThrows(RequestHeaderFieldsTooLargeException.class, () -> parser.parse(inBuffer, inputStream));
}
Also used : SessionInputBuffer(org.apache.hc.core5.http.io.SessionInputBuffer) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.jupiter.api.Test)

Example 44 with SessionInputBuffer

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

the class TestResponseParser method testBasicMessageParsingTooManyLeadingEmptyLines.

@Test
public void testBasicMessageParsingTooManyLeadingEmptyLines() throws Exception {
    final String s = "\r\n" + "\r\n" + "\r\n" + "HTTP/1.1 200 OK\r\n" + "Server: whatever\r\n" + "\r\n";
    final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.US_ASCII));
    final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16, StandardCharsets.US_ASCII.newDecoder());
    final DefaultHttpResponseParser parser = new DefaultHttpResponseParser(Http1Config.custom().setMaxEmptyLineCount(3).build());
    Assertions.assertThrows(MessageConstraintException.class, () -> parser.parse(inBuffer, inputStream));
}
Also used : SessionInputBuffer(org.apache.hc.core5.http.io.SessionInputBuffer) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.jupiter.api.Test)

Example 45 with SessionInputBuffer

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

the class TestResponseParser method testBasicMessageParsing.

@Test
public void testBasicMessageParsing() throws Exception {
    final String s = "HTTP/1.1 200 OK\r\n" + "Server: whatever\r\n" + "Date: some date\r\n" + "Set-Cookie: c1=stuff\r\n" + "\r\n";
    final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.US_ASCII));
    final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16, StandardCharsets.US_ASCII.newDecoder());
    final DefaultHttpResponseParser parser = new DefaultHttpResponseParser();
    final ClassicHttpResponse httpresponse = parser.parse(inBuffer, inputStream);
    Assertions.assertEquals(200, httpresponse.getCode());
    Assertions.assertEquals("OK", httpresponse.getReasonPhrase());
    final Header[] headers = httpresponse.getHeaders();
    Assertions.assertEquals(3, headers.length);
}
Also used : ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse) SessionInputBuffer(org.apache.hc.core5.http.io.SessionInputBuffer) Header(org.apache.hc.core5.http.Header) 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