Skip to content

Commit 6bd12e8

Browse files
committed
Clear CONTENT_DISPOSITION before error handling
Closes gh-35116
1 parent 1051592 commit 6bd12e8

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,6 +1346,7 @@ protected ModelAndView processHandlerException(HttpServletRequest request, HttpS
13461346
// leaving the other response headers in place.
13471347
try {
13481348
response.setHeader(HttpHeaders.CONTENT_TYPE, null);
1349+
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, null);
13491350
response.resetBuffer();
13501351
}
13511352
catch (IllegalStateException illegalStateException) {

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -924,8 +924,8 @@ void shouldAttemptToResetResponseBufferIfCommitted() throws Exception {
924924
assertThat(response.getHeader("Test-Header")).isEqualTo("spring");
925925
}
926926

927-
@Test
928-
void shouldResetContentTypeIfNotCommitted() throws Exception {
927+
@Test // gh-34366, gh-35116
928+
void shouldResetContentHeadersIfNotCommitted() throws Exception {
929929
StaticWebApplicationContext context = new StaticWebApplicationContext();
930930
context.setServletContext(getServletContext());
931931
context.registerSingleton("/error", ErrorController.class);
@@ -934,11 +934,15 @@ void shouldResetContentTypeIfNotCommitted() throws Exception {
934934

935935
MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/error");
936936
MockHttpServletResponse response = new MockHttpServletResponse();
937-
assertThatThrownBy(() -> servlet.service(request, response)).isInstanceOf(ServletException.class)
937+
938+
assertThatThrownBy(() -> servlet.service(request, response))
939+
.isInstanceOf(ServletException.class)
938940
.hasCauseInstanceOf(IllegalArgumentException.class);
941+
939942
assertThat(response.getContentAsByteArray()).isEmpty();
940943
assertThat(response.getStatus()).isEqualTo(400);
941944
assertThat(response.getHeaderNames()).doesNotContain(HttpHeaders.CONTENT_TYPE);
945+
assertThat(response.getHeaderNames()).doesNotContain(HttpHeaders.CONTENT_DISPOSITION);
942946
}
943947

944948

@@ -994,6 +998,7 @@ public ModelAndView handleRequest(HttpServletRequest request, HttpServletRespons
994998
response.setStatus(400);
995999
response.setHeader("Test-Header", "spring");
9961000
response.addHeader("Content-Type", "application/json");
1001+
response.addHeader("Content-Disposition", "attachment; filename=\"report.txt\"");
9971002
if (request.getAttribute("commit") != null) {
9981003
response.flushBuffer();
9991004
}

0 commit comments

Comments
 (0)