Skip to content

Commit 3771339

Browse files
authored
Optimize parseCharset method for best performance. (#15203)
1 parent ace86c9 commit 3771339

File tree

4 files changed

+15
-10
lines changed

4 files changed

+15
-10
lines changed

dubbo-remoting/dubbo-remoting-http12/src/main/java/org/apache/dubbo/remoting/http12/HttpUtils.java

+8-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package org.apache.dubbo.remoting.http12;
1818

19+
import org.apache.dubbo.common.constants.CommonConstants;
1920
import org.apache.dubbo.common.io.StreamUtils;
2021
import org.apache.dubbo.common.utils.StringUtils;
2122
import org.apache.dubbo.remoting.http12.exception.DecodeException;
@@ -81,7 +82,7 @@ public static List<HttpCookie> decodeCookies(String value) {
8182
return cookies;
8283
}
8384

84-
public static String getCharsetFromContentType(String contentType) {
85+
public static String parseCharset(String contentType) {
8586
String charset = null;
8687
if (contentType == null) {
8788
charset = StringUtils.EMPTY_STRING;
@@ -91,7 +92,12 @@ public static String getCharsetFromContentType(String contentType) {
9192
charset = StringUtils.EMPTY_STRING;
9293
} else {
9394
charset = contentType.substring(index + CHARSET_PREFIX.length()).trim();
94-
charset = charset.split(";")[0];
95+
int splits = charset.indexOf(CommonConstants.SEMICOLON_SEPARATOR);
96+
if (splits == -1) {
97+
return charset;
98+
} else {
99+
return charset.substring(0, splits).trim();
100+
}
95101
}
96102
}
97103
return charset;

dubbo-remoting/dubbo-remoting-http12/src/main/java/org/apache/dubbo/remoting/http12/message/DefaultHttpRequest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ public String charset() {
243243
String charset = this.charset;
244244
if (charset == null) {
245245
String contentType = contentType();
246-
charset = HttpUtils.getCharsetFromContentType(contentType);
246+
charset = HttpUtils.parseCharset(contentType);
247247
this.charset = charset;
248248
}
249249
return charset.isEmpty() ? null : charset;

dubbo-remoting/dubbo-remoting-http12/src/main/java/org/apache/dubbo/remoting/http12/message/DefaultHttpResponse.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ public String charset() {
166166
String charset = this.charset;
167167
if (charset == null) {
168168
String contentType = contentType();
169-
charset = HttpUtils.getCharsetFromContentType(contentType);
169+
charset = HttpUtils.parseCharset(contentType);
170170
this.charset = charset;
171171
}
172172
return charset.isEmpty() ? null : charset;

dubbo-remoting/dubbo-remoting-http12/src/test/java/org/apache/dubbo/remoting/http12/message/codec/HttpUtilsTest.java

+5-6
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,14 @@
2424
public class HttpUtilsTest {
2525

2626
@Test
27-
void testGetCharsetFromContentType() {
28-
String charset = HttpUtils.getCharsetFromContentType("text/html;charset=utf-8");
27+
void testParseCharset() {
28+
String charset = HttpUtils.parseCharset("text/html;charset=utf-8");
2929
Assertions.assertEquals("utf-8", charset);
30-
charset = HttpUtils.getCharsetFromContentType("text/html");
30+
charset = HttpUtils.parseCharset("text/html");
3131
Assertions.assertEquals("", charset);
32-
charset = HttpUtils.getCharsetFromContentType("application/json;charset=utf-8; boundary=__X_PAW_BOUNDARY__");
32+
charset = HttpUtils.parseCharset("application/json;charset=utf-8; boundary=__X_PAW_BOUNDARY__");
3333
Assertions.assertEquals("utf-8", charset);
34-
charset =
35-
HttpUtils.getCharsetFromContentType("multipart/form-data; charset=utf-8; boundary=__X_PAW_BOUNDARY__");
34+
charset = HttpUtils.parseCharset("multipart/form-data; charset=utf-8; boundary=__X_PAW_BOUNDARY__");
3635
Assertions.assertEquals("utf-8", charset);
3736
}
3837
}

0 commit comments

Comments
 (0)