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 4.1: HTTP: Preserve encoder state after header encoding fai… · lltopk/netty@693f7ee

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
@@ -527,16 +527,17 @@ private ByteBuf encodeInitHttpMessage(ChannelHandlerContext ctx, H m) throws Exc
527527
try {
528528
// Encode the message.
529529
encodeInitialLine(buf, m);
530-
state = isContentAlwaysEmpty(m) ? ST_CONTENT_ALWAYS_EMPTY :
530+
final int nextState = isContentAlwaysEmpty(m) ? ST_CONTENT_ALWAYS_EMPTY :
531531
HttpUtil.isTransferEncodingChunked(m) ? ST_CONTENT_CHUNK : ST_CONTENT_NON_CHUNK;
532532

533-
sanitizeHeadersBeforeEncode(m, state == ST_CONTENT_ALWAYS_EMPTY);
533+
sanitizeHeadersBeforeEncode(m, nextState == ST_CONTENT_ALWAYS_EMPTY);
534534

535535
encodeHeaders(m.headers(), buf);
536536
ByteBufUtil.writeShortBE(buf, CRLF_SHORT);
537537

538538
headersEncodedSizeAccumulator = HEADERS_WEIGHT_NEW * padSizeForAccumulation(buf.readableBytes()) +
539539
HEADERS_WEIGHT_HISTORICAL * headersEncodedSizeAccumulator;
540+
state = nextState;
540541
success = true;
541542
return buf;
542543
} 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
@@ -413,7 +413,33 @@ private static void testStatusResetContentTransferContentLength0(CharSequence he
413413
}
414414

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

433459
assertAllTrackedBuffersReleased(allocator);
460+
assertEncoderStillUsable(channel);
434461
channel.finishAndReleaseAll();
435462
}
436463

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.