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 — Fix UnsupportedOperationException in readTrailingHeaders · netty/netty@50bb4b6

File tree

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

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

Copy file name to clipboardExpand all lines: codec-http/src/main/java/io/netty/handler/codec/http/HttpObjectDecoder.java
+27-21Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -875,44 +875,50 @@ private LastHttpContent readTrailingHeaders(ByteBuf buffer) {
875875
return LastHttpContent.EMPTY_LAST_CONTENT;
876876
}
877877

878-
CharSequence lastHeader = null;
879878
if (trailer == null) {
880879
trailer = this.trailer = new DefaultLastHttpContent(Unpooled.EMPTY_BUFFER, trailersFactory);
881880
}
882881
while (lineLength > 0) {
883882
final byte[] lineContent = line.array();
884883
final int startLine = line.arrayOffset() + line.readerIndex();
885884
final byte firstChar = lineContent[startLine];
886-
if (lastHeader != null && (firstChar == ' ' || firstChar == '\t')) {
887-
List<String> current = trailer.trailingHeaders().getAll(lastHeader);
888-
if (!current.isEmpty()) {
889-
int lastPos = current.size() - 1;
890-
//please do not make one line from below code
891-
//as it breaks +XX:OptimizeStringConcat optimization
892-
String lineTrimmed = langAsciiString(lineContent, startLine, line.readableBytes()).trim();
893-
String currentLastPos = current.get(lastPos);
894-
current.set(lastPos, currentLastPos + lineTrimmed);
895-
}
885+
if (name != null && (firstChar == ' ' || firstChar == '\t')) {
886+
//please do not make one line from below code
887+
//as it breaks +XX:OptimizeStringConcat optimization
888+
String trimmedLine = langAsciiString(lineContent, startLine, lineLength).trim();
889+
String valueStr = value;
890+
value = valueStr + ' ' + trimmedLine;
896891
} else {
897-
splitHeader(lineContent, startLine, lineLength);
898-
AsciiString headerName = name;
899-
if (!HttpHeaderNames.CONTENT_LENGTH.contentEqualsIgnoreCase(headerName) &&
900-
!HttpHeaderNames.TRANSFER_ENCODING.contentEqualsIgnoreCase(headerName) &&
901-
!HttpHeaderNames.TRAILER.contentEqualsIgnoreCase(headerName)) {
902-
trailer.trailingHeaders().add(headerName, value);
892+
if (name != null) {
893+
if (!HttpHeaderNames.CONTENT_LENGTH.contentEqualsIgnoreCase(name) &&
894+
!HttpHeaderNames.TRANSFER_ENCODING.contentEqualsIgnoreCase(name) &&
895+
!HttpHeaderNames.TRAILER.contentEqualsIgnoreCase(name)) {
896+
trailer.trailingHeaders().add(name, value);
897+
}
903898
}
904-
lastHeader = name;
905-
// reset name and value fields
906-
name = null;
907-
value = null;
899+
splitHeader(lineContent, startLine, lineLength);
908900
}
901+
909902
line = headerParser.parse(buffer, defaultStrictCRLFCheck);
910903
if (line == null) {
911904
return null;
912905
}
913906
lineLength = line.readableBytes();
914907
}
915908

909+
// Add the last trailer
910+
if (name != null) {
911+
if (!HttpHeaderNames.CONTENT_LENGTH.contentEqualsIgnoreCase(name) &&
912+
!HttpHeaderNames.TRANSFER_ENCODING.contentEqualsIgnoreCase(name) &&
913+
!HttpHeaderNames.TRAILER.contentEqualsIgnoreCase(name)) {
914+
trailer.trailingHeaders().add(name, value);
915+
}
916+
}
917+
918+
// reset name and value fields
919+
name = null;
920+
value = null;
921+
916922
this.trailer = null;
917923
return trailer;
918924
}
Collapse file

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

Copy file name to clipboardExpand all lines: codec-http/src/test/java/io/netty/handler/codec/http/HttpRequestDecoderTest.java
+84Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,90 @@ public void testEmptyHeaderValue() {
269269
assertEquals("", req.headers().get(of("EmptyHeader")));
270270
}
271271

272+
@Test
273+
public void testSingleTrailingHeader() {
274+
EmbeddedChannel channel = new EmbeddedChannel(new HttpRequestDecoder());
275+
String request = "POST / HTTP/1.1\r\n" +
276+
"Host: localhost\r\n" +
277+
"Transfer-Encoding: chunked\r\n" +
278+
"\r\n" +
279+
"5\r\n" +
280+
"hello\r\n" +
281+
"0\r\n" +
282+
"X-Checksum: abc123\r\n" +
283+
"\r\n";
284+
assertTrue(channel.writeInbound(Unpooled.copiedBuffer(request, CharsetUtil.US_ASCII)));
285+
HttpRequest req = channel.readInbound();
286+
assertFalse(req.decoderResult().isFailure());
287+
HttpContent body = channel.readInbound();
288+
body.release();
289+
LastHttpContent last = channel.readInbound();
290+
assertFalse(last.decoderResult().isFailure());
291+
assertEquals("abc123", last.trailingHeaders().get(of("X-Checksum")));
292+
last.release();
293+
assertFalse(channel.finish());
294+
}
295+
296+
@Test
297+
public void testMultiLineTrailingHeader() {
298+
// Regression: folded trailer values previously threw UnsupportedOperationException
299+
// because trailingHeaders().getAll() returns an AbstractList that does not implement set().
300+
EmbeddedChannel channel = new EmbeddedChannel(new HttpRequestDecoder());
301+
String request = "POST / HTTP/1.1\r\n" +
302+
"Host: localhost\r\n" +
303+
"Transfer-Encoding: chunked\r\n" +
304+
"\r\n" +
305+
"5\r\n" +
306+
"hello\r\n" +
307+
"0\r\n" +
308+
"X-Long: part1\r\n" +
309+
" part2\r\n" +
310+
"\t\t\t part3\r\n" +
311+
"X-Short: value\r\n" +
312+
"\r\n";
313+
assertTrue(channel.writeInbound(Unpooled.copiedBuffer(request, CharsetUtil.US_ASCII)));
314+
HttpRequest req = channel.readInbound();
315+
assertFalse(req.decoderResult().isFailure());
316+
HttpContent body = channel.readInbound();
317+
body.release();
318+
LastHttpContent last = channel.readInbound();
319+
assertFalse(last.decoderResult().isFailure());
320+
assertEquals("part1 part2 part3", last.trailingHeaders().get(of("X-Long")));
321+
assertEquals("value", last.trailingHeaders().get(of("X-Short")));
322+
last.release();
323+
assertFalse(channel.finish());
324+
}
325+
326+
@Test
327+
public void testForbiddenTrailingHeadersAreDropped() {
328+
EmbeddedChannel channel = new EmbeddedChannel(new HttpRequestDecoder());
329+
String request = "POST / HTTP/1.1\r\n" +
330+
"Host: localhost\r\n" +
331+
"Transfer-Encoding: chunked\r\n" +
332+
"\r\n" +
333+
"5\r\n" +
334+
"hello\r\n" +
335+
"0\r\n" +
336+
HttpHeaderNames.CONTENT_LENGTH + ": 5\r\n" +
337+
HttpHeaderNames.TRANSFER_ENCODING + ": chunked\r\n" +
338+
"X-Custom: keep\r\n" +
339+
HttpHeaderNames.TRAILER + ": X-Checksum\r\n" + // covering post-loop flush path
340+
"\r\n";
341+
assertTrue(channel.writeInbound(Unpooled.copiedBuffer(request, CharsetUtil.US_ASCII)));
342+
HttpRequest req = channel.readInbound();
343+
assertFalse(req.decoderResult().isFailure());
344+
HttpContent body = channel.readInbound();
345+
body.release();
346+
LastHttpContent last = channel.readInbound();
347+
assertFalse(last.decoderResult().isFailure());
348+
assertNull(last.trailingHeaders().get(HttpHeaderNames.CONTENT_LENGTH));
349+
assertNull(last.trailingHeaders().get(HttpHeaderNames.TRANSFER_ENCODING));
350+
assertNull(last.trailingHeaders().get(HttpHeaderNames.TRAILER));
351+
assertEquals("keep", last.trailingHeaders().get(of("X-Custom")));
352+
last.release();
353+
assertFalse(channel.finish());
354+
}
355+
272356
@Test
273357
public void test100Continue() {
274358
HttpRequestDecoder decoder = new HttpRequestDecoder();
Collapse file

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

Copy file name to clipboardExpand all lines: codec-http/src/test/java/io/netty/handler/codec/http/HttpResponseDecoderTest.java
+56Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,62 @@ private static void testLastResponseWithTrailingHeaderFragmented(byte[] content,
665665
assertNull(ch.readInbound());
666666
}
667667

668+
@Test
669+
public void testMultiLineTrailingHeader() {
670+
// Regression: folded trailer values previously threw UnsupportedOperationException
671+
// because trailingHeaders().getAll() returns an AbstractList that does not implement set().
672+
EmbeddedChannel ch = new EmbeddedChannel(new HttpResponseDecoder());
673+
String response = "HTTP/1.1 200 OK\r\n" +
674+
"Transfer-Encoding: chunked\r\n" +
675+
"\r\n" +
676+
"0\r\n" +
677+
"X-Long: part1\r\n" +
678+
" part2\r\n" +
679+
"\t\t\t part3\r\n" +
680+
"X-Short: value\r\n" +
681+
"\r\n";
682+
assertTrue(ch.writeInbound(Unpooled.copiedBuffer(response, CharsetUtil.US_ASCII)));
683+
HttpResponse res = ch.readInbound();
684+
assertFalse(res.decoderResult().isFailure());
685+
assertSame(HttpVersion.HTTP_1_1, res.protocolVersion());
686+
assertEquals(HttpResponseStatus.OK, res.status());
687+
688+
LastHttpContent last = ch.readInbound();
689+
assertFalse(last.decoderResult().isFailure());
690+
assertEquals("part1 part2 part3", last.trailingHeaders().get(of("X-Long")));
691+
assertEquals("value", last.trailingHeaders().get(of("X-Short")));
692+
last.release();
693+
assertFalse(ch.finish());
694+
}
695+
696+
@Test
697+
public void testForbiddenTrailingHeadersAreDropped() {
698+
EmbeddedChannel ch = new EmbeddedChannel(new HttpResponseDecoder());
699+
String response = "HTTP/1.1 200 OK\r\n" +
700+
"Transfer-Encoding: chunked\r\n" +
701+
"\r\n" +
702+
"0\r\n" +
703+
HttpHeaderNames.CONTENT_LENGTH + ": 5\r\n" +
704+
HttpHeaderNames.TRANSFER_ENCODING + ": chunked\r\n" +
705+
"X-Custom: keep\r\n" +
706+
HttpHeaderNames.TRAILER + ": X-Checksum\r\n" + // covering post-loop flush path
707+
"\r\n";
708+
assertTrue(ch.writeInbound(Unpooled.copiedBuffer(response, CharsetUtil.US_ASCII)));
709+
HttpResponse res = ch.readInbound();
710+
assertFalse(res.decoderResult().isFailure());
711+
assertSame(HttpVersion.HTTP_1_1, res.protocolVersion());
712+
assertEquals(HttpResponseStatus.OK, res.status());
713+
714+
LastHttpContent last = ch.readInbound();
715+
assertFalse(last.decoderResult().isFailure());
716+
assertNull(last.trailingHeaders().get(HttpHeaderNames.CONTENT_LENGTH));
717+
assertNull(last.trailingHeaders().get(HttpHeaderNames.TRANSFER_ENCODING));
718+
assertNull(last.trailingHeaders().get(HttpHeaderNames.TRAILER));
719+
assertEquals("keep", last.trailingHeaders().get(of("X-Custom")));
720+
last.release();
721+
assertFalse(ch.finish());
722+
}
723+
668724
@Test
669725
public void testResponseWithContentLength() {
670726
EmbeddedChannel ch = new EmbeddedChannel(new HttpResponseDecoder());

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.