From 32b671f4dcb54bf4eaa3c910911425fb4b97a9e2 Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Sun, 14 Jul 2013 15:48:00 +0200 Subject: [PATCH] [1575] Correctly parse Content-Type value --- .../multipart/HttpPostRequestDecoder.java | 9 ++++--- .../multipart/HttpPostRequestDecoderTest.java | 24 +++++++++++++++---- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/multipart/HttpPostRequestDecoder.java b/codec-http/src/main/java/io/netty/handler/codec/http/multipart/HttpPostRequestDecoder.java index 28038ff3b0..52d38c4c1c 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/multipart/HttpPostRequestDecoder.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/multipart/HttpPostRequestDecoder.java @@ -2029,20 +2029,19 @@ public class HttpPostRequestDecoder { * @return the array of 2 Strings */ private static String[] splitHeaderContentType(String sb) { - int size = sb.length(); int aStart; int aEnd; int bStart; int bEnd; aStart = HttpPostBodyUtil.findNonWhitespace(sb, 0); - aEnd = HttpPostBodyUtil.findWhitespace(sb, aStart); - if (aEnd >= size) { + aEnd = sb.indexOf(';'); + if (aEnd == -1) { return new String[] { sb, "" }; } - if (sb.charAt(aEnd) == ';') { + if (sb.charAt(aEnd - 1) == ' ') { aEnd--; } - bStart = HttpPostBodyUtil.findNonWhitespace(sb, aEnd); + bStart = HttpPostBodyUtil.findNonWhitespace(sb, aEnd + 1); bEnd = HttpPostBodyUtil.findEndOfString(sb); return new String[] { sb.substring(aStart, aEnd), sb.substring(bStart, bEnd) }; } diff --git a/codec-http/src/test/java/io/netty/handler/codec/http/multipart/HttpPostRequestDecoderTest.java b/codec-http/src/test/java/io/netty/handler/codec/http/multipart/HttpPostRequestDecoderTest.java index bb8bb7d2b7..52c7ce7de8 100644 --- a/codec-http/src/test/java/io/netty/handler/codec/http/multipart/HttpPostRequestDecoderTest.java +++ b/codec-http/src/test/java/io/netty/handler/codec/http/multipart/HttpPostRequestDecoderTest.java @@ -32,15 +32,31 @@ import static org.junit.Assert.*; /** {@link HttpPostRequestDecoder} test case. */ public class HttpPostRequestDecoderTest { - @Test - public void testBinaryStreamUpload() throws Exception { - final String boundary = "dLV9Wyq26L_-JQxk6ferf-RT153LhOO"; + @Test + public void testBinaryStreamUploadWithSpace() throws Exception { + testBinaryStreamUpload(true); + } + + // https://github.com/netty/netty/issues/1575 + @Test + public void testBinaryStreamUploadWithoutSpace() throws Exception { + testBinaryStreamUpload(false); + } + + private static void testBinaryStreamUpload(boolean withSpace) throws Exception { + final String boundary = "dLV9Wyq26L_-JQxk6ferf-RT153LhOO"; + final String contentTypeValue; + if (withSpace) { + contentTypeValue = "multipart/form-data; boundary=" + boundary; + } else { + contentTypeValue = "multipart/form-data;boundary=" + boundary; + } final DefaultHttpRequest req = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "http://localhost"); req.setDecoderResult(DecoderResult.SUCCESS); - req.headers().add(HttpHeaders.Names.CONTENT_TYPE, "multipart/form-data; boundary=" + boundary); + req.headers().add(HttpHeaders.Names.CONTENT_TYPE, contentTypeValue); req.headers().add(HttpHeaders.Names.TRANSFER_ENCODING, HttpHeaders.Values.CHUNKED); // Force to use memory-based data.