Skip to content

Commit d5622dc

Browse files
committed
MultipartEntityBuilder to include a random UUID in the boundary value by default
1 parent 8d0f3b1 commit d5622dc

File tree

2 files changed

+6
-21
lines changed

2 files changed

+6
-21
lines changed

httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/MultipartEntityBuilder.java

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,12 @@
2929

3030
import java.io.File;
3131
import java.io.InputStream;
32-
import java.nio.CharBuffer;
3332
import java.nio.charset.Charset;
3433
import java.nio.charset.StandardCharsets;
3534
import java.util.ArrayList;
3635
import java.util.Collections;
3736
import java.util.List;
38-
import java.util.concurrent.ThreadLocalRandom;
37+
import java.util.UUID;
3938

4039
import org.apache.hc.core5.http.ContentType;
4140
import org.apache.hc.core5.http.HttpEntity;
@@ -46,22 +45,16 @@
4645
/**
4746
* Builder for multipart {@link HttpEntity}s.
4847
* <p>
49-
* IMPORTANT: it is responsibility of the caller to validate / sanitize content of body
50-
* parts, for instance, to ensure they do not contain the boundary value that can prevent
51-
* the consumer of the entity from correctly parsing / processing the body parts.
48+
* IMPORTANT: it is responsibility of the caller to validate / sanitize content of body
49+
* parts. For instance, when using an explicit boundary, it's the caller's responsibility to
50+
* ensure the body parts do not contain the boundary value, which can prevent the consumer of
51+
* the entity from correctly parsing / processing the body parts.
5252
* </p>
5353
*
5454
* @since 5.0
5555
*/
5656
public class MultipartEntityBuilder {
5757

58-
/**
59-
* The pool of ASCII chars to be used for generating a multipart boundary.
60-
*/
61-
private final static char[] MULTIPART_CHARS =
62-
"-_1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
63-
.toCharArray();
64-
6558
private ContentType contentType;
6659
private HttpMultipartMode mode = HttpMultipartMode.STRICT;
6760
private String boundary;
@@ -237,14 +230,7 @@ public MultipartEntityBuilder addEpilogue(final String epilogue) {
237230
}
238231

239232
private String generateBoundary() {
240-
final ThreadLocalRandom rand = ThreadLocalRandom.current();
241-
final int count = rand.nextInt(30, 41); // a random size from 30 to 40
242-
final CharBuffer buffer = CharBuffer.allocate(count);
243-
while (buffer.hasRemaining()) {
244-
buffer.put(MULTIPART_CHARS[rand.nextInt(MULTIPART_CHARS.length)]);
245-
}
246-
buffer.flip();
247-
return buffer.toString();
233+
return "httpclient_boundary_" + UUID.randomUUID();
248234
}
249235

250236
MultipartFormEntity buildEntity() {

httpclient5/src/test/java/org/apache/hc/client5/http/entity/mime/TestMultipartFormHttpEntity.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ void testImplicitContractorParams() {
7979
Assertions.assertNotNull(boundary);
8080

8181
Assertions.assertTrue(boundary.length() >= 30);
82-
Assertions.assertTrue(boundary.length() <= 40);
8382

8483
final NameValuePair p2 = elem.getParameterByName("charset");
8584
Assertions.assertNull(p2);

0 commit comments

Comments
 (0)