diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/MultiPartFormInputStream.java b/jetty-server/src/main/java/org/eclipse/jetty/server/MultiPartFormInputStream.java index 492c52a12394..5d8eaffaa4ac 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/MultiPartFormInputStream.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/MultiPartFormInputStream.java @@ -94,7 +94,7 @@ private enum State private volatile boolean _deleteOnExit; private volatile boolean _writeFilesWithFilenames; private volatile int _bufferSize = 16 * 1024; - private volatile State state = State.UNPARSED; + private State state = State.UNPARSED; public class MultiPart implements Part { @@ -356,7 +356,11 @@ public String getContentDispositionFilename() */ public MultiPartFormInputStream(InputStream in, String contentType, MultipartConfigElement config, File contextTmpDir) { + // Must be a multipart request. _contentType = contentType; + if (_contentType == null || !_contentType.startsWith("multipart/form-data")) + throw new IllegalArgumentException("content type is not multipart/form-data"); + _contextTmpDir = (contextTmpDir != null) ? contextTmpDir : new File(System.getProperty("java.io.tmpdir")); _config = (config != null) ? config : new MultipartConfigElement(_contextTmpDir.getAbsolutePath()); @@ -520,10 +524,6 @@ protected void parse() MultiPartParser parser = null; try { - // if its not a multipart request, don't parse it - if (_contentType == null || !_contentType.startsWith("multipart/form-data")) - return; - // sort out the location to which to write the files if (_config.getLocation() == null) _tmpDir = _contextTmpDir; diff --git a/jetty-server/src/test/java/org/eclipse/jetty/server/MultiPartFormInputStreamTest.java b/jetty-server/src/test/java/org/eclipse/jetty/server/MultiPartFormInputStreamTest.java index 4854445e6be0..733bb7421516 100644 --- a/jetty-server/src/test/java/org/eclipse/jetty/server/MultiPartFormInputStreamTest.java +++ b/jetty-server/src/test/java/org/eclipse/jetty/server/MultiPartFormInputStreamTest.java @@ -206,12 +206,10 @@ public void testNonMultiPartRequest() throws Exception { MultipartConfigElement config = new MultipartConfigElement(_dirname, 1024, 3072, 50); - MultiPartFormInputStream mpis = new MultiPartFormInputStream(new ByteArrayInputStream(_multi.getBytes()), - "Content-type: text/plain", - config, - _tmpDir); - mpis.setDeleteOnExit(true); - assertTrue(mpis.getParts().isEmpty()); + Throwable t = assertThrows(IllegalArgumentException.class, () -> + new MultiPartFormInputStream(new ByteArrayInputStream(_multi.getBytes()), + "Content-type: text/plain", config, _tmpDir)); + assertThat(t.getMessage(), is("content type is not multipart/form-data")); } @Test