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: HttpUtil.getCharset() must accept whitespace before th… · netty/netty@15fbfbf

File tree

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

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

Copy file name to clipboardExpand all lines: codec-http/src/main/java/io/netty/handler/codec/http/HttpUtil.java
+20Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,18 @@ public static Charset getCharset(CharSequence contentTypeValue, Charset defaultC
451451
if (contentTypeValue != null) {
452452
CharSequence charsetRaw = getCharsetAsSequence(contentTypeValue);
453453
if (charsetRaw != null) {
454+
// Remove only the trailing optional whitespace (OWS) that RFC 9110 allows before
455+
// the semicolon starting the next parameter, see
456+
// https://www.rfc-editor.org/rfc/rfc9110#section-5.6.6
457+
// Leading whitespace after '=' is not grammar-legal and is deliberately kept, so
458+
// such values keep failing the charset lookup below.
459+
int end = charsetRaw.length();
460+
while (end > 0 && isOws(charsetRaw.charAt(end - 1))) {
461+
end--;
462+
}
463+
if (end < charsetRaw.length()) {
464+
charsetRaw = charsetRaw.subSequence(0, end);
465+
}
454466
if (charsetRaw.length() > 2) { // at least contains 2 quotes(")
455467
if (charsetRaw.charAt(0) == '"' && charsetRaw.charAt(charsetRaw.length() - 1) == '"') {
456468
charsetRaw = charsetRaw.subSequence(1, charsetRaw.length() - 1);
@@ -466,6 +478,14 @@ public static Charset getCharset(CharSequence contentTypeValue, Charset defaultC
466478
return defaultCharset;
467479
}
468480

481+
/**
482+
* Whether the given character is optional whitespace (OWS) as defined by
483+
* <a href="https://www.rfc-editor.org/rfc/rfc9110#section-5.6.3">RFC 9110 section 5.6.3</a>.
484+
*/
485+
private static boolean isOws(char c) {
486+
return c == ' ' || c == '\t';
487+
}
488+
469489
/**
470490
* Fetch charset from message's Content-Type header as a char sequence.
471491
*
Collapse file

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

Copy file name to clipboardExpand all lines: codec-http/src/test/java/io/netty/handler/codec/http/HttpUtilTest.java
+33Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.net.InetAddress;
2828
import java.net.InetSocketAddress;
2929
import java.net.URI;
30+
import java.nio.CharBuffer;
3031
import java.nio.charset.StandardCharsets;
3132
import java.util.ArrayList;
3233
import java.util.List;
@@ -160,6 +161,38 @@ private void testGetCharsetUtf8(String contentType) {
160161
assertEquals(CharsetUtil.UTF_8, HttpUtil.getCharset(UPPER_CASE_NORMAL_CONTENT_TYPE));
161162
}
162163

164+
@Test
165+
public void testGetCharsetWithWhitespaceBeforeNextParameter() {
166+
// RFC 9110 5.6.6: parameters = *( OWS ";" OWS [ parameter ] ), so optional
167+
// whitespace is allowed before the semicolon starting the next parameter
168+
testGetCharsetUtf8("text/html; charset=utf-8 ; action=\"foo\"");
169+
}
170+
171+
@Test
172+
public void testGetCharsetQuotedWithWhitespaceBeforeNextParameter() {
173+
testGetCharsetUtf8("text/html; charset=\"utf-8\" ; action=\"foo\"");
174+
}
175+
176+
@Test
177+
public void testGetCharsetWithWhitespaceBeforeNextParameterNonStringCharSequence() {
178+
// must behave identically for a general CharSequence implementation
179+
CharSequence contentType = CharBuffer.wrap("text/html; charset=utf-8 ; action=\"foo\"");
180+
assertEquals(CharsetUtil.UTF_8, HttpUtil.getCharset(contentType, CharsetUtil.ISO_8859_1));
181+
182+
CharSequence emptyCharset = CharBuffer.wrap("text/html; charset= ; action=\"foo\"");
183+
assertEquals(CharsetUtil.ISO_8859_1, HttpUtil.getCharset(emptyCharset, CharsetUtil.ISO_8859_1));
184+
}
185+
186+
@Test
187+
public void testGetCharsetWithLeadingWhitespaceIsNotAccepted() {
188+
// Only trailing OWS is grammar-legal per RFC 9110 5.6.6; whitespace after '='
189+
// is not, so such values keep falling back to the default charset.
190+
assertEquals(CharsetUtil.ISO_8859_1,
191+
HttpUtil.getCharset("text/html; charset= utf-8 ; action=\"foo\"", CharsetUtil.ISO_8859_1));
192+
assertEquals(CharsetUtil.ISO_8859_1,
193+
HttpUtil.getCharset("text/html; charset= ; action=\"foo\"", CharsetUtil.ISO_8859_1));
194+
}
195+
163196
@Test
164197
public void testGetCharsetNoLeadingQuotes() {
165198
testGetCharsetInvalidQuotes("text/html;charset=utf-8\"");

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.