Search in sources :

Example 61 with DefaultLastHttpContent

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

the class HttpPostMultiPartRequestDecoderTest method commonTestFileDelimiterLFLastChunk.

// Issue #11668
private static void commonTestFileDelimiterLFLastChunk(HttpDataFactory factory, boolean inMemory) throws IOException {
    int nbChunks = 2;
    int bytesPerChunk = 100000;
    int bytesLastChunk = 10000;
    // set Xmx to a number lower than this and it crashes
    int fileSize = bytesPerChunk * nbChunks + bytesLastChunk;
    String delimiter = "--861fbeab-cd20-470c-9609-d40a0f704466";
    String prefix = delimiter + "\n" + "Content-Disposition: form-data; name=\"image\"; filename=\"guangzhou.jpeg\"\n" + "Content-Type: image/jpeg\n" + "Content-Length: " + fileSize + "\n" + "\n";
    String suffix = "--861fbeab-cd20-470c-9609-d40a0f704466--";
    byte[] bsuffix = suffix.getBytes(CharsetUtil.UTF_8);
    byte[] bsuffixReal = new byte[bsuffix.length + 2];
    for (int i = 0; i < bsuffix.length; i++) {
        bsuffixReal[1 + i] = bsuffix[i];
    }
    bsuffixReal[0] = HttpConstants.LF;
    bsuffixReal[bsuffixReal.length - 1] = HttpConstants.CR;
    byte[] lastbody = { HttpConstants.LF };
    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");
    // +4 => 2xCRLF (beginning, end)
    request.headers().set("content-length", prefix.length() + fileSize + suffix.length() + 4);
    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()).content());
    httpContent.release();
    byte[] body = new byte[bytesPerChunk];
    Arrays.fill(body, (byte) 1);
    // Set first bytes as CRLF to ensure it is correctly getting the last CRLF
    body[0] = HttpConstants.CR;
    body[1] = HttpConstants.LF;
    for (int i = 0; i < nbChunks; i++) {
        ByteBuf content = Unpooled.wrappedBuffer(body, 0, bytesPerChunk);
        httpContent = new DefaultHttpContent(content);
        // **OutOfMemory previously here**
        decoder.offer(httpContent);
        assertNotNull(((HttpData) decoder.currentPartialHttpData()).content());
        httpContent.release();
    }
    // Last -2 body = content + CR but no delimiter
    byte[] previousLastbody = new byte[bytesLastChunk + 1];
    Arrays.fill(previousLastbody, (byte) 1);
    previousLastbody[bytesLastChunk] = HttpConstants.CR;
    ByteBuf content2 = Unpooled.wrappedBuffer(previousLastbody, 0, previousLastbody.length);
    httpContent = new DefaultHttpContent(content2);
    decoder.offer(httpContent);
    assertNotNull(decoder.currentPartialHttpData());
    httpContent.release();
    // Last -1 body = LF+delimiter+CR but no LF
    content2 = Unpooled.wrappedBuffer(bsuffixReal, 0, bsuffixReal.length);
    httpContent = new DefaultHttpContent(content2);
    decoder.offer(httpContent);
    assertNull(decoder.currentPartialHttpData());
    httpContent.release();
    // Last (LF)
    content2 = Unpooled.wrappedBuffer(lastbody, 0, lastbody.length);
    httpContent = new DefaultHttpContent(content2);
    decoder.offer(httpContent);
    assertNull(decoder.currentPartialHttpData());
    httpContent.release();
    // End
    decoder.offer(new DefaultLastHttpContent());
    FileUpload data = (FileUpload) decoder.getBodyHttpDatas().get(0);
    assertEquals(data.length(), fileSize);
    assertEquals(inMemory, data.isInMemory());
    if (data.isInMemory()) {
        // To be done only if not inMemory: assertEquals(data.get().length, fileSize);
        assertFalse(data.getByteBuf().capacity() < fileSize, "Capacity should be at least file size");
    }
    assertTrue(decoder.getCurrentAllocatedCapacity() < fileSize, "Capacity should be less than 1M");
    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(inMemory ? 1 : 0, 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) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent) ByteBuf(io.netty.buffer.ByteBuf)

Example 62 with DefaultLastHttpContent

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

the class HttpPostMultiPartRequestDecoderTest method commonTestBigFileDelimiterInMiddleChunk.

