Skip to content

Commit d8a2e0a

Browse files
author
Stephane Landelle
committed
1 parent b7a0ae2 commit d8a2e0a

10 files changed

+63
-54
lines changed

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,7 @@
468468
<exclude>**/Cookie</exclude>
469469
<exclude>**/Part</exclude>
470470
<exclude>**/PartBase</exclude>
471+
<exclude>**/MultipartRequestEntity</exclude>
471472
</excludes>
472473
</configuration>
473474
<executions>

src/main/java/com/ning/http/client/providers/grizzly/GrizzlyAsyncHttpProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2072,7 +2072,7 @@ public boolean doHandle(final FilterChainContext ctx,
20722072
MultipartRequestEntity mre =
20732073
AsyncHttpProviderUtils.createMultipartRequestEntity(
20742074
request.getParts(),
2075-
request.getParams());
2075+
request.getHeaders());
20762076
requestPacket.setContentLengthLong(mre.getContentLength());
20772077
requestPacket.setContentType(mre.getContentType());
20782078
final MemoryManager mm = ctx.getMemoryManager();

src/main/java/com/ning/http/client/providers/jdk/JDKAsyncHttpProvider.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import com.ning.http.util.ProxyUtils;
4444
import com.ning.http.util.SslUtils;
4545
import com.ning.http.util.UTF8UrlEncoder;
46+
4647
import org.slf4j.Logger;
4748
import org.slf4j.LoggerFactory;
4849

@@ -617,7 +618,7 @@ private void configure(URI uri, HttpURLConnection urlConnection, Request request
617618
lenght = MAX_BUFFERED_BYTES;
618619
}
619620

620-
MultipartRequestEntity mre = AsyncHttpProviderUtils.createMultipartRequestEntity(request.getParts(), request.getParams());
621+
MultipartRequestEntity mre = AsyncHttpProviderUtils.createMultipartRequestEntity(request.getParts(), request.getHeaders());
621622

622623
urlConnection.setRequestProperty("Content-Type", mre.getContentType());
623624
urlConnection.setRequestProperty("Content-Length", String.valueOf(mre.getContentLength()));

src/main/java/com/ning/http/client/providers/netty/NettyAsyncHttpProvider.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -554,9 +554,9 @@ public void operationComplete(ChannelFuture cf) {
554554
* TODO: AHC-78: SSL + zero copy isn't supported by the MultiPart class and pretty complex to implements.
555555
*/
556556
if (future.getRequest().getParts() != null) {
557-
String boundary = future.getNettyRequest().getHeader("Content-Type");
557+
String contentType = future.getNettyRequest().getHeader("Content-Type");
558558
String length = future.getNettyRequest().getHeader("Content-Length");
559-
body = new MultipartBody(future.getRequest().getParts(), boundary, length);
559+
body = new MultipartBody(future.getRequest().getParts(), contentType, length);
560560
}
561561

562562
ChannelFuture writeFuture;
@@ -858,8 +858,8 @@ else if (uri.getRawQuery() != null)
858858
lenght = MAX_BUFFERED_BYTES;
859859
}
860860

861-
MultipartRequestEntity mre = AsyncHttpProviderUtils.createMultipartRequestEntity(request.getParts(), request.getParams());
862-
861+
MultipartRequestEntity mre = AsyncHttpProviderUtils.createMultipartRequestEntity(request.getParts(), request.getHeaders());
862+
863863
nettyRequest.setHeader(HttpHeaders.Names.CONTENT_TYPE, mre.getContentType());
864864
nettyRequest.setHeader(HttpHeaders.Names.CONTENT_LENGTH, String.valueOf(mre.getContentLength()));
865865

