Skip to content

Set uri encoding to UTF-8 by default for Tomcat #542

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
* {@link EmbeddedServletContainerCustomizerBeanPostProcessor} is active.
*
* @author Dave Syer
* @author Stephane Nicoll
*/
@ConfigurationProperties(name = "server", ignoreUnknownFields = false)
public class ServerProperties implements EmbeddedServletContainerCustomizer {
Expand Down Expand Up @@ -144,6 +145,8 @@ public static class Tomcat {

private int maxThreads = 0; // Number of threads in protocol handler

private String uriEncoding;

public int getMaxThreads() {
return this.maxThreads;
}
Expand Down Expand Up @@ -200,6 +203,14 @@ public void setRemoteIpHeader(String remoteIpHeader) {
this.remoteIpHeader = remoteIpHeader;
}

public String getUriEncoding() {
return uriEncoding;
}

public void setUriEncoding(String uriEncoding) {
this.uriEncoding = uriEncoding;
}

void customizeTomcat(TomcatEmbeddedServletContainerFactory factory) {
if (getBasedir() != null) {
factory.setBaseDirectory(getBasedir());
Expand Down Expand Up @@ -247,6 +258,9 @@ public void customize(Connector connector) {
valve.setSuffix(".log");
factory.addContextValves(valve);
}
if (getUriEncoding() != null) {
factory.setUriEncoding(getUriEncoding());
}
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
* Tests for {@link ServerProperties}.
*
* @author Dave Syer
* @author Stephane Nicoll
*/
public class ServerPropertiesTests {

Expand Down Expand Up @@ -87,4 +88,13 @@ public void testCustomizeTomcatPort() throws Exception {
verify(factory).setPort(8080);
}

@Test
public void testCustomizeUriEncoding() throws Exception {
Map<String, String> map = new HashMap<String, String>();
map.put("server.tomcat.uriEncoding", "US-ASCII");
new RelaxedDataBinder(this.properties, "server").bind(new MutablePropertyValues(
map));
assertEquals("US-ASCII", this.properties.getTomcat().getUriEncoding());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ server.tomcat.remote-ip-header=x-forwarded-for
server.tomcat.basedir=/tmp # base dir (usually not needed, defaults to tmp)
server.tomcat.background-processor-delay=30; # in seconds
server.tomcat.max-threads = 0 # number of threads in protocol handler
server.tomcat.uri-encoding = UTF-8 # character encoding to use for URL decoding

# SPRING MVC ({sc-spring-boot-autoconfigure}/web/HttpMapperProperties.{sc-ext}[HttpMapperProperties])
http.mappers.json-pretty-print=false # pretty print JSON
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
* @author Phillip Webb
* @author Dave Syer
* @author Brock Mills
* @author Stephane Nicoll
* @see #setPort(int)
* @see #setContextLifecycleListeners(Collection)
* @see TomcatEmbeddedServletContainer
Expand Down Expand Up @@ -93,6 +94,8 @@ public class TomcatEmbeddedServletContainerFactory extends

private String tldSkip;

private String uriEncoding = "UTF-8";

/**
* Create a new {@link TomcatEmbeddedServletContainerFactory} instance.
*/
Expand Down Expand Up @@ -206,6 +209,10 @@ protected void customizeConnector(Connector connector) {
.setAddress(getAddress());
}
}
if (getUriEncoding() != null) {
connector.setURIEncoding(getUriEncoding());
}

// If ApplicationContext is slow to start we want Tomcat not to bind to the socket
// prematurely...
connector.setProperty("bindOnInit", "false");
Expand Down Expand Up @@ -455,6 +462,22 @@ public List<Connector> getAdditionalTomcatConnectors() {
return this.additionalTomcatConnectors;
}

/**
* Set the character encoding to use for URL decoding. If not
* specified 'UTF-8' will be used.
* @param uriEncoding the uri encoding to set
*/
public void setUriEncoding(String uriEncoding) {
this.uriEncoding = uriEncoding;
}

/**
* Returns the character encoding to use for URL decoding.
*/
public String getUriEncoding() {
return uriEncoding;
}

private static class TomcatErrorPage {

private final String location;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
*
* @author Phillip Webb
* @author Dave Syer
* @author Stephane Nicoll
*/
public class TomcatEmbeddedServletContainerFactoryTests extends
AbstractEmbeddedServletContainerFactoryTests {
Expand Down Expand Up @@ -190,11 +191,30 @@ public void addNullConnectorCustomizersThrows() {
factory.addConnectorCustomizers((TomcatConnectorCustomizer[]) null);
}

@Test
public void uriEncoding() throws Exception {
TomcatEmbeddedServletContainerFactory factory = getFactory();
factory.setUriEncoding("US-ASCII");
Tomcat tomcat = getTomcat(factory);
assertEquals("US-ASCII", tomcat.getConnector().getURIEncoding());
}

@Test
public void defaultUriEncoding() throws Exception {
TomcatEmbeddedServletContainerFactory factory = getFactory();
Tomcat tomcat = getTomcat(factory);
assertEquals("UTF-8", tomcat.getConnector().getURIEncoding());
}

private void assertTimeout(TomcatEmbeddedServletContainerFactory factory, int expected) {
this.container = factory.getEmbeddedServletContainer();
Tomcat tomcat = ((TomcatEmbeddedServletContainer) this.container).getTomcat();
Tomcat tomcat = getTomcat(factory);
Context context = (Context) tomcat.getHost().findChildren()[0];
assertThat(context.getSessionTimeout(), equalTo(expected));
}

private Tomcat getTomcat(TomcatEmbeddedServletContainerFactory factory) {
this.container = factory.getEmbeddedServletContainer();
return ((TomcatEmbeddedServletContainer) this.container).getTomcat();
}

}