Skip to content

Commit

Permalink
Disable Jetty's default Server header
Browse files Browse the repository at this point in the history
Following the upgrade to Tomcat 8.5, Jetty became the only container
that sends a Server header by default. This commit updates the factory
for Jetty to disable the default Server header bringing it into line
with Tomcat and Undertow.

All three containers continue to support server.server-header which
can be used to configure a custom server header.

Closes spring-projectsgh-4730
  • Loading branch information
wilkinsona committed Jun 20, 2016
1 parent 06aa35b commit 3009e51
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -891,6 +891,13 @@ public AbstractConnector createConnector(Server server, InetSocketAddress addres
ServerConnector connector = new ServerConnector(server, acceptors, selectors);
connector.setHost(address.getHostName());
connector.setPort(address.getPort());
for (ConnectionFactory connectionFactory : connector
.getConnectionFactories()) {
if (connectionFactory instanceof HttpConfiguration.ConnectionFactory) {
((HttpConfiguration.ConnectionFactory) connectionFactory)
.getHttpConfiguration().setSendServerVersion(false);
}
}
return connector;
}

Expand All @@ -912,8 +919,16 @@ public Server createServer(ThreadPool threadPool) {
.findMethod(Server.class, "setThreadPool", ThreadPool.class)
.invoke(server, threadPool);
}
catch (Exception e) {
throw new RuntimeException("Failed to configure Jetty 8 ThreadPool", e);
catch (Exception ex) {
throw new RuntimeException("Failed to configure Jetty 8 ThreadPool", ex);
}
try {
ReflectionUtils
.findMethod(Server.class, "setSendServerVersion", boolean.class)
.invoke(server, false);
}
catch (Exception ex) {
throw new RuntimeException("Failed to disable Server header", ex);
}
return server;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,16 @@ public void customServerHeader() throws Exception {
assertThat(response.getHeaders().getFirst("server")).isEqualTo("MyServer");
}

@Test
public void serverHeaderIsDisabledByDefault() throws Exception {
AbstractEmbeddedServletContainerFactory factory = getFactory();
this.container = factory
.getEmbeddedServletContainer(exampleServletRegistration());
this.container.start();
ClientHttpResponse response = getClientResponse(getLocalUrl("/hello"));
assertThat(response.getHeaders().getFirst("server")).isNull();
}

@Test
public void portClashOfPrimaryConnectorResultsInPortInUseException()
throws IOException {
Expand Down

0 comments on commit 3009e51

Please sign in to comment.