private void commonTestBigFileDelimiterInMiddleChunk(HttpDataFactory factory, boolean inMemory) throws IOException {
    int nbChunks = 100;
    int bytesPerChunk = 100000;
    int bytesLastChunk = 10000;
    // set Xmx to a number lower than this and it crashes
    int fileSize = bytesPerChunk * nbChunks + bytesLastChunk;
    String delimiter = "--861fbeab-cd20-470c-9609-d40a0f704466";
    String prefix = delimiter + "\n" + "Content-Disposition: form-data; name=\"image\"; filename=\"guangzhou.jpeg\"\n" + "Content-Type: image/jpeg\n" + "Content-Length: " + fileSize + "\n" + "\n";
    String suffix1 = "\n" + "--861fbeab-";
    String suffix2 = "cd20-470c-9609-d40a0f704466--\n";
    String suffix = suffix1 + suffix2;
    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() + fileSize + suffix.length());
    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()).content());
    httpContent.release();
    byte[] body = new byte[bytesPerChunk];
    Arrays.fill(body, (byte) 1);
    // Set first bytes as CRLF to ensure it is correctly getting the last CRLF
    body[0] = HttpConstants.CR;
    body[1] = HttpConstants.LF;
    for (int i = 0; i < nbChunks; i++) {
        ByteBuf content = Unpooled.wrappedBuffer(body, 0, bytesPerChunk);
        httpContent = new DefaultHttpContent(content);
        // **OutOfMemory previously here**
        decoder.offer(httpContent);
        assertNotNull(((HttpData) decoder.currentPartialHttpData()).content());
        httpContent.release();
    }
    byte[] bsuffix1 = suffix1.getBytes(CharsetUtil.UTF_8);
    byte[] previousLastbody = new byte[bytesLastChunk - bsuffix1.length];
    byte[] bdelimiter = delimiter.getBytes(CharsetUtil.UTF_8);
    byte[] lastbody = new byte[2 * bsuffix1.length];
    Arrays.fill(previousLastbody, (byte) 1);
    previousLastbody[0] = HttpConstants.CR;
    previousLastbody[1] = HttpConstants.LF;
    Arrays.fill(lastbody, (byte) 1);
    // put somewhere a not valid delimiter
    for (int i = 0; i < bdelimiter.length; i++) {
        previousLastbody[i + 10] = bdelimiter[i];
    }
    lastbody[0] = HttpConstants.CR;
    lastbody[1] = HttpConstants.LF;
    for (int i = 0; i < bsuffix1.length; i++) {
        lastbody[bsuffix1.length + i] = bsuffix1[i];
    }
    ByteBuf content2 = Unpooled.wrappedBuffer(previousLastbody, 0, previousLastbody.length);
    httpContent = new DefaultHttpContent(content2);
    decoder.offer(httpContent);
    assertNotNull(((HttpData) decoder.currentPartialHttpData()).content());
    httpContent.release();
    content2 = Unpooled.wrappedBuffer(lastbody, 0, lastbody.length);
    httpContent = new DefaultHttpContent(content2);
    decoder.offer(httpContent);
    assertNotNull(((HttpData) decoder.currentPartialHttpData()).content());
    httpContent.release();
    content2 = Unpooled.wrappedBuffer(suffix2.getBytes(CharsetUtil.UTF_8));
    httpContent = new DefaultHttpContent(content2);
    decoder.offer(httpContent);
    assertNull(decoder.currentPartialHttpData());
    httpContent.release();
    decoder.offer(new DefaultLastHttpContent());
    FileUpload data = (FileUpload) decoder.getBodyHttpDatas().get(0);
    assertEquals(data.length(), fileSize);
    assertEquals(inMemory, data.isInMemory());
    if (data.isInMemory()) {
        // To be done only if not inMemory: assertEquals(data.get().length, fileSize);
        assertFalse(data.getByteBuf().capacity() < 1024 * 1024, "Capacity should be higher than 1M");
    }
    assertTrue(decoder.getCurrentAllocatedCapacity() < 1024 * 1024, "Capacity should be less than 1M");
    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(inMemory ? 1 : 0, 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) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent) ByteBuf(io.netty.buffer.ByteBuf)

Example 63 with DefaultLastHttpContent

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

the class HttpPostRequestDecoderTest method testChunkCorrect.

