Search in sources :

Example 96 with CharArrayBuffer

use of org.apache.hc.core5.util.CharArrayBuffer in project httpcomponents-core by apache.

the class TestBasicLineFormatter method testHeaderFormattingInvalidInput.

@Test
public void testHeaderFormattingInvalidInput() throws Exception {
    final CharArrayBuffer buf = new CharArrayBuffer(64);
    final Header header = new BasicHeader("name", "value");
    Assertions.assertThrows(NullPointerException.class, () -> formatter.formatHeader(null, header));
    Assertions.assertThrows(NullPointerException.class, () -> formatter.formatHeader(buf, null));
}
Also used : Header(org.apache.hc.core5.http.Header) CharArrayBuffer(org.apache.hc.core5.util.CharArrayBuffer) Test(org.junit.jupiter.api.Test)

Example 97 with CharArrayBuffer

use of org.apache.hc.core5.util.CharArrayBuffer in project httpcomponents-core by apache.

the class AbstractMessageParser method parseHeader.

private void parseHeader() throws IOException {
    final CharArrayBuffer current = this.lineBuf;
    final int count = this.headerBufs.size();
    if ((this.lineBuf.charAt(0) == ' ' || this.lineBuf.charAt(0) == '\t') && count > 0) {
        // Handle folded header line
        final CharArrayBuffer previous = this.headerBufs.get(count - 1);
        int i = 0;
        while (i < current.length()) {
            final char ch = current.charAt(i);
            if (ch != ' ' && ch != '\t') {
                break;
            }
            i++;
        }
        final int maxLineLen = this.messageConstraints.getMaxLineLength();
        if (maxLineLen > 0 && previous.length() + 1 + current.length() - i > maxLineLen) {
            throw new MessageConstraintException("Maximum line length limit exceeded");
        }
        previous.append(' ');
        previous.append(current, i, current.length() - i);
    } else {
        this.headerBufs.add(current);
        this.lineBuf = null;
    }
}
Also used : CharArrayBuffer(org.apache.hc.core5.util.CharArrayBuffer) MessageConstraintException(org.apache.hc.core5.http.MessageConstraintException)

Example 98 with CharArrayBuffer

use of org.apache.hc.core5.util.CharArrayBuffer in project httpcomponents-core by apache.

the class AbstractMessageWriter method write.

@Override
public void write(final T message, final SessionOutputBuffer sessionBuffer) throws IOException, HttpException {
    Args.notNull(message, "HTTP message");
    Args.notNull(sessionBuffer, "Session output buffer");
    writeHeadLine(message, this.lineBuf);
    sessionBuffer.writeLine(this.lineBuf);
    for (final Iterator<Header> it = message.headerIterator(); it.hasNext(); ) {
        final Header header = it.next();
        if (header instanceof FormattedHeader) {
            final CharArrayBuffer buffer = ((FormattedHeader) header).getBuffer();
            sessionBuffer.writeLine(buffer);
        } else {
            this.lineBuf.clear();
            this.lineFormatter.formatHeader(this.lineBuf, header);
            sessionBuffer.writeLine(this.lineBuf);
        }
    }
    this.lineBuf.clear();
    sessionBuffer.writeLine(this.lineBuf);
}
Also used : FormattedHeader(org.apache.hc.core5.http.FormattedHeader) Header(org.apache.hc.core5.http.Header) CharArrayBuffer(org.apache.hc.core5.util.CharArrayBuffer) FormattedHeader(org.apache.hc.core5.http.FormattedHeader)

Example 99 with CharArrayBuffer

use of org.apache.hc.core5.util.CharArrayBuffer in project httpcomponents-core by apache.

the class ChunkDecoder method read.

