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 7b5db490119..41bb58e0d0b 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 @@ -64,6 +64,17 @@ public class MultipartFilterTest private ServletTester tester; FilterHolder multipartFilter; + + public static class NullServlet extends HttpServlet + { + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException + { + resp.setStatus(200); + } + + } + public static class FilenameServlet extends TestServlet { @Override @@ -143,6 +154,70 @@ public class MultipartFilterTest tester.stop(); tester=null; } + + @Test + public void testFinalBoundaryOnly() throws Exception + { + + tester.addServlet(NullServlet.class,"/null"); + HttpTester.Request request = HttpTester.newRequest(); + HttpTester.Response response; + + // test GET + request.setMethod("POST"); + request.setVersion("HTTP/1.0"); + request.setHeader("Host","tester"); + request.setURI("/context/null"); + + String delimiter = "\r\n"; + final String boundary = "MockMultiPartTestBoundary"; + String content = + delimiter + + "Hello world" + + delimiter + // Two delimiter markers, which make an empty line. + delimiter + + "--" + boundary + "--" + delimiter; + + request.setHeader("Content-Type","multipart/form-data; boundary="+boundary); + request.setContent(content); + + try(StacklessLogging stackless = new StacklessLogging(ServletHandler.class)) + { + response = HttpTester.parseResponse(tester.getResponses(request.generate())); + assertEquals(HttpServletResponse.SC_OK,response.getStatus()); + } + } + + @Test + public void testEmpty() throws Exception + { + + tester.addServlet(NullServlet.class,"/null"); + + HttpTester.Request request = HttpTester.newRequest(); + HttpTester.Response response; + + // test GET + request.setMethod("POST"); + request.setVersion("HTTP/1.0"); + request.setHeader("Host","tester"); + request.setURI("/context/null"); + + String delimiter = "\r\n"; + final String boundary = "MockMultiPartTestBoundary"; + String content = + delimiter + + "--" + boundary + "--" + delimiter; + + request.setHeader("Content-Type","multipart/form-data; boundary="+boundary); + request.setContent(content); + + try(StacklessLogging stackless = new StacklessLogging(ServletHandler.class)) + { + response = HttpTester.parseResponse(tester.getResponses(request.generate())); + assertEquals(HttpServletResponse.SC_OK,response.getStatus()); + } + } @Test public void testBadPost() throws Exception diff --git a/jetty-util/src/test/java/org/eclipse/jetty/util/MultiPartInputStreamTest.java b/jetty-util/src/test/java/org/eclipse/jetty/util/MultiPartInputStreamTest.java index e9ed42aa745..5496450243f 100644 --- a/jetty-util/src/test/java/org/eclipse/jetty/util/MultiPartInputStreamTest.java +++ b/jetty-util/src/test/java/org/eclipse/jetty/util/MultiPartInputStreamTest.java @@ -63,6 +63,7 @@ public class MultiPartInputStreamTest _tmpDir.deleteOnExit(); } + @Test public void testBadMultiPartRequest() throws Exception { @@ -72,12 +73,12 @@ public class MultiPartInputStreamTest "Content-Type: application/octet-stream\r\n\r\n"+ "How now brown cow."+ "\r\n--" + boundary + "-\r\n\r\n"; - + MultipartConfigElement config = new MultipartConfigElement(_dirname, 1024, 3072, 50); MultiPartInputStreamParser mpis = new MultiPartInputStreamParser(new ByteArrayInputStream(str.getBytes()), - "multipart/form-data, boundary="+boundary, - config, - _tmpDir); + "multipart/form-data, boundary="+boundary, + config, + _tmpDir); mpis.setDeleteOnExit(true); try { @@ -90,7 +91,56 @@ public class MultiPartInputStreamTest } } + + @Test + public void testFinalBoundaryOnly() + throws Exception + { + String delimiter = "\r\n"; + final String boundary = "MockMultiPartTestBoundary"; + + + // Malformed multipart request body containing only an arbitrary string of text, followed by the final boundary marker, delimited by empty lines. + String str = + delimiter + + "Hello world" + + delimiter + // Two delimiter markers, which make an empty line. + delimiter + + "--" + boundary + "--" + delimiter; + + MultipartConfigElement config = new MultipartConfigElement(_dirname, 1024, 3072, 50); + MultiPartInputStreamParser mpis = new MultiPartInputStreamParser(new ByteArrayInputStream(str.getBytes()), + "multipart/form-data, boundary="+boundary, + config, + _tmpDir); + mpis.setDeleteOnExit(true); + Collection parts = mpis.getParts(); + assertTrue(mpis.getParts().isEmpty()); + } + + + @Test + public void testEmpty() + throws Exception + { + String delimiter = "\r\n"; + final String boundary = "MockMultiPartTestBoundary"; + + String str = + delimiter + + "--" + boundary + "--" + delimiter; + + MultipartConfigElement config = new MultipartConfigElement(_dirname, 1024, 3072, 50); + MultiPartInputStreamParser mpis = new MultiPartInputStreamParser(new ByteArrayInputStream(str.getBytes()), + "multipart/form-data, boundary="+boundary, + config, + _tmpDir); + mpis.setDeleteOnExit(true); + assertTrue(mpis.getParts().isEmpty()); + } + + @Test public void testNoBoundaryRequest() throws Exception {