Skip to content

Commit a1c0905

Browse files
committed
Consistently accept empty Content-Type header and empty character encoding
Issue: SPR-12173 (cherry picked from commit d501137)
1 parent 6f9d7da commit a1c0905

File tree

4 files changed

+67
-32
lines changed

4 files changed

+67
-32
lines changed

spring-test/src/main/java/org/springframework/mock/web/MockHttpServletRequest.java

+13-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -36,7 +36,6 @@
3636
import java.util.Locale;
3737
import java.util.Map;
3838
import java.util.Set;
39-
4039
import javax.servlet.AsyncContext;
4140
import javax.servlet.DispatcherType;
4241
import javax.servlet.RequestDispatcher;
@@ -58,7 +57,7 @@
5857
/**
5958
* Mock implementation of the {@link javax.servlet.http.HttpServletRequest} interface.
6059
*
61-
* <p>As of Spring 4.0, this set of mocks is designed on a Servlet 3.0 baseline.
60+
* <p>As of Spring Framework 4.0, this set of mocks is designed on a Servlet 3.0 baseline.
6261
*
6362
* @author Juergen Hoeller
6463
* @author Rod Johnson
@@ -70,10 +69,14 @@
7069
*/
7170
public class MockHttpServletRequest implements HttpServletRequest {
7271

72+
private static final String HTTP = "http";
73+
74+
private static final String HTTPS = "https";
75+
7376
/**
7477
* The default protocol: 'http'.
7578
*/
76-
public static final String DEFAULT_PROTOCOL = "http";
79+
public static final String DEFAULT_PROTOCOL = HTTP;
7780

7881
/**
7982
* The default server address: '127.0.0.1'.
@@ -330,9 +333,10 @@ public void setCharacterEncoding(String characterEncoding) {
330333
}
331334

332335
private void updateContentTypeHeader() {
333-
if (this.contentType != null) {
336+
if (StringUtils.hasLength(this.contentType)) {
334337
StringBuilder sb = new StringBuilder(this.contentType);
335-
if (!this.contentType.toLowerCase().contains(CHARSET_PREFIX) && this.characterEncoding != null) {
338+
if (!this.contentType.toLowerCase().contains(CHARSET_PREFIX) &&
339+
StringUtils.hasLength(this.characterEncoding)) {
336340
sb.append(";").append(CHARSET_PREFIX).append(this.characterEncoding);
337341
}
338342
doAddHeaderValue(CONTENT_TYPE_HEADER, sb.toString(), true);
@@ -357,8 +361,7 @@ public void setContentType(String contentType) {
357361
if (contentType != null) {
358362
int charsetIndex = contentType.toLowerCase().indexOf(CHARSET_PREFIX);
359363
if (charsetIndex != -1) {
360-
String encoding = contentType.substring(charsetIndex + CHARSET_PREFIX.length());
361-
this.characterEncoding = encoding;
364+
this.characterEncoding = contentType.substring(charsetIndex + CHARSET_PREFIX.length());
362365
}
363366
updateContentTypeHeader();
364367
}
@@ -955,8 +958,8 @@ public String getRequestURI() {
955958
public StringBuffer getRequestURL() {
956959
StringBuffer url = new StringBuffer(this.scheme).append("://").append(this.serverName);
957960

958-
if (this.serverPort > 0
959-
&& (("http".equalsIgnoreCase(scheme) && this.serverPort != 80) || ("https".equalsIgnoreCase(scheme) && this.serverPort != 443))) {
961+
if (this.serverPort > 0 && ((HTTP.equalsIgnoreCase(this.scheme) && this.serverPort != 80) ||
962+
(HTTPS.equalsIgnoreCase(this.scheme) && this.serverPort != 443))) {
960963
url.append(':').append(this.serverPort);
961964
}
962965

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

+24-14
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import java.security.Principal;
3131
import java.util.Arrays;
3232
import java.util.Enumeration;
33-
import java.util.HashMap;
3433
import java.util.Iterator;
3534
import java.util.List;
3635
import java.util.Map;
@@ -40,6 +39,8 @@
4039
import org.springframework.http.HttpMethod;
4140
import org.springframework.http.MediaType;
4241
import org.springframework.util.Assert;
42+
import org.springframework.util.LinkedCaseInsensitiveMap;
43+
import org.springframework.util.StringUtils;
4344

4445
/**
4546
* {@link ServerHttpRequest} implementation that is based on a {@link HttpServletRequest}.
@@ -111,21 +112,30 @@ public HttpHeaders getHeaders() {
111112
}
112113
}
113114
// HttpServletRequest exposes some headers as properties: we should include those if not already present
114-
if (this.headers.getContentType() == null && this.servletRequest.getContentType() != null) {
115-
MediaType contentType = MediaType.parseMediaType(this.servletRequest.getContentType());
116-
this.headers.setContentType(contentType);
115+
MediaType contentType = this.headers.getContentType();
116+
if (contentType == null) {
117+
String requestContentType = this.servletRequest.getContentType();
118+
if (StringUtils.hasLength(requestContentType)) {
119+
contentType = MediaType.parseMediaType(requestContentType);
120+
this.headers.setContentType(contentType);
121+
}
117122
}
118-
if (this.headers.getContentType() != null && this.headers.getContentType().getCharSet() == null &&
119-
this.servletRequest.getCharacterEncoding() != null) {
120-
MediaType oldContentType = this.headers.getContentType();
121-
Charset charSet = Charset.forName(this.servletRequest.getCharacterEncoding());
122-
Map<String, String> params = new HashMap<String, String>(oldContentType.getParameters());
123-
params.put("charset", charSet.toString());
124-
MediaType newContentType = new MediaType(oldContentType.getType(), oldContentType.getSubtype(), params);
125-
this.headers.setContentType(newContentType);
123+
if (contentType != null && contentType.getCharSet() == null) {
124+
String requestEncoding = this.servletRequest.getCharacterEncoding();
125+
if (StringUtils.hasLength(requestEncoding)) {
126+
Charset charSet = Charset.forName(requestEncoding);
127+
Map<String, String> params = new LinkedCaseInsensitiveMap<String>();
128+
params.putAll(contentType.getParameters());
129+
params.put("charset", charSet.toString());
130+
MediaType newContentType = new MediaType(contentType.getType(), contentType.getSubtype(), params);
131+
this.headers.setContentType(newContentType);
132+
}
126133
}
127-
if (this.headers.getContentLength() == -1 && this.servletRequest.getContentLength() != -1) {
128-
this.headers.setContentLength(this.servletRequest.getContentLength());
134+
if (this.headers.getContentLength() == -1) {
135+
int requestContentLength = this.servletRequest.getContentLength();
136+
if (requestContentLength != -1) {
137+
this.headers.setContentLength(requestContentLength);
138+
}
129139
}
130140
}
131141
return this.headers;

spring-web/src/test/java/org/springframework/http/server/ServletServerHttpRequestTests.java

+26-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,6 +22,7 @@
2222

2323
import org.junit.Before;
2424
import org.junit.Test;
25+
2526
import org.springframework.http.HttpHeaders;
2627
import org.springframework.http.HttpMethod;
2728
import org.springframework.http.MediaType;
@@ -39,12 +40,14 @@ public class ServletServerHttpRequestTests {
3940

4041
private MockHttpServletRequest mockRequest;
4142

43+
4244
@Before
4345
public void create() throws Exception {
4446
mockRequest = new MockHttpServletRequest();
4547
request = new ServletServerHttpRequest(mockRequest);
4648
}
4749

50+
4851
@Test
4952
public void getMethod() throws Exception {
5053
mockRequest.setMethod("POST");
@@ -65,8 +68,8 @@ public void getURI() throws Exception {
6568
public void getHeaders() throws Exception {
6669
String headerName = "MyHeader";
6770
String headerValue1 = "value1";
68-
mockRequest.addHeader(headerName, headerValue1);
6971
String headerValue2 = "value2";
72+
mockRequest.addHeader(headerName, headerValue1);
7073
mockRequest.addHeader(headerName, headerValue2);
7174
mockRequest.setContentType("text/plain");
7275
mockRequest.setCharacterEncoding("UTF-8");
@@ -82,6 +85,26 @@ public void getHeaders() throws Exception {
8285
headers.getContentType());
8386
}
8487

88+
@Test
89+
public void getHeadersWithEmptyContentTypeAndEncoding() throws Exception {
90+
String headerName = "MyHeader";
91+
String headerValue1 = "value1";
92+
String headerValue2 = "value2";
93+
mockRequest.addHeader(headerName, headerValue1);
94+
mockRequest.addHeader(headerName, headerValue2);
95+
mockRequest.setContentType("");
96+
mockRequest.setCharacterEncoding("");
97+
98+
HttpHeaders headers = request.getHeaders();
99+
assertNotNull("No HttpHeaders returned", headers);
100+
assertTrue("Invalid headers returned", headers.containsKey(headerName));
101+
List<String> headerValues = headers.get(headerName);
102+
assertEquals("Invalid header values returned", 2, headerValues.size());
103+
assertTrue("Invalid header values returned", headerValues.contains(headerValue1));
104+
assertTrue("Invalid header values returned", headerValues.contains(headerValue2));
105+
assertNull(headers.getContentType());
106+
}
107+
85108
@Test
86109
public void getBody() throws Exception {
87110
byte[] content = "Hello World".getBytes("UTF-8");
@@ -105,4 +128,4 @@ public void getFormBody() throws Exception {
105128
assertArrayEquals("Invalid content returned", content, result);
106129
}
107130

108-
}
131+
}

spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletRequest.java

+4-5
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import java.util.Locale;
3737
import java.util.Map;
3838
import java.util.Set;
39-
4039
import javax.servlet.AsyncContext;
4140
import javax.servlet.DispatcherType;
4241
import javax.servlet.RequestDispatcher;
@@ -333,9 +332,10 @@ public void setCharacterEncoding(String characterEncoding) {
333332
}
334333

335334
private void updateContentTypeHeader() {
336-
if (this.contentType != null) {
335+
if (StringUtils.hasLength(this.contentType)) {
337336
StringBuilder sb = new StringBuilder(this.contentType);
338-
if (!this.contentType.toLowerCase().contains(CHARSET_PREFIX) && this.characterEncoding != null) {
337+
if (!this.contentType.toLowerCase().contains(CHARSET_PREFIX) &&
338+
StringUtils.hasLength(this.characterEncoding)) {
339339
sb.append(";").append(CHARSET_PREFIX).append(this.characterEncoding);
340340
}
341341
doAddHeaderValue(CONTENT_TYPE_HEADER, sb.toString(), true);
@@ -360,8 +360,7 @@ public void setContentType(String contentType) {
360360
if (contentType != null) {
361361
int charsetIndex = contentType.toLowerCase().indexOf(CHARSET_PREFIX);
362362
if (charsetIndex != -1) {
363-
String encoding = contentType.substring(charsetIndex + CHARSET_PREFIX.length());
364-
this.characterEncoding = encoding;
363+
this.characterEncoding = contentType.substring(charsetIndex + CHARSET_PREFIX.length());
365364
}
366365
updateContentTypeHeader();
367366
}

0 commit comments

Comments
 (0)