Browse Source

ServletServerHttpRequest.getHeaders() ignores invalid content type

Issue: SPR-14309
pull/1069/head
Juergen Hoeller 10 years ago
parent
commit
f7f2327f60
  1. 42
      spring-web/src/main/java/org/springframework/http/server/ServletServerHttpRequest.java

42
spring-web/src/main/java/org/springframework/http/server/ServletServerHttpRequest.java

@ -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) {
if (contentType != null && contentType.getCharset() == null) { String requestEncoding = this.servletRequest.getCharacterEncoding();
String requestEncoding = this.servletRequest.getCharacterEncoding(); if (StringUtils.hasLength(requestEncoding)) {
if (StringUtils.hasLength(requestEncoding)) { Charset charSet = Charset.forName(requestEncoding);
Charset charSet = Charset.forName(requestEncoding); Map<String, String> params = new LinkedCaseInsensitiveMap<String>();
Map<String, String> params = new LinkedCaseInsensitiveMap<String>(); params.putAll(contentType.getParameters());
params.putAll(contentType.getParameters()); params.put("charset", charSet.toString());
params.put("charset", charSet.toString()); MediaType newContentType = new MediaType(contentType.getType(), contentType.getSubtype(), params);
MediaType newContentType = new MediaType(contentType.getType(), contentType.getSubtype(), params); this.headers.setContentType(newContentType);
this.headers.setContentType(newContentType); }
} }
} }
catch (InvalidMediaTypeException ex) {
// Ignore: simply not exposing an invalid content type in HttpHeaders...
}
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;
} }

Loading…
Cancel
Save