Skip to content

Commit d501137

Browse files
committed
Consistently accept empty Content-Type header and empty character encoding
Issue: SPR-12173
1 parent 2df03d6 commit d501137

File tree

4 files changed

+65
-39
lines changed

4 files changed

+65
-39
lines changed

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

+11-17
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;
@@ -58,12 +57,11 @@
5857
/**
5958
* Mock implementation of the {@link javax.servlet.http.HttpServletRequest} interface.
6059
*
61-
* <p>The default, preferred {@link Locale} for the <em>server</em> mocked
62-
* by this request is {@link Locale#ENGLISH}. This value can be changed
63-
* via {@link #addPreferredLocale} or {@link #setPreferredLocales}.
60+
* <p>The default, preferred {@link Locale} for the <em>server</em> mocked by this request
61+
* is {@link Locale#ENGLISH}. This value can be changed via {@link #addPreferredLocale}
62+
* or {@link #setPreferredLocales}.
6463
*
65-
* <p>As of Spring Framework 4.0, this set of mocks is designed on a Servlet
66-
* 3.0 baseline.
64+
* <p>As of Spring Framework 4.0, this set of mocks is designed on a Servlet 3.0 baseline.
6765
*
6866
* @author Juergen Hoeller
6967
* @author Rod Johnson
@@ -344,9 +342,10 @@ public void setCharacterEncoding(String characterEncoding) {
344342
}
345343

346344
private void updateContentTypeHeader() {
347-
if (this.contentType != null) {
345+
if (StringUtils.hasLength(this.contentType)) {
348346
StringBuilder sb = new StringBuilder(this.contentType);
349-
if (!this.contentType.toLowerCase().contains(CHARSET_PREFIX) && this.characterEncoding != null) {
347+
if (!this.contentType.toLowerCase().contains(CHARSET_PREFIX) &&
348+
StringUtils.hasLength(this.characterEncoding)) {
350349
sb.append(";").append(CHARSET_PREFIX).append(this.characterEncoding);
351350
}
352351
doAddHeaderValue(CONTENT_TYPE_HEADER, sb.toString(), true);
@@ -371,8 +370,7 @@ public void setContentType(String contentType) {
371370
if (contentType != null) {
372371
int charsetIndex = contentType.toLowerCase().indexOf(CHARSET_PREFIX);
373372
if (charsetIndex != -1) {
374-
String encoding = contentType.substring(charsetIndex + CHARSET_PREFIX.length());
375-
this.characterEncoding = encoding;
373+
this.characterEncoding = contentType.substring(charsetIndex + CHARSET_PREFIX.length());
376374
}
377375
updateContentTypeHeader();
378376
}
@@ -708,8 +706,6 @@ public Enumeration<Locale> getLocales() {
708706
/**
709707
* Set the boolean {@code secure} flag indicating whether the mock request
710708
* was made using a secure channel, such as HTTPS.
711-
* @param secure a boolean indicating if the mock request was made using a
712-
* secure channel
713709
* @see #isSecure()
714710
* @see #getScheme()
715711
* @see #setScheme(String)
@@ -721,13 +717,11 @@ public void setSecure(boolean secure) {
721717
/**
722718
* Returns {@code true} if the {@link #setSecure secure} flag has been set
723719
* to {@code true} or if the {@link #getScheme scheme} is {@code https}.
724-
* <p>
725-
* {@inheritDoc}
726720
* @see javax.servlet.ServletRequest#isSecure()
727721
*/
728722
@Override
729723
public boolean isSecure() {
730-
return this.secure || HTTPS.equalsIgnoreCase(this.getScheme());
724+
return (this.secure || HTTPS.equalsIgnoreCase(this.scheme));
731725
}
732726

733727
@Override
@@ -1040,8 +1034,8 @@ public String getRequestURI() {
10401034
public StringBuffer getRequestURL() {
10411035
StringBuffer url = new StringBuffer(this.scheme).append("://").append(this.serverName);
10421036

1043-
if (this.serverPort > 0
1044-
&& ((HTTP.equalsIgnoreCase(scheme) && this.serverPort != 80) || (HTTPS.equalsIgnoreCase(scheme) && this.serverPort != 443))) {
1037+
if (this.serverPort > 0 && ((HTTP.equalsIgnoreCase(this.scheme) && this.serverPort != 80) ||
1038+
(HTTPS.equalsIgnoreCase(this.scheme) && this.serverPort != 443))) {
10451039
url.append(':').append(this.serverPort);
10461040
}
10471041

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;
@@ -336,9 +335,10 @@ public void setCharacterEncoding(String characterEncoding) {
336335
}
337336

338337
private void updateContentTypeHeader() {
339-
if (this.contentType != null) {
338+
if (StringUtils.hasLength(this.contentType)) {
340339
StringBuilder sb = new StringBuilder(this.contentType);
341-
if (!this.contentType.toLowerCase().contains(CHARSET_PREFIX) && this.characterEncoding != null) {
340+
if (!this.contentType.toLowerCase().contains(CHARSET_PREFIX) &&
341+
StringUtils.hasLength(this.characterEncoding)) {
342342
sb.append(";").append(CHARSET_PREFIX).append(this.characterEncoding);
343343
}
344344
doAddHeaderValue(CONTENT_TYPE_HEADER, sb.toString(), true);
@@ -363,8 +363,7 @@ public void setContentType(String contentType) {
363363
if (contentType != null) {
364364
int charsetIndex = contentType.toLowerCase().indexOf(CHARSET_PREFIX);
365365
if (charsetIndex != -1) {
366-
String encoding = contentType.substring(charsetIndex + CHARSET_PREFIX.length());
367-
this.characterEncoding = encoding;
366+
this.characterEncoding = contentType.substring(charsetIndex + CHARSET_PREFIX.length());
368367
}
369368
updateContentTypeHeader();
370369
}

0 commit comments

Comments
 (0)