use of org.apache.hc.core5.http.io.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;
}
use of org.apache.hc.core5.http.io.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));
}
use of org.apache.hc.core5.http.io.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));
}
use of org.apache.hc.core5.http.io.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()));
}
use of org.apache.hc.core5.http.io.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));
}
Aggregations