From dcc8dcdff81dd76cfcb7df8c6f1d61b4a3d4f207 Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Wed, 6 Jan 2021 20:34:25 +0100 Subject: [PATCH] Set content length on ServletHttpResponse Prior to this commit, the `ServletServerHttpResponse` would copy headers from the `HttpHeaders` map and calls methods related to headers exposed as properties (content-type, character encoding). Unlike its reactive variant, this would not set the content length. Depending on the Servlet container implementation, this could cause duplicate Content-Length response headers in the actual HTTP response. This commit aligns both implementations and ensures that the `setContentLengthLong` method is called if necessary so that the Servlet container can ensure a single header for that. Fixes gh-26330 --- .../http/server/ServletServerHttpResponse.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/spring-web/src/main/java/org/springframework/http/server/ServletServerHttpResponse.java b/spring-web/src/main/java/org/springframework/http/server/ServletServerHttpResponse.java index 0e6b46a1e2d..64436ffb1d8 100644 --- a/spring-web/src/main/java/org/springframework/http/server/ServletServerHttpResponse.java +++ b/spring-web/src/main/java/org/springframework/http/server/ServletServerHttpResponse.java @@ -125,6 +125,10 @@ public class ServletServerHttpResponse implements ServerHttpResponse { this.headers.getContentType().getCharset() != null) { this.servletResponse.setCharacterEncoding(this.headers.getContentType().getCharset().name()); } + long contentLength = getHeaders().getContentLength(); + if (contentLength != -1) { + this.servletResponse.setContentLengthLong(contentLength); + } this.headersWritten = true; } }