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 — `HttpServerCodec`: do not consume the method queue for 1xx interim re… · netty/netty@ae41417

File tree

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

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

Copy file name to clipboardExpand all lines: codec-http/src/main/java/io/netty/handler/codec/http/HttpServerCodec.java
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,12 @@ protected void sanitizeHeadersBeforeEncode(HttpResponse msg, boolean isAlwaysEmp
282282

283283
@Override
284284
protected boolean isContentAlwaysEmpty(HttpResponse msg) {
285+
if (msg.status().codeClass() == HttpStatusClass.INFORMATIONAL) {
286+
// An informational response should be excluded from paired comparison. This covers 101 as well:
287+
// once the protocol is switched this handler is removed from the pipeline, so the entry that is
288+
// left behind goes away with it. Just delegate to super method which has all the needed handling.
289+
return super.isContentAlwaysEmpty(msg);
290+
}
285291
methodFlag = pollMethod();
286292
return methodFlag == METHOD_FLAG_HEAD || super.isContentAlwaysEmpty(msg);
287293
}
Collapse file

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

Copy file name to clipboardExpand all lines: codec-http/src/test/java/io/netty/handler/codec/http/HttpServerCodecTest.java
+117Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,123 @@ public void testPollFromEmptyQueueReturnsNone() {
458458
ch.finishAndReleaseAll();
459459
}
460460

461+
@Test
462+
public void testHeadResponseHasNoContentAfterInterimResponse() {
463+
// A 1xx interim response must not consume an entry of the method queue, as it is not the
464+
// final response for the queued request.
465+
EmbeddedChannel ch = new EmbeddedChannel(new HttpServerCodec());
466+
467+
// Send a single HEAD request. Method queue = [HEAD].
468+
assertTrue(ch.writeInbound(Unpooled.copiedBuffer(
469+
"HEAD /a HTTP/1.1\r\nHost: a\r\n\r\n", CharsetUtil.UTF_8)));
470+
HttpRequest request = ch.readInbound();
471+
assertEquals(HttpMethod.HEAD, request.method());
472+
LastHttpContent requestContent = ch.readInbound();
473+
assertFalse(requestContent.content().isReadable());
474+
requestContent.release();
475+
476+
// Write a 103 Early Hints interim response. The HEAD entry must stay in the queue.
477+
FullHttpResponse earlyHints = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.EARLY_HINTS);
478+
assertTrue(ch.writeOutbound(earlyHints));
479+
ByteBuf earlyHintsBuf = ch.readOutbound();
480+
assertEquals("HTTP/1.1 103 Early Hints\r\n\r\n", earlyHintsBuf.toString(CharsetUtil.US_ASCII));
481+
earlyHintsBuf.release();
482+
483+
// Now write the final response for the HEAD request, with content attached.
484+
FullHttpResponse finalResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK,
485+
Unpooled.copiedBuffer("body", CharsetUtil.UTF_8));
486+
finalResponse.headers().setInt(HttpHeaderNames.CONTENT_LENGTH, 4);
487+
assertTrue(ch.writeOutbound(finalResponse));
488+
489+
// A HEAD response must never carry a body per RFC 9110 section 9.3.2, regardless of
490+
// any interim responses sent before it.
491+
ByteBuf buf = ch.readOutbound();
492+
assertEquals("HTTP/1.1 200 OK\r\ncontent-length: 4\r\n\r\n", buf.toString(CharsetUtil.US_ASCII));
493+
buf.release();
494+
495+
assertFalse(ch.finishAndReleaseAll());
496+
}
497+
498+
@Test
499+
public void testPipelinedResponseContentPreservedAfterInterimResponse() {
500+
// A 1xx interim response for one pipelined request must not consume a method queue entry,
501+
// so the queue order is preserved for the remaining pipelined requests.
502+
EmbeddedChannel ch = new EmbeddedChannel(new HttpServerCodec());
503+
504+
// Pipeline two requests: GET /a then HEAD /b. Method queue = [OTHER, HEAD].
505+
assertTrue(ch.writeInbound(Unpooled.copiedBuffer(
506+
"GET /a HTTP/1.1\r\nHost: a\r\n\r\n" +
507+
"HEAD /b HTTP/1.1\r\nHost: a\r\n\r\n", CharsetUtil.UTF_8)));
508+
509+
HttpRequest requestA = ch.readInbound();
510+
assertEquals(HttpMethod.GET, requestA.method());
511+
LastHttpContent requestAContent = ch.readInbound();
512+
assertFalse(requestAContent.content().isReadable());
513+
requestAContent.release();
514+
515+
HttpRequest requestB = ch.readInbound();
516+
assertEquals(HttpMethod.HEAD, requestB.method());
517+
LastHttpContent requestBContent = ch.readInbound();
518+
assertFalse(requestBContent.content().isReadable());
519+
requestBContent.release();
520+
521+
// Send a 1xx interim response for /a. The queue must stay at [OTHER, HEAD].
522+
FullHttpResponse earlyHints = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.EARLY_HINTS);
523+
assertTrue(ch.writeOutbound(earlyHints));
524+
ByteBuf earlyHintsBuf = ch.readOutbound();
525+
assertEquals("HTTP/1.1 103 Early Hints\r\n\r\n", earlyHintsBuf.toString(CharsetUtil.US_ASCII));
526+
earlyHintsBuf.release();
527+
528+
// Now send the final response to /a, with content attached.
529+
FullHttpResponse finalResponseA = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK,
530+
Unpooled.copiedBuffer("body-a", CharsetUtil.UTF_8));
531+
finalResponseA.headers().setInt(HttpHeaderNames.CONTENT_LENGTH, 6);
532+
assertTrue(ch.writeOutbound(finalResponseA));
533+
534+
// The GET /a response body must be preserved.
535+
ByteBuf buf = ch.readOutbound();
536+
assertEquals("HTTP/1.1 200 OK\r\ncontent-length: 6\r\n\r\nbody-a", buf.toString(CharsetUtil.US_ASCII));
537+
buf.release();
538+
539+
assertFalse(ch.finishAndReleaseAll());
540+
}
541+
542+
@Test
543+
public void testSwitchingProtocolsResponseHasNoContent() {
544+
// A 101 Switching Protocols response must never carry a body.
545+
EmbeddedChannel ch = new EmbeddedChannel(new HttpServerCodec());
546+
547+
// Send a HEAD request that asks to switch protocols. Method queue = [HEAD].
548+
assertTrue(ch.writeInbound(Unpooled.copiedBuffer(
549+
"HEAD / HTTP/1.1\r\n" +
550+
"Host: a\r\n" +
551+
"Connection: upgrade\r\n" +
552+
"Upgrade: websocket\r\n\r\n", CharsetUtil.UTF_8)));
553+
HttpRequest request = ch.readInbound();
554+
assertEquals(HttpMethod.HEAD, request.method());
555+
LastHttpContent requestContent = ch.readInbound();
556+
assertFalse(requestContent.content().isReadable());
557+
requestContent.release();
558+
559+
HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.SWITCHING_PROTOCOLS);
560+
response.headers().set(HttpHeaderNames.CONNECTION, "upgrade");
561+
response.headers().set(HttpHeaderNames.UPGRADE, "websocket");
562+
assertTrue(ch.writeOutbound(response));
563+
assertTrue(ch.writeOutbound(LastHttpContent.EMPTY_LAST_CONTENT));
564+
565+
ByteBuf buf = ch.readOutbound();
566+
assertEquals("HTTP/1.1 101 Switching Protocols\r\n" +
567+
"connection: upgrade\r\n" +
568+
"upgrade: websocket\r\n\r\n", buf.toString(CharsetUtil.US_ASCII));
569+
buf.release();
570+
571+
buf = ch.readOutbound();
572+
assertFalse(buf.isReadable());
573+
buf.release();
574+
575+
assertFalse(ch.finishAndReleaseAll());
576+
}
577+
461578
private static ByteBuf prepareDataChunk(int size) {
462579
StringBuilder sb = new StringBuilder();
463580
for (int i = 0; i < size; ++i) {

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.