Skip to content

Commit

Permalink
Handle special headers in Tomcat and Jetty
Browse files Browse the repository at this point in the history
This commit adds special processing of some HTTP response headers in
Jetty and Tomcat; they both consider some headers like  "Content-Length"
as specific and require explicit calls on the `HttpServletResponse`
itself on top of setting the HTTP response header.

Issue: SPR-17250
  • Loading branch information
bclozel committed Oct 11, 2018
1 parent 8de5c05 commit 10d5de7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import javax.servlet.AsyncContext;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
Expand All @@ -32,6 +33,7 @@
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;

/**
* {@link ServletHttpHandlerAdapter} extension that uses Jetty APIs for writing
Expand Down Expand Up @@ -94,6 +96,19 @@ private static HttpHeaders createHeaders(HttpServletResponse response) {

@Override
protected void applyHeaders() {
MediaType contentType = getHeaders().getContentType();
HttpServletResponse response = getNativeResponse();
if (response.getContentType() == null && contentType != null) {
response.setContentType(contentType.toString());
}
Charset charset = (contentType != null ? contentType.getCharset() : null);
if (response.getCharacterEncoding() == null && charset != null) {
response.setCharacterEncoding(charset.name());
}
long contentLength = getHeaders().getContentLength();
if (contentLength != -1) {
response.setContentLengthLong(contentLength);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.lang.reflect.Field;
import java.net.URISyntaxException;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import javax.servlet.AsyncContext;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
Expand All @@ -37,6 +38,7 @@
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.util.ReflectionUtils;

Expand Down Expand Up @@ -159,6 +161,19 @@ private static HttpHeaders createTomcatHttpHeaders(HttpServletResponse response)

@Override
protected void applyHeaders() {
HttpServletResponse response = getNativeResponse();
MediaType contentType = getHeaders().getContentType();
if (response.getContentType() == null && contentType != null) {
response.setContentType(contentType.toString());
}
Charset charset = (contentType != null ? contentType.getCharset() : null);
if (response.getCharacterEncoding() == null && charset != null) {
response.setCharacterEncoding(charset.name());
}
long contentLength = getHeaders().getContentLength();
if (contentLength != -1) {
response.setContentLengthLong(contentLength);
}
}

@Override
Expand Down

0 comments on commit 10d5de7

Please sign in to comment.