Skip to content

Commit 2607a22

Browse files
committed
HTTP OPTIONS lists all HTTP methods except TRACE
This is in line with the current behavior of HttpServlet that would have been in used with dispatchOptionsRequest on the DispatcherSerlvet set to false (the default prior to 4.3). Issue: SPR-13130
1 parent 319e8e2 commit 2607a22

File tree

7 files changed

+18
-21
lines changed

7 files changed

+18
-21
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/AbstractController.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,11 @@
1616

1717
package org.springframework.web.servlet.mvc;
1818

19-
import java.util.Arrays;
20-
import java.util.List;
2119
import javax.servlet.http.HttpServletRequest;
2220
import javax.servlet.http.HttpServletResponse;
2321
import javax.servlet.http.HttpSession;
2422

2523
import org.springframework.http.HttpMethod;
26-
import org.springframework.util.ObjectUtils;
27-
import org.springframework.util.StringUtils;
2824
import org.springframework.web.servlet.ModelAndView;
2925
import org.springframework.web.servlet.support.WebContentGenerator;
3026
import org.springframework.web.util.WebUtils;
@@ -155,10 +151,8 @@ public final boolean isSynchronizeOnSession() {
155151
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
156152
throws Exception {
157153

158-
String[] supportedMethods = getSupportedMethods();
159-
if (HttpMethod.OPTIONS.matches(request.getMethod()) && !ObjectUtils.isEmpty(supportedMethods)) {
160-
List<String> value = Arrays.asList(supportedMethods);
161-
response.setHeader("Allow", StringUtils.collectionToCommaDelimitedString(value));
154+
if (HttpMethod.OPTIONS.matches(request.getMethod())) {
155+
response.setHeader("Allow", getAllowHeader());
162156
return null;
163157
}
164158

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/RequestMappingInfoHandlerMapping.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,8 +317,11 @@ public HttpOptionsHandler(Set<String> declaredMethods) {
317317
private static Set<HttpMethod> initAllowedHttpMethods(Set<String> declaredMethods) {
318318
Set<HttpMethod> result = new LinkedHashSet<HttpMethod>(declaredMethods.size());
319319
if (declaredMethods.isEmpty()) {
320-
result.add(HttpMethod.GET);
321-
result.add(HttpMethod.HEAD);
320+
for (HttpMethod method : HttpMethod.values()) {
321+
if (!HttpMethod.TRACE.equals(method)) {
322+
result.add(method);
323+
}
324+
}
322325
}
323326
else {
324327
boolean hasHead = declaredMethods.contains("HEAD");

spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public class ResourceHttpRequestHandler extends WebContentGenerator
112112

113113

114114
public ResourceHttpRequestHandler() {
115-
super(HttpMethod.GET.name(), HttpMethod.HEAD.name(), HttpMethod.OPTIONS.name());
115+
super(HttpMethod.GET.name(), HttpMethod.HEAD.name());
116116
this.resourceResolvers.add(new PathResourceResolver());
117117
}
118118

@@ -226,6 +226,11 @@ protected void initAllowedLocations() {
226226
public void handleRequest(HttpServletRequest request, HttpServletResponse response)
227227
throws ServletException, IOException {
228228

229+
if (HttpMethod.OPTIONS.matches(request.getMethod())) {
230+
response.setHeader("Allow", getAllowHeader());
231+
return;
232+
}
233+
229234
// Supported methods and required session
230235
checkRequest(request);
231236

@@ -237,11 +242,6 @@ public void handleRequest(HttpServletRequest request, HttpServletResponse respon
237242
return;
238243
}
239244

240-
if (HttpMethod.OPTIONS.matches(request.getMethod())) {
241-
response.setHeader("Allow", "GET,HEAD");
242-
return;
243-
}
244-
245245
// Header phase
246246
if (new ServletWebRequest(request, response).checkNotModified(resource.lastModified())) {
247247
logger.trace("Resource not modified - returning 304");

spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/ParameterizableViewControllerTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public void handleRequestHttpOptions() throws Exception {
7777
ModelAndView mav = this.controller.handleRequest(this.request, response);
7878

7979
assertNull(mav);
80-
assertEquals("GET,HEAD", response.getHeader("Allow"));
80+
assertEquals("GET,HEAD,OPTIONS", response.getHeader("Allow"));
8181
}
8282

8383
}

spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/RequestMappingInfoHandlerMappingTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ public void getHandlerMediaTypeNotSupported() throws Exception {
180180
public void getHandlerHttpOptions() throws Exception {
181181
testHttpOptions("/foo", "GET,HEAD");
182182
testHttpOptions("/person/1", "PUT");
183-
testHttpOptions("/persons", "GET,HEAD");
183+
testHttpOptions("/persons", "GET,HEAD,POST,PUT,PATCH,DELETE,OPTIONS");
184184
testHttpOptions("/something", "PUT,POST");
185185
}
186186

spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandlerTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public void getResourceHttpOptions() throws Exception {
119119
this.handler.handleRequest(this.request, this.response);
120120

121121
assertEquals(200, this.response.getStatus());
122-
assertEquals("GET,HEAD", this.response.getHeader("Allow"));
122+
assertEquals("GET,HEAD,OPTIONS", this.response.getHeader("Allow"));
123123
}
124124

125125
@Test

src/asciidoc/web-mvc.adoc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,8 +1174,8 @@ the number of bytes are counted and the "Content-Length" header set.
11741174
HTTP OPTIONS request is handled by setting the "Allow" response header to the
11751175
HTTP methods explicitly declared on all `@RequestMapping` methods with matching
11761176
URL patterns. When no HTTP methods are explicitly declared the "Allow" header
1177-
is set to "GET,HEAD". Therefore it's highly recommended to always explicitly
1178-
declare the HTTP method(s) an `@RequestMapping` method is meant to handle.
1177+
is set to "GET,HEAD,POST,PUT,PATCH,DELETE,OPTIONS". Ideally always declare the
1178+
HTTP method(s) an `@RequestMapping` method is intended to handle.
11791179

11801180
Although not necessary an `@RequestMapping` method can be mapped to and handle
11811181
either HTTP HEAD or HTTP OPTIONS, or both.

0 commit comments

Comments
 (0)