[#1358] Fix Encoding of multipart attribute names and filenames for non ASCII chars

This commit is contained in:
Norman Maurer 2013-05-14 15:44:25 +02:00
parent 1e380851bd
commit fd75994b16
2 changed files with 39 additions and 14 deletions

View File

@ -440,14 +440,14 @@ public class HttpPostRequestEncoder implements ChunkedInput {
*/
if (data instanceof Attribute) {
if (duringMixedMode) {
InternalAttribute internal = new InternalAttribute();
InternalAttribute internal = new InternalAttribute(charset);
internal.addValue("\r\n--" + multipartMixedBoundary + "--");
multipartHttpDatas.add(internal);
multipartMixedBoundary = null;
currentFileUpload = null;
duringMixedMode = false;
}
InternalAttribute internal = new InternalAttribute();
InternalAttribute internal = new InternalAttribute(charset);
if (!multipartHttpDatas.isEmpty()) {
// previously a data field so CRLF
internal.addValue("\r\n");
@ -472,7 +472,7 @@ public class HttpPostRequestEncoder implements ChunkedInput {
globalBodySize += attribute.length() + internal.size();
} else if (data instanceof FileUpload) {
FileUpload fileUpload = (FileUpload) data;
InternalAttribute internal = new InternalAttribute();
InternalAttribute internal = new InternalAttribute(charset);
if (!multipartHttpDatas.isEmpty()) {
// previously a data field so CRLF
internal.addValue("\r\n");
@ -493,7 +493,7 @@ public class HttpPostRequestEncoder implements ChunkedInput {
multipartHttpDatas.add(internal);
multipartMixedBoundary = null;
// start a new one (could be replaced if mixed start again from here
internal = new InternalAttribute();
internal = new InternalAttribute(charset);
internal.addValue("\r\n");
localMixed = false;
// new currentFileUpload and no more in Mixed mode
@ -617,7 +617,7 @@ public class HttpPostRequestEncoder implements ChunkedInput {
// Finalize the multipartHttpDatas
if (! headerFinalized) {
if (isMultipart) {
InternalAttribute internal = new InternalAttribute();
InternalAttribute internal = new InternalAttribute(charset);
if (duringMixedMode) {
internal.addValue("\r\n--" + multipartMixedBoundary + "--");
}
@ -771,14 +771,8 @@ public class HttpPostRequestEncoder implements ChunkedInput {
}
ChannelBuffer buffer;
if (currentData instanceof InternalAttribute) {
String internal = currentData.toString();
byte[] bytes;
try {
bytes = internal.getBytes("ASCII");
} catch (UnsupportedEncodingException e) {
throw new ErrorDataEncoderException(e);
}
buffer = ChannelBuffers.wrappedBuffer(bytes);
buffer = ((InternalAttribute) currentData).toChannelBuffer();
currentData = null;
} else {
if (currentData instanceof Attribute) {

View File

@ -15,6 +15,12 @@
*/
package org.jboss.netty.handler.codec.http.multipart;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.util.CharsetUtil;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
@ -23,12 +29,25 @@ import java.util.List;
* (like Multipart Mixed mode)
*/
public class InternalAttribute implements InterfaceHttpData {
@Deprecated
protected final List<String> value = new ArrayList<String>();
private final Charset charset;
@Deprecated
public InternalAttribute() {
this(CharsetUtil.UTF_8);
}
@Deprecated
public InternalAttribute(Charset charset) {
this.charset = charset;
}
public HttpDataType getHttpDataType() {
return HttpDataType.InternalAttribute;
}
@Deprecated
public List<String> getValue() {
return value;
}
@ -83,7 +102,11 @@ public class InternalAttribute implements InterfaceHttpData {
public int size() {
int size = 0;
for (String elt : value) {
size += elt.length();
try {
size += elt.getBytes(charset.name()).length;
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
return size;
}
@ -96,6 +119,14 @@ public class InternalAttribute implements InterfaceHttpData {
return result.toString();
}
public ChannelBuffer toChannelBuffer() {
ChannelBuffer[] buffers = new ChannelBuffer[value.size()];
for (int i = 0; i < buffers.length; i++) {
buffers[i] = ChannelBuffers.copiedBuffer(value.get(i), charset);
}
return ChannelBuffers.wrappedBuffer(buffers);
}
public String getName() {
return "InternalAttribute";
}