Skip to content

Commit a5da112

Browse files
ashraf-revospencergibb
authored andcommitted
fix host predicate use host of InetSocketAddress insted of headers
Fixes gh-3037
1 parent 8263985 commit a5da112

File tree

3 files changed

+31
-13
lines changed

3 files changed

+31
-13
lines changed

spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/handler/predicate/HostRoutePredicateFactory.java

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.cloud.gateway.handler.predicate;
1818

19+
import java.net.InetSocketAddress;
1920
import java.util.ArrayList;
2021
import java.util.Collections;
2122
import java.util.List;
@@ -59,22 +60,25 @@ public Predicate<ServerWebExchange> apply(Config config) {
5960
return new GatewayPredicate() {
6061
@Override
6162
public boolean test(ServerWebExchange exchange) {
62-
String host = exchange.getRequest().getHeaders().getFirst("Host");
63-
String match = null;
64-
for (int i = 0; i < config.getPatterns().size(); i++) {
65-
String pattern = config.getPatterns().get(i);
66-
if (pathMatcher.match(pattern, host)) {
67-
match = pattern;
68-
break;
63+
InetSocketAddress address = exchange.getRequest().getHeaders().getHost();
64+
if (address != null) {
65+
String match = null;
66+
String host = address.getHostName();
67+
for (int i = 0; i < config.getPatterns().size(); i++) {
68+
String pattern = config.getPatterns().get(i);
69+
if (pathMatcher.match(pattern, host)) {
70+
match = pattern;
71+
break;
72+
}
6973
}
70-
}
7174

72-
if (match != null) {
73-
Map<String, String> variables = pathMatcher.extractUriTemplateVariables(match, host);
74-
ServerWebExchangeUtils.putUriTemplateVariables(exchange, variables);
75-
return true;
76-
}
75+
if (match != null) {
76+
Map<String, String> variables = pathMatcher.extractUriTemplateVariables(match, host);
77+
ServerWebExchangeUtils.putUriTemplateVariables(exchange, variables);
78+
return true;
79+
}
7780

81+
}
7882
return false;
7983
}
8084

spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/handler/predicate/HostRoutePredicateFactoryTests.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ public void mulitHostRouteDslWorks() {
7474
expectHostRoute("www.hostmultidsl2.org", "host_multi_dsl");
7575
}
7676

77+
@Test
78+
public void sameHostWithPort() {
79+
expectHostRoute("hostpatternarg.org:8080", "without_pattern");
80+
}
81+
7782
@Test
7883
public void toStringFormat() {
7984
Config config = new Config().setPatterns(Arrays.asList("pattern1", "pattern2"));

spring-cloud-gateway-server/src/test/resources/application.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,15 @@ spring:
214214
predicates:
215215
- Header=Foo, .*
216216

217+
218+
# =====================================
219+
- id: without_pattern
220+
uri: ${test.uri}
221+
predicates:
222+
- name: Host
223+
args:
224+
pattern: 'hostpatternarg.org'
225+
217226
# =====================================
218227
- id: host_backwards_compatible_test
219228
uri: ${test.uri}

0 commit comments

Comments
 (0)