File tree Expand file tree Collapse file tree 3 files changed +22
-7
lines changed
spring-webmvc/src/main/java/org/springframework/web/servlet/function
spring-web/src/main/java/org/springframework/http/server Expand file tree Collapse file tree 3 files changed +22
-7
lines changed Original file line number Diff line number Diff line change 1818
1919import java .io .IOException ;
2020import java .io .OutputStream ;
21+ import java .nio .charset .Charset ;
2122
2223import jakarta .servlet .http .HttpServletResponse ;
2324import 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}
Original file line number Diff line number Diff line change 1818
1919import java .io .IOException ;
2020import java .nio .ByteBuffer ;
21+ import java .nio .charset .Charset ;
2122
2223import jakarta .servlet .AsyncContext ;
2324import 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 );
Original file line number Diff line number Diff line change 1717package org .springframework .web .servlet .function ;
1818
1919import java .io .IOException ;
20+ import java .nio .charset .Charset ;
2021import java .util .Collection ;
2122import 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 ) {
You can’t perform that action at this time.
0 commit comments