Search in sources :

Example 1 with HeaderMatcher

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));
}
Also used : FractionMatcher(io.grpc.xds.internal.Matchers.FractionMatcher) HeaderMatcher(io.grpc.xds.internal.Matchers.HeaderMatcher) PathMatcher(io.grpc.xds.VirtualHost.Route.RouteMatch.PathMatcher) ArrayList(java.util.ArrayList) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Nullable(javax.annotation.Nullable)

Example 2 with HeaderMatcher

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");
    }
}
Also used : Pattern(com.google.re2j.Pattern) HeaderMatcher(io.grpc.xds.internal.Matchers.HeaderMatcher) PatternSyntaxException(com.google.re2j.PatternSyntaxException) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 3 with HeaderMatcher

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();
}
Also used : HeaderMatcher(io.grpc.xds.internal.Matchers.HeaderMatcher) Test(org.junit.Test)

Aggregations

HeaderMatcher (io.grpc.xds.internal.Matchers.HeaderMatcher)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 Pattern (com.google.re2j.Pattern)1 PatternSyntaxException (com.google.re2j.PatternSyntaxException)1 PathMatcher (io.grpc.xds.VirtualHost.Route.RouteMatch.PathMatcher)1 FractionMatcher (io.grpc.xds.internal.Matchers.FractionMatcher)1 ArrayList (java.util.ArrayList)1 Nullable (javax.annotation.Nullable)1 Test (org.junit.Test)1