ServletServerHttpRequest.getHeaders() ignores invalid content type

Issue: SPR-14309
This commit is contained in:
Juergen Hoeller
2016-05-29 13:22:00 +02:00
parent 98eaf05c8f
commit f7f2327f60

View File

@@ -37,6 +37,7 @@ import javax.servlet.http.HttpServletRequest;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod; import org.springframework.http.HttpMethod;
import org.springframework.http.InvalidMediaTypeException;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.LinkedCaseInsensitiveMap; import org.springframework.util.LinkedCaseInsensitiveMap;
@@ -104,6 +105,7 @@ public class ServletServerHttpRequest implements ServerHttpRequest {
public HttpHeaders getHeaders() { public HttpHeaders getHeaders() {
if (this.headers == null) { if (this.headers == null) {
this.headers = new HttpHeaders(); this.headers = new HttpHeaders();
for (Enumeration<?> headerNames = this.servletRequest.getHeaderNames(); headerNames.hasMoreElements();) { for (Enumeration<?> headerNames = this.servletRequest.getHeaderNames(); headerNames.hasMoreElements();) {
String headerName = (String) headerNames.nextElement(); String headerName = (String) headerNames.nextElement();
for (Enumeration<?> headerValues = this.servletRequest.getHeaders(headerName); for (Enumeration<?> headerValues = this.servletRequest.getHeaders(headerName);
@@ -112,26 +114,33 @@ public class ServletServerHttpRequest implements ServerHttpRequest {
this.headers.add(headerName, headerValue); this.headers.add(headerName, headerValue);
} }
} }
// HttpServletRequest exposes some headers as properties: we should include those if not already present // HttpServletRequest exposes some headers as properties: we should include those if not already present
MediaType contentType = this.headers.getContentType(); try {
if (contentType == null) { MediaType contentType = this.headers.getContentType();
String requestContentType = this.servletRequest.getContentType(); if (contentType == null) {
if (StringUtils.hasLength(requestContentType)) { String requestContentType = this.servletRequest.getContentType();
contentType = MediaType.parseMediaType(requestContentType); if (StringUtils.hasLength(requestContentType)) {
this.headers.setContentType(contentType); contentType = MediaType.parseMediaType(requestContentType);
this.headers.setContentType(contentType);
}
}
if (contentType != null && contentType.getCharset() == null) {
String requestEncoding = this.servletRequest.getCharacterEncoding();
if (StringUtils.hasLength(requestEncoding)) {
Charset charSet = Charset.forName(requestEncoding);
Map<String, String> params = new LinkedCaseInsensitiveMap<String>();
params.putAll(contentType.getParameters());
params.put("charset", charSet.toString());
MediaType newContentType = new MediaType(contentType.getType(), contentType.getSubtype(), params);
this.headers.setContentType(newContentType);
}
} }
} }
if (contentType != null && contentType.getCharset() == null) { catch (InvalidMediaTypeException ex) {
String requestEncoding = this.servletRequest.getCharacterEncoding(); // Ignore: simply not exposing an invalid content type in HttpHeaders...
if (StringUtils.hasLength(requestEncoding)) {
Charset charSet = Charset.forName(requestEncoding);
Map<String, String> params = new LinkedCaseInsensitiveMap<String>();
params.putAll(contentType.getParameters());
params.put("charset", charSet.toString());
MediaType newContentType = new MediaType(contentType.getType(), contentType.getSubtype(), params);
this.headers.setContentType(newContentType);
}
} }
if (this.headers.getContentLength() < 0) { if (this.headers.getContentLength() < 0) {
int requestContentLength = this.servletRequest.getContentLength(); int requestContentLength = this.servletRequest.getContentLength();
if (requestContentLength != -1) { if (requestContentLength != -1) {
@@ -139,6 +148,7 @@ public class ServletServerHttpRequest implements ServerHttpRequest {
} }
} }
} }
return this.headers; return this.headers;
} }