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 — Remove Content-Length from informational responses in Http2StreamFram… · netty/netty@7754dcd

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • codec-http2/src
    • main/java/io/netty/handler/codec/http2
      • Http2StreamFrameToHttpObjectCodec.java
    • test/java/io/netty/handler/codec/http2
      • Http2StreamFrameToHttpObjectCodecTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎codec-http2/src/main/java/io/netty/handler/codec/http2/Http2StreamFrameToHttpObjectCodec.java‎

Copy file name to clipboardExpand all lines: codec-http2/src/main/java/io/netty/handler/codec/http2/Http2StreamFrameToHttpObjectCodec.java
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,9 @@ protected void encode(ChannelHandlerContext ctx, HttpObject obj, List<Object> ou
163163
if (statusClass == HttpStatusClass.INFORMATIONAL && code != 101) {
164164
if (res instanceof FullHttpResponse) {
165165
final Http2Headers headers = toHttp2Headers(ctx, res);
166+
// Like HttpResponseEncoder, drop the Content-Length header of an informational response.
167+
// Its content is not written, and is released with the message.
168+
headers.remove(HttpHeaderNames.CONTENT_LENGTH);
166169
out.add(new DefaultHttp2HeadersFrame(headers, false));
167170
return;
168171
} else {
Collapse file

‎codec-http2/src/test/java/io/netty/handler/codec/http2/Http2StreamFrameToHttpObjectCodecTest.java‎

Copy file name to clipboardExpand all lines: codec-http2/src/test/java/io/netty/handler/codec/http2/Http2StreamFrameToHttpObjectCodecTest.java
+75Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import io.netty.handler.codec.http.FullHttpRequest;
3535
import io.netty.handler.codec.http.FullHttpResponse;
3636
import io.netty.handler.codec.http.HttpContent;
37+
import io.netty.handler.codec.http.HttpHeaderNames;
3738
import io.netty.handler.codec.http.HttpHeaders;
3839
import io.netty.handler.codec.http.HttpMethod;
3940
import io.netty.handler.codec.http.HttpRequest;
@@ -104,6 +105,80 @@ public void execute() {
104105
ch.finishAndReleaseAll();
105106
}
106107

108+
@ParameterizedTest
109+
@ValueSource(ints = {100, 103})
110+
public void encodeInformationalResponseWithoutContentLength(int code) throws Exception {
111+
EmbeddedChannel ch = new EmbeddedChannel(new Http2StreamFrameToHttpObjectCodec(true));
112+
// Like the 100 (Continue) that HttpServerExpectContinueHandler writes.
113+
FullHttpResponse response = new DefaultFullHttpResponse(
114+
HttpVersion.HTTP_1_1, HttpResponseStatus.valueOf(code), Unpooled.EMPTY_BUFFER);
115+
response.headers().set(HttpHeaderNames.CONTENT_LENGTH, 0);
116+
response.headers().set("link", "</style.css>; rel=preload; as=style");
117+
assertTrue(ch.writeOutbound(response));
118+
119+
Http2HeadersFrame headersFrame = ch.readOutbound();
120+
assertEquals(String.valueOf(code), headersFrame.headers().status().toString());
121+
assertFalse(headersFrame.headers().contains(HttpHeaderNames.CONTENT_LENGTH));
122+
assertEquals("</style.css>; rel=preload; as=style", headersFrame.headers().get("link").toString());
123+
assertFalse(headersFrame.isEndStream());
124+
125+
assertNull(ch.readOutbound());
126+
assertFalse(ch.finish());
127+
}
128+
129+
@Test
130+
public void encodeInformationalFullResponseDropsContent() throws Exception {
131+
EmbeddedChannel ch = new EmbeddedChannel(new Http2StreamFrameToHttpObjectCodec(true));
132+
ByteBuf content = Unpooled.copiedBuffer("ignored", CharsetUtil.UTF_8);
133+
FullHttpResponse response = new DefaultFullHttpResponse(
134+
HttpVersion.HTTP_1_1, HttpResponseStatus.CONTINUE, content);
135+
response.headers().set(HttpHeaderNames.CONTENT_LENGTH, content.readableBytes());
136+
assertTrue(ch.writeOutbound(response));
137+
assertEquals(0, content.refCnt());
138+
139+
Http2HeadersFrame headersFrame = ch.readOutbound();
140+
assertEquals("100", headersFrame.headers().status().toString());
141+
assertFalse(headersFrame.headers().contains(HttpHeaderNames.CONTENT_LENGTH));
142+
assertFalse(headersFrame.isEndStream());
143+
144+
assertNull(ch.readOutbound());
145+
assertFalse(ch.finish());
146+
}
147+
148+
@Test
149+
public void encode100ContinueThenFinalResponse() throws Exception {
150+
EmbeddedChannel ch = new EmbeddedChannel(new Http2StreamFrameToHttpObjectCodec(true));
151+
FullHttpResponse continueResponse = new DefaultFullHttpResponse(
152+
HttpVersion.HTTP_1_1, HttpResponseStatus.CONTINUE, Unpooled.EMPTY_BUFFER);
153+
continueResponse.headers().set(HttpHeaderNames.CONTENT_LENGTH, 0);
154+
ByteBuf hello = Unpooled.copiedBuffer("hello world", CharsetUtil.UTF_8);
155+
FullHttpResponse response = new DefaultFullHttpResponse(
156+
HttpVersion.HTTP_1_1, HttpResponseStatus.OK, hello);
157+
response.headers().set(HttpHeaderNames.CONTENT_LENGTH, hello.readableBytes());
158+
assertTrue(ch.writeOutbound(continueResponse, response));
159+
160+
Http2HeadersFrame continueFrame = ch.readOutbound();
161+
Http2HeadersFrame headersFrame = ch.readOutbound();
162+
Http2DataFrame dataFrame = ch.readOutbound();
163+
try {
164+
assertEquals("100", continueFrame.headers().status().toString());
165+
assertFalse(continueFrame.headers().contains(HttpHeaderNames.CONTENT_LENGTH));
166+
assertFalse(continueFrame.isEndStream());
167+
168+
assertEquals("200", headersFrame.headers().status().toString());
169+
assertEquals("11", headersFrame.headers().get(HttpHeaderNames.CONTENT_LENGTH).toString());
170+
assertFalse(headersFrame.isEndStream());
171+
172+
assertEquals("hello world", dataFrame.content().toString(CharsetUtil.UTF_8));
173+
assertTrue(dataFrame.isEndStream());
174+
} finally {
175+
dataFrame.release();
176+
}
177+
178+
assertNull(ch.readOutbound());
179+
assertFalse(ch.finish());
180+
}
181+
107182
@Test
108183
public void testUpgradeNonEmptyFullResponse() throws Exception {
109184
EmbeddedChannel ch = new EmbeddedChannel(new Http2StreamFrameToHttpObjectCodec(true));

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.