From 05deb2860529c356a463f44331199224dc75b6d0 Mon Sep 17 00:00:00 2001 From: Oleg Kalnichevski Date: Mon, 13 Sep 2010 18:55:56 +0000 Subject: [PATCH] HTTPCLIENT-960: HttpMultipart doesn't generate Content-Type header for binary parts in BROWSER_COMPATIBLE mode git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@996639 13f79535-47bb-0310-9956-ffa450edef68 --- RELEASE_NOTES.txt | 4 +++ .../http/entity/mime/HttpMultipart.java | 31 ++++++++++++++----- .../http/entity/mime/TestMultipartForm.java | 2 ++ 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/RELEASE_NOTES.txt b/RELEASE_NOTES.txt index a331d2549..5f471d0d6 100644 --- a/RELEASE_NOTES.txt +++ b/RELEASE_NOTES.txt @@ -1,6 +1,10 @@ Changes since 4.1 ALPHA2 ------------------- +* [HTTPCLIENT-960] HttpMultipart doesn't generate Content-Type header for binary parts in + BROWSER_COMPATIBLE mode. + Contributed by Oleg Kalnichevski + * [HTTPCLIENT-989] DefaultHttpRequestRetryHandler no longer retries non-idempotent http methods if NoHttpResponseException is thrown. Contributed by Oleg Kalnichevski diff --git a/httpmime/src/main/java/org/apache/http/entity/mime/HttpMultipart.java b/httpmime/src/main/java/org/apache/http/entity/mime/HttpMultipart.java index 5ec0b7f91..017238d94 100644 --- a/httpmime/src/main/java/org/apache/http/entity/mime/HttpMultipart.java +++ b/httpmime/src/main/java/org/apache/http/entity/mime/HttpMultipart.java @@ -73,6 +73,22 @@ public class HttpMultipart { writeBytes(b, out); } + private static void writeField( + final MinimalField field, final OutputStream out) throws IOException { + writeBytes(field.getName(), out); + writeBytes(FIELD_SEP, out); + writeBytes(field.getBody(), out); + writeBytes(CR_LF, out); + } + + private static void writeField( + final MinimalField field, final Charset charset, final OutputStream out) throws IOException { + writeBytes(field.getName(), charset, out); + writeBytes(FIELD_SEP, out); + writeBytes(field.getBody(), charset, out); + writeBytes(CR_LF, out); + } + private static final ByteArrayBuffer FIELD_SEP = encode(MIME.DEFAULT_CHARSET, ": "); private static final ByteArrayBuffer CR_LF = encode(MIME.DEFAULT_CHARSET, "\r\n"); private static final ByteArrayBuffer TWO_DASHES = encode(MIME.DEFAULT_CHARSET, "--"); @@ -151,20 +167,19 @@ public class HttpMultipart { switch (mode) { case STRICT: for (MinimalField field: header) { - writeBytes(field.getName(), out); - writeBytes(FIELD_SEP, out); - writeBytes(field.getBody(), out); - writeBytes(CR_LF, out); + writeField(field, out); } break; case BROWSER_COMPATIBLE: // Only write Content-Disposition // Use content charset MinimalField cd = part.getHeader().getField(MIME.CONTENT_DISPOSITION); - writeBytes(cd.getName(), this.charset, out); - writeBytes(FIELD_SEP, out); - writeBytes(cd.getBody(), this.charset, out); - writeBytes(CR_LF, out); + writeField(cd, this.charset, out); + String filename = part.getBody().getFilename(); + if (filename != null) { + MinimalField ct = part.getHeader().getField(MIME.CONTENT_TYPE); + writeField(ct, this.charset, out); + } break; } writeBytes(CR_LF, out); diff --git a/httpmime/src/test/java/org/apache/http/entity/mime/TestMultipartForm.java b/httpmime/src/test/java/org/apache/http/entity/mime/TestMultipartForm.java index dea3c8346..1e0f6068a 100644 --- a/httpmime/src/test/java/org/apache/http/entity/mime/TestMultipartForm.java +++ b/httpmime/src/test/java/org/apache/http/entity/mime/TestMultipartForm.java @@ -258,11 +258,13 @@ public class TestMultipartForm { "--foo\r\n" + "Content-Disposition: form-data; name=\"field1\"; " + "filename=\"" + s1 + ".tmp\"\r\n" + + "Content-Type: application/octet-stream\r\n" + "\r\n" + "some random whatever\r\n" + "--foo\r\n" + "Content-Disposition: form-data; name=\"field2\"; " + "filename=\"" + s2 + ".tmp\"\r\n" + + "Content-Type: application/octet-stream\r\n" + "\r\n" + "some random whatever\r\n" + "--foo--\r\n";