Skip to content

Commit df171ef

Browse files
committed
Optimize content type parsing
This commit avoid calling HttpHeaders#getContentType multiple times in ServletServerHttpResponse#writeHeaders. Closes spring-projectsgh-32339
1 parent ab419ab commit df171ef

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

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

+6-5
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,13 @@ private void writeHeaders() {
120120
}
121121
});
122122
// HttpServletResponse exposes some headers as properties: we should include those if not already present
123-
if (this.servletResponse.getContentType() == null && this.headers.getContentType() != null) {
124-
this.servletResponse.setContentType(this.headers.getContentType().toString());
123+
MediaType contentTypeHeader = this.headers.getContentType();
124+
if (this.servletResponse.getContentType() == null && contentTypeHeader != null) {
125+
this.servletResponse.setContentType(contentTypeHeader.toString());
125126
}
126-
if (this.servletResponse.getCharacterEncoding() == null && this.headers.getContentType() != null &&
127-
this.headers.getContentType().getCharset() != null) {
128-
this.servletResponse.setCharacterEncoding(this.headers.getContentType().getCharset().name());
127+
if (this.servletResponse.getCharacterEncoding() == null && contentTypeHeader != null &&
128+
contentTypeHeader.getCharset() != null) {
129+
this.servletResponse.setCharacterEncoding(contentTypeHeader.getCharset().name());
129130
}
130131
long contentLength = getHeaders().getContentLength();
131132
if (contentLength != -1) {

0 commit comments

Comments
 (0)