Search in sources :

Example 21 with CharArrayBuffer

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

the class DefaultHttpResponseParser method createMessage.

@Override
protected ClassicHttpResponse createMessage(final CharArrayBuffer buffer) throws IOException, HttpException {
    final StatusLine statusline = getLineParser().parseStatusLine(buffer);
    final ClassicHttpResponse response = this.responseFactory.newHttpResponse(statusline.getStatusCode(), statusline.getReasonPhrase());
    response.setVersion(statusline.getProtocolVersion());
    return response;
}
Also used : StatusLine(org.apache.hc.core5.http.message.StatusLine) ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse)

Example 22 with CharArrayBuffer

use of org.apache.hc.core5.util.CharArrayBuffer 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 23 with CharArrayBuffer

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

the class ChunkDecoder method readChunkHead.

private void readChunkHead() throws IOException {
    if (this.lineBuf == null) {
        this.lineBuf = new CharArrayBuffer(32);
    } else {
        this.lineBuf.clear();
    }
    if (this.endOfChunk) {
        if (this.buffer.readLine(this.lineBuf, this.endOfStream)) {
            if (!this.lineBuf.isEmpty()) {
                throw new MalformedChunkCodingException("CRLF expected at end of chunk");
            }
        } else {
            if (this.buffer.length() > 2 || this.endOfStream) {
                throw new MalformedChunkCodingException("CRLF expected at end of chunk");
            }
            return;
        }
        this.endOfChunk = false;
    }
    final boolean lineComplete = this.buffer.readLine(this.lineBuf, this.endOfStream);
    final int maxLineLen = this.http1Config.getMaxLineLength();
    if (maxLineLen > 0 && (this.lineBuf.length() > maxLineLen || (!lineComplete && this.buffer.length() > maxLineLen))) {
        throw new MessageConstraintException("Maximum line length limit exceeded");
    }
    if (lineComplete) {
        int separator = this.lineBuf.indexOf(';');
        if (separator < 0) {
            separator = this.lineBuf.length();
        }
        final String s = this.lineBuf.substringTrimmed(0, separator);
        try {
            this.chunkSize = Long.parseLong(s, 16);
        } catch (final NumberFormatException e) {
            throw new MalformedChunkCodingException("Bad chunk header: " + s);
        }
        this.pos = 0L;
    } else if (this.endOfStream) {
        throw new ConnectionClosedException("Premature end of chunk coded message body: closing chunk expected");
    }
}
Also used : MalformedChunkCodingException(org.apache.hc.core5.http.MalformedChunkCodingException) CharArrayBuffer(org.apache.hc.core5.util.CharArrayBuffer) ConnectionClosedException(org.apache.hc.core5.http.ConnectionClosedException) MessageConstraintException(org.apache.hc.core5.http.MessageConstraintException)

Example 24 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 buffer, final OutputStream outputStream) throws IOException, HttpException {
    Args.notNull(message, "HTTP message");
    Args.notNull(buffer, "Session output buffer");
    Args.notNull(outputStream, "Output stream");
    writeHeadLine(message, this.lineBuf);
    buffer.writeLine(this.lineBuf, outputStream);
    for (final Iterator<Header> it = message.headerIterator(); it.hasNext(); ) {
        final Header header = it.next();
        if (header instanceof FormattedHeader) {
            final CharArrayBuffer chbuffer = ((FormattedHeader) header).getBuffer();
            buffer.writeLine(chbuffer, outputStream);
        } else {
            this.lineBuf.clear();
            lineFormatter.formatHeader(this.lineBuf, header);
            buffer.writeLine(this.lineBuf, outputStream);
        }
    }
    this.lineBuf.clear();
    buffer.writeLine(this.lineBuf, outputStream);
}
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 25 with CharArrayBuffer

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

the class TestLengthDelimitedEncoder method testCodingNoFragmentBuffering.

@Test
public void testCodingNoFragmentBuffering() throws Exception {
    final WritableByteChannelMock channel = Mockito.spy(new WritableByteChannelMock(64));
    final SessionOutputBuffer outbuf = Mockito.spy(new SessionOutputBufferImpl(1024, 128));
    final BasicHttpTransportMetrics metrics = new BasicHttpTransportMetrics();
    final CharArrayBuffer chbuffer = new CharArrayBuffer(16);
    chbuffer.append("header");
    outbuf.writeLine(chbuffer);
    final LengthDelimitedEncoder encoder = new LengthDelimitedEncoder(channel, outbuf, metrics, 100, 0);
    Assertions.assertEquals(5, encoder.write(CodecTestUtils.wrap("stuff")));
    Mockito.verify(channel, Mockito.times(2)).write(ArgumentMatchers.any());
    Mockito.verify(outbuf, Mockito.never()).write(ArgumentMatchers.<ByteBuffer>any());
    Mockito.verify(outbuf, Mockito.times(1)).flush(channel);
    Assertions.assertEquals(13, metrics.getBytesTransferred());
    outbuf.flush(channel);
    final String s = channel.dump(StandardCharsets.US_ASCII);
    Assertions.assertEquals("header\r\nstuff", s);
}
Also used : SessionOutputBuffer(org.apache.hc.core5.http.nio.SessionOutputBuffer) WritableByteChannelMock(org.apache.hc.core5.http.WritableByteChannelMock) BasicHttpTransportMetrics(org.apache.hc.core5.http.impl.BasicHttpTransportMetrics) CharArrayBuffer(org.apache.hc.core5.util.CharArrayBuffer) Test(org.junit.jupiter.api.Test)

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