Search in sources :

Example 6 with DefaultHttpContent

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()));
}
Also used : EmptyByteBuf(io.netty.buffer.EmptyByteBuf) DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent) Charset(java.nio.charset.Charset) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteBuf(io.netty.buffer.ByteBuf) EmptyByteBuf(io.netty.buffer.EmptyByteBuf) HttpContent(io.netty.handler.codec.http.HttpContent) DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent) Test(org.junit.Test)

Example 7 with DefaultHttpContent

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));
}
Also used : DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent) ByteArrayOutputStream(java.io.ByteArrayOutputStream) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) HttpContent(io.netty.handler.codec.http.HttpContent) DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) Test(org.junit.Test)

Example 8 with DefaultHttpContent

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());
}
Also used : EmptyByteBuf(io.netty.buffer.EmptyByteBuf) DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent) HttpContent(io.netty.handler.codec.http.HttpContent) DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent) Test(org.junit.Test)

Example 9 with DefaultHttpContent

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();
        }
    }
}
Also used : DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent) ByteBuf(io.netty.buffer.ByteBuf)

Example 10 with DefaultHttpContent

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;
}
Also used : DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent) ByteBuf(io.netty.buffer.ByteBuf) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) WebSocketFrameImpl(io.vertx.core.http.impl.ws.WebSocketFrameImpl) HttpContent(io.netty.handler.codec.http.HttpContent) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent)

Aggregations

DefaultHttpContent (io.netty.handler.codec.http.DefaultHttpContent)19 ByteBuf (io.netty.buffer.ByteBuf)11 HttpContent (io.netty.handler.codec.http.HttpContent)10 Test (org.junit.Test)10 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)9 DefaultLastHttpContent (io.netty.handler.codec.http.DefaultLastHttpContent)7 DefaultHttpRequest (io.netty.handler.codec.http.DefaultHttpRequest)4 EmptyByteBuf (io.netty.buffer.EmptyByteBuf)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)2 HttpRequest (io.netty.handler.codec.http.HttpRequest)2 IOException (java.io.IOException)2 Charset (java.nio.charset.Charset)2 DataProvider (com.tngtech.java.junit.dataprovider.DataProvider)1 ByteBufAllocator (io.netty.buffer.ByteBufAllocator)1 UnpooledByteBufAllocator (io.netty.buffer.UnpooledByteBufAllocator)1 ChannelFuture (io.netty.channel.ChannelFuture)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 ChannelPromise (io.netty.channel.ChannelPromise)1 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)1