Skip to content

Commit fa8aacc

Browse files
arturobernalgmarkt-asf
authored andcommitted
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.
1 parent d05cd91 commit fa8aacc

2 files changed

Lines changed: 46 additions & 1 deletion

File tree

‎java/jakarta/servlet/http/HttpServlet.java‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,8 @@ private void sendMethodNotAllowed(HttpServletRequest req, HttpServletResponse re
394394
if (protocol.isEmpty() || protocol.endsWith("0.9") || protocol.endsWith("1.0")) {
395395
resp.sendError(HttpServletResponse.SC_BAD_REQUEST, msg);
396396
} else {
397+
// RFC 9110, section 15.5.6: a 405 response must include an Allow header field
398+
resp.setHeader("Allow", getAllowHeaderValue(req));
397399
resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, msg);
398400
}
399401
}
@@ -538,7 +540,11 @@ private static Method[] getAllDeclaredMethods(Class<?> c) {
538540
* @exception ServletException if the request for the OPTIONS cannot be handled
539541
*/
540542
protected void doOptions(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
543+
resp.setHeader("Allow", getAllowHeaderValue(req));
544+
}
545+
541546

547+
private String getAllowHeaderValue(HttpServletRequest req) {
542548
String allow = getCachedAllowHeaderValue();
543549

544550
// Tomcat specific hack to see if TRACE is allowed
@@ -550,7 +556,7 @@ protected void doOptions(HttpServletRequest req, HttpServletResponse resp) throw
550556
}
551557
}
552558

553-
resp.setHeader("Allow", allow);
559+
return allow;
554560
}
555561

556562

‎test/jakarta/servlet/http/TestHttpServlet.java‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,18 @@ public void testDoOptionsQuery() throws Exception {
231231
}
232232

233233

234+
@Test
235+
public void testMethodNotAllowed() throws Exception {
236+
doTestMethodNotAllowed(new OptionsServlet(), "GET, HEAD, OPTIONS");
237+
}
238+
239+
240+
@Test
241+
public void testMethodNotAllowedSub() throws Exception {
242+
doTestMethodNotAllowed(new OptionsServletSub(), "GET, HEAD, POST, OPTIONS");
243+
}
244+
245+
234246
@Test
235247
public void testQueryMethod() throws Exception {
236248
Tomcat tomcat = getTomcatInstance();
@@ -363,6 +375,33 @@ private void doTestDoOptions(Servlet servlet, String expectedAllow) throws Excep
363375
}
364376

365377

378+
/*
379+
* RFC 9110, section 15.5.6: a 405 response must contain an Allow header field that lists the methods supported by
380+
* the target resource.
381+
*/
382+
private void doTestMethodNotAllowed(Servlet servlet, String expectedAllow) throws Exception {
383+
Tomcat tomcat = getTomcatInstance();
384+
385+
// No file system docBase required
386+
StandardContext ctx = (StandardContext) getProgrammaticRootContext();
387+
388+
// Map the test Servlet
389+
Tomcat.addServlet(ctx, "servlet", servlet);
390+
ctx.addServletMapping("/", "servlet");
391+
392+
tomcat.start();
393+
394+
Map<String,List<String>> resHeaders = new HashMap<>();
395+
int rc = methodUrl("http://localhost:" + getPort() + "/", new ByteChunk(), DEFAULT_CLIENT_TIMEOUT_MS, null,
396+
resHeaders, Method.PUT);
397+
398+
Assert.assertEquals(HttpServletResponse.SC_METHOD_NOT_ALLOWED, rc);
399+
List<String> allow = resHeaders.get("Allow");
400+
Assert.assertNotNull(allow);
401+
Assert.assertEquals(expectedAllow, allow.get(0));
402+
}
403+
404+
366405
@Test
367406
public void testUnimplementedMethodHttp10() throws Exception {
368407
doTestUnimplementedMethod("1.0");

0 commit comments

Comments
 (0)