From a5da11269f3d26d54160e7864ce886899a3e2d84 Mon Sep 17 00:00:00 2001 From: ashraf Date: Wed, 23 Aug 2023 19:28:10 +0300 Subject: [PATCH 1/2] fix host predicate use host of InetSocketAddress insted of headers Fixes gh-3037 --- .../predicate/HostRoutePredicateFactory.java | 30 +++++++++++-------- .../HostRoutePredicateFactoryTests.java | 5 ++++ .../src/test/resources/application.yml | 9 ++++++ 3 files changed, 31 insertions(+), 13 deletions(-) diff --git a/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/handler/predicate/HostRoutePredicateFactory.java b/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/handler/predicate/HostRoutePredicateFactory.java index f9b5111485..9cf82025f8 100644 --- a/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/handler/predicate/HostRoutePredicateFactory.java +++ b/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/handler/predicate/HostRoutePredicateFactory.java @@ -16,6 +16,7 @@ package org.springframework.cloud.gateway.handler.predicate; +import java.net.InetSocketAddress; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -59,22 +60,25 @@ public Predicate apply(Config config) { return new GatewayPredicate() { @Override public boolean test(ServerWebExchange exchange) { - String host = exchange.getRequest().getHeaders().getFirst("Host"); - String match = null; - for (int i = 0; i < config.getPatterns().size(); i++) { - String pattern = config.getPatterns().get(i); - if (pathMatcher.match(pattern, host)) { - match = pattern; - break; + InetSocketAddress address = exchange.getRequest().getHeaders().getHost(); + if (address != null) { + String match = null; + String host = address.getHostName(); + for (int i = 0; i < config.getPatterns().size(); i++) { + String pattern = config.getPatterns().get(i); + if (pathMatcher.match(pattern, host)) { + match = pattern; + break; + } } - } - if (match != null) { - Map variables = pathMatcher.extractUriTemplateVariables(match, host); - ServerWebExchangeUtils.putUriTemplateVariables(exchange, variables); - return true; - } + if (match != null) { + Map variables = pathMatcher.extractUriTemplateVariables(match, host); + ServerWebExchangeUtils.putUriTemplateVariables(exchange, variables); + return true; + } + } return false; } diff --git a/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/handler/predicate/HostRoutePredicateFactoryTests.java b/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/handler/predicate/HostRoutePredicateFactoryTests.java index 10fb4f857f..b6a528d190 100644 --- a/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/handler/predicate/HostRoutePredicateFactoryTests.java +++ b/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/handler/predicate/HostRoutePredicateFactoryTests.java @@ -74,6 +74,11 @@ public void mulitHostRouteDslWorks() { expectHostRoute("www.hostmultidsl2.org", "host_multi_dsl"); } + @Test + public void sameHostWithPort() { + expectHostRoute("hostpatternarg.org:8080", "without_pattern"); + } + @Test public void toStringFormat() { Config config = new Config().setPatterns(Arrays.asList("pattern1", "pattern2")); diff --git a/spring-cloud-gateway-server/src/test/resources/application.yml b/spring-cloud-gateway-server/src/test/resources/application.yml index b031e4139a..5e62bd4b28 100644 --- a/spring-cloud-gateway-server/src/test/resources/application.yml +++ b/spring-cloud-gateway-server/src/test/resources/application.yml @@ -214,6 +214,15 @@ spring: predicates: - Header=Foo, .* + + # ===================================== + - id: without_pattern + uri: ${test.uri} + predicates: + - name: Host + args: + pattern: 'hostpatternarg.org' + # ===================================== - id: host_backwards_compatible_test uri: ${test.uri} From 6630eb87c39cba8f43c63b652efbcae6d62bf6b6 Mon Sep 17 00:00:00 2001 From: sgibb Date: Tue, 5 Dec 2023 19:08:18 -0500 Subject: [PATCH 2/2] formatting --- .../cloud/gateway/support/ShortcutConfigurable.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/support/ShortcutConfigurable.java b/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/support/ShortcutConfigurable.java index ec917fbe79..7cb200965a 100644 --- a/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/support/ShortcutConfigurable.java +++ b/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/support/ShortcutConfigurable.java @@ -151,7 +151,8 @@ public Map normalize(Map args, ShortcutConfigura // strip boolean flag if last entry is true or false int lastIdx = values.size() - 1; String lastValue = values.get(lastIdx); - if ("true".equalsIgnoreCase(lastValue) || "false".equalsIgnoreCase(lastValue) || lastValue == null) { + if ("true".equalsIgnoreCase(lastValue) || "false".equalsIgnoreCase(lastValue) + || lastValue == null) { values = values.subList(0, lastIdx); map.put(fieldOrder.get(1), getValue(parser, beanFactory, lastValue)); }