// See https://github.com/netty/netty/issues/2305
@Test
public void testChunkCorrect() throws Exception {
    String payload = "town=794649819&town=784444184&town=794649672&town=794657800&town=" + "794655734&town=794649377&town=794652136&town=789936338&town=789948986&town=" + "789949643&town=786358677&town=794655880&town=786398977&town=789901165&town=" + "789913325&town=789903418&town=789903579&town=794645251&town=794694126&town=" + "794694831&town=794655274&town=789913656&town=794653956&town=794665634&town=" + "789936598&town=789904658&town=789899210&town=799696252&town=794657521&town=" + "789904837&town=789961286&town=789958704&town=789948839&town=789933899&town=" + "793060398&town=794659180&town=794659365&town=799724096&town=794696332&town=" + "789953438&town=786398499&town=794693372&town=789935439&town=794658041&town=" + "789917595&town=794655427&town=791930372&town=794652891&town=794656365&town=" + "789960339&town=794645586&town=794657688&town=794697211&town=789937427&town=" + "789902813&town=789941130&town=794696907&town=789904328&town=789955151&town=" + "789911570&town=794655074&town=789939531&town=789935242&town=789903835&town=" + "789953800&town=794649962&town=789939841&town=789934819&town=789959672&town=" + "794659043&town=794657035&town=794658938&town=794651746&town=794653732&town=" + "794653881&town=786397909&town=794695736&town=799724044&town=794695926&town=" + "789912270&town=794649030&town=794657946&town=794655370&town=794659660&town=" + "794694617&town=799149862&town=789953234&town=789900476&town=794654995&town=" + "794671126&town=789908868&town=794652942&town=789955605&town=789901934&town=" + "789950015&town=789937922&town=789962576&town=786360170&town=789954264&town=" + "789911738&town=789955416&town=799724187&town=789911879&town=794657462&town=" + "789912561&town=789913167&town=794655195&town=789938266&town=789952099&town=" + "794657160&town=789949414&town=794691293&town=794698153&town=789935636&town=" + "789956374&town=789934635&town=789935475&town=789935085&town=794651425&town=" + "794654936&town=794655680&town=789908669&town=794652031&town=789951298&town=" + "789938382&town=794651503&town=794653330&town=817675037&town=789951623&town=" + "789958999&town=789961555&town=794694050&town=794650241&town=794656286&town=" + "794692081&town=794660090&town=794665227&town=794665136&town=794669931";
    DefaultHttpRequest defaultHttpRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/");
    HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(defaultHttpRequest);
    int firstChunk = 10;
    int middleChunk = 1024;
    byte[] payload1 = payload.substring(0, firstChunk).getBytes();
    byte[] payload2 = payload.substring(firstChunk, firstChunk + middleChunk).getBytes();
    byte[] payload3 = payload.substring(firstChunk + middleChunk, firstChunk + middleChunk * 2).getBytes();
    byte[] payload4 = payload.substring(firstChunk + middleChunk * 2).getBytes();
    ByteBuf buf1 = Unpooled.directBuffer(payload1.length);
    ByteBuf buf2 = Unpooled.directBuffer(payload2.length);
    ByteBuf buf3 = Unpooled.directBuffer(payload3.length);
    ByteBuf buf4 = Unpooled.directBuffer(payload4.length);
    buf1.writeBytes(payload1);
    buf2.writeBytes(payload2);
    buf3.writeBytes(payload3);
    buf4.writeBytes(payload4);
    decoder.offer(new DefaultHttpContent(buf1));
    decoder.offer(new DefaultHttpContent(buf2));
    decoder.offer(new DefaultHttpContent(buf3));
    decoder.offer(new DefaultLastHttpContent(buf4));
    assertFalse(decoder.getBodyHttpDatas().isEmpty());
    assertEquals(139, decoder.getBodyHttpDatas().size());
    Attribute attr = (Attribute) decoder.getBodyHttpData("town");
    assertEquals("794649819", attr.getValue());
    decoder.destroy();
    buf1.release();
    buf2.release();
    buf3.release();
    buf4.release();
}
Also used : DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.jupiter.api.Test)

Example 64 with DefaultLastHttpContent

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

the class HttpPostRequestDecoderTest method testFormEncodeIncorrect.

@Test
public void testFormEncodeIncorrect() throws Exception {
    LastHttpContent content = new DefaultLastHttpContent(Unpooled.copiedBuffer("project=netty&&project=netty", CharsetUtil.US_ASCII));
    DefaultHttpRequest req = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/");
    HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(req);
    try {
        decoder.offer(content);
        fail();
    } catch (HttpPostRequestDecoder.ErrorDataDecoderException e) {
        assertTrue(e.getCause() instanceof IllegalArgumentException);
    } finally {
        content.release();
        decoder.destroy();
    }
}
Also used : DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) Test(org.junit.jupiter.api.Test)

Example 65 with DefaultLastHttpContent

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

the class HttpObjectEncoderBenchmark method setup.

@Setup(Level.Trial)
public void setup() {
    byte[] bytes = new byte[256];
    content = Unpooled.buffer(bytes.length);
    content.writeBytes(bytes);
    ByteBuf testContent = Unpooled.unreleasableBuffer(content.asReadOnly());
    HttpHeaders headersWithChunked = new DefaultHttpHeaders(false);
    headersWithChunked.add(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED);
    HttpHeaders headersWithContentLength = new DefaultHttpHeaders(false);
    headersWithContentLength.add(HttpHeaderNames.CONTENT_LENGTH, testContent.readableBytes());
    fullRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/index", testContent, headersWithContentLength, EmptyHttpHeaders.INSTANCE);
    contentLengthRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/index", headersWithContentLength);
    chunkedRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/index", headersWithChunked);
    lastContent = new DefaultLastHttpContent(testContent, false);
    encoder = new HttpRequestEncoder();
    context = new EmbeddedChannelWriteReleaseHandlerContext(pooledAllocator ? PooledByteBufAllocator.DEFAULT : UnpooledByteBufAllocator.DEFAULT, encoder) {

        @Override
        protected void handleException(Throwable t) {
            handleUnexpectedException(t);
        }
    };
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) EmptyHttpHeaders(io.netty.handler.codec.http.EmptyHttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) HttpRequestEncoder(io.netty.handler.codec.http.HttpRequestEncoder) EmbeddedChannelWriteReleaseHandlerContext(io.netty.microbench.channel.EmbeddedChannelWriteReleaseHandlerContext) ByteBuf(io.netty.buffer.ByteBuf) Setup(org.openjdk.jmh.annotations.Setup)

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