Merge branch '4.0.x'

This commit is contained in:
sgibb
2023-12-05 19:08:44 -05:00
4 changed files with 33 additions and 14 deletions

View File

@@ -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 class HostRoutePredicateFactory extends AbstractRoutePredicateFactory<Hos
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<String, String> variables = pathMatcher.extractUriTemplateVariables(match, host);
ServerWebExchangeUtils.putUriTemplateVariables(exchange, variables);
return true;
}
if (match != null) {
Map<String, String> variables = pathMatcher.extractUriTemplateVariables(match, host);
ServerWebExchangeUtils.putUriTemplateVariables(exchange, variables);
return true;
}
}
return false;
}

View File

@@ -151,7 +151,8 @@ public interface ShortcutConfigurable {
// 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));
}

View File

@@ -74,6 +74,11 @@ public class HostRoutePredicateFactoryTests extends BaseWebClientTests {
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"));

View File

@@ -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}