src/main/java/com/ning/http/multipart/FilePart.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ public class FilePart extends PartBase {
6969
public FilePart(String name, PartSource partSource, String contentType, String charset, String contentId) {
7070

7171
super(name, contentType == null ? DEFAULT_CONTENT_TYPE : contentType, charset == null ? "ISO-8859-1" : charset, DEFAULT_TRANSFER_ENCODING, contentId);
72-
7372
if (partSource == null) {
7473
throw new IllegalArgumentException("Source may not be null");
7574
}
@@ -147,9 +146,9 @@ public FilePart(String name, String fileName, File file, String contentType, Str
147146
* @throws java.io.IOException If an IO problem occurs
148147
*/
149148
protected void sendDispositionHeader(OutputStream out) throws IOException {
150-
super.sendDispositionHeader(out);
151149
String filename = this.source.getFileName();
152150
if (filename != null) {
151+
super.sendDispositionHeader(out);
153152
out.write(FILE_NAME_BYTES);
154153
out.write(QUOTE_BYTES);
155154
out.write(MultipartEncodingUtil.getAsciiBytes(filename));

src/main/java/com/ning/http/multipart/FilePartSource.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,7 @@ public FilePartSource(File file) throws FileNotFoundException {
7070
public FilePartSource(String fileName, File file)
7171
throws FileNotFoundException {
7272
this(file);
73-
if (fileName != null) {
74-
this.fileName = fileName;
75-
}
73+
this.fileName = fileName;
7674
}
7775

7876
/**
@@ -96,7 +94,7 @@ public long getLength() {
9694
* @see PartSource#getFileName()
9795
*/
9896
public String getFileName() {
99-
return (fileName == null) ? "noname" : fileName;
97+
return fileName;
10098
}
10199

102100
/**

src/main/java/com/ning/http/multipart/MultipartBody.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,9 @@ public class MultipartBody implements RandomAccessBody {
4848

4949
enum FileLocation {NONE, START, MIDDLE, END}
5050

51-
public MultipartBody(List<com.ning.http.client.Part> parts, String boundary, String contentLength) {
52-
this.boundary = MultipartEncodingUtil.getAsciiBytes(boundary.substring("multipart/form-data; boundary=".length()));
51+
public MultipartBody(List<com.ning.http.client.Part> parts, String contentType, String contentLength) {
52+
this.boundary = MultipartEncodingUtil.getAsciiBytes(contentType.substring(contentType.indexOf("boundary=") + "boundary=".length()));
53+
5354
this.contentLength = Long.parseLong(contentLength);
5455
this.parts = parts;
5556

@@ -430,6 +431,7 @@ private ByteArrayOutputStream generateFileStart(FilePart filePart)
430431
filePart.sendDispositionHeader(overhead);
431432
filePart.sendContentTypeHeader(overhead);
432433
filePart.sendTransferEncodingHeader(overhead);
434+
filePart.sendContentIdHeader(overhead);
433435
filePart.sendEndOfHeader(overhead);
434436
return overhead;
435437
}

src/main/java/com/ning/http/multipart/MultipartRequestEntity.java

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
*/
1616
package com.ning.http.multipart;
1717

18-
import com.ning.http.client.FluentStringsMap;
18+
import static com.ning.http.util.MiscUtil.isNonEmpty;
19+
20+
import com.ning.http.client.FluentCaseInsensitiveStringsMap;
21+
1922
import org.slf4j.Logger;
2023
import org.slf4j.LoggerFactory;
2124

@@ -25,7 +28,7 @@
2528

2629
/**
2730
* This class is an adaptation of the Apache HttpClient implementation
28-
*
31+
*
2932
* @link http://hc.apache.org/httpclient-3.x/
3033
*/
3134
public class MultipartRequestEntity implements RequestEntity {
@@ -38,15 +41,14 @@ public class MultipartRequestEntity implements RequestEntity {
3841
/**
3942
* The pool of ASCII chars to be used for generating a multipart boundary.
4043
*/
41-
private static byte[] MULTIPART_CHARS = MultipartEncodingUtil.getAsciiBytes(
42-
"-_1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
44+
private static byte[] MULTIPART_CHARS = MultipartEncodingUtil.getAsciiBytes("-_1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
4345

4446
/**
4547
* Generates a random multipart boundary string.
46-
*
48+
*
4749
* @return
4850
*/
49-
private static byte[] generateMultipartBoundary() {
51+
public static byte[] generateMultipartBoundary() {
5052
Random rand = new Random();
5153
byte[] bytes = new byte[rand.nextInt(11) + 30]; // a random size from 30 to 40
5254
for (int i = 0; i < bytes.length; i++) {
@@ -64,42 +66,35 @@ private static byte[] generateMultipartBoundary() {
6466

6567
private byte[] multipartBoundary;
6668

67-
private FluentStringsMap methodParams;
69+
private final String contentType;
6870

6971
/**
7072
* Creates a new multipart entity containing the given parts.
71-
*
72-
* @param parts The parts to include.
73-
* @param methodParams The params of the HttpMethod using this entity.
73+
*
74+
* @param parts The parts to include.
7475
*/
75-
public MultipartRequestEntity(Part[] parts, FluentStringsMap methodParams) {
76+
public MultipartRequestEntity(Part[] parts, FluentCaseInsensitiveStringsMap requestHeaders) {
7677
if (parts == null) {
7778
throw new IllegalArgumentException("parts cannot be null");
7879
}
79-
if (methodParams == null) {
80-
methodParams = new FluentStringsMap();
81-
}
8280
this.parts = parts;
83-
this.methodParams = methodParams;
81+
String contentTypeHeader = requestHeaders.getFirstValue("Content-Type");
82+
if (isNonEmpty(contentTypeHeader))
83+
this.contentType = contentTypeHeader;
84+
else
85+
this.contentType = MULTIPART_FORM_CONTENT_TYPE;
86+
8487
}
8588

8689
/**
87-
* Returns the MIME boundary string that is used to demarcate boundaries of
88-
* this part. The first call to this method will implicitly create a new
89-
* boundary string. To create a boundary string first the
90-
* HttpMethodParams.MULTIPART_BOUNDARY parameter is considered. Otherwise
91-
* a random one is generated.
92-
*
90+
* Returns the MIME boundary string that is used to demarcate boundaries of this part. The first call to this method will implicitly create a new boundary string. To create a boundary string first the HttpMethodParams.MULTIPART_BOUNDARY parameter is considered. Otherwise a
91+
* random one is generated.
92+
*
9393
* @return The boundary string of this entity in ASCII encoding.
9494
*/
9595
protected byte[] getMultipartBoundary() {
9696
if (multipartBoundary == null) {
97-
String temp = methodParams.get("") == null ? null : methodParams.get("").iterator().next();
98-
if (temp != null) {
99-
multipartBoundary = MultipartEncodingUtil.getAsciiBytes(temp);
100-
} else {
101-
multipartBoundary = generateMultipartBoundary();
102-
}
97+
multipartBoundary = generateMultipartBoundary();
10398
}
10499
return multipartBoundary;
105100
}
@@ -116,14 +111,18 @@ public boolean isRepeatable() {
116111
return true;
117112
}
118113

119-
/* (non-Javadoc)
114+
/*
115+
* (non-Javadoc)
116+
*
120117
* @see org.apache.commons.httpclient.methods.RequestEntity#writeRequest(java.io.OutputStream)
121118
*/
122119
public void writeRequest(OutputStream out) throws IOException {
123120
Part.sendParts(out, parts, getMultipartBoundary());
124121
}
125122

126-
/* (non-Javadoc)
123+
/*
124+
* (non-Javadoc)
125+
*
127126
* @see org.apache.commons.httpclient.methods.RequestEntity#getContentLength()
128127
*/
129128
public long getContentLength() {
@@ -135,14 +134,22 @@ public long getContentLength() {
135134
}
136135
}
137136

138-
/* (non-Javadoc)
137+
/*
138+
* (non-Javadoc)
139+
*
139140
* @see org.apache.commons.httpclient.methods.RequestEntity#getContentType()
140141
*/
141142
public String getContentType() {
142-
StringBuffer buffer = new StringBuffer(MULTIPART_FORM_CONTENT_TYPE);
143-
buffer.append("; boundary=");
144-
buffer.append(MultipartEncodingUtil.getAsciiString(getMultipartBoundary()));
145-
return buffer.toString();
143+
if (contentType.contains("boundary="))
144+
return contentType;
145+
else {
146+
StringBuffer buffer = new StringBuffer(contentType);
147+
if (!contentType.endsWith(";"))
148+
buffer.append(";");
149+
buffer.append(" boundary=");
150+
buffer.append(MultipartEncodingUtil.getAsciiString(getMultipartBoundary()));
151+
return buffer.toString();
152+
}
146153
}
147154

148155
}

src/main/java/com/ning/http/multipart/Part.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ protected void sendTransferEncodingHeader(OutputStream out) throws IOException {
267267
* @param out The output stream
268268
* @throws IOException If an IO problem occurs.
269269
*/
270-
protected void sendContentIDHeader(OutputStream out) throws IOException {
270+
protected void sendContentIdHeader(OutputStream out) throws IOException {
271271
String contentId = getContentId();
272272
if (contentId != null) {
273273
out.write(CRLF_BYTES);
@@ -324,7 +324,7 @@ public void send(OutputStream out) throws IOException {
324324
sendDispositionHeader(out);
325325
sendContentTypeHeader(out);
326326
sendTransferEncodingHeader(out);
327-
sendContentIDHeader(out);
327+
sendContentIdHeader(out);
328328
sendEndOfHeader(out);
329329
sendData(out);
330330
sendEnd(out);
@@ -345,7 +345,7 @@ public long length() throws IOException {
345345
sendDispositionHeader(overhead);
346346
sendContentTypeHeader(overhead);
347347
sendTransferEncodingHeader(overhead);
348-
sendContentIDHeader(overhead);
348+
sendContentIdHeader(overhead);
349349
sendEndOfHeader(overhead);
350350
sendEnd(overhead);
351351
return overhead.size() + lengthOfData();

src/main/java/com/ning/http/util/AsyncHttpProviderUtils.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import com.ning.http.client.ByteArrayPart;
3232
import com.ning.http.client.Cookie;
3333
import com.ning.http.client.FilePart;
34+
import com.ning.http.client.FluentCaseInsensitiveStringsMap;
3435
import com.ning.http.client.FluentStringsMap;
3536
import com.ning.http.client.HttpResponseBodyPart;
3637
import com.ning.http.client.HttpResponseBodyPartsInputStream;
@@ -287,11 +288,11 @@ public final static int getPort(URI uri) {
287288
* This is quite ugly as our internal names are duplicated, but we build on top of HTTP Client implementation.
288289
*
289290
* @param params
290-
* @param methodParams
291+
* @param requestHeaders
291292
* @return a MultipartRequestEntity.
292293
* @throws java.io.FileNotFoundException
293294
*/
294-
public final static MultipartRequestEntity createMultipartRequestEntity(List<Part> params, FluentStringsMap methodParams) throws FileNotFoundException {
295+
public final static MultipartRequestEntity createMultipartRequestEntity(List<Part> params, FluentCaseInsensitiveStringsMap requestHeaders) throws FileNotFoundException {
295296
com.ning.http.multipart.Part[] parts = new com.ning.http.multipart.Part[params.size()];
296297
int i = 0;
297298

@@ -323,7 +324,7 @@ public final static MultipartRequestEntity createMultipartRequestEntity(List<Par
323324
}
324325
++i;
325326
}
326-
return new MultipartRequestEntity(parts, methodParams);
327+
return new MultipartRequestEntity(parts, requestHeaders);
327328
}
328329

329330
public final static byte[] readFully(InputStream in, int[] lengthWrapper) throws IOException {

0 commit comments

Comments
 (0)