|
| 1 | +/* |
| 2 | + * Copyright 2026 The Netty Project |
| 3 | + * |
| 4 | + * The Netty Project licenses this file to you under the Apache License, |
| 5 | + * version 2.0 (the "License"); you may not use this file except in compliance |
| 6 | + * with the License. You may obtain a copy of the License at: |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations |
| 14 | + * under the License. |
| 15 | + */ |
| 16 | +package io.netty.handler.codec.http; |
| 17 | + |
| 18 | +import io.netty.buffer.ByteBuf; |
| 19 | +import io.netty.channel.ChannelHandlerContext; |
| 20 | +import io.netty.channel.ChannelInboundHandlerAdapter; |
| 21 | +import io.netty.channel.embedded.EmbeddedChannel; |
| 22 | +import io.netty.util.ReferenceCountUtil; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | + |
| 25 | +import java.nio.charset.StandardCharsets; |
| 26 | +import java.util.concurrent.atomic.AtomicInteger; |
| 27 | + |
| 28 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 29 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 30 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 31 | + |
| 32 | +/** |
| 33 | + * Demonstrates that decompressing a single compressed HTTP content chunk produces an excessive |
| 34 | + * number of channelRead events due to ByteBufForwarder firing each decompressed buffer |
| 35 | + * individually through the pipeline instead of batching them. |
| 36 | + * |
| 37 | + * See https://github.com/netty/netty/issues/15852 |
| 38 | + */ |
| 39 | +public class ExcessiveChunksTest { |
| 40 | + |
| 41 | + @Test |
| 42 | + public void testDecompressionChunkCount() { |
| 43 | + // Step 1: Generate a large compressible payload (~150KB uncompressed) |
| 44 | + StringBuilder sb = new StringBuilder(); |
| 45 | + sb.append("{\"items\":["); |
| 46 | + for (int i = 0; i < 1000; i++) { |
| 47 | + if (i > 0) { |
| 48 | + sb.append(','); |
| 49 | + } |
| 50 | + sb.append("{\"id\":").append(i) |
| 51 | + .append(",\"name\":\"Item ").append(i).append('"') |
| 52 | + .append(",\"description\":\"This is a sample description for item number ").append(i) |
| 53 | + .append(" which contains some text to make the response larger and more compressible.") |
| 54 | + .append(" The quick brown fox jumps over the lazy dog. Lorem ipsum dolor sit amet.\"") |
| 55 | + .append(",\"value\":").append(i * 1.5) |
| 56 | + .append(",\"active\":").append(i % 2 == 0) |
| 57 | + .append('}'); |
| 58 | + } |
| 59 | + sb.append("]}"); |
| 60 | + byte[] payload = sb.toString().getBytes(StandardCharsets.UTF_8); |
| 61 | + |
| 62 | + // Step 2: Compress the payload using HttpContentCompressor (server-side) |
| 63 | + EmbeddedChannel compressionChannel = new EmbeddedChannel(new HttpContentCompressor()); |
| 64 | + DefaultFullHttpRequest req = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/"); |
| 65 | + req.headers().set(HttpHeaderNames.ACCEPT_ENCODING, "gzip"); |
| 66 | + compressionChannel.writeInbound(req); |
| 67 | + |
| 68 | + ByteBuf payloadBuf = compressionChannel.alloc().buffer(payload.length); |
| 69 | + payloadBuf.writeBytes(payload); |
| 70 | + DefaultFullHttpResponse fullResponse = new DefaultFullHttpResponse( |
| 71 | + HttpVersion.HTTP_1_1, HttpResponseStatus.OK, payloadBuf); |
| 72 | + fullResponse.headers().set(HttpHeaderNames.CONTENT_TYPE, "application/json"); |
| 73 | + fullResponse.headers().set(HttpHeaderNames.CONTENT_LENGTH, payload.length); |
| 74 | + compressionChannel.writeOutbound(fullResponse); |
| 75 | + compressionChannel.finish(); |
| 76 | + compressionChannel.releaseInbound(); |
| 77 | + |
| 78 | + // Read compressed output: headers + all content merged into a single buffer |
| 79 | + HttpMessage compressedMessage = null; |
| 80 | + ByteBuf compressedBody = compressionChannel.alloc().buffer(); |
| 81 | + boolean lastSeen = false; |
| 82 | + while (true) { |
| 83 | + HttpObject obj = compressionChannel.readOutbound(); |
| 84 | + if (obj == null) { |
| 85 | + break; |
| 86 | + } |
| 87 | + if (obj instanceof HttpMessage) { |
| 88 | + compressedMessage = (HttpMessage) obj; |
| 89 | + } |
| 90 | + if (obj instanceof HttpContent) { |
| 91 | + HttpContent content = (HttpContent) obj; |
| 92 | + compressedBody.writeBytes(content.content()); |
| 93 | + if (obj instanceof LastHttpContent) { |
| 94 | + lastSeen = true; |
| 95 | + } |
| 96 | + content.release(); |
| 97 | + } |
| 98 | + } |
| 99 | + assertNotNull(compressedMessage); |
| 100 | + assertTrue(lastSeen); |
| 101 | + assertTrue(compressedBody.isReadable(), "Compressed body should not be empty"); |
| 102 | + |
| 103 | + // Step 3: Decompress and count channelRead events on the downstream handler |
| 104 | + final AtomicInteger channelReadCount = new AtomicInteger(); |
| 105 | + long[] totalBytes = {0}; |
| 106 | + |
| 107 | + EmbeddedChannel decompressChannel = new EmbeddedChannel( |
| 108 | + new HttpContentDecompressor(0), |
| 109 | + new ChannelInboundHandlerAdapter() { |
| 110 | + @Override |
| 111 | + public void channelRead(ChannelHandlerContext ctx, Object msg) { |
| 112 | + if (msg instanceof HttpContent) { |
| 113 | + channelReadCount.incrementAndGet(); |
| 114 | + totalBytes[0] += ((HttpContent) msg).content().readableBytes(); |
| 115 | + } |
| 116 | + ReferenceCountUtil.release(msg); |
| 117 | + } |
| 118 | + }); |
| 119 | + |
| 120 | + // Feed the compressed response as: headers + single content chunk |
| 121 | + decompressChannel.writeInbound(compressedMessage); |
| 122 | + decompressChannel.writeInbound(new DefaultLastHttpContent(compressedBody)); |
| 123 | + decompressChannel.finish(); |
| 124 | + |
| 125 | + // Verify all data was decompressed correctly |
| 126 | + assertEquals(payload.length, totalBytes[0], |
| 127 | + "Total decompressed bytes should match original payload size"); |
| 128 | + |
| 129 | + // With batching (default 16KB), a ~271KB payload should produce significantly fewer content chunks |
| 130 | + // than the 74 that the unbatched ByteBufForwarder produced. |
| 131 | + // See https://github.com/netty/netty/issues/15852 |
| 132 | + assertTrue(channelReadCount.get() <= 25, |
| 133 | + "Decompression should batch output to reduce channelRead events. Got " + |
| 134 | + channelReadCount.get() + " content chunks for a ~" + (payload.length / 1024) + |
| 135 | + "KB payload (expected <= 25 with default 16KB batch size)."); |
| 136 | + } |
| 137 | +} |