Search in sources :

Example 1 with SessionInputBuffer

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

the class AbstractMessageParser method parse.

@Override
public T parse(final SessionInputBuffer sessionBuffer, final boolean endOfStream) throws IOException, HttpException {
    Args.notNull(sessionBuffer, "Session input buffer");
    while (this.state != State.COMPLETED) {
        if (this.lineBuf == null) {
            this.lineBuf = new CharArrayBuffer(64);
        } else {
            this.lineBuf.clear();
        }
        final boolean lineComplete = sessionBuffer.readLine(this.lineBuf, endOfStream);
        final int maxLineLen = this.messageConstraints.getMaxLineLength();
        if (maxLineLen > 0 && (this.lineBuf.length() > maxLineLen || (!lineComplete && sessionBuffer.length() > maxLineLen))) {
            throw new MessageConstraintException("Maximum line length limit exceeded");
        }
        if (!lineComplete) {
            break;
        }
        switch(this.state) {
            case READ_HEAD_LINE:
                this.message = parseHeadLine();
                if (this.message != null) {
                    this.state = State.READ_HEADERS;
                }
                break;
            case READ_HEADERS:
                if (this.lineBuf.length() > 0) {
                    final int maxHeaderCount = this.messageConstraints.getMaxHeaderCount();
                    if (maxHeaderCount > 0 && headerBufs.size() >= maxHeaderCount) {
                        throw new MessageConstraintException("Maximum header count exceeded");
                    }
                    parseHeader();
                } else {
                    this.state = State.COMPLETED;
                }
                break;
        }
        if (endOfStream && !sessionBuffer.hasData()) {
            this.state = State.COMPLETED;
        }
    }
    if (this.state == State.COMPLETED) {
        for (final CharArrayBuffer buffer : this.headerBufs) {
            this.message.addHeader(this.lineParser.parseHeader(buffer));
        }
        return this.message;
    }
    return null;
}
Also used : CharArrayBuffer(org.apache.hc.core5.util.CharArrayBuffer) MessageConstraintException(org.apache.hc.core5.http.MessageConstraintException)

Example 2 with SessionInputBuffer

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

the class TestSessionInOutBuffers method testReadByteBufferWithMaxLen.

@Test
public void testReadByteBufferWithMaxLen() throws Exception {
    final byte[] pattern = "0123456789ABCDEF".getBytes(StandardCharsets.US_ASCII);
    final ReadableByteChannel channel = newChannel(pattern);
    final SessionInputBuffer inbuf = new SessionInputBufferImpl(4096, 1024, 0);
    while (inbuf.fill(channel) > 0) {
    }
    final ByteBuffer dst = ByteBuffer.allocate(16);
    Assertions.assertEquals(10, inbuf.read(dst, 10));
    dst.flip();
    Assertions.assertEquals(dst, ByteBuffer.wrap(pattern, 0, 10));
    dst.clear();
    Assertions.assertEquals(3, inbuf.read(dst, 3));
    dst.flip();
    Assertions.assertEquals(dst, ByteBuffer.wrap(pattern, 10, 3));
    Assertions.assertEquals(3, inbuf.read(dst, 20));
    dst.flip();
    Assertions.assertEquals(dst, ByteBuffer.wrap(pattern, 13, 3));
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) SessionInputBuffer(org.apache.hc.core5.http.nio.SessionInputBuffer) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.jupiter.api.Test)

Example 3 with SessionInputBuffer

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

the class TestSessionInOutBuffers method testComplexReadWriteLine.

