X Tutup
Skip to content

Commit 727ccd0

Browse files
committed
Avoid setCharacterEncoding(Charset) call with null value
Includes consistent content length check for functional response. See gh-36343
1 parent a9f447e commit 727ccd0

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.io.IOException;
2020
import java.io.OutputStream;
21+
import java.nio.charset.Charset;
2122

2223
import jakarta.servlet.http.HttpServletResponse;
2324
import org.jspecify.annotations.Nullable;
@@ -116,15 +117,18 @@ private void writeHeaders() {
116117
// Lazy parsing into MediaType
117118
MediaType contentType = this.headers.getContentType();
118119
if (contentType != null) {
119-
this.servletResponse.setCharacterEncoding(contentType.getCharset());
120+
Charset charset = contentType.getCharset();
121+
if (charset != null) {
122+
this.servletResponse.setCharacterEncoding(charset);
123+
}
120124
}
121125
}
122126
catch (Exception ex) {
123127
// Leave character encoding unspecified
124128
}
125129
}
130+
this.headersWritten = true;
126131
}
127-
this.headersWritten = true;
128132
}
129133

130134
}

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.io.IOException;
2020
import java.nio.ByteBuffer;
21+
import java.nio.charset.Charset;
2122

2223
import jakarta.servlet.AsyncContext;
2324
import jakarta.servlet.AsyncEvent;
@@ -122,25 +123,26 @@ protected void applyHeaders() {
122123

123124
protected void adaptHeaders(boolean removeAdaptedHeaders) {
124125
HttpHeaders headers = getHeaders();
125-
// HttpServletResponse exposes some headers as properties: we should include those if not already present
126126

127+
// HttpServletResponse exposes some headers as properties: we should include those if not already present
127128
if (this.response.getContentType() == null && headers.containsHeader(HttpHeaders.CONTENT_TYPE)) {
128129
this.response.setContentType(headers.getFirst(HttpHeaders.CONTENT_TYPE));
129130
}
130-
131131
if (this.response.getCharacterEncoding() == null && headers.containsHeader(HttpHeaders.CONTENT_TYPE)) {
132132
try {
133133
// Lazy parsing into MediaType
134134
MediaType contentType = headers.getContentType();
135135
if (contentType != null) {
136-
this.response.setCharacterEncoding(contentType.getCharset());
136+
Charset charset = contentType.getCharset();
137+
if (charset != null) {
138+
this.response.setCharacterEncoding(charset);
139+
}
137140
}
138141
}
139142
catch (Exception ex) {
140143
// Leave character encoding unspecified
141144
}
142145
}
143-
144146
long contentLength = headers.getContentLength();
145147
if (contentLength != -1) {
146148
this.response.setContentLengthLong(contentLength);

spring-webmvc/src/main/java/org/springframework/web/servlet/function/AbstractServerResponse.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.web.servlet.function;
1818

1919
import java.io.IOException;
20+
import java.nio.charset.Charset;
2021
import java.util.Collection;
2122
import java.util.Set;
2223

@@ -61,6 +62,7 @@ protected AbstractServerResponse(
6162
this.cookies = CollectionUtils.unmodifiableMultiValueMap(new LinkedMultiValueMap<>(cookies));
6263
}
6364

65+
6466
@Override
6567
public final HttpStatusCode statusCode() {
6668
return this.statusCode;
@@ -121,13 +123,20 @@ private void writeHeaders(HttpServletResponse servletResponse) {
121123
// Lazy parsing into MediaType
122124
MediaType contentType = this.headers.getContentType();
123125
if (contentType != null) {
124-
servletResponse.setCharacterEncoding(contentType.getCharset());
126+
Charset charset = contentType.getCharset();
127+
if (charset != null) {
128+
servletResponse.setCharacterEncoding(charset);
129+
}
125130
}
126131
}
127132
catch (Exception ex) {
128133
// Leave character encoding unspecified
129134
}
130135
}
136+
long contentLength = this.headers.getContentLength();
137+
if (contentLength != -1) {
138+
servletResponse.setContentLengthLong(contentLength);
139+
}
131140
}
132141

133142
private void writeCookies(HttpServletResponse servletResponse) {

0 commit comments

Comments
 (0)
X Tutup