Support non-standard HTTP methods in FrameworkServlet
This commit ensures that HTTP methods not supported by HttpServlet, for instance WebDAV methods, are still supported in FrameworkServlet. Closes gh-29689
This commit is contained in:
@@ -21,6 +21,7 @@ import java.security.Principal;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@@ -167,6 +168,12 @@ public abstract class FrameworkServlet extends HttpServletBean implements Applic
|
||||
*/
|
||||
private static final String INIT_PARAM_DELIMITERS = ",; \t\n";
|
||||
|
||||
/**
|
||||
* HTTP methods supported by {@link jakarta.servlet.http.HttpServlet}.
|
||||
*/
|
||||
private static final Set<String> HTTP_SERVLET_METHODS = Set.of("DELETE", "HEAD", "GET", "OPTIONS", "POST", "PUT",
|
||||
"TRACE");
|
||||
|
||||
|
||||
/** ServletContext attribute to find the WebApplicationContext in. */
|
||||
@Nullable
|
||||
@@ -866,18 +873,18 @@ public abstract class FrameworkServlet extends HttpServletBean implements Applic
|
||||
|
||||
|
||||
/**
|
||||
* Override the parent class implementation in order to intercept PATCH requests.
|
||||
* Override the parent class implementation in order to intercept requests
|
||||
* using PATCH or non-standard HTTP methods (WebDAV).
|
||||
*/
|
||||
@Override
|
||||
protected void service(HttpServletRequest request, HttpServletResponse response)
|
||||
throws ServletException, IOException {
|
||||
|
||||
HttpMethod httpMethod = HttpMethod.valueOf(request.getMethod());
|
||||
if (HttpMethod.PATCH.equals(httpMethod)) {
|
||||
processRequest(request, response);
|
||||
if (HTTP_SERVLET_METHODS.contains(request.getMethod())) {
|
||||
super.service(request, response);
|
||||
}
|
||||
else {
|
||||
super.service(request, response);
|
||||
processRequest(request, response);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -875,6 +875,14 @@ public class DispatcherServletTests {
|
||||
assertThat(getServletContext().getAttribute("otherInitialized")).isEqualTo("true");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void webDavMethod() throws Exception {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "PROPFIND", "/body.do");
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
complexDispatcherServlet.service(request, response);
|
||||
assertThat(response.getContentAsString()).isEqualTo("body");
|
||||
}
|
||||
|
||||
|
||||
public static class ControllerFromParent implements Controller {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user