Search in sources :

Example 46 with LastHttpContent

use of org.apache.flink.shaded.netty4.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 47 with LastHttpContent

use of org.apache.flink.shaded.netty4.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 48 with LastHttpContent

use of org.apache.flink.shaded.netty4.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 49 with LastHttpContent

use of org.apache.flink.shaded.netty4.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 50 with LastHttpContent

use of org.apache.flink.shaded.netty4.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)137 DefaultLastHttpContent (io.netty.handler.codec.http.DefaultLastHttpContent)63 HttpContent (io.netty.handler.codec.http.HttpContent)55 ByteBuf (io.netty.buffer.ByteBuf)49 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)43 HttpResponse (io.netty.handler.codec.http.HttpResponse)42 HttpRequest (io.netty.handler.codec.http.HttpRequest)34 Test (org.junit.Test)27 Test (org.junit.jupiter.api.Test)24 DefaultHttpContent (io.netty.handler.codec.http.DefaultHttpContent)23 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)20 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)18 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)13 IOException (java.io.IOException)12 ChannelFuture (io.netty.channel.ChannelFuture)11 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)10 HttpObject (io.netty.handler.codec.http.HttpObject)10 JsonObjectDecoder (io.netty.handler.codec.json.JsonObjectDecoder)10 HttpProcessingState (com.nike.riposte.server.http.HttpProcessingState)9 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)9