GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — HTTP: Enforce pipeline limit in HttpContentEncoder · lltopk/netty@1d5cdac

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • codec-http/src
    • main/java/io/netty/handler/codec/http
      • HttpContentEncoder.java
    • test/java/io/netty/handler/codec/http
      • HttpContentEncoderTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎codec-http/src/main/java/io/netty/handler/codec/http/HttpContentEncoder.java‎

Copy file name to clipboardExpand all lines: codec-http/src/main/java/io/netty/handler/codec/http/HttpContentEncoder.java
+9Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,18 @@ private enum State {
6565
private static final CharSequence ZERO_LENGTH_HEAD = "HEAD";
6666
private static final CharSequence ZERO_LENGTH_CONNECT = "CONNECT";
6767

68+
private final int maxPipelineDepth;
6869
private final Queue<CharSequence> acceptEncodingQueue = new ArrayDeque<CharSequence>();
6970
private EmbeddedChannel encoder;
7071
private State state = State.AWAIT_HEADERS;
7172

7273
public HttpContentEncoder() {
74+
this(128);
75+
}
76+
77+
public HttpContentEncoder(int maxPipelineDepth) {
7378
super(HttpRequest.class, HttpObject.class);
79+
this.maxPipelineDepth = ObjectUtil.checkPositive(maxPipelineDepth, "maxPipelineDepth");
7480
}
7581

7682
@Override
@@ -80,6 +86,9 @@ public boolean acceptOutboundMessage(Object msg) throws Exception {
8086

8187
@Override
8288
protected void decode(ChannelHandlerContext ctx, HttpRequest msg, List<Object> out) throws Exception {
89+
if (maxPipelineDepth <= acceptEncodingQueue.size()) {
90+
throw new IllegalStateException("maxPipelineDepth exceeded: " + maxPipelineDepth);
91+
}
8392
CharSequence acceptEncoding;
8493
List<String> acceptEncodingHeaders = msg.headers().getAll(ACCEPT_ENCODING);
8594
switch (acceptEncodingHeaders.size()) {
Collapse file

‎codec-http/src/test/java/io/netty/handler/codec/http/HttpContentEncoderTest.java‎

Copy file name to clipboardExpand all lines: codec-http/src/test/java/io/netty/handler/codec/http/HttpContentEncoderTest.java
+19Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import io.netty.channel.ChannelInboundHandlerAdapter;
2323
import io.netty.channel.embedded.EmbeddedChannel;
2424
import io.netty.handler.codec.CodecException;
25+
import io.netty.handler.codec.DecoderException;
2526
import io.netty.handler.codec.DecoderResult;
2627
import io.netty.handler.codec.EncoderException;
2728
import io.netty.handler.codec.MessageToByteEncoder;
@@ -44,6 +45,14 @@
4445
public class HttpContentEncoderTest {
4546

4647
private static final class TestEncoder extends HttpContentEncoder {
48+
49+
TestEncoder() {
50+
}
51+
52+
TestEncoder(int maxPipelineDepth) {
53+
super(maxPipelineDepth);
54+
}
55+
4756
@Override
4857
protected Result beginEncode(HttpResponse httpResponse, String acceptEncoding) {
4958
return new Result("test", new EmbeddedChannel(new MessageToByteEncoder<ByteBuf>() {
@@ -435,6 +444,16 @@ public void execute() {
435444
assertEquals(0, content.refCnt());
436445
}
437446

447+
@Test
448+
public void testPipelineDepthLimited() {
449+
EmbeddedChannel ch = new EmbeddedChannel(new TestEncoder(2));
450+
ch.writeInbound(new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/"));
451+
ch.writeInbound(new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/"));
452+
453+
assertThrows(DecoderException.class, () -> ch.writeInbound(
454+
new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/")));
455+
}
456+
438457
private static void assertEmptyResponse(EmbeddedChannel ch) {
439458
Object o = ch.readOutbound();
440459
assertInstanceOf(HttpResponse.class, o);

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.