Browse Source

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
pull/26362/head
Brian Clozel 5 years ago
parent
commit
dcc8dcdff8
  1. 4
      spring-web/src/main/java/org/springframework/http/server/ServletServerHttpResponse.java

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

@ -125,6 +125,10 @@ public class ServletServerHttpResponse implements ServerHttpResponse { @@ -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;
}
}

Loading…
Cancel
Save