Search in sources :

Example 51 with HttpContent

use of io.netty.handler.codec.http.HttpContent in project riposte by Nike-Inc.

the class ProcessFinalResponseOutputHandlerTest method write_sets_finalContentLength_if_msg_is_HttpContent_and_finalContentLength_is_null.

@Test
public void write_sets_finalContentLength_if_msg_is_HttpContent_and_finalContentLength_is_null() throws Exception {
    // given
    HttpContent msgMock = mock(HttpContent.class);
    ByteBuf contentMock = mock(ByteBuf.class);
    int contentBytes = (int) (Math.random() * 10000);
    doReturn(contentMock).when(msgMock).content();
    doReturn(contentBytes).when(contentMock).readableBytes();
    assertThat(responseInfo.getFinalContentLength()).isNull();
    // when
    handler.write(ctxMock, msgMock, promiseMock);
    // then
    assertThat(responseInfo.getFinalContentLength()).isEqualTo(contentBytes);
}
Also used : ByteBuf(io.netty.buffer.ByteBuf) HttpContent(io.netty.handler.codec.http.HttpContent) Test(org.junit.Test)

Example 52 with HttpContent

use of io.netty.handler.codec.http.HttpContent in project riposte by Nike-Inc.

the class HttpUtils method convertContentChunksToRawBytes.

public static byte[] convertContentChunksToRawBytes(Collection<HttpContent> contentChunks) {
    if (contentChunks == null || contentChunks.size() == 0)
        return null;
    ByteBuf[] chunkByteBufs = contentChunks.stream().map(ByteBufHolder::content).toArray(ByteBuf[]::new);
    int totalNumBytes = contentChunks.stream().collect(Collectors.summingInt(chunk -> chunk.content().readableBytes()));
    if (totalNumBytes == 0)
        return null;
    byte[] comboBytes = new byte[totalNumBytes];
    int bytesWrittenSoFar = 0;
    for (ByteBuf chunk : chunkByteBufs) {
        int numBytesInThisChunk = chunk.readableBytes();
        chunk.getBytes(0, comboBytes, bytesWrittenSoFar, chunk.readableBytes());
        bytesWrittenSoFar += numBytesInThisChunk;
    }
    return comboBytes;
}
Also used : HttpContent(io.netty.handler.codec.http.HttpContent) RequestInfo(com.nike.riposte.server.http.RequestInfo) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) HttpRequest(io.netty.handler.codec.http.HttpRequest) Collection(java.util.Collection) Set(java.util.Set) COOKIE(io.netty.handler.codec.http.HttpHeaders.Names.COOKIE) Cookie(io.netty.handler.codec.http.cookie.Cookie) Collectors(java.util.stream.Collectors) ByteBufHolder(io.netty.buffer.ByteBufHolder) InvalidCharsetInContentTypeHeaderException(com.nike.riposte.server.error.exception.InvalidCharsetInContentTypeHeaderException) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) HashSet(java.util.HashSet) Endpoint(com.nike.riposte.server.http.Endpoint) List(java.util.List) Matcher(java.util.regex.Matcher) Charset(java.nio.charset.Charset) ByteBuf(io.netty.buffer.ByteBuf) ServerCookieDecoder(io.netty.handler.codec.http.cookie.ServerCookieDecoder) Map(java.util.Map) PathParameterMatchingException(com.nike.riposte.server.error.exception.PathParameterMatchingException) Pattern(java.util.regex.Pattern) Collections(java.util.Collections) ByteBuf(io.netty.buffer.ByteBuf) Endpoint(com.nike.riposte.server.http.Endpoint)

Example 53 with HttpContent

use of io.netty.handler.codec.http.HttpContent in project Glowstone by GlowstoneMC.

the class HttpHandler method channelRead0.

@Override
protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (msg instanceof HttpResponse) {
        HttpResponse response = (HttpResponse) msg;
        int responseCode = response.status().code();
        if (responseCode == HttpResponseStatus.NO_CONTENT.code()) {
            done(ctx);
            return;
        }
        if (responseCode != HttpResponseStatus.OK.code()) {
            throw new IllegalStateException("Expected HTTP response 200 OK, got " + responseCode);
        }
    }
    if (msg instanceof HttpContent) {
        HttpContent httpContent = (HttpContent) msg;
        content.append(httpContent.content().toString(Charset.forName("UTF-8")));
        if (msg instanceof LastHttpContent) {
            done(ctx);
        }
    }
}
Also used : HttpResponse(io.netty.handler.codec.http.HttpResponse) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) HttpContent(io.netty.handler.codec.http.HttpContent) LastHttpContent(io.netty.handler.codec.http.LastHttpContent)

Aggregations

HttpContent (io.netty.handler.codec.http.HttpContent)53 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)38 Test (org.junit.Test)23 ByteBuf (io.netty.buffer.ByteBuf)22 DefaultHttpContent (io.netty.handler.codec.http.DefaultHttpContent)20 HttpResponse (io.netty.handler.codec.http.HttpResponse)16 DefaultLastHttpContent (io.netty.handler.codec.http.DefaultLastHttpContent)13 HttpRequest (io.netty.handler.codec.http.HttpRequest)12 Map (java.util.Map)7 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)6 QueryStringDecoder (io.netty.handler.codec.http.QueryStringDecoder)5 ChannelFuture (io.netty.channel.ChannelFuture)4 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)4 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 IOException (java.io.IOException)4 List (java.util.List)4 PipelineContinuationBehavior (com.nike.riposte.server.handler.base.PipelineContinuationBehavior)3 HttpProcessingState (com.nike.riposte.server.http.HttpProcessingState)3 RequestInfo (com.nike.riposte.server.http.RequestInfo)3