Skip to content

Commit eaf6109

Browse files
author
Stephane Landelle
committed
Fix NPE when passing a null charset, close AsyncHttpClient#651
1 parent cec8290 commit eaf6109

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

api/src/main/java/org/asynchttpclient/multipart/StringPart.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import java.nio.channels.WritableByteChannel;
1919
import java.nio.charset.Charset;
2020

21+
import org.asynchttpclient.util.StandardCharsets;
22+
2123
public class StringPart extends PartBase {
2224

2325
/**
@@ -28,7 +30,7 @@ public class StringPart extends PartBase {
2830
/**
2931
* Default charset of string parameters
3032
*/
31-
public static final String DEFAULT_CHARSET = "US-ASCII";
33+
public static final Charset DEFAULT_CHARSET = StandardCharsets.US_ASCII;
3234

3335
/**
3436
* Default transfer encoding of string parameters
@@ -40,6 +42,10 @@ public class StringPart extends PartBase {
4042
*/
4143
private final byte[] content;
4244

45+
private static Charset charsetOrDefault(String charset) {
46+
return charset == null ? DEFAULT_CHARSET : Charset.forName(charset);
47+
}
48+
4349
public StringPart(String name, String value, String charset) {
4450
this(name, value, charset, null);
4551
}
@@ -58,15 +64,13 @@ public StringPart(String name, String value, String charset) {
5864
*/
5965
public StringPart(String name, String value, String charset, String contentId) {
6066

61-
super(name, DEFAULT_CONTENT_TYPE, charset == null ? DEFAULT_CHARSET : charset, DEFAULT_TRANSFER_ENCODING, contentId);
62-
if (value == null) {
67+
super(name, DEFAULT_CONTENT_TYPE, charsetOrDefault(charset).name(), DEFAULT_TRANSFER_ENCODING, contentId);
68+
if (value == null)
6369
throw new NullPointerException("value");
64-
}
65-
if (value.indexOf(0) != -1) {
70+
if (value.indexOf(0) != -1)
6671
// See RFC 2048, 2.8. "8bit Data"
6772
throw new IllegalArgumentException("NULs may not be present in string parts");
68-
}
69-
content = value.getBytes(Charset.forName(charset));
73+
content = value.getBytes(charsetOrDefault(charset));
7074
}
7175

7276
/**

0 commit comments

Comments
 (0)