ServletServerHttpRequest.getHeaders() ignores invalid content type
Issue: SPR-14309
This commit is contained in:
@@ -37,6 +37,7 @@ import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.InvalidMediaTypeException;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.LinkedCaseInsensitiveMap;
|
||||
@@ -104,6 +105,7 @@ public class ServletServerHttpRequest implements ServerHttpRequest {
|
||||
public HttpHeaders getHeaders() {
|
||||
if (this.headers == null) {
|
||||
this.headers = new HttpHeaders();
|
||||
|
||||
for (Enumeration<?> headerNames = this.servletRequest.getHeaderNames(); headerNames.hasMoreElements();) {
|
||||
String headerName = (String) headerNames.nextElement();
|
||||
for (Enumeration<?> headerValues = this.servletRequest.getHeaders(headerName);
|
||||
@@ -112,26 +114,33 @@ public class ServletServerHttpRequest implements ServerHttpRequest {
|
||||
this.headers.add(headerName, headerValue);
|
||||
}
|
||||
}
|
||||
|
||||
// HttpServletRequest exposes some headers as properties: we should include those if not already present
|
||||
MediaType contentType = this.headers.getContentType();
|
||||
if (contentType == null) {
|
||||
String requestContentType = this.servletRequest.getContentType();
|
||||
if (StringUtils.hasLength(requestContentType)) {
|
||||
contentType = MediaType.parseMediaType(requestContentType);
|
||||
this.headers.setContentType(contentType);
|
||||
try {
|
||||
MediaType contentType = this.headers.getContentType();
|
||||
if (contentType == null) {
|
||||
String requestContentType = this.servletRequest.getContentType();
|
||||
if (StringUtils.hasLength(requestContentType)) {
|
||||
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) {
|
||||
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);
|
||||
}
|
||||
catch (InvalidMediaTypeException ex) {
|
||||
// Ignore: simply not exposing an invalid content type in HttpHeaders...
|
||||
}
|
||||
|
||||
if (this.headers.getContentLength() < 0) {
|
||||
int requestContentLength = this.servletRequest.getContentLength();
|
||||
if (requestContentLength != -1) {
|
||||
@@ -139,6 +148,7 @@ public class ServletServerHttpRequest implements ServerHttpRequest {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return this.headers;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user