WW-4785 Allows disable multipart support via an config option
Project: http://git-wip-us.apache.org/repos/asf/struts/repo Commit: http://git-wip-us.apache.org/repos/asf/struts/commit/13f49a09 Tree: http://git-wip-us.apache.org/repos/asf/struts/tree/13f49a09 Diff: http://git-wip-us.apache.org/repos/asf/struts/diff/13f49a09 Branch: refs/heads/master Commit: 13f49a09780e2bfba3af48ee736598fbf5c2b7b5 Parents: 2fe0505 Author: Lukasz Lenart <lukaszlen...@apache.org> Authored: Sun Apr 16 14:33:15 2017 +0200 Committer: Lukasz Lenart <lukaszlen...@apache.org> Committed: Sun Apr 16 14:33:15 2017 +0200 ---------------------------------------------------------------------- .../org/apache/struts2/StrutsConstants.java | 5 ++++ .../apache/struts2/dispatcher/Dispatcher.java | 24 +++++++++++++++++++- .../struts2/dispatcher/DispatcherTest.java | 12 +++++++++- 3 files changed, 39 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/struts/blob/13f49a09/core/src/main/java/org/apache/struts2/StrutsConstants.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/struts2/StrutsConstants.java b/core/src/main/java/org/apache/struts2/StrutsConstants.java index b41f7e6..f9d9b24 100644 --- a/core/src/main/java/org/apache/struts2/StrutsConstants.java +++ b/core/src/main/java/org/apache/struts2/StrutsConstants.java @@ -139,6 +139,11 @@ public final class StrutsConstants { */ public static final String STRUTS_MULTIPART_PARSER = "struts.multipart.parser"; + /** + * A global switch to disable support for multipart requests + */ + public static final String STRUTS_MULTIPART_ENABLED = "struts.multipart.enabled"; + public static final String STRUTS_MULTIPART_VALIDATION_REGEX = "struts.multipart.validationRegex"; /** How Spring should autowire. Valid values are 'name', 'type', 'auto', and 'constructor' */ http://git-wip-us.apache.org/repos/asf/struts/blob/13f49a09/core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java b/core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java index ea92eaf..3645241 100644 --- a/core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java +++ b/core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java @@ -130,6 +130,11 @@ public class Dispatcher { private String multipartHandlerName; /** + * Stores the value of {@link StrutsConstants#STRUTS_MULTIPART_ENABLED} + */ + private boolean multipartSupportEnabled = true; + + /** * A regular expression used to validate if request is a multipart/form-data request */ private Pattern multipartValidationPattern = Pattern.compile(MULTIPART_FORM_DATA_REGEX); @@ -277,6 +282,11 @@ public class Dispatcher { multipartHandlerName = val; } + @Inject(value = StrutsConstants.STRUTS_MULTIPART_ENABLED, required = false) + public void setMultipartSupportEnabled(String multipartSupportEnabled) { + this.multipartSupportEnabled = Boolean.parseBoolean(multipartSupportEnabled); + } + @Inject(value = StrutsConstants.STRUTS_MULTIPART_VALIDATION_REGEX, required = false) public void setMultipartValidationRegex(String multipartValidationRegex) { this.multipartValidationPattern = Pattern.compile(multipartValidationRegex); @@ -799,7 +809,7 @@ public class Dispatcher { return request; } - if (isMultipartRequest(request)) { + if (isMultipartSupportEnabled(request) && isMultipartRequest(request)) { MultiPartRequest multiPartRequest = getMultiPartRequest(); LocaleProviderFactory localeProviderFactory = getContainer().getInstance(LocaleProviderFactory.class); @@ -818,6 +828,18 @@ public class Dispatcher { } /** + * Checks if support to parse multipart requests is enabled + * + * @param request current servlet request + * @return false if disabled + * + * @since 2.5.11 + */ + protected boolean isMultipartSupportEnabled(HttpServletRequest request) { + return multipartSupportEnabled; + } + + /** * Checks if request is a multipart request (a file upload request) * * @param request current servlet request http://git-wip-us.apache.org/repos/asf/struts/blob/13f49a09/core/src/test/java/org/apache/struts2/dispatcher/DispatcherTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/struts2/dispatcher/DispatcherTest.java b/core/src/test/java/org/apache/struts2/dispatcher/DispatcherTest.java index ffa43e1..8001bd2 100644 --- a/core/src/test/java/org/apache/struts2/dispatcher/DispatcherTest.java +++ b/core/src/test/java/org/apache/struts2/dispatcher/DispatcherTest.java @@ -257,7 +257,17 @@ public class DispatcherTest extends StrutsInternalTestCase { mockContainer.verify(); mockConfiguration.verify(); } - + + public void testMultipartSupportEnabledByDefault() throws Exception { + HttpServletRequest req = new MockHttpServletRequest(); + HttpServletResponse res = new MockHttpServletResponse(); + + Dispatcher du = initDispatcher(Collections.<String, String>emptyMap()); + du.prepare(req, res); + + assertTrue(du.isMultipartSupportEnabled(req)); + } + class InternalConfigurationManager extends ConfigurationManager { public boolean destroyConfiguration = false;