Skip to content

Commit d507c2b

Browse files
committed
"dispatchOptionsRequest" only sets the default 'Allow' header if actually needed (SPR-7837); "dispatchTraceRequest" only generates default response body if actually needed
1 parent 450a3d7 commit d507c2b

File tree

1 file changed

+27
-19
lines changed

1 file changed

+27
-19
lines changed

org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/FrameworkServlet.java

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2010 the original author or authors.
2+
* Copyright 2002-2011 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -143,9 +143,9 @@ public abstract class FrameworkServlet extends HttpServletBean {
143143
/**
144144
* Any number of these characters are considered delimiters between
145145
* multiple values in a single init-param String value.
146-
* @see #initializeWebApplicationContext
147146
*/
148-
private String INIT_PARAM_DELIMITERS = ",; \t\n";
147+
private static final String INIT_PARAM_DELIMITERS = ",; \t\n";
148+
149149

150150
/** ServletContext attribute to find the WebApplicationContext in */
151151
private String contextAttribute;
@@ -188,7 +188,7 @@ public abstract class FrameworkServlet extends HttpServletBean {
188188

189189
/** Actual ApplicationContextInitializer instances to apply to the context */
190190
private ArrayList<ApplicationContextInitializer<ConfigurableApplicationContext>> contextInitializers =
191-
new ArrayList<ApplicationContextInitializer<ConfigurableApplicationContext>>();
191+
new ArrayList<ApplicationContextInitializer<ConfigurableApplicationContext>>();
192192

193193

194194
/**
@@ -408,8 +408,8 @@ public void setThreadContextInheritable(boolean threadContextInheritable) {
408408
* means that your controllers will receive those requests; make sure
409409
* that those endpoints are actually able to handle an OPTIONS request.
410410
* <p>Note that HttpServlet's default OPTIONS processing will be applied
411-
* in any case. Your controllers are simply available to override the
412-
* default headers and optionally generate a response body.
411+
* in any case if your controllers happen to not set the 'Allow' header
412+
* (as required for an OPTIONS response).
413413
*/
414414
public void setDispatchOptionsRequest(boolean dispatchOptionsRequest) {
415415
this.dispatchOptionsRequest = dispatchOptionsRequest;
@@ -425,9 +425,8 @@ public void setDispatchOptionsRequest(boolean dispatchOptionsRequest) {
425425
* means that your controllers will receive those requests; make sure
426426
* that those endpoints are actually able to handle a TRACE request.
427427
* <p>Note that HttpServlet's default TRACE processing will be applied
428-
* in any case. Your controllers are simply available to override the
429-
* default headers and the default body, calling <code>response.reset()</code>
430-
* if necessary.
428+
* in any case if your controllers happen to not generate a response
429+
* of content type 'message/http' (as required for a TRACE response).
431430
*/
432431
public void setDispatchTraceRequest(boolean dispatchTraceRequest) {
433432
this.dispatchTraceRequest = dispatchTraceRequest;
@@ -661,23 +660,23 @@ protected WebApplicationContext createWebApplicationContext(WebApplicationContex
661660
@SuppressWarnings("unchecked")
662661
protected void applyInitializers(ConfigurableApplicationContext wac) {
663662
if (this.contextInitializerClasses != null) {
664-
String[] initializerClassNames = StringUtils.tokenizeToStringArray(this.contextInitializerClasses, INIT_PARAM_DELIMITERS);
665-
for(String initializerClassName : initializerClassNames) {
666-
ApplicationContextInitializer<ConfigurableApplicationContext> initializer = null;
663+
String[] initializerClassNames =
664+
StringUtils.tokenizeToStringArray(this.contextInitializerClasses, INIT_PARAM_DELIMITERS);
665+
for (String initializerClassName : initializerClassNames) {
666+
ApplicationContextInitializer<ConfigurableApplicationContext> initializer;
667667
try {
668668
Class<?> initializerClass = ClassUtils.forName(initializerClassName, wac.getClassLoader());
669669
initializer = BeanUtils.instantiateClass(initializerClass, ApplicationContextInitializer.class);
670-
} catch (Exception ex) {
670+
}
671+
catch (Exception ex) {
671672
throw new IllegalArgumentException(
672673
String.format("Could not instantiate class [%s] specified via " +
673674
"'contextInitializerClasses' init-param", initializerClassName), ex);
674675
}
675676
this.contextInitializers.add(initializer);
676677
}
677678
}
678-
679679
Collections.sort(this.contextInitializers, new AnnotationAwareOrderComparator());
680-
681680
for (ApplicationContextInitializer<ConfigurableApplicationContext> initializer : this.contextInitializers) {
682681
initializer.initialize(wac);
683682
}
@@ -814,32 +813,41 @@ protected final void doDelete(HttpServletRequest request, HttpServletResponse re
814813

815814
/**
816815
* Delegate OPTIONS requests to {@link #processRequest}, if desired.
817-
* <p>Applies HttpServlet's standard OPTIONS processing first.
816+
* <p>Applies HttpServlet's standard OPTIONS processing otherwise,
817+
* and also if there is still no 'Allow' header set after dispatching.
818818
* @see #doService
819819
*/
820820
@Override
821821
protected void doOptions(HttpServletRequest request, HttpServletResponse response)
822822
throws ServletException, IOException {
823823

824-
super.doOptions(request, response);
825824
if (this.dispatchOptionsRequest) {
826825
processRequest(request, response);
826+
if (response.containsHeader("Allow")) {
827+
// Proper OPTIONS response coming from a handler - we're done.
828+
return;
829+
}
827830
}
831+
super.doOptions(request, response);
828832
}
829833

830834
/**
831835
* Delegate TRACE requests to {@link #processRequest}, if desired.
832-
* <p>Applies HttpServlet's standard TRACE processing first.
836+
* <p>Applies HttpServlet's standard TRACE processing otherwise.
833837
* @see #doService
834838
*/
835839
@Override
836840
protected void doTrace(HttpServletRequest request, HttpServletResponse response)
837841
throws ServletException, IOException {
838842

839-
super.doTrace(request, response);
840843
if (this.dispatchTraceRequest) {
841844
processRequest(request, response);
845+
if ("message/http".equals(response.getContentType())) {
846+
// Proper TRACE response coming from a handler - we're done.
847+
return;
848+
}
842849
}
850+
super.doTrace(request, response);
843851
}
844852

845853

0 commit comments

Comments
 (0)