@Test
public void testComplexReadWriteLine() throws Exception {
    final SessionOutputBuffer outbuf = new SessionOutputBufferImpl(1024, 16);
    outbuf.write(ByteBuffer.wrap(new byte[] { 'a', '\n' }));
    outbuf.write(ByteBuffer.wrap(new byte[] { '\r', '\n' }));
    outbuf.write(ByteBuffer.wrap(new byte[] { '\r', '\r', '\n' }));
    outbuf.write(ByteBuffer.wrap(new byte[] { '\n' }));
    final StringBuilder buffer = new StringBuilder();
    for (int i = 0; i < 14; i++) {
        buffer.append("a");
    }
    final String s1 = buffer.toString();
    buffer.append("\r\n");
    outbuf.write(ByteBuffer.wrap(buffer.toString().getBytes(StandardCharsets.US_ASCII)));
    buffer.setLength(0);
    for (int i = 0; i < 15; i++) {
        buffer.append("a");
    }
    final String s2 = buffer.toString();
    buffer.append("\r\n");
    outbuf.write(ByteBuffer.wrap(buffer.toString().getBytes(StandardCharsets.US_ASCII)));
    buffer.setLength(0);
    for (int i = 0; i < 16; i++) {
        buffer.append("a");
    }
    final String s3 = buffer.toString();
    buffer.append("\r\n");
    outbuf.write(ByteBuffer.wrap(buffer.toString().getBytes(StandardCharsets.US_ASCII)));
    outbuf.write(ByteBuffer.wrap(new byte[] { 'a' }));
    final ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    final WritableByteChannel outChannel = newChannel(outStream);
    outbuf.flush(outChannel);
    final ReadableByteChannel channel = newChannel(outStream.toByteArray());
    final SessionInputBuffer inbuf = new SessionInputBufferImpl(1024, 16, 0);
    inbuf.fill(channel);
    final CharArrayBuffer chbuffer = new CharArrayBuffer(32);
    chbuffer.clear();
    inbuf.readLine(chbuffer, true);
    Assertions.assertEquals("a", chbuffer.toString());
    chbuffer.clear();
    inbuf.readLine(chbuffer, true);
    Assertions.assertEquals("", chbuffer.toString());
    chbuffer.clear();
    inbuf.readLine(chbuffer, true);
    Assertions.assertEquals("\r", chbuffer.toString());
    chbuffer.clear();
    inbuf.readLine(chbuffer, true);
    Assertions.assertEquals("", chbuffer.toString());
    chbuffer.clear();
    inbuf.readLine(chbuffer, true);
    Assertions.assertEquals(s1, chbuffer.toString());
    chbuffer.clear();
    inbuf.readLine(chbuffer, true);
    Assertions.assertEquals(s2, chbuffer.toString());
    chbuffer.clear();
    inbuf.readLine(chbuffer, true);
    Assertions.assertEquals(s3, chbuffer.toString());
    chbuffer.clear();
    inbuf.readLine(chbuffer, true);
    Assertions.assertEquals("a", chbuffer.toString());
    chbuffer.clear();
    inbuf.readLine(chbuffer, true);
    Assertions.assertFalse(inbuf.readLine(chbuffer, true));
    chbuffer.clear();
    inbuf.readLine(chbuffer, true);
    Assertions.assertFalse(inbuf.readLine(chbuffer, true));
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) SessionInputBuffer(org.apache.hc.core5.http.nio.SessionInputBuffer) SessionOutputBuffer(org.apache.hc.core5.http.nio.SessionOutputBuffer) WritableByteChannel(java.nio.channels.WritableByteChannel) CharArrayBuffer(org.apache.hc.core5.util.CharArrayBuffer) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.jupiter.api.Test)

Example 4 with SessionInputBuffer

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

the class TestSessionInOutBuffers method testReadToChannel.

@Test
public void testReadToChannel() throws Exception {
    final byte[] pattern = "0123456789ABCDEF".getBytes(StandardCharsets.US_ASCII);
    final ReadableByteChannel channel = newChannel(pattern);
    final SessionInputBuffer inbuf = new SessionInputBufferImpl(4096, 1024, 0);
    while (inbuf.fill(channel) > 0) {
    }
    final ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    final WritableByteChannel dst = newChannel(outStream);
    Assertions.assertEquals(16, inbuf.read(dst));
    Assertions.assertEquals(ByteBuffer.wrap(pattern), ByteBuffer.wrap(outStream.toByteArray()));
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) SessionInputBuffer(org.apache.hc.core5.http.nio.SessionInputBuffer) WritableByteChannel(java.nio.channels.WritableByteChannel) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.jupiter.api.Test)

Example 5 with SessionInputBuffer

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

the class TestSessionInOutBuffers method testLineLimitBufferFull.

@Test
public void testLineLimitBufferFull() throws Exception {
    final String s = "LoooooooooooooooooooooooooOOOOOOOOOOOOOOOOOOoooooooooooooooooooooong line\r\n";
    final CharArrayBuffer line = new CharArrayBuffer(64);
    final SessionInputBuffer inbuf1 = new SessionInputBufferImpl(32, 32);
    final ReadableByteChannel channel1 = newChannel(s);
    inbuf1.fill(channel1);
    Assertions.assertFalse(inbuf1.readLine(line, false));
    line.clear();
    final SessionInputBuffer inbuf2 = new SessionInputBufferImpl(32, 32, 10);
    final ReadableByteChannel channel2 = newChannel(s);
    inbuf2.fill(channel2);
    Assertions.assertThrows(MessageConstraintException.class, () -> inbuf2.readLine(line, false));
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) SessionInputBuffer(org.apache.hc.core5.http.nio.SessionInputBuffer) CharArrayBuffer(org.apache.hc.core5.util.CharArrayBuffer) 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