You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2017/09/30 21:54:04 UTC
[1/3] httpcomponents-client git commit: HTTPCLIENT-293 Refactored
code in order to support multipart header field parameters in the data model
and postpone the formatting and encoding of the parameters until the moment
written into a stream, which is ess [Forced Update!]
Repository: httpcomponents-client
Updated Branches:
refs/heads/master b017a1f2b -> f959d44ec (forced update)
HTTPCLIENT-293 Refactored code in order to support multipart header field parameters in the data model and postpone the formatting and encoding of the parameters until the moment written into a stream, which is essential in order to avoid multiple encodings of the same value.
Project: http://git-wip-us.apache.org/repos/asf/httpcomponents-client/repo
Commit: http://git-wip-us.apache.org/repos/asf/httpcomponents-client/commit/9560aef4
Tree: http://git-wip-us.apache.org/repos/asf/httpcomponents-client/tree/9560aef4
Diff: http://git-wip-us.apache.org/repos/asf/httpcomponents-client/diff/9560aef4
Branch: refs/heads/master
Commit: 9560aef4765ec7c50d29cd7ca7ee735bf3a6c3b6
Parents: 9ac5808
Author: Ioannis Sermetziadis <se...@gmail.com>
Authored: Thu Sep 28 23:56:49 2017 +0300
Committer: Oleg Kalnichevski <ol...@apache.org>
Committed: Sat Sep 30 23:51:22 2017 +0200
----------------------------------------------------------------------
.../http/entity/mime/FormBodyPartBuilder.java | 39 ++++++++-----------
.../hc/client5/http/entity/mime/MIME.java | 3 ++
.../client5/http/entity/mime/MinimalField.java | 40 +++++++++++++++++++-
.../entity/mime/MultipartEntityBuilder.java | 3 +-
.../entity/mime/TestFormBodyPartBuilder.java | 22 -----------
.../entity/mime/TestMultipartEntityBuilder.java | 31 +++++++++++++++
6 files changed, 90 insertions(+), 48 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/httpcomponents-client/blob/9560aef4/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/FormBodyPartBuilder.java
----------------------------------------------------------------------
diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/FormBodyPartBuilder.java b/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/FormBodyPartBuilder.java
index 5195cc6..b3c5e49 100644
--- a/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/FormBodyPartBuilder.java
+++ b/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/FormBodyPartBuilder.java
@@ -27,9 +27,12 @@
package org.apache.hc.client5.http.entity.mime;
+import java.util.ArrayList;
import java.util.List;
import org.apache.hc.core5.http.ContentType;
+import org.apache.hc.core5.http.NameValuePair;
+import org.apache.hc.core5.http.message.BasicNameValuePair;
import org.apache.hc.core5.util.Args;
import org.apache.hc.core5.util.Asserts;
@@ -72,6 +75,15 @@ public class FormBodyPartBuilder {
return this;
}
+ /**
+ * @since 4.6
+ */
+ public FormBodyPartBuilder addField(final String name, final String value, final List<NameValuePair> parameters) {
+ Args.notNull(name, "Field name");
+ this.header.addField(new MinimalField(name, value, parameters));
+ return this;
+ }
+
public FormBodyPartBuilder addField(final String name, final String value) {
Args.notNull(name, "Field name");
this.header.addField(new MinimalField(name, value));
@@ -99,16 +111,12 @@ public class FormBodyPartBuilder {
headerCopy.addField(field);
}
if (headerCopy.getField(MIME.CONTENT_DISPOSITION) == null) {
- final StringBuilder buffer = new StringBuilder();
- buffer.append("form-data; name=\"");
- buffer.append(encodeForHeader(this.name));
- buffer.append("\"");
+ final List<NameValuePair> fieldParameters = new ArrayList<NameValuePair>();
+ fieldParameters.add(new BasicNameValuePair(MIME.FIELD_PARAM_NAME, this.name));
if (this.body.getFilename() != null) {
- buffer.append("; filename=\"");
- buffer.append(encodeForHeader(this.body.getFilename()));
- buffer.append("\"");
+ fieldParameters.add(new BasicNameValuePair(MIME.FIELD_PARAM_FILENAME, this.body.getFilename()));
}
- headerCopy.addField(new MinimalField(MIME.CONTENT_DISPOSITION, buffer.toString()));
+ headerCopy.addField(new MinimalField(MIME.CONTENT_DISPOSITION, "form-data", fieldParameters));
}
if (headerCopy.getField(MIME.CONTENT_TYPE) == null) {
final ContentType contentType;
@@ -136,19 +144,4 @@ public class FormBodyPartBuilder {
return new FormBodyPart(this.name, this.body, headerCopy);
}
- private static String encodeForHeader(final String headerName) {
- if (headerName == null) {
- return null;
- }
- final StringBuilder sb = new StringBuilder();
- for (int i = 0; i < headerName.length(); i++) {
- final char x = headerName.charAt(i);
- if (x == '"' || x == '\\' || x == '\r') {
- sb.append("\\");
- }
- sb.append(x);
- }
- return sb.toString();
- }
-
}
http://git-wip-us.apache.org/repos/asf/httpcomponents-client/blob/9560aef4/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/MIME.java
----------------------------------------------------------------------
diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/MIME.java b/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/MIME.java
index 68d8490..ebb5b84 100644
--- a/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/MIME.java
+++ b/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/MIME.java
@@ -37,6 +37,9 @@ public final class MIME {
public static final String CONTENT_TRANSFER_ENC = "Content-Transfer-Encoding";
public static final String CONTENT_DISPOSITION = "Content-Disposition";
+ public static final String FIELD_PARAM_NAME = "name";
+ public static final String FIELD_PARAM_FILENAME = "filename";
+
public static final String ENC_8BIT = "8bit";
public static final String ENC_BINARY = "binary";
http://git-wip-us.apache.org/repos/asf/httpcomponents-client/blob/9560aef4/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/MinimalField.java
----------------------------------------------------------------------
diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/MinimalField.java b/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/MinimalField.java
index 4b5ed59..ef1e872 100644
--- a/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/MinimalField.java
+++ b/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/MinimalField.java
@@ -27,6 +27,12 @@
package org.apache.hc.client5.http.entity.mime;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import org.apache.hc.core5.http.NameValuePair;
+
/**
* Minimal MIME field.
*
@@ -36,11 +42,27 @@ public class MinimalField {
private final String name;
private final String value;
+ private final List<NameValuePair> parameters;
public MinimalField(final String name, final String value) {
super();
this.name = name;
this.value = value;
+ this.parameters = Collections.emptyList();
+ }
+
+ /**
+ * @since 4.6
+ */
+ public MinimalField(final String name, final String value, final List<NameValuePair> parameters) {
+ this.name = name;
+ this.value = value;
+ this.parameters = parameters != null ?
+ Collections.unmodifiableList(new ArrayList<NameValuePair>(parameters)) : Collections.<NameValuePair>emptyList();
+ }
+
+ public MinimalField(final MinimalField from) {
+ this(from.name, from.value, from.parameters);
}
public String getName() {
@@ -48,7 +70,21 @@ public class MinimalField {
}
public String getBody() {
- return this.value;
+ final StringBuilder sb = new StringBuilder();
+ sb.append(this.value);
+ for (int i = 0; i < this.parameters.size(); i++) {
+ final NameValuePair parameter = this.parameters.get(i);
+ sb.append("; ");
+ sb.append(parameter.getName());
+ sb.append("=\"");
+ sb.append(parameter.getValue());
+ sb.append("\"");
+ }
+ return sb.toString();
+ }
+
+ public List<NameValuePair> getParameters() {
+ return this.parameters;
}
@Override
@@ -56,7 +92,7 @@ public class MinimalField {
final StringBuilder buffer = new StringBuilder();
buffer.append(this.name);
buffer.append(": ");
- buffer.append(this.value);
+ buffer.append(this.getBody());
return buffer.toString();
}
http://git-wip-us.apache.org/repos/asf/httpcomponents-client/blob/9560aef4/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/MultipartEntityBuilder.java
----------------------------------------------------------------------
diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/MultipartEntityBuilder.java b/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/MultipartEntityBuilder.java
index 4f98ea6..57197ad 100644
--- a/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/MultipartEntityBuilder.java
+++ b/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/MultipartEntityBuilder.java
@@ -30,6 +30,7 @@ package org.apache.hc.client5.http.entity.mime;
import java.io.File;
import java.io.InputStream;
import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@@ -216,7 +217,7 @@ public class MultipartEntityBuilder {
form = new HttpRFC6532Multipart(charsetCopy, boundaryCopy, bodyPartsCopy);
break;
default:
- form = new HttpStrictMultipart(charsetCopy, boundaryCopy, bodyPartsCopy);
+ form = new HttpStrictMultipart(StandardCharsets.US_ASCII, boundaryCopy, bodyPartsCopy);
}
return new MultipartFormEntity(form, contentTypeCopy, form.getTotalLength());
}
http://git-wip-us.apache.org/repos/asf/httpcomponents-client/blob/9560aef4/httpclient5/src/test/java/org/apache/hc/client5/http/entity/mime/TestFormBodyPartBuilder.java
----------------------------------------------------------------------
diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/entity/mime/TestFormBodyPartBuilder.java b/httpclient5/src/test/java/org/apache/hc/client5/http/entity/mime/TestFormBodyPartBuilder.java
index 741865f..e333b5f 100644
--- a/httpclient5/src/test/java/org/apache/hc/client5/http/entity/mime/TestFormBodyPartBuilder.java
+++ b/httpclient5/src/test/java/org/apache/hc/client5/http/entity/mime/TestFormBodyPartBuilder.java
@@ -28,7 +28,6 @@
package org.apache.hc.client5.http.entity.mime;
import java.io.File;
-import java.io.ByteArrayInputStream;
import java.util.Arrays;
import java.util.List;
@@ -58,27 +57,6 @@ public class TestFormBodyPartBuilder {
}
@Test
- public void testCharacterStuffing() throws Exception {
- final FormBodyPartBuilder builder = FormBodyPartBuilder.create();
- final InputStreamBody fileBody = new InputStreamBody(new ByteArrayInputStream("hello world".getBytes("UTF-8")), "stuff_with \"quotes\" and \\slashes\\.bin");
- final FormBodyPart bodyPart2 = builder
- .setName("yada_with \"quotes\" and \\slashes\\")
- .setBody(fileBody)
- .build();
-
- Assert.assertNotNull(bodyPart2);
- Assert.assertEquals("yada_with \"quotes\" and \\slashes\\", bodyPart2.getName());
- Assert.assertEquals(fileBody, bodyPart2.getBody());
- final Header header2 = bodyPart2.getHeader();
- Assert.assertNotNull(header2);
- assertFields(Arrays.asList(
- new MinimalField("Content-Disposition", "form-data; name=\"yada_with \\\"quotes\\\" and \\\\slashes\\\\\"; filename=\"stuff_with \\\"quotes\\\" and \\\\slashes\\\\.bin\""),
- new MinimalField("Content-Type", "application/octet-stream"),
- new MinimalField("Content-Transfer-Encoding", "binary")),
- header2.getFields());
- }
-
- @Test
public void testBuildBodyPartMultipleBuilds() throws Exception {
final StringBody stringBody = new StringBody("stuff", ContentType.TEXT_PLAIN);
final FormBodyPartBuilder builder = FormBodyPartBuilder.create();
http://git-wip-us.apache.org/repos/asf/httpcomponents-client/blob/9560aef4/httpclient5/src/test/java/org/apache/hc/client5/http/entity/mime/TestMultipartEntityBuilder.java
----------------------------------------------------------------------
diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/entity/mime/TestMultipartEntityBuilder.java b/httpclient5/src/test/java/org/apache/hc/client5/http/entity/mime/TestMultipartEntityBuilder.java
index c130b69..dd779c1 100644
--- a/httpclient5/src/test/java/org/apache/hc/client5/http/entity/mime/TestMultipartEntityBuilder.java
+++ b/httpclient5/src/test/java/org/apache/hc/client5/http/entity/mime/TestMultipartEntityBuilder.java
@@ -28,11 +28,14 @@
package org.apache.hc.client5.http.entity.mime;
import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
import java.io.File;
import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
import java.util.List;
import org.apache.hc.core5.http.ContentType;
+import org.apache.hc.core5.http.NameValuePair;
import org.apache.hc.core5.http.message.BasicNameValuePair;
import org.junit.Assert;
import org.junit.Test;
@@ -115,4 +118,32 @@ public class TestMultipartEntityBuilder {
entity.getContentType());
}
+ @Test
+ public void testMultipartWriteTo() throws Exception {
+ final List<NameValuePair> parameters = new ArrayList<>();
+ parameters.add(new BasicNameValuePair(MIME.FIELD_PARAM_NAME, "test"));
+ parameters.add(new BasicNameValuePair(MIME.FIELD_PARAM_FILENAME, "hello world"));
+ final MultipartFormEntity entity = MultipartEntityBuilder.create()
+ .setStrictMode()
+ .setBoundary("xxxxxxxxxxxxxxxxxxxxxxxx")
+ .addPart(new FormBodyPartBuilder()
+ .setName("test")
+ .setBody(new StringBody("hello world", ContentType.TEXT_PLAIN))
+ .addField("Content-Disposition", "multipart/form-data", parameters)
+ .build())
+ .buildEntity();
+
+
+ final ByteArrayOutputStream out = new ByteArrayOutputStream();
+ entity.getMultipart().writeTo(out);
+ out.close();
+ Assert.assertEquals("--xxxxxxxxxxxxxxxxxxxxxxxx\r\n" +
+ "Content-Disposition: multipart/form-data; name=\"test\"; filename=\"hello world\"\r\n" +
+ "Content-Type: text/plain; charset=ISO-8859-1\r\n" +
+ "Content-Transfer-Encoding: 8bit\r\n" +
+ "\r\n" +
+ "hello world\r\n" +
+ "--xxxxxxxxxxxxxxxxxxxxxxxx--\r\n", out.toString(StandardCharsets.US_ASCII.name()));
+ }
+
}
[3/3] httpcomponents-client git commit: Deprecated
Content-Transfer-Encoding field in MIME body parts per RFC 7578, section 4.7
Posted by ol...@apache.org.
Deprecated Content-Transfer-Encoding field in MIME body parts per RFC 7578, section 4.7
Project: http://git-wip-us.apache.org/repos/asf/httpcomponents-client/repo
Commit: http://git-wip-us.apache.org/repos/asf/httpcomponents-client/commit/f959d44e
Tree: http://git-wip-us.apache.org/repos/asf/httpcomponents-client/tree/f959d44e
Diff: http://git-wip-us.apache.org/repos/asf/httpcomponents-client/diff/f959d44e
Branch: refs/heads/master
Commit: f959d44ec77d190821aa8f3a096fdeebd940bfe6
Parents: a424709
Author: Oleg Kalnichevski <ol...@apache.org>
Authored: Sat Sep 30 23:35:41 2017 +0200
Committer: Oleg Kalnichevski <ol...@apache.org>
Committed: Sat Sep 30 23:51:52 2017 +0200
----------------------------------------------------------------------
.../hc/client5/http/entity/mime/ByteArrayBody.java | 1 +
.../client5/http/entity/mime/ContentDescriptor.java | 3 +++
.../http/entity/mime/FormBodyPartBuilder.java | 4 ----
.../org/apache/hc/client5/http/entity/mime/MIME.java | 4 ++++
.../http/entity/mime/TestFormBodyPartBuilder.java | 15 +++++----------
.../http/entity/mime/TestMultipartContentBody.java | 4 ----
.../http/entity/mime/TestMultipartEntityBuilder.java | 2 --
.../client5/http/entity/mime/TestMultipartForm.java | 15 ---------------
8 files changed, 13 insertions(+), 35 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/httpcomponents-client/blob/f959d44e/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/ByteArrayBody.java
----------------------------------------------------------------------
diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/ByteArrayBody.java b/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/ByteArrayBody.java
index 54e63ad..f3a56ba 100644
--- a/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/ByteArrayBody.java
+++ b/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/ByteArrayBody.java
@@ -87,6 +87,7 @@ public class ByteArrayBody extends AbstractContentBody {
}
@Override
+ @Deprecated
public String getTransferEncoding() {
return MIME.ENC_BINARY;
}
http://git-wip-us.apache.org/repos/asf/httpcomponents-client/blob/f959d44e/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/ContentDescriptor.java
----------------------------------------------------------------------
diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/ContentDescriptor.java b/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/ContentDescriptor.java
index d000a92..cb977ce 100644
--- a/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/ContentDescriptor.java
+++ b/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/ContentDescriptor.java
@@ -76,7 +76,10 @@ public interface ContentDescriptor {
* Returns the body descriptors transfer encoding.
* @return The transfer encoding. Must not be null, but "7bit",
* if no transfer-encoding was specified.
+ *
+ * @deprecated Deprecated per RFC 7578, section 4.7. Do not use
*/
+ @Deprecated
String getTransferEncoding();
/**
http://git-wip-us.apache.org/repos/asf/httpcomponents-client/blob/f959d44e/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/FormBodyPartBuilder.java
----------------------------------------------------------------------
diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/FormBodyPartBuilder.java b/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/FormBodyPartBuilder.java
index b3c5e49..d2610e2 100644
--- a/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/FormBodyPartBuilder.java
+++ b/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/FormBodyPartBuilder.java
@@ -137,10 +137,6 @@ public class FormBodyPartBuilder {
headerCopy.addField(new MinimalField(MIME.CONTENT_TYPE, buffer.toString()));
}
}
- if (headerCopy.getField(MIME.CONTENT_TRANSFER_ENC) == null) {
- // TE cannot be null
- headerCopy.addField(new MinimalField(MIME.CONTENT_TRANSFER_ENC, body.getTransferEncoding()));
- }
return new FormBodyPart(this.name, this.body, headerCopy);
}
http://git-wip-us.apache.org/repos/asf/httpcomponents-client/blob/f959d44e/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/MIME.java
----------------------------------------------------------------------
diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/MIME.java b/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/MIME.java
index ebb5b84..53e1654 100644
--- a/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/MIME.java
+++ b/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/MIME.java
@@ -34,6 +34,10 @@ package org.apache.hc.client5.http.entity.mime;
public final class MIME {
public static final String CONTENT_TYPE = "Content-Type";
+ /**
+ * @deprecated Deprecated per RFC 7578, section 4.7. Do not use
+ */
+ @Deprecated
public static final String CONTENT_TRANSFER_ENC = "Content-Transfer-Encoding";
public static final String CONTENT_DISPOSITION = "Content-Disposition";
http://git-wip-us.apache.org/repos/asf/httpcomponents-client/blob/f959d44e/httpclient5/src/test/java/org/apache/hc/client5/http/entity/mime/TestFormBodyPartBuilder.java
----------------------------------------------------------------------
diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/entity/mime/TestFormBodyPartBuilder.java b/httpclient5/src/test/java/org/apache/hc/client5/http/entity/mime/TestFormBodyPartBuilder.java
index e333b5f..ab99a26 100644
--- a/httpclient5/src/test/java/org/apache/hc/client5/http/entity/mime/TestFormBodyPartBuilder.java
+++ b/httpclient5/src/test/java/org/apache/hc/client5/http/entity/mime/TestFormBodyPartBuilder.java
@@ -51,8 +51,7 @@ public class TestFormBodyPartBuilder {
Assert.assertNotNull(header);
assertFields(Arrays.asList(
new MinimalField("Content-Disposition", "form-data; name=\"blah\""),
- new MinimalField("Content-Type", "text/plain; charset=ISO-8859-1"),
- new MinimalField("Content-Transfer-Encoding", "8bit")),
+ new MinimalField("Content-Type", "text/plain; charset=ISO-8859-1")),
header.getFields());
}
@@ -71,8 +70,7 @@ public class TestFormBodyPartBuilder {
Assert.assertNotNull(header1);
assertFields(Arrays.asList(
new MinimalField("Content-Disposition", "form-data; name=\"blah\""),
- new MinimalField("Content-Type", "text/plain; charset=ISO-8859-1"),
- new MinimalField("Content-Transfer-Encoding", "8bit")),
+ new MinimalField("Content-Type", "text/plain; charset=ISO-8859-1")),
header1.getFields());
final FileBody fileBody = new FileBody(new File("/path/stuff.bin"), ContentType.DEFAULT_BINARY);
final FormBodyPart bodyPart2 = builder
@@ -87,8 +85,7 @@ public class TestFormBodyPartBuilder {
Assert.assertNotNull(header2);
assertFields(Arrays.asList(
new MinimalField("Content-Disposition", "form-data; name=\"yada\"; filename=\"stuff.bin\""),
- new MinimalField("Content-Type", "application/octet-stream"),
- new MinimalField("Content-Transfer-Encoding", "binary")),
+ new MinimalField("Content-Type", "application/octet-stream")),
header2.getFields());
}
@@ -117,8 +114,7 @@ public class TestFormBodyPartBuilder {
new MinimalField("header3", "blah"),
new MinimalField("header3", "blah"),
new MinimalField("Content-Disposition", "form-data; name=\"blah\""),
- new MinimalField("Content-Type", "text/plain; charset=ISO-8859-1"),
- new MinimalField("Content-Transfer-Encoding", "8bit")),
+ new MinimalField("Content-Type", "text/plain; charset=ISO-8859-1")),
header1.getFields());
final FormBodyPart bodyPart2 = builder
@@ -134,8 +130,7 @@ public class TestFormBodyPartBuilder {
new MinimalField("header1", "blah"),
new MinimalField("header2", "yada"),
new MinimalField("Content-Disposition", "form-data; name=\"blah\""),
- new MinimalField("Content-Type", "text/plain; charset=ISO-8859-1"),
- new MinimalField("Content-Transfer-Encoding", "8bit")),
+ new MinimalField("Content-Type", "text/plain; charset=ISO-8859-1")),
header2.getFields());
final FormBodyPart bodyPart3 = builder
http://git-wip-us.apache.org/repos/asf/httpcomponents-client/blob/f959d44e/httpclient5/src/test/java/org/apache/hc/client5/http/entity/mime/TestMultipartContentBody.java
----------------------------------------------------------------------
diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/entity/mime/TestMultipartContentBody.java b/httpclient5/src/test/java/org/apache/hc/client5/http/entity/mime/TestMultipartContentBody.java
index 37590fd..3b426fd 100644
--- a/httpclient5/src/test/java/org/apache/hc/client5/http/entity/mime/TestMultipartContentBody.java
+++ b/httpclient5/src/test/java/org/apache/hc/client5/http/entity/mime/TestMultipartContentBody.java
@@ -85,8 +85,6 @@ public class TestMultipartContentBody {
Assert.assertEquals("application", b1.getMediaType());
Assert.assertEquals("octet-stream", b1.getSubType());
- Assert.assertEquals(MIME.ENC_BINARY, b1.getTransferEncoding());
-
final InputStreamBody b2 = new InputStreamBody(
new ByteArrayInputStream(stuff), ContentType.create("some/stuff"), "stuff");
Assert.assertEquals(-1, b2.getContentLength());
@@ -95,8 +93,6 @@ public class TestMultipartContentBody {
Assert.assertEquals("some/stuff", b2.getMimeType());
Assert.assertEquals("some", b2.getMediaType());
Assert.assertEquals("stuff", b2.getSubType());
-
- Assert.assertEquals(MIME.ENC_BINARY, b2.getTransferEncoding());
}
}
http://git-wip-us.apache.org/repos/asf/httpcomponents-client/blob/f959d44e/httpclient5/src/test/java/org/apache/hc/client5/http/entity/mime/TestMultipartEntityBuilder.java
----------------------------------------------------------------------
diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/entity/mime/TestMultipartEntityBuilder.java b/httpclient5/src/test/java/org/apache/hc/client5/http/entity/mime/TestMultipartEntityBuilder.java
index 6182899..e2e8285 100644
--- a/httpclient5/src/test/java/org/apache/hc/client5/http/entity/mime/TestMultipartEntityBuilder.java
+++ b/httpclient5/src/test/java/org/apache/hc/client5/http/entity/mime/TestMultipartEntityBuilder.java
@@ -140,7 +140,6 @@ public class TestMultipartEntityBuilder {
Assert.assertEquals("--xxxxxxxxxxxxxxxxxxxxxxxx\r\n" +
"Content-Disposition: multipart/form-data; name=\"test\"; filename=\"hello world\"\r\n" +
"Content-Type: text/plain; charset=ISO-8859-1\r\n" +
- "Content-Transfer-Encoding: 8bit\r\n" +
"\r\n" +
"hello world\r\n" +
"--xxxxxxxxxxxxxxxxxxxxxxxx--\r\n", out.toString(StandardCharsets.US_ASCII.name()));
@@ -167,7 +166,6 @@ public class TestMultipartEntityBuilder {
Assert.assertEquals("--xxxxxxxxxxxxxxxxxxxxxxxx\r\n" +
"Content-Disposition: multipart/form-data; name=\"test\"; filename=\"hello%20%CE%BA%CF%8C%CF%83%CE%BC%CE%B5!%25\"\r\n" +
"Content-Type: text/plain; charset=ISO-8859-1\r\n" +
- "Content-Transfer-Encoding: 8bit\r\n" +
"\r\n" +
"hello world\r\n" +
"--xxxxxxxxxxxxxxxxxxxxxxxx--\r\n", out.toString(StandardCharsets.US_ASCII.name()));
http://git-wip-us.apache.org/repos/asf/httpcomponents-client/blob/f959d44e/httpclient5/src/test/java/org/apache/hc/client5/http/entity/mime/TestMultipartForm.java
----------------------------------------------------------------------
diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/entity/mime/TestMultipartForm.java b/httpclient5/src/test/java/org/apache/hc/client5/http/entity/mime/TestMultipartForm.java
index 8c729b7..90bf5ca 100644
--- a/httpclient5/src/test/java/org/apache/hc/client5/http/entity/mime/TestMultipartForm.java
+++ b/httpclient5/src/test/java/org/apache/hc/client5/http/entity/mime/TestMultipartForm.java
@@ -75,19 +75,16 @@ public class TestMultipartForm {
"--foo\r\n" +
"Content-Disposition: form-data; name=\"field1\"\r\n" +
"Content-Type: text/plain; charset=ISO-8859-1\r\n" +
- "Content-Transfer-Encoding: 8bit\r\n" +
"\r\n" +
"this stuff\r\n" +
"--foo\r\n" +
"Content-Disposition: form-data; name=\"field2\"\r\n" +
"Content-Type: text/plain; charset=UTF-8\r\n" +
- "Content-Transfer-Encoding: 8bit\r\n" +
"\r\n" +
"that stuff\r\n" +
"--foo\r\n" +
"Content-Disposition: form-data; name=\"field3\"\r\n" +
"Content-Type: text/plain; charset=ISO-8859-1\r\n" +
- "Content-Transfer-Encoding: 8bit\r\n" +
"\r\n" +
"all kind of stuff\r\n" +
"--foo--\r\n";
@@ -115,13 +112,11 @@ public class TestMultipartForm {
"--foo\r\n" +
"Content-Disposition: form-data; name=\"field1\"\r\n" +
"Content-Type: text/plain; charset=ISO-8859-1\r\n" +
- "Content-Transfer-Encoding: 8bit\r\n" +
"\r\n" +
"this stuff\r\n" +
"--foo\r\n" +
"Content-Disposition: form-data; name=\"field2\"\r\n" +
"Content-Type: stuff/plain; param=value\r\n" +
- "Content-Transfer-Encoding: 8bit\r\n" +
"\r\n" +
"that stuff\r\n" +
"--foo--\r\n";
@@ -156,14 +151,12 @@ public class TestMultipartForm {
"Content-Disposition: form-data; name=\"field1\"; " +
"filename=\"" + tmpfile.getName() + "\"\r\n" +
"Content-Type: application/octet-stream\r\n" +
- "Content-Transfer-Encoding: binary\r\n" +
"\r\n" +
"some random whatever\r\n" +
"--foo\r\n" +
"Content-Disposition: form-data; name=\"field2\"; " +
"filename=\"file.tmp\"\r\n" +
"Content-Type: application/octet-stream\r\n" +
- "Content-Transfer-Encoding: binary\r\n" +
"\r\n" +
"some random whatever\r\n" +
"--foo--\r\n";
@@ -201,21 +194,18 @@ public class TestMultipartForm {
"Content-Disposition: form-data; name=\"field1\"; " +
"filename=\"" + tmpfile.getName() + "\"\r\n" +
"Content-Type: application/octet-stream\r\n" +
- "Content-Transfer-Encoding: binary\r\n" +
"\r\n" +
"some random whatever\r\n" +
"--foo\r\n" +
"Content-Disposition: form-data; name=\"field2\"; " +
"filename=\"test-file\"\r\n" +
"Content-Type: text/plain; charset=US-ASCII\r\n" +
- "Content-Transfer-Encoding: binary\r\n" +
"\r\n" +
"some random whatever\r\n" +
"--foo\r\n" +
"Content-Disposition: form-data; name=\"field3\"; " +
"filename=\"file.tmp\"\r\n" +
"Content-Type: application/octet-stream\r\n" +
- "Content-Transfer-Encoding: binary\r\n" +
"\r\n" +
"some random whatever\r\n" +
"--foo--\r\n";
@@ -253,21 +243,18 @@ public class TestMultipartForm {
"Content-Disposition: form-data; name=\"field1\u0414\"; " +
"filename=\"" + tmpfile.getName() + "\"\r\n" +
"Content-Type: application/octet-stream\r\n" +
- "Content-Transfer-Encoding: binary\r\n" +
"\r\n" +
"some random whatever\r\n" +
"--foo\r\n" +
"Content-Disposition: form-data; name=\"field2\"; " +
"filename=\"test-file\"\r\n" +
"Content-Type: text/plain; charset=US-ASCII\r\n" +
- "Content-Transfer-Encoding: binary\r\n" +
"\r\n" +
"some random whatever\r\n" +
"--foo\r\n" +
"Content-Disposition: form-data; name=\"field3\"; " +
"filename=\"file.tmp\"\r\n" +
"Content-Type: application/octet-stream\r\n" +
- "Content-Transfer-Encoding: binary\r\n" +
"\r\n" +
"some random whatever\r\n" +
"--foo--\r\n";
@@ -364,14 +351,12 @@ public class TestMultipartForm {
"--foo\r\n" +
"Content-Disposition: form-data; name=\"field1\"\r\n" +
"Content-Type: text/plain; charset=ISO-8859-1\r\n" +
- "Content-Transfer-Encoding: 8bit\r\n" +
"\r\n").getBytes(StandardCharsets.US_ASCII));
out2.write(s1.getBytes(StandardCharsets.ISO_8859_1));
out2.write(("\r\n" +
"--foo\r\n" +
"Content-Disposition: form-data; name=\"field2\"\r\n" +
"Content-Type: text/plain; charset=KOI8-R\r\n" +
- "Content-Transfer-Encoding: 8bit\r\n" +
"\r\n").getBytes(StandardCharsets.US_ASCII));
out2.write(s2.getBytes(Charset.forName("KOI8-R")));
out2.write(("\r\n" +
[2/3] httpcomponents-client git commit: HTTPCLIENT-293 Implemented
the percent encoding of the filename parameter of the Content-Disposition
header based on RFC7578 sections 2 and 4.2. In the new MultipartForm
implementation I included a PercentCodec tha
Posted by ol...@apache.org.
HTTPCLIENT-293 Implemented the percent encoding of the filename parameter of the Content-Disposition header based on RFC7578 sections 2 and 4.2. In the new MultipartForm implementation I included a PercentCodec that performs encoding/decoding to/from the percent encoding as described in RFC7578 and RFC3986.
Project: http://git-wip-us.apache.org/repos/asf/httpcomponents-client/repo
Commit: http://git-wip-us.apache.org/repos/asf/httpcomponents-client/commit/a424709d
Tree: http://git-wip-us.apache.org/repos/asf/httpcomponents-client/tree/a424709d
Diff: http://git-wip-us.apache.org/repos/asf/httpcomponents-client/diff/a424709d
Branch: refs/heads/master
Commit: a424709d89504aadd7b3d59129902666d79d0c15
Parents: 9560aef
Author: Ioannis Sermetziadis <se...@gmail.com>
Authored: Fri Sep 29 00:12:13 2017 +0300
Committer: Oleg Kalnichevski <ol...@apache.org>
Committed: Sat Sep 30 23:51:43 2017 +0200
----------------------------------------------------------------------
.../http/entity/mime/AbstractMultipartForm.java | 18 +-
.../http/entity/mime/HttpMultipartMode.java | 4 +-
.../http/entity/mime/HttpRFC7578Multipart.java | 192 +++++++++++++++++++
.../client5/http/entity/mime/MinimalField.java | 7 +
.../entity/mime/MultipartEntityBuilder.java | 6 +
.../entity/mime/TestMultipartEntityBuilder.java | 29 ++-
6 files changed, 245 insertions(+), 11 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/httpcomponents-client/blob/a424709d/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/AbstractMultipartForm.java
----------------------------------------------------------------------
diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/AbstractMultipartForm.java b/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/AbstractMultipartForm.java
index 0ef2266..b36afc8 100644
--- a/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/AbstractMultipartForm.java
+++ b/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/AbstractMultipartForm.java
@@ -48,7 +48,7 @@ import org.apache.hc.core5.util.ByteArrayBuffer;
*/
abstract class AbstractMultipartForm {
- private static ByteArrayBuffer encode(
+ static ByteArrayBuffer encode(
final Charset charset, final String string) {
final ByteBuffer encoded = charset.encode(CharBuffer.wrap(string));
final ByteArrayBuffer bab = new ByteArrayBuffer(encoded.remaining());
@@ -56,24 +56,24 @@ abstract class AbstractMultipartForm {
return bab;
}
- private static void writeBytes(
+ static void writeBytes(
final ByteArrayBuffer b, final OutputStream out) throws IOException {
out.write(b.array(), 0, b.length());
}
- private static void writeBytes(
+ static void writeBytes(
final String s, final Charset charset, final OutputStream out) throws IOException {
final ByteArrayBuffer b = encode(charset, s);
writeBytes(b, out);
}
- private static void writeBytes(
+ static void writeBytes(
final String s, final OutputStream out) throws IOException {
final ByteArrayBuffer b = encode(StandardCharsets.ISO_8859_1, s);
writeBytes(b, out);
}
- protected static void writeField(
+ static void writeField(
final MinimalField field, final OutputStream out) throws IOException {
writeBytes(field.getName(), out);
writeBytes(FIELD_SEP, out);
@@ -81,7 +81,7 @@ abstract class AbstractMultipartForm {
writeBytes(CR_LF, out);
}
- protected static void writeField(
+ static void writeField(
final MinimalField field, final Charset charset, final OutputStream out) throws IOException {
writeBytes(field.getName(), charset, out);
writeBytes(FIELD_SEP, out);
@@ -89,9 +89,9 @@ abstract class AbstractMultipartForm {
writeBytes(CR_LF, out);
}
- private static final ByteArrayBuffer FIELD_SEP = encode(StandardCharsets.ISO_8859_1, ": ");
- private static final ByteArrayBuffer CR_LF = encode(StandardCharsets.ISO_8859_1, "\r\n");
- private static final ByteArrayBuffer TWO_DASHES = encode(StandardCharsets.ISO_8859_1, "--");
+ static final ByteArrayBuffer FIELD_SEP = encode(StandardCharsets.ISO_8859_1, ": ");
+ static final ByteArrayBuffer CR_LF = encode(StandardCharsets.ISO_8859_1, "\r\n");
+ static final ByteArrayBuffer TWO_DASHES = encode(StandardCharsets.ISO_8859_1, "--");
final Charset charset;
final String boundary;
http://git-wip-us.apache.org/repos/asf/httpcomponents-client/blob/a424709d/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/HttpMultipartMode.java
----------------------------------------------------------------------
diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/HttpMultipartMode.java b/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/HttpMultipartMode.java
index c749436..c4f7ceb 100644
--- a/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/HttpMultipartMode.java
+++ b/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/HttpMultipartMode.java
@@ -38,6 +38,8 @@ public enum HttpMultipartMode {
/** browser-compatible mode, i.e. only write Content-Disposition; use content charset */
BROWSER_COMPATIBLE,
/** RFC 6532 compliant */
- RFC6532
+ RFC6532,
+ /** RFC 7578 compliant */
+ RFC7578
}
http://git-wip-us.apache.org/repos/asf/httpcomponents-client/blob/a424709d/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/HttpRFC7578Multipart.java
----------------------------------------------------------------------
diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/HttpRFC7578Multipart.java b/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/HttpRFC7578Multipart.java
new file mode 100644
index 0000000..e3437f5
--- /dev/null
+++ b/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/HttpRFC7578Multipart.java
@@ -0,0 +1,192 @@
+/*
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation. For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.hc.client5.http.entity.mime;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.nio.charset.Charset;
+import java.nio.charset.CharsetEncoder;
+import java.nio.charset.StandardCharsets;
+import java.util.BitSet;
+import java.util.List;
+
+import org.apache.commons.codec.DecoderException;
+import org.apache.hc.core5.http.NameValuePair;
+import org.apache.hc.core5.util.ByteArrayBuffer;
+
+public class HttpRFC7578Multipart extends AbstractMultipartForm {
+
+ private static final PercentCodec PERCENT_CODEC = new PercentCodec();
+
+ private final List<FormBodyPart> parts;
+
+ public HttpRFC7578Multipart(
+ final Charset charset,
+ final String boundary,
+ final List<FormBodyPart> parts) {
+ super(charset, boundary);
+ this.parts = parts;
+ }
+
+ @Override
+ public List<FormBodyPart> getBodyParts() {
+ return parts;
+ }
+
+ @Override
+ protected void formatMultipartHeader(final FormBodyPart part, final OutputStream out) throws IOException {
+ for (final MinimalField field: part.getHeader()) {
+ if (MIME.CONTENT_DISPOSITION.equalsIgnoreCase(field.getName())) {
+ writeBytes(field.getName(), charset, out);
+ writeBytes(FIELD_SEP, out);
+ writeBytes(field.getValue(), out);
+ final List<NameValuePair> parameters = field.getParameters();
+ for (int i = 0; i < parameters.size(); i++) {
+ final NameValuePair parameter = parameters.get(i);
+ final String name = parameter.getName();
+ final String value = parameter.getValue();
+ writeBytes("; ", out);
+ writeBytes(name, out);
+ writeBytes("=\"", out);
+ if (value != null) {
+ if (name.equalsIgnoreCase(MIME.FIELD_PARAM_FILENAME)) {
+ out.write(PERCENT_CODEC.encode(value.getBytes(charset)));
+ } else {
+ writeBytes(value, out);
+ }
+ }
+ writeBytes("\"", out);
+ }
+ writeBytes(CR_LF, out);
+ } else {
+ writeField(field, charset, out);
+ }
+ }
+ }
+
+ static class PercentCodec {
+
+ private static final byte ESCAPE_CHAR = '%';
+
+ private static final BitSet ALWAYSENCODECHARS = new BitSet();
+
+ static {
+ ALWAYSENCODECHARS.set(' ');
+ ALWAYSENCODECHARS.set('%');
+ }
+
+ /**
+ * Percent-Encoding implementation based on RFC 3986
+ */
+ public byte[] encode(final byte[] bytes) {
+ if (bytes == null) {
+ return null;
+ }
+
+ final CharsetEncoder characterSetEncoder = StandardCharsets.US_ASCII.newEncoder();
+ final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
+ for (final byte c : bytes) {
+ int b = c;
+ if (b < 0) {
+ b = 256 + b;
+ }
+ if (characterSetEncoder.canEncode((char) b) && !ALWAYSENCODECHARS.get(c)) {
+ buffer.write(b);
+ } else {
+ buffer.write(ESCAPE_CHAR);
+ final char hex1 = Utils.hexDigit(b >> 4);
+ final char hex2 = Utils.hexDigit(b);
+ buffer.write(hex1);
+ buffer.write(hex2);
+ }
+ }
+ return buffer.toByteArray();
+ }
+
+ public byte[] decode(final byte[] bytes) throws DecoderException {
+ if (bytes == null) {
+ return null;
+ }
+ final ByteArrayBuffer buffer = new ByteArrayBuffer(bytes.length);
+ for (int i = 0; i < bytes.length; i++) {
+ final int b = bytes[i];
+ if (b == ESCAPE_CHAR) {
+ try {
+ final int u = Utils.digit16(bytes[++i]);
+ final int l = Utils.digit16(bytes[++i]);
+ buffer.append((char) ((u << 4) + l));
+ } catch (final ArrayIndexOutOfBoundsException e) {
+ throw new DecoderException("Invalid URL encoding: ", e);
+ }
+ } else {
+ buffer.append(b);
+ }
+ }
+ return buffer.toByteArray();
+ }
+ }
+
+ static class Utils {
+
+ /**
+ * Radix used in encoding and decoding.
+ */
+ private static final int RADIX = 16;
+
+ /**
+ * Returns the numeric value of the character <code>b</code> in radix 16.
+ *
+ * @param b
+ * The byte to be converted.
+ * @return The numeric value represented by the character in radix 16.
+ *
+ * @throws DecoderException
+ * Thrown when the byte is not valid per {@link Character#digit(char,int)}
+ */
+ static int digit16(final byte b) throws DecoderException {
+ final int i = Character.digit((char) b, RADIX);
+ if (i == -1) {
+ throw new DecoderException("Invalid URL encoding: not a valid digit (radix " + RADIX + "): " + b);
+ }
+ return i;
+ }
+
+ /**
+ * Returns the upper case hex digit of the lower 4 bits of the int.
+ *
+ * @param b the input int
+ * @return the upper case hex digit of the lower 4 bits of the int.
+ */
+ static char hexDigit(final int b) {
+ return Character.toUpperCase(Character.forDigit(b & 0xF, RADIX));
+ }
+
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/httpcomponents-client/blob/a424709d/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/MinimalField.java
----------------------------------------------------------------------
diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/MinimalField.java b/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/MinimalField.java
index ef1e872..437330f 100644
--- a/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/MinimalField.java
+++ b/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/MinimalField.java
@@ -69,6 +69,13 @@ public class MinimalField {
return this.name;
}
+ /**
+ * @since 4.6
+ */
+ public String getValue() {
+ return this.value;
+ }
+
public String getBody() {
final StringBuilder sb = new StringBuilder();
sb.append(this.value);
http://git-wip-us.apache.org/repos/asf/httpcomponents-client/blob/a424709d/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/MultipartEntityBuilder.java
----------------------------------------------------------------------
diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/MultipartEntityBuilder.java b/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/MultipartEntityBuilder.java
index 57197ad..441b1c5 100644
--- a/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/MultipartEntityBuilder.java
+++ b/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/MultipartEntityBuilder.java
@@ -216,6 +216,12 @@ public class MultipartEntityBuilder {
case RFC6532:
form = new HttpRFC6532Multipart(charsetCopy, boundaryCopy, bodyPartsCopy);
break;
+ case RFC7578:
+ if (charsetCopy == null) {
+ charsetCopy = StandardCharsets.UTF_8;
+ }
+ form = new HttpRFC7578Multipart(charsetCopy, boundaryCopy, bodyPartsCopy);
+ break;
default:
form = new HttpStrictMultipart(StandardCharsets.US_ASCII, boundaryCopy, bodyPartsCopy);
}
http://git-wip-us.apache.org/repos/asf/httpcomponents-client/blob/a424709d/httpclient5/src/test/java/org/apache/hc/client5/http/entity/mime/TestMultipartEntityBuilder.java
----------------------------------------------------------------------
diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/entity/mime/TestMultipartEntityBuilder.java b/httpclient5/src/test/java/org/apache/hc/client5/http/entity/mime/TestMultipartEntityBuilder.java
index dd779c1..6182899 100644
--- a/httpclient5/src/test/java/org/apache/hc/client5/http/entity/mime/TestMultipartEntityBuilder.java
+++ b/httpclient5/src/test/java/org/apache/hc/client5/http/entity/mime/TestMultipartEntityBuilder.java
@@ -135,7 +135,7 @@ public class TestMultipartEntityBuilder {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
- entity.getMultipart().writeTo(out);
+ entity.writeTo(out);
out.close();
Assert.assertEquals("--xxxxxxxxxxxxxxxxxxxxxxxx\r\n" +
"Content-Disposition: multipart/form-data; name=\"test\"; filename=\"hello world\"\r\n" +
@@ -145,5 +145,32 @@ public class TestMultipartEntityBuilder {
"hello world\r\n" +
"--xxxxxxxxxxxxxxxxxxxxxxxx--\r\n", out.toString(StandardCharsets.US_ASCII.name()));
}
+ @Test
+ public void testMultipartWriteToRFC7578Mode() throws Exception {
+ final List<NameValuePair> parameters = new ArrayList<NameValuePair>();
+ parameters.add(new BasicNameValuePair(MIME.FIELD_PARAM_NAME, "test"));
+ parameters.add(new BasicNameValuePair(MIME.FIELD_PARAM_FILENAME, "hello \u03BA\u03CC\u03C3\u03BC\u03B5!%"));
+
+ final MultipartFormEntity entity = MultipartEntityBuilder.create()
+ .setMode(HttpMultipartMode.RFC7578)
+ .setBoundary("xxxxxxxxxxxxxxxxxxxxxxxx")
+ .addPart(new FormBodyPartBuilder()
+ .setName("test")
+ .setBody(new StringBody("hello world", ContentType.TEXT_PLAIN))
+ .addField("Content-Disposition", "multipart/form-data", parameters)
+ .build())
+ .buildEntity();
+
+ final ByteArrayOutputStream out = new ByteArrayOutputStream();
+ entity.writeTo(out);
+ out.close();
+ Assert.assertEquals("--xxxxxxxxxxxxxxxxxxxxxxxx\r\n" +
+ "Content-Disposition: multipart/form-data; name=\"test\"; filename=\"hello%20%CE%BA%CF%8C%CF%83%CE%BC%CE%B5!%25\"\r\n" +
+ "Content-Type: text/plain; charset=ISO-8859-1\r\n" +
+ "Content-Transfer-Encoding: 8bit\r\n" +
+ "\r\n" +
+ "hello world\r\n" +
+ "--xxxxxxxxxxxxxxxxxxxxxxxx--\r\n", out.toString(StandardCharsets.US_ASCII.name()));
+ }
}