Skip to content

Commit e203912

Browse files
committed
Merge branch '4.0.x'
2 parents 733ce03 + 6630eb8 commit e203912

File tree

4 files changed

+33
-14
lines changed

4 files changed

+33
-14
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/main/java/org/springframework/cloud/gateway/support/ShortcutConfigurable.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,8 @@ public Map<String, Object> normalize(Map<String, String> args, ShortcutConfigura
151151
// strip boolean flag if last entry is true or false
152152
int lastIdx = values.size() - 1;
153153
String lastValue = values.get(lastIdx);
154-
if ("true".equalsIgnoreCase(lastValue) || "false".equalsIgnoreCase(lastValue) || lastValue == null) {
154+
if ("true".equalsIgnoreCase(lastValue) || "false".equalsIgnoreCase(lastValue)
155+
|| lastValue == null) {
155156
values = values.subList(0, lastIdx);
156157
map.put(fieldOrder.get(1), getValue(parser, beanFactory, lastValue));
157158
}

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)