Skip to content

Commit ce9dc19

Browse files
committed
Optimize content type parsing
This commit avoids calling HttpHeaders#getContentType multiple times in ServletServerHttpResponse#writeHeaders. Closes gh-32361
1 parent 4b96cd2 commit ce9dc19

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import org.springframework.http.HttpHeaders;
2929
import org.springframework.http.HttpStatusCode;
30+
import org.springframework.http.MediaType;
3031
import org.springframework.lang.Nullable;
3132
import org.springframework.util.Assert;
3233
import org.springframework.util.CollectionUtils;
@@ -118,12 +119,13 @@ private void writeHeaders() {
118119
}
119120
});
120121
// HttpServletResponse exposes some headers as properties: we should include those if not already present
121-
if (this.servletResponse.getContentType() == null && this.headers.getContentType() != null) {
122-
this.servletResponse.setContentType(this.headers.getContentType().toString());
122+
MediaType contentTypeHeader = this.headers.getContentType();
123+
if (this.servletResponse.getContentType() == null && contentTypeHeader != null) {
124+
this.servletResponse.setContentType(contentTypeHeader.toString());
123125
}
124-
if (this.servletResponse.getCharacterEncoding() == null && this.headers.getContentType() != null &&
125-
this.headers.getContentType().getCharset() != null) {
126-
this.servletResponse.setCharacterEncoding(this.headers.getContentType().getCharset().name());
126+
if (this.servletResponse.getCharacterEncoding() == null && contentTypeHeader != null &&
127+
contentTypeHeader.getCharset() != null) {
128+
this.servletResponse.setCharacterEncoding(contentTypeHeader.getCharset().name());
127129
}
128130
long contentLength = getHeaders().getContentLength();
129131
if (contentLength != -1) {

0 commit comments

Comments
 (0)