Search in sources :

Example 1 with DefaultLastHttpContent

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

the class Http2ServerDowngraderTest method testUpgradeTrailers.

@Test
public void testUpgradeTrailers() throws Exception {
    EmbeddedChannel ch = new EmbeddedChannel(new Http2ServerDowngrader());
    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.Test)

Example 2 with DefaultLastHttpContent

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

the class Http2ServerDowngraderTest method testUpgradeDataEndWithTrailers.

@Test
public void testUpgradeDataEndWithTrailers() throws Exception {
    EmbeddedChannel ch = new EmbeddedChannel(new Http2ServerDowngrader());
    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.Test)

Example 3 with DefaultLastHttpContent

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

the class Http2ServerDowngraderTest method testUpgradeDataEnd.

@Test
public void testUpgradeDataEnd() throws Exception {
    EmbeddedChannel ch = new EmbeddedChannel(new Http2ServerDowngrader());
    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.Test)

Example 4 with DefaultLastHttpContent

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

the class Http2ServerDowngrader method decode.

@Override
protected void decode(ChannelHandlerContext ctx, Http2StreamFrame frame, List<Object> out) throws Exception {
    if (frame instanceof Http2HeadersFrame) {
        // not really the id
        int id = 0;
        Http2HeadersFrame headersFrame = (Http2HeadersFrame) frame;
        Http2Headers headers = headersFrame.headers();
        if (headersFrame.isEndStream()) {
            if (headers.method() == null) {
                LastHttpContent last = new DefaultLastHttpContent(Unpooled.EMPTY_BUFFER, validateHeaders);
                HttpConversionUtil.addHttp2ToHttpHeaders(id, headers, last.trailingHeaders(), HttpVersion.HTTP_1_1, true, true);
                out.add(last);
            } else {
                FullHttpRequest full = HttpConversionUtil.toFullHttpRequest(id, headers, ctx.alloc(), validateHeaders);
                out.add(full);
            }
        } else {
            HttpRequest req = HttpConversionUtil.toHttpRequest(id, headersFrame.headers(), validateHeaders);
            if (!HttpUtil.isContentLengthSet(req)) {
                req.headers().add(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED);
            }
            out.add(req);
        }
    } else if (frame instanceof Http2DataFrame) {
        Http2DataFrame dataFrame = (Http2DataFrame) frame;
        if (dataFrame.isEndStream()) {
            out.add(new DefaultLastHttpContent(dataFrame.content(), validateHeaders));
        } else {
            out.add(new DefaultHttpContent(dataFrame.content()));
        }
    }
    ReferenceCountUtil.retain(frame);
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent)

Example 5 with DefaultLastHttpContent

use of io.netty.handler.codec.http.DefaultLastHttpContent in project rest.li by linkedin.

the class TestChannelPoolStreamHandler method testConnectionClose.

@Test(dataProvider = "connectionClose")
public void testConnectionClose(String headerName, List<String> headerValue) {
    EmbeddedChannel ch = getChannel();
    FakePool pool = new FakePool();
    ch.attr(ChannelPoolStreamHandler.CHANNEL_POOL_ATTR_KEY).set(pool);
    HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.ACCEPTED);
    HttpContent lastChunk = new DefaultLastHttpContent();
    response.headers().set(headerName, headerValue);
    ch.writeInbound(response);
    ch.writeInbound(lastChunk);
    Assert.assertTrue(pool.isDisposeCalled());
    Assert.assertFalse(pool.isPutCalled());
}
Also used : DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(io.netty.handler.codec.http.HttpResponse) HttpContent(io.netty.handler.codec.http.HttpContent) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) Test(org.testng.annotations.Test)

Aggregations

DefaultLastHttpContent (io.netty.handler.codec.http.DefaultLastHttpContent)73 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)32 Test (org.junit.Test)27 ByteBuf (io.netty.buffer.ByteBuf)26 DefaultHttpContent (io.netty.handler.codec.http.DefaultHttpContent)26 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)24 HttpContent (io.netty.handler.codec.http.HttpContent)17 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)12 HttpRequest (io.netty.handler.codec.http.HttpRequest)12 DefaultHttpRequest (io.netty.handler.codec.http.DefaultHttpRequest)11 HttpResponse (io.netty.handler.codec.http.HttpResponse)11 DefaultHttpResponse (io.netty.handler.codec.http.DefaultHttpResponse)10 Test (org.junit.jupiter.api.Test)10 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)9 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)7 Channel (io.netty.channel.Channel)6 Test (org.testng.annotations.Test)6 SessionContext (com.netflix.zuul.context.SessionContext)5 JsonObjectDecoder (io.netty.handler.codec.json.JsonObjectDecoder)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5