Skip to content

Commit bc00e36

Browse files
committed
Use UTF-8 instead of US-ASCII when no charset parameter is present.
1 parent d8798b6 commit bc00e36

1 file changed

Lines changed: 7 additions & 24 deletions

File tree

  • httpclient5/src/main/java/org/apache/hc/client5/http/async/methods

httpclient5/src/main/java/org/apache/hc/client5/http/async/methods/SimpleBody.java

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -50,31 +50,13 @@ public final class SimpleBody {
5050
this.contentType = contentType;
5151
}
5252

53-
private static Charset getCharset(final ContentType contentType) {
54-
if (contentType != null) {
55-
final Charset charset = contentType.getCharset();
56-
if (charset != null) {
57-
return charset;
58-
}
59-
final String mimeType = contentType.getMimeType();
60-
if (contentType.isSameMimeType(ContentType.APPLICATION_JSON)
61-
|| contentType.isSameMimeType(ContentType.APPLICATION_NDJSON)
62-
|| contentType.isSameMimeType(ContentType.APPLICATION_PROBLEM_JSON)
63-
|| mimeType != null && mimeType.length() >= 5
64-
&& mimeType.regionMatches(true, mimeType.length() - 5, "+json", 0, 5)) {
65-
return StandardCharsets.UTF_8;
66-
}
67-
return StandardCharsets.US_ASCII;
68-
}
69-
return ContentType.DEFAULT_TEXT.getCharset();
70-
}
71-
7253
static SimpleBody create(final String body, final ContentType contentType) {
7354
Args.notNull(body, "Body");
7455
if (body.length() > 2048) {
7556
return new SimpleBody(null, body, contentType);
7657
}
77-
final byte[] bytes = body.getBytes(getCharset(contentType));
58+
final Charset charset = (contentType != null ? contentType : ContentType.DEFAULT_TEXT).getCharset(StandardCharsets.UTF_8);
59+
final byte[] bytes = body.getBytes(charset);
7860
return new SimpleBody(bytes, null, contentType);
7961
}
8062

@@ -101,7 +83,8 @@ public byte[] getBodyBytes() {
10183
if (bodyAsBytes != null) {
10284
return bodyAsBytes;
10385
} else if (bodyAsText != null) {
104-
return bodyAsText.getBytes(getCharset(contentType));
86+
final Charset charset = (contentType != null ? contentType : ContentType.DEFAULT_TEXT).getCharset(StandardCharsets.UTF_8);
87+
return bodyAsText.getBytes(charset);
10588
} else {
10689
return null;
10790
}
@@ -114,7 +97,8 @@ public byte[] getBodyBytes() {
11497
*/
11598
public String getBodyText() {
11699
if (bodyAsBytes != null) {
117-
return new String(bodyAsBytes, getCharset(contentType));
100+
final Charset charset = (contentType != null ? contentType : ContentType.DEFAULT_TEXT).getCharset(StandardCharsets.UTF_8);
101+
return new String(bodyAsBytes, charset);
118102
}
119103
return bodyAsText;
120104
}
@@ -143,5 +127,4 @@ public String toString() {
143127
", content type=" + contentType + "}";
144128
}
145129

146-
}
147-
130+
}

0 commit comments

Comments
 (0)