Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce the garbage produced by GraphQL while handling the request #33676

Merged
merged 1 commit into from
Jun 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -242,14 +242,54 @@ private String getRequestAccept(RoutingContext ctx) {
return DEFAULT_RESPONSE_CONTENT_TYPE;
}

private String getCharset(String mimeType) {
if (mimeType != null && mimeType.contains(";")) {
String[] parts = mimeType.split(";");
for (String part : parts) {
if (part.trim().startsWith("charset")) {
return part.split("=")[1];
}
private static int indexOfSemicolonOrWhitespace(String s, int start) {
for (int i = start; i < s.length(); i++) {
final char c = s.charAt(i);
if (c == ';' || Character.isWhitespace(c)) {
return i;
}
}
return -1;
}

private static final String CHARSET_TOKEN = "charset=";
private static final String UTF_8_LOW = "utf-8";
private static final String UTF_8_HIGH = "UTF-8";

private static String parseCharset(String mimeType) {
if (mimeType == null) {
return null;
}
final int charsetIndex = mimeType.indexOf(CHARSET_TOKEN);
if (charsetIndex == -1) {
return null;
}
final int charsetValueStart = charsetIndex + CHARSET_TOKEN.length();
final int firstSemicolonWhitespace = indexOfSemicolonOrWhitespace(mimeType, charsetValueStart);
final int charsetValueEnd = firstSemicolonWhitespace == -1 ? mimeType.length() : firstSemicolonWhitespace;
final int charsetLen = charsetValueEnd - charsetValueStart;
if (charsetLen == 0) {
return null;
}
return charsetOf(mimeType, charsetValueStart, charsetLen);
}

private static String charsetOf(String mimeType, int charsetValueStart, int charsetLen) {
franz1981 marked this conversation as resolved.
Show resolved Hide resolved
if (charsetLen == UTF_8_LOW.length()) {
if (mimeType.regionMatches(charsetValueStart, UTF_8_LOW, 0, UTF_8_LOW.length())) {
return UTF_8_LOW;
}
if (mimeType.regionMatches(charsetValueStart, UTF_8_HIGH, 0, UTF_8_LOW.length())) {
return UTF_8_HIGH;
}
}
return mimeType.substring(charsetValueStart, charsetValueStart + charsetLen);
}

private static String getCharset(String mimeType) {
final String parsedCharset = parseCharset(mimeType);
if (parsedCharset != null) {
return parsedCharset;
}
return StandardCharsets.UTF_8.name();
}
Expand Down