@Override
public int read(final ByteBuffer dst) throws IOException {
    Args.notNull(dst, "Byte buffer");
    if (this.state == State.COMPLETED) {
        return -1;
    }
    int totalRead = 0;
    while (this.state != State.COMPLETED) {
        if (!this.buffer.hasData() || this.chunkSize == -1L) {
            final int bytesRead = fillBufferFromChannel();
            if (bytesRead == -1) {
                this.endOfStream = true;
            }
        }
        switch(this.state) {
            case READ_CONTENT:
                if (this.chunkSize == -1L) {
                    readChunkHead();
                    if (this.chunkSize == -1L) {
                        // Unable to read a chunk head
                        return totalRead;
                    }
                    if (this.chunkSize == 0L) {
                        // Last chunk. Read footers
                        this.chunkSize = -1L;
                        this.state = State.READ_FOOTERS;
                        break;
                    }
                }
                final long maxLen = this.chunkSize - this.pos;
                final int len = this.buffer.read(dst, (int) Math.min(maxLen, Integer.MAX_VALUE));
                if (len > 0) {
                    this.pos += len;
                    totalRead += len;
                } else {
                    if (!this.buffer.hasData() && this.endOfStream) {
                        this.state = State.COMPLETED;
                        setCompleted();
                        throw new TruncatedChunkException("Truncated chunk (expected size: %d; actual size: %d)", chunkSize, pos);
                    }
                }
                if (this.pos == this.chunkSize) {
                    // At the end of the chunk
                    this.chunkSize = -1L;
                    this.pos = 0L;
                    this.endOfChunk = true;
                    break;
                }
                return totalRead;
            case READ_FOOTERS:
                if (this.lineBuf == null) {
                    this.lineBuf = new CharArrayBuffer(32);
                } else {
                    this.lineBuf.clear();
                }
                if (!this.buffer.readLine(this.lineBuf, this.endOfStream)) {
                    // Unable to read a footer
                    if (this.endOfStream) {
                        this.state = State.COMPLETED;
                        setCompleted();
                    }
                    return totalRead;
                }
                if (this.lineBuf.length() > 0) {
                    final int maxHeaderCount = this.http1Config.getMaxHeaderCount();
                    if (maxHeaderCount > 0 && trailerBufs.size() >= maxHeaderCount) {
                        throw new MessageConstraintException("Maximum header count exceeded");
                    }
                    parseHeader();
                } else {
                    this.state = State.COMPLETED;
                    setCompleted();
                    processFooters();
                }
                break;
        }
    }
    return totalRead;
}
Also used : CharArrayBuffer(org.apache.hc.core5.util.CharArrayBuffer) TruncatedChunkException(org.apache.hc.core5.http.TruncatedChunkException) MessageConstraintException(org.apache.hc.core5.http.MessageConstraintException)

Example 100 with CharArrayBuffer

use of org.apache.hc.core5.util.CharArrayBuffer in project httpcomponents-core by apache.

the class ChunkDecoder method parseHeader.

private void parseHeader() throws IOException {
    final CharArrayBuffer current = this.lineBuf;
    final int count = this.trailerBufs.size();
    if ((this.lineBuf.charAt(0) == ' ' || this.lineBuf.charAt(0) == '\t') && count > 0) {
        // Handle folded header line
        final CharArrayBuffer previous = this.trailerBufs.get(count - 1);
        int i = 0;
        while (i < current.length()) {
            final char ch = current.charAt(i);
            if (ch != ' ' && ch != '\t') {
                break;
            }
            i++;
        }
        final int maxLineLen = this.http1Config.getMaxLineLength();
        if (maxLineLen > 0 && previous.length() + 1 + current.length() - i > maxLineLen) {
            throw new MessageConstraintException("Maximum line length limit exceeded");
        }
        previous.append(' ');
        previous.append(current, i, current.length() - i);
    } else {
        this.trailerBufs.add(current);
        this.lineBuf = null;
    }
}
Also used : CharArrayBuffer(org.apache.hc.core5.util.CharArrayBuffer) MessageConstraintException(org.apache.hc.core5.http.MessageConstraintException)

Aggregations

CharArrayBuffer (org.apache.hc.core5.util.CharArrayBuffer)91 Test (org.junit.jupiter.api.Test)74 SessionOutputBuffer (org.apache.hc.core5.http.nio.SessionOutputBuffer)18 ByteArrayOutputStream (java.io.ByteArrayOutputStream)16 ByteArrayInputStream (java.io.ByteArrayInputStream)14 SessionInputBuffer (org.apache.hc.core5.http.io.SessionInputBuffer)13 BasicHttpTransportMetrics (org.apache.hc.core5.http.impl.BasicHttpTransportMetrics)11 ReadableByteChannel (java.nio.channels.ReadableByteChannel)10 Header (org.apache.hc.core5.http.Header)10 WritableByteChannelMock (org.apache.hc.core5.http.WritableByteChannelMock)10 SessionInputBuffer (org.apache.hc.core5.http.nio.SessionInputBuffer)10 HeaderElement (org.apache.hc.core5.http.HeaderElement)9 SessionOutputBuffer (org.apache.hc.core5.http.io.SessionOutputBuffer)9 NameValuePair (org.apache.hc.core5.http.NameValuePair)8 MessageConstraintException (org.apache.hc.core5.http.MessageConstraintException)7 WritableByteChannel (java.nio.channels.WritableByteChannel)6 CharsetDecoder (java.nio.charset.CharsetDecoder)6 CharsetEncoder (java.nio.charset.CharsetEncoder)6 ProtocolVersion (org.apache.hc.core5.http.ProtocolVersion)6 FormattedHeader (org.apache.hc.core5.http.FormattedHeader)5