Skip to content

Commit 994bef6

Browse files
authored
🐛 Charset quality factor (#344)
* ✅ add unit test for charset in content type header with quality factor * 🐛 fix bug where charset with quality factor fails to parse
1 parent 306eaa4 commit 994bef6

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

javaagent-core/src/main/java/org/hypertrace/agent/core/instrumentation/utils/ContentTypeUtils.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public class ContentTypeUtils {
2121
private ContentTypeUtils() {}
2222

2323
private static final String CHARSET_EQUALS = "charset=";
24+
private static final String SEPARATOR = ";";
2425

2526
/**
2627
* Returns true if the request/response with this content type should be captured.
@@ -51,7 +52,13 @@ public static String parseCharset(String contentType) {
5152

5253
int indexOfEncoding = indexOfCharset + CHARSET_EQUALS.length();
5354
if (indexOfEncoding < contentType.length()) {
54-
return contentType.substring(indexOfEncoding, contentType.length());
55+
String substring = contentType.substring(indexOfEncoding, contentType.length());
56+
int semicolonIndex = substring.indexOf(SEPARATOR);
57+
if (semicolonIndex == -1) {
58+
return substring;
59+
} else {
60+
return substring.substring(0, semicolonIndex);
61+
}
5562
}
5663
return null;
5764
}

javaagent-core/src/test/java/org/hypertrace/agent/core/instrumentation/utils/ContentTypeUtilsTest.java

+11
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,15 @@ public void noCharset() {
5858
Assertions.assertEquals(
5959
null, ContentTypeUtils.parseCharset("Content-Type: application/json; charset="));
6060
}
61+
62+
/**
63+
* Charsets can contain a "quality factor" per <a
64+
* href="https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.2"></a>
65+
*/
66+
@Test
67+
void charsetWithCharsetQualityFactor() {
68+
Assertions.assertEquals(
69+
"utf-8",
70+
ContentTypeUtils.parseCharset("Content-Type: application/json; charset=utf-8;q=.2"));
71+
}
6172
}

0 commit comments

Comments
 (0)