This is an automated email from the ASF dual-hosted git repository.
lukaszlenart pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/struts.git
The following commit(s) were added to refs/heads/main by this push:
new 53f2ec581 WW-5821 refactor(core): use one HTTP method matching rule
(#2151)
53f2ec581 is described below
commit 53f2ec5811d1a05c3e11c635de7e8c545f82f24b
Author: Lukasz Lenart <[email protected]>
AuthorDate: Sun Nov 22 10:18:38 2026 +0100
WW-5821 refactor(core): use one HTTP method matching rule (#2151)
Follow-up to #2043.
- HttpMethod.parse builds on parseOrNull and throws
IllegalArgumentException for an unknown method, instead of a
separate valueOf(toUpperCase(Locale.ROOT)) lookup.
- HttpMethodInterceptor#isAllowed resolves the request method with
parseOrNull and checks Set.contains, so an unknown verb is never
allowed by construction. The result is unchanged for every input.
- Cover an unrestricted HttpMethodAware action receiving PROPFIND:
the invocation proceeds and setMethod(null) overwrites the previous
value.
- Drop the unused static import of Assert.assertNull; the JUnit 3
TestCase method is the one in use.
Co-authored-by: Claude Opus 5.5 <[email protected]>
---
.../struts2/interceptor/httpmethod/HttpMethod.java | 10 +++++--
.../httpmethod/HttpMethodInterceptor.java | 3 +-
.../httpmethod/HttpMethodInterceptorTest.java | 33 +++++++++++++++++++++-
3 files changed, 42 insertions(+), 4 deletions(-)
diff --git
a/core/src/main/java/org/apache/struts2/interceptor/httpmethod/HttpMethod.java
b/core/src/main/java/org/apache/struts2/interceptor/httpmethod/HttpMethod.java
index 9cc018d3a..02332647a 100644
---
a/core/src/main/java/org/apache/struts2/interceptor/httpmethod/HttpMethod.java
+++
b/core/src/main/java/org/apache/struts2/interceptor/httpmethod/HttpMethod.java
@@ -19,7 +19,6 @@
package org.apache.struts2.interceptor.httpmethod;
import java.util.Arrays;
-import java.util.Locale;
/**
* Enum represents possible http request types
@@ -49,8 +48,15 @@ public enum HttpMethod {
.orElse(null);
}
+ /**
+ * @throws IllegalArgumentException when the request method is not one of
the constants
+ */
public static HttpMethod parse(String httpRequestMethod) {
- return valueOf(httpRequestMethod.toUpperCase(Locale.ROOT));
+ HttpMethod method = parseOrNull(httpRequestMethod);
+ if (method == null) {
+ throw new IllegalArgumentException("Unknown HTTP method: " +
httpRequestMethod);
+ }
+ return method;
}
}
diff --git
a/core/src/main/java/org/apache/struts2/interceptor/httpmethod/HttpMethodInterceptor.java
b/core/src/main/java/org/apache/struts2/interceptor/httpmethod/HttpMethodInterceptor.java
index 5498e6729..acd44fa54 100644
---
a/core/src/main/java/org/apache/struts2/interceptor/httpmethod/HttpMethodInterceptor.java
+++
b/core/src/main/java/org/apache/struts2/interceptor/httpmethod/HttpMethodInterceptor.java
@@ -235,7 +235,8 @@ public class HttpMethodInterceptor extends
AbstractInterceptor {
}
private boolean isAllowed(String requestedMethod, Set<HttpMethod>
allowedMethods) {
- return allowedMethods.stream().anyMatch(method ->
method.name().equalsIgnoreCase(requestedMethod));
+ HttpMethod method = HttpMethod.parseOrNull(requestedMethod);
+ return method != null && allowedMethods.contains(method);
}
/**
diff --git
a/core/src/test/java/org/apache/struts2/interceptor/httpmethod/HttpMethodInterceptorTest.java
b/core/src/test/java/org/apache/struts2/interceptor/httpmethod/HttpMethodInterceptorTest.java
index 7cea5e708..4269ffcd4 100644
---
a/core/src/test/java/org/apache/struts2/interceptor/httpmethod/HttpMethodInterceptorTest.java
+++
b/core/src/test/java/org/apache/struts2/interceptor/httpmethod/HttpMethodInterceptorTest.java
@@ -39,7 +39,6 @@ import org.springframework.mock.web.MockHttpServletResponse;
import java.util.List;
import java.util.Map;
-import static org.junit.Assert.assertNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
@@ -485,6 +484,20 @@ public class HttpMethodInterceptorTest extends
StrutsInternalTestCase {
assertNull(action.getHttpMethod());
}
+ public void
testUnrestrictedHttpMethodAwareActionWithUnknownRequestMethodIsInvoked() throws
Exception {
+ UnrestrictedHttpMethodAwareAction action = new
UnrestrictedHttpMethodAwareAction();
+ action.setMethod(HttpMethod.GET);
+ prepareActionInvocation(action, actionConfig());
+ invocation.setResultCode("success");
+ MockHttpServletResponse response = prepareRequest("PROPFIND");
+
+ String resultName = interceptor.intercept(invocation);
+
+ assertEquals("success", resultName);
+ assertEquals(HttpServletResponse.SC_OK, response.getStatus());
+ assertNull(action.getHttpMethod());
+ }
+
public void testHttpMethodAttributeRejectsWith405AndAllowHeader() throws
Exception {
MockHttpServletResponse response = new MockHttpServletResponse();
ActionProxy proxy = proxyFor("", "PostOnly", "GET", response);
@@ -683,6 +696,24 @@ public class HttpMethodInterceptorTest extends
StrutsInternalTestCase {
public static class PostOnlyWithoutExecute {
}
+ public static class UnrestrictedHttpMethodAwareAction implements
HttpMethodAware {
+ private HttpMethod httpMethod;
+
+ @Override
+ public void setMethod(HttpMethod httpMethod) {
+ this.httpMethod = httpMethod;
+ }
+
+ public HttpMethod getHttpMethod() {
+ return httpMethod;
+ }
+
+ @Override
+ public String getBadRequestResultName() {
+ return null;
+ }
+ }
+
public interface PostMethod {
@HttpPost
String save();