Skip to content
Permalink
Browse files

Should decode multipart header field params

  • Loading branch information...
dminkovsky committed Sep 29, 2017
1 parent 47f016b commit e4a98530078894b1132a929fe007c7e7b854a9d7
@@ -22,6 +22,7 @@
import io.netty.handler.codec.http.HttpHeaderValues;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.LastHttpContent;
import io.netty.handler.codec.http.QueryStringDecoder;
import io.netty.handler.codec.http.multipart.HttpPostBodyUtil.SeekAheadOptimize;
import io.netty.handler.codec.http.multipart.HttpPostBodyUtil.TransferEncodingMechanism;
import io.netty.handler.codec.http.multipart.HttpPostRequestDecoder.EndOfDataDecoderException;
@@ -694,10 +695,22 @@ private InterfaceHttpData findMultipartDisposition() {
String name = cleanString(values[0]);
String value = values[1];

boolean shouldDecode = false;
int last = name.length() - 1;
if (name.charAt(last) == '*') {
shouldDecode = true;
name = name.substring(0, last);
}

// See http://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html
if (HttpHeaderValues.FILENAME.contentEquals(name)) {
// filename value is quoted string so strip them
value = value.substring(1, value.length() - 1);
if (!shouldDecode) {
value = value.substring(1, value.length() - 1);
} else {
String[] split = value.split("''", 2);
value = QueryStringDecoder.decodeComponent(split[1], Charset.forName(split[0]));
}
} else {
// otherwise we need to clean the value
value = cleanString(value);
@@ -33,6 +33,7 @@
import io.netty.util.CharsetUtil;
import org.junit.Test;

import java.net.URLEncoder;
import java.nio.charset.UnsupportedCharsetException;
import java.util.Arrays;

@@ -491,4 +492,37 @@ public void testFormEncodeIncorrect() throws Exception {
content.release();
}
}

// https://github.com/netty/netty/pull/7265
@Test
public void testDecodeContentDispositionFieldParameters() throws Exception {

final String boundary = "74e78d11b0214bdcbc2f86491eeb4902";

String encoding = "utf-8";
String filename = "attached_файл.txt";
String filenameEncoded = URLEncoder.encode(filename, encoding);

final String body = "--" + boundary + "\r\n" +
"Content-Disposition: form-data; name=\"file\"; filename*=" + encoding + "''" + filenameEncoded + "\r\n" +
"\r\n" +
"foo\r\n" +
"\r\n" +
"--" + boundary + "--";

final DefaultFullHttpRequest req = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1,
HttpMethod.POST,
"http://localhost",
Unpooled.wrappedBuffer(body.getBytes()));

req.headers().add(HttpHeaderNames.CONTENT_TYPE, "multipart/form-data; boundary=" + boundary);
final DefaultHttpDataFactory inMemoryFactory = new DefaultHttpDataFactory(false);
final HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(inMemoryFactory, req);
assertFalse(decoder.getBodyHttpDatas().isEmpty());
InterfaceHttpData part1 = decoder.getBodyHttpDatas().get(0);
assertTrue("the item should be a FileUpload", part1 instanceof FileUpload);
FileUpload fileUpload = (FileUpload) part1;
assertEquals("the filename should be decoded", filename, fileUpload.getFilename());
decoder.destroy();
}
}

0 comments on commit e4a9853

Please sign in to comment.
You can’t perform that action at this time.