Search in sources :

Example 26 with LastHttpContent

use of io.netty.handler.codec.http.LastHttpContent in project netty by netty.

the class HttpProxyHandler method handleResponse.

@Override
protected boolean handleResponse(ChannelHandlerContext ctx, Object response) throws Exception {
    if (response instanceof HttpResponse) {
        if (status != null) {
            throw new HttpProxyConnectException(exceptionMessage("too many responses"), /*headers=*/
            null);
        }
        HttpResponse res = (HttpResponse) response;
        status = res.status();
        inboundHeaders = res.headers();
    }
    boolean finished = response instanceof LastHttpContent;
    if (finished) {
        if (status == null) {
            throw new HttpProxyConnectException(exceptionMessage("missing response"), inboundHeaders);
        }
        if (status.code() != 200) {
            throw new HttpProxyConnectException(exceptionMessage("status: " + status), inboundHeaders);
        }
    }
    return finished;
}
Also used : HttpResponse(io.netty.handler.codec.http.HttpResponse) LastHttpContent(io.netty.handler.codec.http.LastHttpContent)

Example 27 with LastHttpContent

use of io.netty.handler.codec.http.LastHttpContent in project netty by netty.

the class HttpPostStandardRequestDecoder method offer.

/**
 * Initialized the internals from a new chunk
 *
 * @param content
 *            the new received chunk
 * @throws ErrorDataDecoderException
 *             if there is a problem with the charset decoding or other
 *             errors
 */
@Override
public HttpPostStandardRequestDecoder offer(HttpContent content) {
    checkDestroyed();
    if (content instanceof LastHttpContent) {
        isLastChunk = true;
    }
    ByteBuf buf = content.content();
    if (undecodedChunk == null) {
        undecodedChunk = // which is not really usable for us as we may exceed it once we add more bytes.
        buf.alloc().buffer(buf.readableBytes()).writeBytes(buf);
    } else {
        undecodedChunk.writeBytes(buf);
    }
    parseBody();
    if (undecodedChunk != null && undecodedChunk.writerIndex() > discardThreshold) {
        if (undecodedChunk.refCnt() == 1) {
            // It's safe to call discardBytes() as we are the only owner of the buffer.
            undecodedChunk.discardReadBytes();
        } else {
            // There seems to be multiple references of the buffer. Let's copy the data and release the buffer to
            // ensure we can give back memory to the system.
            ByteBuf buffer = undecodedChunk.alloc().buffer(undecodedChunk.readableBytes());
            buffer.writeBytes(undecodedChunk);
            undecodedChunk.release();
            undecodedChunk = buffer;
        }
    }
    return this;
}
Also used : LastHttpContent(io.netty.handler.codec.http.LastHttpContent) ByteBuf(io.netty.buffer.ByteBuf)

Example 28 with LastHttpContent

use of io.netty.handler.codec.http.LastHttpContent in project netty by netty.

the class Http2StreamFrameToHttpObjectCodecTest method testEncodeDataEndAsClient.

@Test
public void testEncodeDataEndAsClient() throws Exception {
    EmbeddedChannel ch = new EmbeddedChannel(new Http2StreamFrameToHttpObjectCodec(false));
    ByteBuf hello = Unpooled.copiedBuffer("hello world", CharsetUtil.UTF_8);
    LastHttpContent end = new DefaultLastHttpContent(hello, true);
    assertTrue(ch.writeOutbound(end));
    Http2DataFrame dataFrame = ch.readOutbound();
    try {
        assertThat(dataFrame.content().toString(CharsetUtil.UTF_8), is("hello world"));
        assertTrue(dataFrame.isEndStream());
    } finally {
        dataFrame.release();
    }
    assertThat(ch.readOutbound(), is(nullValue()));
    assertFalse(ch.finish());
}
Also used : DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ByteBuf(io.netty.buffer.ByteBuf) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) Test(org.junit.jupiter.api.Test)

Example 29 with LastHttpContent

use of io.netty.handler.codec.http.LastHttpContent in project netty by netty.

the class Http2StreamFrameToHttpObjectCodecTest method testEncodeTrailersAsClient.

@Test
public void testEncodeTrailersAsClient() throws Exception {
    EmbeddedChannel ch = new EmbeddedChannel(new Http2StreamFrameToHttpObjectCodec(false));
    LastHttpContent trailers = new DefaultLastHttpContent(Unpooled.EMPTY_BUFFER, true);
    HttpHeaders headers = trailers.trailingHeaders();
    headers.set("key", "value");
    assertTrue(ch.writeOutbound(trailers));
    Http2HeadersFrame headerFrame = ch.readOutbound();
    assertThat(headerFrame.headers().get("key").toString(), is("value"));
    assertTrue(headerFrame.isEndStream());
    assertThat(ch.readOutbound(), is(nullValue()));
    assertFalse(ch.finish());
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) Test(org.junit.jupiter.api.Test)

Example 30 with LastHttpContent

use of io.netty.handler.codec.http.LastHttpContent in project netty by netty.

the class Http2StreamFrameToHttpObjectCodecTest method testEncodeDataEndWithTrailersAsClient.

@Test
public void testEncodeDataEndWithTrailersAsClient() throws Exception {
    EmbeddedChannel ch = new EmbeddedChannel(new Http2StreamFrameToHttpObjectCodec(false));
    ByteBuf hello = Unpooled.copiedBuffer("hello world", CharsetUtil.UTF_8);
    LastHttpContent trailers = new DefaultLastHttpContent(hello, true);
    HttpHeaders headers = trailers.trailingHeaders();
    headers.set("key", "value");
    assertTrue(ch.writeOutbound(trailers));
    Http2DataFrame dataFrame = ch.readOutbound();
    try {
        assertThat(dataFrame.content().toString(CharsetUtil.UTF_8), is("hello world"));
        assertFalse(dataFrame.isEndStream());
    } finally {
        dataFrame.release();
    }
    Http2HeadersFrame headerFrame = ch.readOutbound();
    assertThat(headerFrame.headers().get("key").toString(), is("value"));
    assertTrue(headerFrame.isEndStream());
    assertThat(ch.readOutbound(), is(nullValue()));
    assertFalse(ch.finish());
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ByteBuf(io.netty.buffer.ByteBuf) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) Test(org.junit.jupiter.api.Test)

Aggregations

LastHttpContent (io.netty.handler.codec.http.LastHttpContent)122 DefaultLastHttpContent (io.netty.handler.codec.http.DefaultLastHttpContent)56 HttpContent (io.netty.handler.codec.http.HttpContent)52 ByteBuf (io.netty.buffer.ByteBuf)40 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)38 HttpResponse (io.netty.handler.codec.http.HttpResponse)38 HttpRequest (io.netty.handler.codec.http.HttpRequest)31 Test (org.junit.Test)27 DefaultHttpContent (io.netty.handler.codec.http.DefaultHttpContent)21 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)19 Test (org.junit.jupiter.api.Test)19 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)17 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)13 HttpObject (io.netty.handler.codec.http.HttpObject)10 HttpProcessingState (com.nike.riposte.server.http.HttpProcessingState)9 ChannelFuture (io.netty.channel.ChannelFuture)9 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)9 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)9 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)9 IOException (java.io.IOException)9