Skip to content

Commit 23be5df

Browse files
committed
Handle invalid MediaType in Jetty/Tomcat adapters
See: gh-23553
1 parent b7eaab4 commit 23be5df

File tree

4 files changed

+35
-5
lines changed

4 files changed

+35
-5
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,13 @@ protected Mono<Void> doCommit(@Nullable Supplier<? extends Mono<Void>> writeActi
262262
protected abstract void applyStatusCode();
263263

264264
/**
265-
* Apply header changes from {@link #getHeaders()} to the underlying response.
266-
* This method is called once only.
265+
* Invoked when the response is getting committed allowing sub-classes to
266+
* make apply header values to the underlying response.
267+
* <p>Note that most sub-classes use an {@link HttpHeaders} instance that
268+
* wraps an adapter to the native response headers such that changes are
269+
* propagated to the underlying response on the go. That means this callback
270+
* is typically not used other than for specialized updates such as setting
271+
* the contentType or characterEncoding fields in a Servlet response.
267272
*/
268273
protected abstract void applyHeaders();
269274

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,15 @@ private static HttpHeaders createHeaders(HttpServletResponse response) {
101101

102102
@Override
103103
protected void applyHeaders() {
104-
MediaType contentType = getHeaders().getContentType();
105104
HttpServletResponse response = getNativeResponse();
105+
MediaType contentType = null;
106+
try {
107+
contentType = getHeaders().getContentType();
108+
}
109+
catch (Exception ex) {
110+
String rawContentType = getHeaders().getFirst(HttpHeaders.CONTENT_TYPE);
111+
response.setContentType(rawContentType);
112+
}
106113
if (response.getContentType() == null && contentType != null) {
107114
response.setContentType(contentType.toString());
108115
}

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,25 @@ protected void applyHeaders() {
118118
this.response.addHeader(headerName, headerValue);
119119
}
120120
});
121-
MediaType contentType = getHeaders().getContentType();
121+
MediaType contentType = null;
122+
try {
123+
contentType = getHeaders().getContentType();
124+
}
125+
catch (Exception ex) {
126+
String rawContentType = getHeaders().getFirst(HttpHeaders.CONTENT_TYPE);
127+
this.response.setContentType(rawContentType);
128+
}
122129
if (this.response.getContentType() == null && contentType != null) {
123130
this.response.setContentType(contentType.toString());
124131
}
125132
Charset charset = (contentType != null ? contentType.getCharset() : null);
126133
if (this.response.getCharacterEncoding() == null && charset != null) {
127134
this.response.setCharacterEncoding(charset.name());
128135
}
136+
long contentLength = getHeaders().getContentLength();
137+
if (contentLength != -1) {
138+
this.response.setContentLengthLong(contentLength);
139+
}
129140
}
130141

131142
@Override

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,14 @@ else if (response instanceof HttpServletResponseWrapper) {
204204
@Override
205205
protected void applyHeaders() {
206206
HttpServletResponse response = getNativeResponse();
207-
MediaType contentType = getHeaders().getContentType();
207+
MediaType contentType = null;
208+
try {
209+
contentType = getHeaders().getContentType();
210+
}
211+
catch (Exception ex) {
212+
String rawContentType = getHeaders().getFirst(HttpHeaders.CONTENT_TYPE);
213+
response.setContentType(rawContentType);
214+
}
208215
if (response.getContentType() == null && contentType != null) {
209216
response.setContentType(contentType.toString());
210217
}

0 commit comments

Comments
 (0)