Search in sources :

Example 56 with DefaultLastHttpContent

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

the class Http2StreamFrameToHttpObjectCodec method decode.

@Override
protected void decode(ChannelHandlerContext ctx, Http2StreamFrame frame, List<Object> out) throws Exception {
    if (frame instanceof Http2HeadersFrame) {
        Http2HeadersFrame headersFrame = (Http2HeadersFrame) frame;
        Http2Headers headers = headersFrame.headers();
        Http2FrameStream stream = headersFrame.stream();
        int id = stream == null ? 0 : stream.id();
        final CharSequence status = headers.status();
        // but we need to decode it as a FullHttpResponse to play nice with HttpObjectAggregator.
        if (null != status && HttpResponseStatus.CONTINUE.codeAsText().contentEquals(status)) {
            final FullHttpMessage fullMsg = newFullMessage(id, headers, ctx.alloc());
            out.add(fullMsg);
            return;
        }
        if (headersFrame.isEndStream()) {
            if (headers.method() == null && status == 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 {
                FullHttpMessage full = newFullMessage(id, headers, ctx.alloc());
                out.add(full);
            }
        } else {
            HttpMessage req = newMessage(id, headers);
            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().retain(), validateHeaders));
        } else {
            out.add(new DefaultHttpContent(dataFrame.content().retain()));
        }
    }
}
Also used : DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) FullHttpMessage(io.netty.handler.codec.http.FullHttpMessage) DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) HttpMessage(io.netty.handler.codec.http.HttpMessage) FullHttpMessage(io.netty.handler.codec.http.FullHttpMessage)

Example 57 with DefaultLastHttpContent

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

the class Http2StreamFrameToHttpObjectCodecTest method testUpgradeDataEndWithTrailers.

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

Example 58 with DefaultLastHttpContent

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

the class Http2StreamFrameToHttpObjectCodecTest method testUpgradeTrailers.

@Test
public void testUpgradeTrailers() throws Exception {
    EmbeddedChannel ch = new EmbeddedChannel(new Http2StreamFrameToHttpObjectCodec(true));
    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 59 with DefaultLastHttpContent

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

the class Http2StreamFrameToHttpObjectCodecTest method testUpgradeDataEnd.

@Test
public void testUpgradeDataEnd() throws Exception {
    EmbeddedChannel ch = new EmbeddedChannel(new Http2StreamFrameToHttpObjectCodec(true));
    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 60 with DefaultLastHttpContent

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

the class HttpPostMultiPartRequestDecoderTest method testDelimiterExceedLeftSpaceInCurrentBuffer.

@Test
public void testDelimiterExceedLeftSpaceInCurrentBuffer() {
    String delimiter = "--861fbeab-cd20-470c-9609-d40a0f704466";
    String suffix = '\n' + delimiter + "--\n";
    byte[] bsuffix = suffix.getBytes(CharsetUtil.UTF_8);
    int partOfDelimiter = bsuffix.length / 2;
    // to try to have an out of bound since content is > delimiter
    int bytesLastChunk = 355 - partOfDelimiter;
    byte[] bsuffix1 = Arrays.copyOf(bsuffix, partOfDelimiter);
    byte[] bsuffix2 = Arrays.copyOfRange(bsuffix, partOfDelimiter, bsuffix.length);
    String prefix = delimiter + "\n" + "Content-Disposition: form-data; name=\"image\"; filename=\"guangzhou.jpeg\"\n" + "Content-Type: image/jpeg\n" + "Content-Length: " + bytesLastChunk + "\n\n";
    HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/upload");
    request.headers().set("content-type", "multipart/form-data; boundary=861fbeab-cd20-470c-9609-d40a0f704466");
    request.headers().set("content-length", prefix.length() + bytesLastChunk + suffix.length());
    // Factory using Memory mode
    HttpDataFactory factory = new DefaultHttpDataFactory(false);
    HttpPostMultipartRequestDecoder decoder = new HttpPostMultipartRequestDecoder(factory, request);
    ByteBuf buf = Unpooled.wrappedBuffer(prefix.getBytes(CharsetUtil.UTF_8));
    DefaultHttpContent httpContent = new DefaultHttpContent(buf);
    decoder.offer(httpContent);
    assertNotNull((HttpData) decoder.currentPartialHttpData());
    httpContent.release();
    // Chunk less than Delimiter size but containing part of delimiter
    byte[] body = new byte[bytesLastChunk + bsuffix1.length];
    Arrays.fill(body, (byte) 2);
    for (int i = 0; i < bsuffix1.length; i++) {
        body[bytesLastChunk + i] = bsuffix1[i];
    }
    ByteBuf content = Unpooled.wrappedBuffer(body);
    httpContent = new DefaultHttpContent(content);
    // Ouf of range before here
    decoder.offer(httpContent);
    assertNotNull(((HttpData) decoder.currentPartialHttpData()).content());
    httpContent.release();
    content = Unpooled.wrappedBuffer(bsuffix2);
    httpContent = new DefaultHttpContent(content);
    decoder.offer(httpContent);
    assertNull((HttpData) decoder.currentPartialHttpData());
    httpContent.release();
    decoder.offer(new DefaultLastHttpContent());
    FileUpload data = (FileUpload) decoder.getBodyHttpDatas().get(0);
    assertEquals(data.length(), bytesLastChunk);
    assertEquals(true, data.isInMemory());
    InterfaceHttpData[] httpDatas = decoder.getBodyHttpDatas().toArray(new InterfaceHttpData[0]);
    for (InterfaceHttpData httpData : httpDatas) {
        assertEquals(1, httpData.refCnt(), "Before cleanAllHttpData should be 1");
    }
    factory.cleanAllHttpData();
    for (InterfaceHttpData httpData : httpDatas) {
        assertEquals(1, httpData.refCnt(), "After cleanAllHttpData should be 1 if in Memory");
    }
    decoder.destroy();
    for (InterfaceHttpData httpData : httpDatas) {
        assertEquals(0, httpData.refCnt(), "RefCnt should be 0");
    }
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent) ByteBuf(io.netty.buffer.ByteBuf) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) Test(org.junit.jupiter.api.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