Skip to content
Merged
Show file tree
Hide file tree
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 @@ -16,6 +16,7 @@
*/
package org.apache.dubbo.remoting.http12;

import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.io.StreamUtils;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.remoting.http12.exception.DecodeException;
Expand Down Expand Up @@ -81,7 +82,7 @@ public static List<HttpCookie> decodeCookies(String value) {
return cookies;
}

public static String getCharsetFromContentType(String contentType) {
public static String parseCharset(String contentType) {
String charset = null;
if (contentType == null) {
charset = StringUtils.EMPTY_STRING;
Expand All @@ -91,7 +92,12 @@ public static String getCharsetFromContentType(String contentType) {
charset = StringUtils.EMPTY_STRING;
} else {
charset = contentType.substring(index + CHARSET_PREFIX.length()).trim();
charset = charset.split(";")[0];
int splits = charset.indexOf(CommonConstants.SEMICOLON_SEPARATOR);
if (splits == -1) {
return charset;
} else {
return charset.substring(0, splits).trim();
}
}
}
return charset;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ public String charset() {
String charset = this.charset;
if (charset == null) {
String contentType = contentType();
charset = HttpUtils.getCharsetFromContentType(contentType);
charset = HttpUtils.parseCharset(contentType);
this.charset = charset;
}
return charset.isEmpty() ? null : charset;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public String charset() {
String charset = this.charset;
if (charset == null) {
String contentType = contentType();
charset = HttpUtils.getCharsetFromContentType(contentType);
charset = HttpUtils.parseCharset(contentType);
this.charset = charset;
}
return charset.isEmpty() ? null : charset;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,14 @@
public class HttpUtilsTest {

@Test
void testGetCharsetFromContentType() {
String charset = HttpUtils.getCharsetFromContentType("text/html;charset=utf-8");
void testParseCharset() {
String charset = HttpUtils.parseCharset("text/html;charset=utf-8");
Assertions.assertEquals("utf-8", charset);
charset = HttpUtils.getCharsetFromContentType("text/html");
charset = HttpUtils.parseCharset("text/html");
Assertions.assertEquals("", charset);
charset = HttpUtils.getCharsetFromContentType("application/json;charset=utf-8; boundary=__X_PAW_BOUNDARY__");
charset = HttpUtils.parseCharset("application/json;charset=utf-8; boundary=__X_PAW_BOUNDARY__");
Assertions.assertEquals("utf-8", charset);
charset =
HttpUtils.getCharsetFromContentType("multipart/form-data; charset=utf-8; boundary=__X_PAW_BOUNDARY__");
charset = HttpUtils.parseCharset("multipart/form-data; charset=utf-8; boundary=__X_PAW_BOUNDARY__");
Assertions.assertEquals("utf-8", charset);
}
}
Loading