use of io.netty.handler.codec.http.DefaultHttpContent in project riposte by Nike-Inc.
the class HttpUtilsTest method convertContentChunksToRawString_and_convertContentChunksToRawBytes_works_with_EmptyByteBuf_chunks.
@Test
public void convertContentChunksToRawString_and_convertContentChunksToRawBytes_works_with_EmptyByteBuf_chunks() throws IOException {
// given
Charset contentCharset = CharsetUtil.UTF_8;
String chunk1Content = UUID.randomUUID().toString();
String chunk2Content = UUID.randomUUID().toString();
byte[] chunk1Bytes = chunk1Content.getBytes(contentCharset);
byte[] chunk2Bytes = chunk2Content.getBytes(contentCharset);
ByteBuf chunk1ByteBuf = Unpooled.copiedBuffer(chunk1Bytes);
ByteBuf chunk2ByteBuf = Unpooled.copiedBuffer(chunk2Bytes);
Collection<HttpContent> chunkCollection = Arrays.asList(new DefaultHttpContent(chunk1ByteBuf), new DefaultHttpContent(new EmptyByteBuf(ByteBufAllocator.DEFAULT)), new DefaultHttpContent(chunk2ByteBuf), new DefaultHttpContent(new EmptyByteBuf(ByteBufAllocator.DEFAULT)));
// when
String resultString = HttpUtils.convertContentChunksToRawString(contentCharset, chunkCollection);
byte[] resultBytes = HttpUtils.convertContentChunksToRawBytes(chunkCollection);
// then
String expectedResultString = chunk1Content + chunk2Content;
assertThat(resultString, is(expectedResultString));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos.write(chunk1Bytes);
baos.write(chunk2Bytes);
assertThat(resultBytes, is(baos.toByteArray()));
}
use of io.netty.handler.codec.http.DefaultHttpContent in project riposte by Nike-Inc.
the class RequestInfoImplTest method addContentChunk_and_getRawConent_and_getRawContentBytes_work_as_expected_for_last_chunk.
@Test
public void addContentChunk_and_getRawConent_and_getRawContentBytes_work_as_expected_for_last_chunk() throws IOException {
// given
RequestInfoImpl<?> requestInfo = RequestInfoImpl.dummyInstanceForUnknownRequests();
requestInfo.isCompleteRequestWithAllChunks = false;
String chunk1String = UUID.randomUUID().toString();
String lastChunkString = UUID.randomUUID().toString();
byte[] chunk1Bytes = chunk1String.getBytes();
byte[] lastChunkBytes = lastChunkString.getBytes();
HttpContent chunk1 = new DefaultHttpContent(Unpooled.copiedBuffer(chunk1Bytes));
HttpContent lastChunk = new DefaultLastHttpContent(Unpooled.copiedBuffer(lastChunkBytes));
assertThat(chunk1.refCnt(), is(1));
assertThat(lastChunk.refCnt(), is(1));
assertThat(requestInfo.getRawContentBytes(), nullValue());
assertThat(requestInfo.getRawContent(), nullValue());
// when
requestInfo.addContentChunk(chunk1);
requestInfo.addContentChunk(lastChunk);
// then
assertThat(chunk1.refCnt(), is(2));
assertThat(lastChunk.refCnt(), is(2));
assertThat(requestInfo.contentChunks.size(), is(2));
assertThat(requestInfo.isCompleteRequestWithAllChunks(), is(true));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos.write(chunk1Bytes);
baos.write(lastChunkBytes);
assertThat(requestInfo.getRawContentBytes(), is(baos.toByteArray()));
String rawContentString = requestInfo.getRawContent();
assertThat(requestInfo.getRawContent(), is(chunk1String + lastChunkString));
// Verify that the raw content string is cached the first time it's loaded and reused for subsequent calls
assertThat(requestInfo.getRawContent() == rawContentString, is(true));
assertThat(chunk1.refCnt(), is(1));
assertThat(lastChunk.refCnt(), is(1));
}
use of io.netty.handler.codec.http.DefaultHttpContent in project riposte by Nike-Inc.
the class HttpUtilsTest method convertContentChunksToRawBytes_returns_null_if_total_bytes_is_zero.
@Test
public void convertContentChunksToRawBytes_returns_null_if_total_bytes_is_zero() {
// given
Collection<HttpContent> chunkCollection = Arrays.asList(new DefaultHttpContent(new EmptyByteBuf(ByteBufAllocator.DEFAULT)), new DefaultHttpContent(new EmptyByteBuf(ByteBufAllocator.DEFAULT)));
// when
byte[] resultBytes = HttpUtils.convertContentChunksToRawBytes(chunkCollection);
// then
assertThat(resultBytes, nullValue());
}
use of io.netty.handler.codec.http.DefaultHttpContent in project asterixdb by apache.
the class ChunkedNettyOutputStream method flush.
@Override
public void flush() throws IOException {
ensureWritable();
if (buffer.readableBytes() > 0) {
if (response.status() == HttpResponseStatus.OK) {
int size = buffer.capacity();
response.beforeFlush();
DefaultHttpContent content = new DefaultHttpContent(buffer);
ctx.write(content, ctx.channel().voidPromise());
buffer = ctx.alloc().buffer(size);
} else {
ByteBuf aBuffer = ctx.alloc().buffer(buffer.readableBytes());
aBuffer.writeBytes(buffer);
response.error(aBuffer);
buffer.clear();
}
}
}
use of io.netty.handler.codec.http.DefaultHttpContent in project vert.x by eclipse.
the class VertxHttpHandler method safeObject.
@Override
protected Object safeObject(Object msg, ByteBufAllocator allocator) throws Exception {
if (msg instanceof HttpContent) {
HttpContent content = (HttpContent) msg;
ByteBuf buf = content.content();
if (buf != Unpooled.EMPTY_BUFFER && buf.isDirect()) {
ByteBuf newBuf = safeBuffer(content, allocator);
if (msg instanceof LastHttpContent) {
LastHttpContent last = (LastHttpContent) msg;
return new AssembledLastHttpContent(newBuf, last.trailingHeaders(), last.getDecoderResult());
} else {
return new DefaultHttpContent(newBuf);
}
}
} else if (msg instanceof WebSocketFrame) {
ByteBuf payload = safeBuffer((WebSocketFrame) msg, allocator);
boolean isFinal = ((WebSocketFrame) msg).isFinalFragment();
FrameType frameType;
if (msg instanceof BinaryWebSocketFrame) {
frameType = FrameType.BINARY;
} else if (msg instanceof CloseWebSocketFrame) {
frameType = FrameType.CLOSE;
} else if (msg instanceof PingWebSocketFrame) {
frameType = FrameType.PING;
} else if (msg instanceof PongWebSocketFrame) {
frameType = FrameType.PONG;
} else if (msg instanceof TextWebSocketFrame) {
frameType = FrameType.TEXT;
} else if (msg instanceof ContinuationWebSocketFrame) {
frameType = FrameType.CONTINUATION;
} else {
throw new IllegalStateException("Unsupported websocket msg " + msg);
}
return new WebSocketFrameImpl(frameType, payload, isFinal);
}
return msg;
}
Aggregations