This is an automated email from the ASF dual-hosted git repository. schultz pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/tomcat.git
The following commit(s) were added to refs/heads/master by this push: new 8dddc11 Fix https://bz.apache.org/bugzilla/show_bug.cgi?id=64384 8dddc11 is described below commit 8dddc11512fbd3b91ed9d737a42e4b8415458ddf Author: Christopher Schultz <ch...@christopherschultz.net> AuthorDate: Mon Apr 27 18:01:00 2020 -0400 Fix https://bz.apache.org/bugzilla/show_bug.cgi?id=64384 Respect partial multipart-config elements. --- .../org/apache/catalina/startup/ContextConfig.java | 30 ++-- .../catalina/startup/TestMultipartConfig.java | 171 +++++++++++++++++++++ 2 files changed, 189 insertions(+), 12 deletions(-) diff --git a/java/org/apache/catalina/startup/ContextConfig.java b/java/org/apache/catalina/startup/ContextConfig.java index 2f3ebd0..18517e2 100644 --- a/java/org/apache/catalina/startup/ContextConfig.java +++ b/java/org/apache/catalina/startup/ContextConfig.java @@ -1362,19 +1362,25 @@ public class ContextConfig implements LifecycleListener { wrapper.setServletClass(servlet.getServletClass()); MultipartDef multipartdef = servlet.getMultipartDef(); if (multipartdef != null) { - if (multipartdef.getMaxFileSize() != null && - multipartdef.getMaxRequestSize()!= null && - multipartdef.getFileSizeThreshold() != null) { - wrapper.setMultipartConfigElement(new MultipartConfigElement( - multipartdef.getLocation(), - Long.parseLong(multipartdef.getMaxFileSize()), - Long.parseLong(multipartdef.getMaxRequestSize()), - Integer.parseInt( - multipartdef.getFileSizeThreshold()))); - } else { - wrapper.setMultipartConfigElement(new MultipartConfigElement( - multipartdef.getLocation())); + long maxFileSize = -1; + long maxRequestSize = -1; + int fileSizeThreshold = 0; + + if(null != multipartdef.getMaxFileSize()) { + maxFileSize = Long.parseLong(multipartdef.getMaxFileSize()); + } + if(null != multipartdef.getMaxRequestSize()) { + maxRequestSize = Long.parseLong(multipartdef.getMaxRequestSize()); } + if(null != multipartdef.getFileSizeThreshold()) { + fileSizeThreshold = Integer.parseInt(multipartdef.getFileSizeThreshold()); + } + + wrapper.setMultipartConfigElement(new MultipartConfigElement( + multipartdef.getLocation(), + maxFileSize, + maxRequestSize, + fileSizeThreshold)); } if (servlet.getAsyncSupported() != null) { wrapper.setAsyncSupported( diff --git a/test/org/apache/catalina/startup/TestMultipartConfig.java b/test/org/apache/catalina/startup/TestMultipartConfig.java new file mode 100644 index 0000000..d0ca905 --- /dev/null +++ b/test/org/apache/catalina/startup/TestMultipartConfig.java @@ -0,0 +1,171 @@ +package org.apache.catalina.startup; + +import java.lang.reflect.Method; + +import jakarta.servlet.MultipartConfigElement; + +import org.apache.catalina.Container; +import org.apache.catalina.Context; +import org.apache.catalina.LifecycleState; +import org.apache.catalina.connector.Connector; +import org.apache.catalina.core.StandardContext; +import org.apache.catalina.core.StandardEngine; +import org.apache.catalina.core.StandardHost; +import org.apache.catalina.core.StandardService; +import org.apache.catalina.core.StandardWrapper; +import org.apache.tomcat.util.descriptor.web.MultipartDef; +import org.apache.tomcat.util.descriptor.web.ServletDef; +import org.apache.tomcat.util.descriptor.web.WebXml; +import org.junit.Assert; +import org.junit.Test; + +public class TestMultipartConfig { + @Test + public void testNoMultipartConfig() throws Exception { + StandardWrapper servlet = config(null); + + MultipartConfigElement mce = servlet.getMultipartConfigElement(); + + Assert.assertNull(mce); + } + + @Test + public void testDefaultMultipartConfig() throws Exception { + MultipartDef multipartDef = new MultipartDef(); + // Do not set any attributes on multipartDef: expect defaults + + StandardWrapper servlet = config(multipartDef); + MultipartConfigElement mce = servlet.getMultipartConfigElement(); + + Assert.assertNotNull(mce); + Assert.assertEquals("", mce.getLocation()); + Assert.assertEquals(-1, mce.getMaxFileSize()); + Assert.assertEquals(-1, mce.getMaxRequestSize()); + Assert.assertEquals(0, mce.getFileSizeThreshold()); + } + + @Test + public void testPartialMultipartConfigMaxFileSize() throws Exception { + MultipartDef multipartDef = new MultipartDef(); + multipartDef.setMaxFileSize("1024"); + + StandardWrapper servlet = config(multipartDef); + MultipartConfigElement mce = servlet.getMultipartConfigElement(); + + Assert.assertNotNull(mce); + Assert.assertEquals("", mce.getLocation()); + Assert.assertEquals(1024, mce.getMaxFileSize()); + Assert.assertEquals(-1, mce.getMaxRequestSize()); + Assert.assertEquals(0, mce.getFileSizeThreshold()); + } + + @Test + public void testPartialMultipartConfigMaxReqeustSize() throws Exception { + MultipartDef multipartDef = new MultipartDef(); + multipartDef.setMaxRequestSize("10240"); + + StandardWrapper servlet = config(multipartDef); + MultipartConfigElement mce = servlet.getMultipartConfigElement(); + + Assert.assertNotNull(mce); + Assert.assertEquals("", mce.getLocation()); + Assert.assertEquals(-1, mce.getMaxFileSize()); + Assert.assertEquals(10240, mce.getMaxRequestSize()); + Assert.assertEquals(0, mce.getFileSizeThreshold()); + } + + @Test + public void testPartialMultipartConfigFileSizeThreshold() throws Exception { + MultipartDef multipartDef = new MultipartDef(); + multipartDef.setFileSizeThreshold("24"); + + StandardWrapper servlet = config(multipartDef); + MultipartConfigElement mce = servlet.getMultipartConfigElement(); + + Assert.assertNotNull(mce); + Assert.assertEquals("", mce.getLocation()); + Assert.assertEquals(-1, mce.getMaxFileSize()); + Assert.assertEquals(-1, mce.getMaxRequestSize()); + Assert.assertEquals(24, mce.getFileSizeThreshold()); + } + + @Test + public void testCompleteMultipartConfig() throws Exception { + MultipartDef multipartDef = new MultipartDef(); + multipartDef.setMaxFileSize("1024"); + multipartDef.setMaxRequestSize("10240"); + multipartDef.setFileSizeThreshold("24"); + multipartDef.setLocation("/tmp/foo"); + + StandardWrapper servlet = config(multipartDef); + + MultipartConfigElement mce = servlet.getMultipartConfigElement(); + + Assert.assertNotNull(mce); + Assert.assertEquals("/tmp/foo", mce.getLocation()); + Assert.assertEquals(1024, mce.getMaxFileSize()); + Assert.assertEquals(10240, mce.getMaxRequestSize()); + Assert.assertEquals(24, mce.getFileSizeThreshold()); + } + + private StandardWrapper config(MultipartDef multipartDef) throws Exception { + MyContextConfig config = new MyContextConfig(); + + WebXml webxml = new WebXml(); + + ServletDef servletDef = new ServletDef(); + servletDef.setServletName("test"); + servletDef.setServletClass("org.apache.catalina.startup.ParamServlet"); + servletDef.setMultipartDef(multipartDef); + webxml.addServlet(servletDef); + + Method m = ContextConfig.class.getDeclaredMethod("configureContext", WebXml.class); + + // Force our way in + m.setAccessible(true); + + m.invoke(config, webxml); + + StandardWrapper servlet = (StandardWrapper)config.getContext().findChild("test"); + + return servlet; + } + + private static class MyContextConfig extends ContextConfig { + public MyContextConfig() { + CustomContext context = new CustomContext(); + super.context = context; + context.setConfigured(false); + context.setState(LifecycleState.STARTING_PREP); + context.setName("test"); + + Connector connector = new Connector(); + StandardService service = new StandardService(); + service.addConnector(connector); + StandardEngine engine = new StandardEngine(); + engine.setService(service); + Container parent = new StandardHost(); + parent.setParent(engine); + super.context.setParent(parent); + context.getState().equals(LifecycleState.STARTING_PREP); + } + public Context getContext() { + return super.context; + } + } + + private static class CustomContext extends StandardContext { + private volatile LifecycleState state; + + @Override + public LifecycleState getState() { + return state; + } + + @Override + public synchronized void setState(LifecycleState state) { + this.state = state; + } + } + +} --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org