Improve MockHttpServletRequest/Response charset parsing
Issue: SPR-12677
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -50,6 +50,7 @@ import javax.servlet.http.HttpServletResponse;
|
||||
import javax.servlet.http.HttpSession;
|
||||
import javax.servlet.http.Part;
|
||||
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.LinkedCaseInsensitiveMap;
|
||||
import org.springframework.util.StringUtils;
|
||||
@@ -368,9 +369,18 @@ public class MockHttpServletRequest implements HttpServletRequest {
|
||||
public void setContentType(String contentType) {
|
||||
this.contentType = contentType;
|
||||
if (contentType != null) {
|
||||
int charsetIndex = contentType.toLowerCase().indexOf(CHARSET_PREFIX);
|
||||
if (charsetIndex != -1) {
|
||||
this.characterEncoding = contentType.substring(charsetIndex + CHARSET_PREFIX.length());
|
||||
try {
|
||||
MediaType mediaType = MediaType.parseMediaType(contentType);
|
||||
if (mediaType.getCharSet() != null) {
|
||||
this.characterEncoding = mediaType.getCharSet().name();
|
||||
}
|
||||
}
|
||||
catch (Exception ex) {
|
||||
// Try to get charset value anyway
|
||||
int charsetIndex = contentType.toLowerCase().indexOf(CHARSET_PREFIX);
|
||||
if (charsetIndex != -1) {
|
||||
this.characterEncoding = contentType.substring(charsetIndex + CHARSET_PREFIX.length());
|
||||
}
|
||||
}
|
||||
updateContentTypeHeader();
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -33,6 +33,7 @@ import javax.servlet.ServletOutputStream;
|
||||
import javax.servlet.http.Cookie;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.LinkedCaseInsensitiveMap;
|
||||
import org.springframework.web.util.WebUtils;
|
||||
@@ -216,10 +217,20 @@ public class MockHttpServletResponse implements HttpServletResponse {
|
||||
public void setContentType(String contentType) {
|
||||
this.contentType = contentType;
|
||||
if (contentType != null) {
|
||||
int charsetIndex = contentType.toLowerCase().indexOf(CHARSET_PREFIX);
|
||||
if (charsetIndex != -1) {
|
||||
this.characterEncoding = contentType.substring(charsetIndex + CHARSET_PREFIX.length());
|
||||
this.charset = true;
|
||||
try {
|
||||
MediaType mediaType = MediaType.parseMediaType(contentType);
|
||||
if (mediaType.getCharSet() != null) {
|
||||
this.characterEncoding = mediaType.getCharSet().name();
|
||||
this.charset = true;
|
||||
}
|
||||
}
|
||||
catch (Exception ex) {
|
||||
// Try to get charset value anyway
|
||||
int charsetIndex = contentType.toLowerCase().indexOf(CHARSET_PREFIX);
|
||||
if (charsetIndex != -1) {
|
||||
this.characterEncoding = contentType.substring(charsetIndex + CHARSET_PREFIX.length());
|
||||
this.charset = true;
|
||||
}
|
||||
}
|
||||
updateContentTypeHeader();
|
||||
}
|
||||
|
||||
@@ -100,6 +100,17 @@ public class MockHttpServletRequestTests {
|
||||
assertEquals("UTF-8", request.getCharacterEncoding());
|
||||
}
|
||||
|
||||
// SPR-12677
|
||||
|
||||
@Test
|
||||
public void setContentTypeHeaderWithMoreComplexCharsetSyntax() {
|
||||
String contentType = "test/plain;charset=\"utf-8\";foo=\"charset=bar\";foocharset=bar;foo=bar";
|
||||
request.addHeader("Content-Type", contentType);
|
||||
assertEquals(contentType, request.getContentType());
|
||||
assertEquals(contentType, request.getHeader("Content-Type"));
|
||||
assertEquals("UTF-8", request.getCharacterEncoding());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setContentTypeThenCharacterEncoding() {
|
||||
request.setContentType("test/plain");
|
||||
|
||||
@@ -90,6 +90,23 @@ public class MockHttpServletResponseTests {
|
||||
assertEquals("UTF-8", response.getCharacterEncoding());
|
||||
}
|
||||
|
||||
// SPR-12677
|
||||
|
||||
@Test
|
||||
public void contentTypeHeaderWithMoreComplexCharsetSyntax() {
|
||||
String contentType = "test/plain;charset=\"utf-8\";foo=\"charset=bar\";foocharset=bar;foo=bar";
|
||||
response.setHeader("Content-Type", contentType);
|
||||
assertEquals(contentType, response.getContentType());
|
||||
assertEquals(contentType, response.getHeader("Content-Type"));
|
||||
assertEquals("UTF-8", response.getCharacterEncoding());
|
||||
|
||||
response = new MockHttpServletResponse();
|
||||
response.addHeader("Content-Type", contentType);
|
||||
assertEquals(contentType, response.getContentType());
|
||||
assertEquals(contentType, response.getHeader("Content-Type"));
|
||||
assertEquals("UTF-8", response.getCharacterEncoding());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setContentTypeThenCharacterEncoding() {
|
||||
response.setContentType("test/plain");
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -50,6 +50,7 @@ import javax.servlet.http.HttpServletResponse;
|
||||
import javax.servlet.http.HttpSession;
|
||||
import javax.servlet.http.Part;
|
||||
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.LinkedCaseInsensitiveMap;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
@@ -361,9 +362,18 @@ public class MockHttpServletRequest implements HttpServletRequest {
|
||||
public void setContentType(String contentType) {
|
||||
this.contentType = contentType;
|
||||
if (contentType != null) {
|
||||
int charsetIndex = contentType.toLowerCase().indexOf(CHARSET_PREFIX);
|
||||
if (charsetIndex != -1) {
|
||||
this.characterEncoding = contentType.substring(charsetIndex + CHARSET_PREFIX.length());
|
||||
try {
|
||||
MediaType mediaType = MediaType.parseMediaType(contentType);
|
||||
if (mediaType.getCharSet() != null) {
|
||||
this.characterEncoding = mediaType.getCharSet().name();
|
||||
}
|
||||
}
|
||||
catch (Exception ex) {
|
||||
// Try to get charset value anyway
|
||||
int charsetIndex = contentType.toLowerCase().indexOf(CHARSET_PREFIX);
|
||||
if (charsetIndex != -1) {
|
||||
this.characterEncoding = contentType.substring(charsetIndex + CHARSET_PREFIX.length());
|
||||
}
|
||||
}
|
||||
updateContentTypeHeader();
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -33,6 +33,7 @@ import javax.servlet.ServletOutputStream;
|
||||
import javax.servlet.http.Cookie;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.LinkedCaseInsensitiveMap;
|
||||
import org.springframework.web.util.WebUtils;
|
||||
@@ -216,11 +217,20 @@ public class MockHttpServletResponse implements HttpServletResponse {
|
||||
public void setContentType(String contentType) {
|
||||
this.contentType = contentType;
|
||||
if (contentType != null) {
|
||||
int charsetIndex = contentType.toLowerCase().indexOf(CHARSET_PREFIX);
|
||||
if (charsetIndex != -1) {
|
||||
String encoding = contentType.substring(charsetIndex + CHARSET_PREFIX.length());
|
||||
this.characterEncoding = encoding;
|
||||
this.charset = true;
|
||||
try {
|
||||
MediaType mediaType = MediaType.parseMediaType(contentType);
|
||||
if (mediaType.getCharSet() != null) {
|
||||
this.characterEncoding = mediaType.getCharSet().name();
|
||||
this.charset = true;
|
||||
}
|
||||
}
|
||||
catch (Exception ex) {
|
||||
// Try to get charset value anyway
|
||||
int charsetIndex = contentType.toLowerCase().indexOf(CHARSET_PREFIX);
|
||||
if (charsetIndex != -1) {
|
||||
this.characterEncoding = contentType.substring(charsetIndex + CHARSET_PREFIX.length());
|
||||
this.charset = true;
|
||||
}
|
||||
}
|
||||
updateContentTypeHeader();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user