This is an automated email from the ASF dual-hosted git repository.
markt-asf pushed a commit to branch 11.0.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git
The following commit(s) were added to refs/heads/11.0.x by this push:
new 679de42c5c Send an Allow header with the 405 response from HttpServlet
679de42c5c is described below
commit 679de42c5c15dfcd34b49f625e2357e49be65ad4
Author: Arturo Bernal <[email protected]>
AuthorDate: Fri Oct 23 18:11:08 2026 +0200
Send an Allow header with the 405 response from HttpServlet
Section 15.5.6 of RFC 9110 requires an origin server to generate an
Allow header field in a 405 response that lists the methods supported by
the target resource. The 405 response sent for a method the servlet does
not implement did not include one. Use the value that the default
OPTIONS handling generates.
---
java/jakarta/servlet/http/HttpServlet.java | 8 +++++-
test/jakarta/servlet/http/TestHttpServlet.java | 39 ++++++++++++++++++++++++++
2 files changed, 46 insertions(+), 1 deletion(-)
diff --git a/java/jakarta/servlet/http/HttpServlet.java
b/java/jakarta/servlet/http/HttpServlet.java
index 51b636abe1..ee176020c1 100644
--- a/java/jakarta/servlet/http/HttpServlet.java
+++ b/java/jakarta/servlet/http/HttpServlet.java
@@ -367,6 +367,8 @@ public abstract class HttpServlet extends GenericServlet {
if (protocol.isEmpty() || protocol.endsWith("0.9") ||
protocol.endsWith("1.0")) {
resp.sendError(HttpServletResponse.SC_BAD_REQUEST, msg);
} else {
+ // RFC 9110, section 15.5.6: a 405 response must include an Allow
header field
+ resp.setHeader("Allow", getAllowHeaderValue(req));
resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, msg);
}
}
@@ -502,7 +504,11 @@ public abstract class HttpServlet extends GenericServlet {
* @exception ServletException if the request for the OPTIONS cannot be
handled
*/
protected void doOptions(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
+ resp.setHeader("Allow", getAllowHeaderValue(req));
+ }
+
+ private String getAllowHeaderValue(HttpServletRequest req) {
String allow = getCachedAllowHeaderValue();
// Tomcat specific hack to see if TRACE is allowed
@@ -514,7 +520,7 @@ public abstract class HttpServlet extends GenericServlet {
}
}
- resp.setHeader("Allow", allow);
+ return allow;
}
diff --git a/test/jakarta/servlet/http/TestHttpServlet.java
b/test/jakarta/servlet/http/TestHttpServlet.java
index 448c6b9ede..b5568f5e39 100644
--- a/test/jakarta/servlet/http/TestHttpServlet.java
+++ b/test/jakarta/servlet/http/TestHttpServlet.java
@@ -225,6 +225,18 @@ public class TestHttpServlet extends TomcatBaseTest {
}
+ @Test
+ public void testMethodNotAllowed() throws Exception {
+ doTestMethodNotAllowed(new OptionsServlet(), "GET, HEAD, OPTIONS");
+ }
+
+
+ @Test
+ public void testMethodNotAllowedSub() throws Exception {
+ doTestMethodNotAllowed(new OptionsServletSub(), "GET, HEAD, POST,
OPTIONS");
+ }
+
+
private void doTestDoOptions(Servlet servlet, String expectedAllow) throws
Exception {
Tomcat tomcat = getTomcatInstance();
@@ -246,6 +258,33 @@ public class TestHttpServlet extends TomcatBaseTest {
}
+ /*
+ * RFC 9110, section 15.5.6: a 405 response must contain an Allow header
field that lists the methods supported by
+ * the target resource.
+ */
+ private void doTestMethodNotAllowed(Servlet servlet, String expectedAllow)
throws Exception {
+ Tomcat tomcat = getTomcatInstance();
+
+ // No file system docBase required
+ StandardContext ctx = (StandardContext) getProgrammaticRootContext();
+
+ // Map the test Servlet
+ Tomcat.addServlet(ctx, "servlet", servlet);
+ ctx.addServletMapping("/", "servlet");
+
+ tomcat.start();
+
+ Map<String,List<String>> resHeaders = new HashMap<>();
+ int rc = methodUrl("http://localhost:" + getPort() + "/", new
ByteChunk(), DEFAULT_CLIENT_TIMEOUT_MS, null,
+ resHeaders, Method.PUT);
+
+ Assert.assertEquals(HttpServletResponse.SC_METHOD_NOT_ALLOWED, rc);
+ List<String> allow = resHeaders.get("Allow");
+ Assert.assertNotNull(allow);
+ Assert.assertEquals(expectedAllow, allow.get(0));
+ }
+
+
@Test
public void testUnimplementedMethodHttp09() throws Exception {
doTestUnimplementedMethod("0.9");
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]