@@ -458,6 +458,123 @@ public void testPollFromEmptyQueueReturnsNone() { |
458 | 458 | ch.finishAndReleaseAll(); |
459 | 459 | } |
460 | 460 |
|
| 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 | + |
461 | 578 | private static ByteBuf prepareDataChunk(int size) { |
462 | 579 | StringBuilder sb = new StringBuilder(); |
463 | 580 | for (int i = 0; i < size; ++i) { |
|