use of io.grpc.xds.internal.Matchers.HeaderMatcher in project grpc-java by grpc.
the class ClientXdsClient method parseRouteMatch.
@VisibleForTesting
@Nullable
static StructOrError<RouteMatch> parseRouteMatch(io.envoyproxy.envoy.config.route.v3.RouteMatch proto) {
if (proto.getQueryParametersCount() != 0) {
return null;
}
StructOrError<PathMatcher> pathMatch = parsePathMatcher(proto);
if (pathMatch.getErrorDetail() != null) {
return StructOrError.fromError(pathMatch.getErrorDetail());
}
FractionMatcher fractionMatch = null;
if (proto.hasRuntimeFraction()) {
StructOrError<FractionMatcher> parsedFraction = parseFractionMatcher(proto.getRuntimeFraction().getDefaultValue());
if (parsedFraction.getErrorDetail() != null) {
return StructOrError.fromError(parsedFraction.getErrorDetail());
}
fractionMatch = parsedFraction.getStruct();
}
List<HeaderMatcher> headerMatchers = new ArrayList<>();
for (io.envoyproxy.envoy.config.route.v3.HeaderMatcher hmProto : proto.getHeadersList()) {
StructOrError<HeaderMatcher> headerMatcher = parseHeaderMatcher(hmProto);
if (headerMatcher.getErrorDetail() != null) {
return StructOrError.fromError(headerMatcher.getErrorDetail());
}
headerMatchers.add(headerMatcher.getStruct());
}
return StructOrError.fromStruct(RouteMatch.create(pathMatch.getStruct(), headerMatchers, fractionMatch));
}
use of io.grpc.xds.internal.Matchers.HeaderMatcher in project grpc-java by grpc.
the class ClientXdsClient method parseHeaderMatcher.
@VisibleForTesting
static StructOrError<HeaderMatcher> parseHeaderMatcher(io.envoyproxy.envoy.config.route.v3.HeaderMatcher proto) {
switch(proto.getHeaderMatchSpecifierCase()) {
case EXACT_MATCH:
return StructOrError.fromStruct(HeaderMatcher.forExactValue(proto.getName(), proto.getExactMatch(), proto.getInvertMatch()));
case SAFE_REGEX_MATCH:
String rawPattern = proto.getSafeRegexMatch().getRegex();
Pattern safeRegExMatch;
try {
safeRegExMatch = Pattern.compile(rawPattern);
} catch (PatternSyntaxException e) {
return StructOrError.fromError("HeaderMatcher [" + proto.getName() + "] contains malformed safe regex pattern: " + e.getMessage());
}
return StructOrError.fromStruct(HeaderMatcher.forSafeRegEx(proto.getName(), safeRegExMatch, proto.getInvertMatch()));
case RANGE_MATCH:
HeaderMatcher.Range rangeMatch = HeaderMatcher.Range.create(proto.getRangeMatch().getStart(), proto.getRangeMatch().getEnd());
return StructOrError.fromStruct(HeaderMatcher.forRange(proto.getName(), rangeMatch, proto.getInvertMatch()));
case PRESENT_MATCH:
return StructOrError.fromStruct(HeaderMatcher.forPresent(proto.getName(), proto.getPresentMatch(), proto.getInvertMatch()));
case PREFIX_MATCH:
return StructOrError.fromStruct(HeaderMatcher.forPrefix(proto.getName(), proto.getPrefixMatch(), proto.getInvertMatch()));
case SUFFIX_MATCH:
return StructOrError.fromStruct(HeaderMatcher.forSuffix(proto.getName(), proto.getSuffixMatch(), proto.getInvertMatch()));
case HEADERMATCHSPECIFIER_NOT_SET:
default:
return StructOrError.fromError("Unknown header matcher type");
}
}
use of io.grpc.xds.internal.Matchers.HeaderMatcher in project grpc-java by grpc.
the class MatcherTest method headerMatcher.
@Test
public void headerMatcher() {
HeaderMatcher matcher = HeaderMatcher.forExactValue("version", "v1", false);
assertThat(matcher.matches("v1")).isTrue();
assertThat(matcher.matches("v2")).isFalse();
assertThat(matcher.matches(null)).isFalse();
matcher = HeaderMatcher.forExactValue("version", "v1", true);
assertThat(matcher.matches("v1")).isFalse();
assertThat(matcher.matches("v2")).isTrue();
assertThat(matcher.matches(null)).isFalse();
matcher = HeaderMatcher.forPresent("version", true, false);
assertThat(matcher.matches("any")).isTrue();
assertThat(matcher.matches(null)).isFalse();
matcher = HeaderMatcher.forPresent("version", true, true);
assertThat(matcher.matches("version")).isFalse();
assertThat(matcher.matches(null)).isTrue();
matcher = HeaderMatcher.forPresent("version", false, true);
assertThat(matcher.matches("tag")).isTrue();
assertThat(matcher.matches(null)).isFalse();
matcher = HeaderMatcher.forPresent("version", false, false);
assertThat(matcher.matches("tag")).isFalse();
assertThat(matcher.matches(null)).isTrue();
matcher = HeaderMatcher.forPrefix("version", "v2", false);
assertThat(matcher.matches("v22")).isTrue();
assertThat(matcher.matches(null)).isFalse();
matcher = HeaderMatcher.forPrefix("version", "v2", true);
assertThat(matcher.matches("v22")).isFalse();
assertThat(matcher.matches(null)).isFalse();
matcher = HeaderMatcher.forSuffix("version", "v1", false);
assertThat(matcher.matches("xv1")).isTrue();
assertThat(matcher.matches("v1x")).isFalse();
assertThat(matcher.matches(null)).isFalse();
matcher = HeaderMatcher.forSuffix("version", "v2", true);
assertThat(matcher.matches("xv1")).isTrue();
assertThat(matcher.matches("1v2")).isFalse();
assertThat(matcher.matches(null)).isFalse();
matcher = HeaderMatcher.forSafeRegEx("version", Pattern.compile("v2.*"), false);
assertThat(matcher.matches("v2..")).isTrue();
assertThat(matcher.matches("v1")).isFalse();
assertThat(matcher.matches(null)).isFalse();
matcher = HeaderMatcher.forSafeRegEx("version", Pattern.compile("v1\\..*"), true);
assertThat(matcher.matches("v1.43")).isFalse();
assertThat(matcher.matches("v2")).isTrue();
assertThat(matcher.matches(null)).isFalse();
matcher = HeaderMatcher.forRange("version", Range.create(8080L, 8090L), false);
assertThat(matcher.matches("8080")).isTrue();
assertThat(matcher.matches("1")).isFalse();
assertThat(matcher.matches(null)).isFalse();
matcher = HeaderMatcher.forRange("version", Range.create(8080L, 8090L), true);
assertThat(matcher.matches("1")).isTrue();
assertThat(matcher.matches("8080")).isFalse();
assertThat(matcher.matches(null)).isFalse();
}
Aggregations