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 — Auto-port 5.0: HTTP: Preserve encoder state after header encoding fai… · netty/netty@d654d03

File tree

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

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

Copy file name to clipboardExpand all lines: codec-http/src/main/java/io/netty/handler/codec/http/HttpObjectEncoder.java
+3-2Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -521,16 +521,17 @@ private ByteBuf encodeInitHttpMessage(ChannelHandlerContext ctx, H m) throws Exc
521521
try {
522522
// Encode the message.
523523
encodeInitialLine(buf, m);
524-
state = isContentAlwaysEmpty(m) ? ST_CONTENT_ALWAYS_EMPTY :
524+
final int nextState = isContentAlwaysEmpty(m) ? ST_CONTENT_ALWAYS_EMPTY :
525525
HttpUtil.isTransferEncodingChunked(m) ? ST_CONTENT_CHUNK : ST_CONTENT_NON_CHUNK;
526526

527-
sanitizeHeadersBeforeEncode(m, state == ST_CONTENT_ALWAYS_EMPTY);
527+
sanitizeHeadersBeforeEncode(m, nextState == ST_CONTENT_ALWAYS_EMPTY);
528528

529529
encodeHeaders(m.headers(), buf);
530530
ByteBufUtil.writeShortBE(buf, CRLF_SHORT);
531531

532532
headersEncodedSizeAccumulator = HEADERS_WEIGHT_NEW * padSizeForAccumulation(buf.readableBytes()) +
533533
HEADERS_WEIGHT_HISTORICAL * headersEncodedSizeAccumulator;
534+
state = nextState;
534535
success = true;
535536
return buf;
536537
} finally {
Collapse file

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

Copy file name to clipboardExpand all lines: codec-http/src/test/java/io/netty/handler/codec/http/HttpResponseEncoderTest.java
+28-1Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,33 @@ private static void testStatusResetContentTransferContentLength0(CharSequence he
414414
}
415415

416416
@Test
417-
public void testInitHttpMessageHeaderEncodingFailureReleasesBuffer() throws Exception {
417+
public void testInitHttpMessageHeaderSanitizationFailureDoesNotCorruptState() throws Exception {
418+
final EmbeddedChannel channel = new EmbeddedChannel(new HttpResponseEncoder());
419+
final DefaultHttpResponse invalidResponse = new DefaultHttpResponse(
420+
HttpVersion.HTTP_1_1, HttpResponseStatus.NO_CONTENT, new ReadOnlyHttpHeaders(false));
421+
422+
EncoderException e = assertThrows(EncoderException.class, new Executable() {
423+
@Override
424+
public void execute() throws Exception {
425+
channel.writeOutbound(invalidResponse);
426+
}
427+
});
428+
assertInstanceOf(UnsupportedOperationException.class, e.getCause());
429+
430+
assertEncoderStillUsable(channel);
431+
assertFalse(channel.finish());
432+
}
433+
434+
private static void assertEncoderStillUsable(EmbeddedChannel channel) throws Exception {
435+
assertTrue(channel.writeOutbound(
436+
new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK)));
437+
ByteBuf buffer = channel.readOutbound();
438+
assertEquals("HTTP/1.1 200 OK\r\n\r\n", buffer.toString(CharsetUtil.US_ASCII));
439+
buffer.release();
440+
}
441+
442+
@Test
443+
public void testInitHttpMessageHeaderEncodingFailureReleasesBufferAndPreservesState() throws Exception {
418444
final TrackingFailingAllocator allocator = new TrackingFailingAllocator();
419445
final EmbeddedChannel channel = new EmbeddedChannel(new HttpResponseEncoder());
420446
channel.config().setAllocator(allocator);
@@ -432,6 +458,7 @@ public void execute() throws Throwable {
432458
assertInstanceOf(OutOfMemoryError.class, e.getCause().getCause());
433459

434460
assertAllTrackedBuffersReleased(allocator);
461+
assertEncoderStillUsable(channel);
435462
channel.finishAndReleaseAll();
436463
}
437464

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.