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;
}
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;
}
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");
}
}
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);
}
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);
}
Aggregations