Skip to content

Commit

Permalink
Handle invalid MediaType in Jetty/Tomcat adapters
Browse files Browse the repository at this point in the history
  • Loading branch information
rstoyanchev committed Sep 12, 2019
1 parent b7eaab4 commit 23be5df
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 5 deletions.
Expand Up @@ -262,8 +262,13 @@ protected Mono<Void> doCommit(@Nullable Supplier<? extends Mono<Void>> writeActi
protected abstract void applyStatusCode();

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

Expand Down
Expand Up @@ -101,8 +101,15 @@ private static HttpHeaders createHeaders(HttpServletResponse response) {

@Override
protected void applyHeaders() {
MediaType contentType = getHeaders().getContentType();
HttpServletResponse response = getNativeResponse();
MediaType contentType = null;
try {
contentType = getHeaders().getContentType();
}
catch (Exception ex) {
String rawContentType = getHeaders().getFirst(HttpHeaders.CONTENT_TYPE);
response.setContentType(rawContentType);
}
if (response.getContentType() == null && contentType != null) {
response.setContentType(contentType.toString());
}
Expand Down
Expand Up @@ -118,14 +118,25 @@ protected void applyHeaders() {
this.response.addHeader(headerName, headerValue);
}
});
MediaType contentType = getHeaders().getContentType();
MediaType contentType = null;
try {
contentType = getHeaders().getContentType();
}
catch (Exception ex) {
String rawContentType = getHeaders().getFirst(HttpHeaders.CONTENT_TYPE);
this.response.setContentType(rawContentType);
}
if (this.response.getContentType() == null && contentType != null) {
this.response.setContentType(contentType.toString());
}
Charset charset = (contentType != null ? contentType.getCharset() : null);
if (this.response.getCharacterEncoding() == null && charset != null) {
this.response.setCharacterEncoding(charset.name());
}
long contentLength = getHeaders().getContentLength();
if (contentLength != -1) {
this.response.setContentLengthLong(contentLength);
}
}

@Override
Expand Down
Expand Up @@ -204,7 +204,14 @@ else if (response instanceof HttpServletResponseWrapper) {
@Override
protected void applyHeaders() {
HttpServletResponse response = getNativeResponse();
MediaType contentType = getHeaders().getContentType();
MediaType contentType = null;
try {
contentType = getHeaders().getContentType();
}
catch (Exception ex) {
String rawContentType = getHeaders().getFirst(HttpHeaders.CONTENT_TYPE);
response.setContentType(rawContentType);
}
if (response.getContentType() == null && contentType != null) {
response.setContentType(contentType.toString());
}
Expand Down

0 comments on commit 23be5df

Please sign in to comment.