From 3bd7760f9c11bc61d68ce308da042bf14b6e4a9c Mon Sep 17 00:00:00 2001 From: Madhura Bhave Date: Wed, 3 Jul 2019 17:48:56 -0700 Subject: [PATCH] Polish "Allow Undertow's options to be configured via the environment See gh-17356 --- .../embedded/UndertowWebServerFactoryCustomizer.java | 10 +++++----- .../UndertowWebServerFactoryCustomizerTests.java | 11 +++++++++++ 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/UndertowWebServerFactoryCustomizer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/UndertowWebServerFactoryCustomizer.java index 4a6b6bdc0a7..c62a302b9a1 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/UndertowWebServerFactoryCustomizer.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/UndertowWebServerFactoryCustomizer.java @@ -142,12 +142,12 @@ public class UndertowWebServerFactoryCustomizer return this.serverProperties.getForwardHeadersStrategy().equals(ServerProperties.ForwardHeadersStrategy.NATIVE); } + @SuppressWarnings("unchecked") private void setCustomOption(ConfigurableUndertowWebServerFactory factory, String key, String value, String type) { Field[] fields = UndertowOptions.class.getDeclaredFields(); for (Field field : fields) { - String name = getLetterAndNumber(key); - if (getLetterAndNumber(field.getName()).equals(name)) { + if (getCanonicalName(field.getName()).equals(getCanonicalName(key))) { Option option = (Option) Option.fromString( UndertowOptions.class.getName() + '.' + field.getName(), getClass().getClassLoader()); T parsed = option.parseValue(value, getClass().getClassLoader()); @@ -162,9 +162,9 @@ public class UndertowWebServerFactoryCustomizer } } - private String getLetterAndNumber(String name) { - StringBuilder canonicalName = new StringBuilder(name.length()); - name.chars().map((c) -> (char) c).filter(Character::isLetterOrDigit).map(Character::toLowerCase) + private String getCanonicalName(String key) { + StringBuilder canonicalName = new StringBuilder(key.length()); + key.chars().map((c) -> (char) c).filter(Character::isLetterOrDigit).map(Character::toLowerCase) .forEach((c) -> canonicalName.append((char) c)); return canonicalName.toString(); } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/embedded/UndertowWebServerFactoryCustomizerTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/embedded/UndertowWebServerFactoryCustomizerTests.java index f499b54a8ff..b07a00684dc 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/embedded/UndertowWebServerFactoryCustomizerTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/embedded/UndertowWebServerFactoryCustomizerTests.java @@ -161,12 +161,23 @@ class UndertowWebServerFactoryCustomizerTests { assertThat(boundServerOption(UndertowOptions.ALWAYS_SET_KEEP_ALIVE)).isFalse(); } + @Test + void customServerOptionShouldBeRelaxed() { + bind("server.undertow.options.server.always-set-keep-alive=false"); + assertThat(boundServerOption(UndertowOptions.ALWAYS_SET_KEEP_ALIVE)).isFalse(); + } + @Test void customSocketOption() { bind("server.undertow.options.socket.ALWAYS_SET_KEEP_ALIVE=false"); assertThat(boundSocketOption(UndertowOptions.ALWAYS_SET_KEEP_ALIVE)).isFalse(); } + void customSocketOptionShouldBeRelaxed() { + bind("server.undertow.options.socket.always-set-keep-alive=false"); + assertThat(boundSocketOption(UndertowOptions.ALWAYS_SET_KEEP_ALIVE)).isFalse(); + } + @Test void deduceUseForwardHeaders() { this.environment.setProperty("DYNO", "-");