Skip to content

Commit 0674fd1

Browse files
author
Stephane Landelle
committed
Remove ugly hack that consists of passing the boundary from MultipartRequestEntity to MultipartBody in the Content-Type
1 parent edc8175 commit 0674fd1

File tree

7 files changed

+14
-95
lines changed

7 files changed

+14
-95
lines changed

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import java.util.Set;
2929

3030
import org.asynchttpclient.RandomAccessBody;
31-
import org.asynchttpclient.util.StandardCharsets;
3231
import org.slf4j.Logger;
3332
import org.slf4j.LoggerFactory;
3433

@@ -55,8 +54,8 @@ enum FileLocation {
5554
NONE, START, MIDDLE, END
5655
}
5756

58-
public MultipartBody(List<Part> parts, String contentType, long contentLength) {
59-
this.boundary = contentType.substring(contentType.indexOf("boundary=") + "boundary=".length()).getBytes(StandardCharsets.US_ASCII);
57+
public MultipartBody(List<Part> parts, String contentType, long contentLength, byte[] boundary) {
58+
this.boundary = boundary;
6059
this.contentLength = contentLength;
6160
this.parts = parts;
6261
}

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

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717

1818
import static org.asynchttpclient.util.MiscUtil.*;
1919

20-
import java.io.IOException;
21-
import java.io.OutputStream;
2220
import java.util.List;
2321
import java.util.Random;
2422

@@ -56,11 +54,6 @@ private static byte[] generateMultipartBoundary() {
5654
return bytes;
5755
}
5856

59-
/**
60-
* The MIME parts as set by the constructor
61-
*/
62-
private final List<Part> parts;
63-
6457
private final byte[] multipartBoundary;
6558

6659
private final String contentType;
@@ -77,7 +70,6 @@ public MultipartRequestEntity(List<Part> parts, FluentCaseInsensitiveStringsMap
7770
if (parts == null) {
7871
throw new IllegalArgumentException("parts cannot be null");
7972
}
80-
this.parts = parts;
8173
String contentTypeHeader = requestHeaders.getFirstValue("Content-Type");
8274
if (isNonEmpty(contentTypeHeader)) {
8375
int boundaryLocation = contentTypeHeader.indexOf("boundary=");
@@ -111,26 +103,10 @@ private String computeContentType(String base) {
111103
*
112104
* @return The boundary string of this entity in ASCII encoding.
113105
*/
114-
protected byte[] getMultipartBoundary() {
106+
public byte[] getMultipartBoundary() {
115107
return multipartBoundary;
116108
}
117109

118-
/**
119-
* Returns <code>true</code> if all parts are repeatable, <code>false</code> otherwise.
120-
*/
121-
public boolean isRepeatable() {
122-
for (Part part : parts) {
123-
if (!part.isRepeatable()) {
124-
return false;
125-
}
126-
}
127-
return true;
128-
}
129-
130-
public void writeRequest(OutputStream out) throws IOException {
131-
Part.sendParts(out, parts, multipartBoundary);
132-
}
133-
134110
public long getContentLength() {
135111
return contentLength;
136112
}

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

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -406,36 +406,6 @@ public String toString() {
406406
return this.getName();
407407
}
408408

409-
/**
410-
* Write all parts and the last boundary to the specified output stream.
411-
*
412-
* @param out
413-
* The stream to write to.
414-
* @param parts
415-
* The parts to write.
416-
* @param partBoundary
417-
* The ASCII bytes to use as the part boundary.
418-
* @throws IOException
419-
* If an I/O error occurs while writing the parts.
420-
* @since 3.0
421-
*/
422-
public static void sendParts(OutputStream out, List<Part> parts, byte[] partBoundary) throws IOException {
423-
424-
if (parts == null) {
425-
throw new IllegalArgumentException("Parts may not be null");
426-
}
427-
if (partBoundary == null || partBoundary.length == 0) {
428-
throw new IllegalArgumentException("partBoundary may not be empty");
429-
}
430-
for (Part part : parts) {
431-
part.send(out, partBoundary);
432-
}
433-
out.write(EXTRA_BYTES);
434-
out.write(partBoundary);
435-
out.write(EXTRA_BYTES);
436-
out.write(CRLF_BYTES);
437-
}
438-
439409
public static void sendMessageEnd(OutputStream out, byte[] partBoundary) throws IOException {
440410

441411
if (partBoundary == null || partBoundary.length == 0) {

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

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -15,49 +15,22 @@
1515
*/
1616
package org.asynchttpclient.multipart;
1717

18-
import java.io.IOException;
19-
import java.io.OutputStream;
20-
21-
/**
22-
* This class is an adaptation of the Apache HttpClient implementation
23-
*
24-
* @link http://hc.apache.org/httpclient-3.x/
25-
*/
2618
public interface RequestEntity {
2719

2820
/**
29-
* Tests if {@link #writeRequest(java.io.OutputStream)} can be called more than once.
30-
*
31-
* @return <tt>true</tt> if the entity can be written to {@link java.io.OutputStream} more than once,
32-
* <tt>false</tt> otherwise.
33-
*/
34-
boolean isRepeatable();
35-
36-
/**
37-
* Writes the request entity to the given stream.
38-
*
39-
* @param out
40-
* @throws java.io.IOException
41-
*/
42-
void writeRequest(OutputStream out) throws IOException;
43-
44-
/**
45-
* Gets the request entity's length. This method should return a non-negative value if the content
46-
* length is known or a negative value if it is not. In the latter case the
47-
* EntityEnclosingMethod will use chunk encoding to
48-
* transmit the request entity.
49-
*
50-
* @return a non-negative value when content length is known or a negative value when content length
51-
* is not known
21+
* Gets the request entity's length. This method should return a non-negative value if the content length is known or a negative value if it is not. In the latter case the
22+
* EntityEnclosingMethod will use chunk encoding to transmit the request entity.
23+
*
24+
* @return a non-negative value when content length is known or a negative value when content length is not known
5225
*/
5326
long getContentLength();
5427

5528
/**
56-
* Gets the entity's content type. This content type will be used as the value for the
57-
* "Content-Type" header.
58-
*
29+
* Gets the entity's content type. This content type will be used as the value for the "Content-Type" header.
30+
*
5931
* @return the entity's content type
6032
*/
6133
String getContentType();
6234

35+
byte[] getMultipartBoundary();
6336
}

api/src/test/java/org/asynchttpclient/multipart/MultipartBodyTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ private static void compareContentLength(final List<Part> parts) {
6868
final long expectedContentLength = mre.getContentLength();
6969

7070
// get real bytes
71-
final Body multipartBody = new MultipartBody(parts, mre.getContentType(), expectedContentLength);
71+
final Body multipartBody = new MultipartBody(parts, mre.getContentType(), expectedContentLength, mre.getMultipartBoundary());
7272
try {
7373
final ByteBuffer buffer = ByteBuffer.allocate(8192);
7474
boolean last = false;

providers/grizzly/src/main/java/org/asynchttpclient/providers/grizzly/bodyhandler/PartsBodyHandler.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public boolean doHandle(final FilterChainContext ctx,
4949
final MultipartRequestEntity mre = new MultipartRequestEntity(parts, request.getHeaders());
5050
final long contentLength = mre.getContentLength();
5151
final String contentType = mre.getContentType();
52+
final byte[] multipartBoundary = mre.getMultipartBoundary();
5253
requestPacket.setContentLengthLong(contentLength);
5354
requestPacket.setContentType(contentType);
5455
if (GrizzlyAsyncHttpProvider.LOGGER.isDebugEnabled()) {
@@ -63,7 +64,7 @@ public boolean doHandle(final FilterChainContext ctx,
6364
final FeedableBodyGenerator generator = new FeedableBodyGenerator() {
6465
@Override
6566
public Body createBody() throws IOException {
66-
return new MultipartBody(parts, contentType, contentLength);
67+
return new MultipartBody(parts, contentType, contentLength, multipartBoundary);
6768
}
6869
};
6970
generator.setFeeder(new FeedableBodyGenerator.BaseFeeder(generator) {

providers/netty/src/main/java/org/asynchttpclient/providers/netty/request/body/NettyMultipartBody.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public NettyMultipartBody(List<Part> parts, FluentCaseInsensitiveStringsMap head
3232
MultipartRequestEntity mre = new MultipartRequestEntity(parts, headers);
3333
contentType = mre.getContentType();
3434
contentLength = mre.getContentLength();
35-
multipartBody = new MultipartBody(parts, contentType, contentLength);
35+
multipartBody = new MultipartBody(parts, contentType, contentLength, mre.getMultipartBoundary());
3636
}
3737

3838
public MultipartBody getMultipartBody() {

0 commit comments

Comments
 (0)