Skip to content

Commit

Permalink
Polish "Set max request header size on Netty when using HTTP/2"
Browse files Browse the repository at this point in the history
  • Loading branch information
mhalbritter committed Aug 17, 2023
1 parent ee5b23b commit dc62e5f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,11 @@ public void customize(NettyReactiveWebServerFactory factory) {
.to((idleTimeout) -> customizeIdleTimeout(factory, idleTimeout));
propertyMapper.from(nettyProperties::getMaxKeepAliveRequests)
.to((maxKeepAliveRequests) -> customizeMaxKeepAliveRequests(factory, maxKeepAliveRequests));
propertyMapper.from(this.serverProperties.getMaxHttpRequestHeaderSize())
.to((maxHttpRequestHeaderSize) -> customizeHttp2MaxHeaderSize(factory, maxHttpRequestHeaderSize.toBytes()));
if (this.serverProperties.getHttp2() != null && this.serverProperties.getHttp2().isEnabled()) {
propertyMapper.from(this.serverProperties.getMaxHttpHeaderSize())
.whenNonNull()
.to((size) -> customizeHttp2MaxHeaderSize(factory, size.toBytes()));
}
customizeRequestDecoder(factory, propertyMapper);
}

Expand Down Expand Up @@ -120,9 +123,9 @@ private void customizeMaxKeepAliveRequests(NettyReactiveWebServerFactory factory
factory.addServerCustomizers((httpServer) -> httpServer.maxKeepAliveRequests(maxKeepAliveRequests));
}

private void customizeHttp2MaxHeaderSize(NettyReactiveWebServerFactory factory, long maxHttpRequestHeaderSize) {
factory.addServerCustomizers(((httpServer) -> httpServer.http2Settings(
(http2SettingsSpecBuilder) -> http2SettingsSpecBuilder.maxHeaderListSize(maxHttpRequestHeaderSize))));
private void customizeHttp2MaxHeaderSize(NettyReactiveWebServerFactory factory, long size) {
factory.addServerCustomizers(
((httpServer) -> httpServer.http2Settings((settings) -> settings.maxHeaderListSize(size))));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ void setMaxKeepAliveRequests() {
@Test
void setHttp2MaxRequestHeaderSize() {
DataSize headerSize = DataSize.ofKilobytes(24);
this.serverProperties.getHttp2().setEnabled(true);
this.serverProperties.setMaxHttpHeaderSize(headerSize);
NettyReactiveWebServerFactory factory = mock(NettyReactiveWebServerFactory.class);
this.customizer.customize(factory);
Expand All @@ -147,8 +148,8 @@ void configureHttpRequestDecoder() {
nettyProperties.setMaxInitialLineLength(DataSize.ofKilobytes(32));
NettyReactiveWebServerFactory factory = mock(NettyReactiveWebServerFactory.class);
this.customizer.customize(factory);
then(factory).should(times(2)).addServerCustomizers(this.customizerCaptor.capture());
NettyServerCustomizer serverCustomizer = this.customizerCaptor.getAllValues().get(1);
then(factory).should().addServerCustomizers(this.customizerCaptor.capture());
NettyServerCustomizer serverCustomizer = this.customizerCaptor.getAllValues().get(0);
HttpServer httpServer = serverCustomizer.apply(HttpServer.create());
HttpRequestDecoderSpec decoder = httpServer.configuration().decoder();
assertThat(decoder.validateHeaders()).isFalse();
Expand All @@ -164,7 +165,7 @@ private void verifyConnectionTimeout(NettyReactiveWebServerFactory factory, Inte
then(factory).should(never()).addServerCustomizers(any(NettyServerCustomizer.class));
return;
}
then(factory).should(times(3)).addServerCustomizers(this.customizerCaptor.capture());
then(factory).should(times(2)).addServerCustomizers(this.customizerCaptor.capture());
NettyServerCustomizer serverCustomizer = this.customizerCaptor.getAllValues().get(0);
HttpServer httpServer = serverCustomizer.apply(HttpServer.create());
Map<ChannelOption<?>, ?> options = httpServer.configuration().options();
Expand All @@ -176,15 +177,15 @@ private void verifyIdleTimeout(NettyReactiveWebServerFactory factory, Duration e
then(factory).should(never()).addServerCustomizers(any(NettyServerCustomizer.class));
return;
}
then(factory).should(times(3)).addServerCustomizers(this.customizerCaptor.capture());
then(factory).should(times(2)).addServerCustomizers(this.customizerCaptor.capture());
NettyServerCustomizer serverCustomizer = this.customizerCaptor.getAllValues().get(0);
HttpServer httpServer = serverCustomizer.apply(HttpServer.create());
Duration idleTimeout = httpServer.configuration().idleTimeout();
assertThat(idleTimeout).isEqualTo(expected);
}

private void verifyMaxKeepAliveRequests(NettyReactiveWebServerFactory factory, int expected) {
then(factory).should(times(3)).addServerCustomizers(this.customizerCaptor.capture());
then(factory).should(times(2)).addServerCustomizers(this.customizerCaptor.capture());
NettyServerCustomizer serverCustomizer = this.customizerCaptor.getAllValues().get(0);
HttpServer httpServer = serverCustomizer.apply(HttpServer.create());
int maxKeepAliveRequests = httpServer.configuration().maxKeepAliveRequests();
Expand Down

0 comments on commit dc62e5f

Please sign in to comment.