Skip to content

Commit dcc8dcd

Browse files
committed
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
1 parent efa360c commit dcc8dcd

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

spring-web/src/main/java/org/springframework/http/server/ServletServerHttpResponse.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@ private void writeHeaders() {
125125
this.headers.getContentType().getCharset() != null) {
126126
this.servletResponse.setCharacterEncoding(this.headers.getContentType().getCharset().name());
127127
}
128+
long contentLength = getHeaders().getContentLength();
129+
if (contentLength != -1) {
130+
this.servletResponse.setContentLengthLong(contentLength);
131+
}
128132
this.headersWritten = true;
129133
}
130134
}

0 commit comments

Comments
 (0)