From 1f368d269dc5b55d056594516c3da8eef4e632ab Mon Sep 17 00:00:00 2001 From: Jan Bartel Date: Thu, 22 Nov 2012 10:14:27 +1100 Subject: [PATCH] 394839 Allow multipart mime with no boundary --- .../jetty/servlets/MultiPartFilter.java | 10 ++- .../jetty/servlets/MultipartFilterTest.java | 63 +++++++++++++++++++ 2 files changed, 70 insertions(+), 3 deletions(-) diff --git a/jetty-servlets/src/main/java/org/eclipse/jetty/servlets/MultiPartFilter.java b/jetty-servlets/src/main/java/org/eclipse/jetty/servlets/MultiPartFilter.java index bceb6b5efda..2da7f8c1d58 100644 --- a/jetty-servlets/src/main/java/org/eclipse/jetty/servlets/MultiPartFilter.java +++ b/jetty-servlets/src/main/java/org/eclipse/jetty/servlets/MultiPartFilter.java @@ -117,12 +117,16 @@ public class MultiPartFilter implements Filter chain.doFilter(request,response); return; } - + InputStream in = new BufferedInputStream(request.getInputStream()); String content_type=srequest.getContentType(); - + // TODO - handle encodings - String boundary="--"+QuotedStringTokenizer.unquote(value(content_type.substring(content_type.indexOf("boundary="))).trim()); + String contentTypeBoundary = ""; + if (content_type.indexOf("boundary=") >= 0) + contentTypeBoundary = QuotedStringTokenizer.unquote(value(content_type.substring(content_type.indexOf("boundary="))).trim()); + + String boundary="--"+contentTypeBoundary; byte[] byteBoundary=(boundary+"--").getBytes(StringUtil.__ISO_8859_1); diff --git a/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/MultipartFilterTest.java b/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/MultipartFilterTest.java index 97da7c63f2b..903fe316f95 100644 --- a/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/MultipartFilterTest.java +++ b/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/MultipartFilterTest.java @@ -36,6 +36,7 @@ import org.eclipse.jetty.servlet.FilterMapping; import org.eclipse.jetty.testing.HttpTester; import org.eclipse.jetty.testing.ServletTester; import org.eclipse.jetty.util.IO; +import org.eclipse.jetty.util.QuotedStringTokenizer; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -46,6 +47,21 @@ public class MultipartFilterTest private ServletTester tester; + public static class BoundaryServlet extends TestServlet + { + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException + { + assertNotNull(req.getParameter("fileName")); + assertEquals("abc", req.getParameter("fileName")); + assertNotNull(req.getParameter("desc")); + assertEquals("123", req.getParameter("desc")); + assertNotNull(req.getParameter("title")); + assertEquals("ttt", req.getParameter("title")); + super.doPost(req, resp); + } + } + public static class TestServlet extends DumpServlet { @@ -252,6 +268,53 @@ public class MultipartFilterTest assertEquals(HttpServletResponse.SC_OK,response.getStatus()); assertTrue(response.getContent().indexOf("brown cow")>=0); } + + + @Test + public void testNoBoundary() throws Exception + { + // generated and parsed test + HttpTester request = new HttpTester(); + HttpTester response = new HttpTester(); + tester.addServlet(BoundaryServlet.class,"/testb"); + request.setMethod("POST"); + request.setVersion("HTTP/1.0"); + request.setHeader("Host","tester"); + request.setURI("/context/testb"); + + request.setHeader("Content-Type","multipart/form-data"); + + String content = "--\r\n"+ + "Content-Disposition: form-data; name=\"fileName\"\r\n"+ + "Content-Type: text/plain; charset=US-ASCII\r\n"+ + "Content-Transfer-Encoding: 8bit\r\n"+ + "\r\n"+ + "abc\r\n"+ + "--\r\n"+ + "Content-Disposition: form-data; name=\"desc\"\r\n"+ + "Content-Type: text/plain; charset=US-ASCII\r\n"+ + "Content-Transfer-Encoding: 8bit\r\n"+ + "\r\n"+ + "123\r\n"+ + "--\r\n"+ + "Content-Disposition: form-data; name=\"title\"\r\n"+ + "Content-Type: text/plain; charset=US-ASCII\r\n"+ + "Content-Transfer-Encoding: 8bit\r\n"+ + "\r\n"+ + "ttt\r\n"+ + "--\r\n"+ + "Content-Disposition: form-data; name=\"fileup\"; filename=\"test.upload\"\r\n"+ + "Content-Type: application/octet-stream\r\n"+ + "Content-Transfer-Encoding: binary\r\n"+ + "\r\n"+ + "000\r\n"+ + "----\r\n"; + request.setContent(content); + + response.parse(tester.getResponses(request.generate())); + assertTrue(response.getMethod()==null); + assertEquals(HttpServletResponse.SC_OK,response.getStatus()); + } /* * see the testParameterMap test