Skip to content

Commit 2a11a2c

Browse files
authored
Merge pull request #2462 from murgatroid99/grpc-js-xds_header_match_string_match
grpc-js-xds: Support string_match in header matching
2 parents 566034a + 81e1f75 commit 2a11a2c

File tree

3 files changed

+65
-14
lines changed

3 files changed

+65
-14
lines changed

packages/grpc-js-xds/src/matcher.ts

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,19 @@ export interface ValueMatcher {
3737
}
3838

3939
export class ExactValueMatcher implements ValueMatcher {
40-
constructor(private targetValue: string) {}
40+
constructor(private targetValue: string, private ignoreCase: boolean) {
41+
}
4142

4243
apply(value: string) {
43-
return value === this.targetValue;
44+
if (this.ignoreCase) {
45+
return value.toLowerCase() === this.targetValue.toLowerCase();
46+
} else {
47+
return value === this.targetValue;
48+
}
4449
}
4550

4651
toString() {
47-
return 'Exact(' + this.targetValue + ')';
52+
return 'Exact(' + this.targetValue + ', ignore_case=' + this.ignoreCase + ')';
4853
}
4954
}
5055

@@ -94,26 +99,51 @@ export class PresentValueMatcher implements ValueMatcher {
9499
}
95100

96101
export class PrefixValueMatcher implements ValueMatcher {
97-
constructor(private prefix: string) {}
102+
constructor(private prefix: string, private ignoreCase: boolean) {
103+
}
98104

99105
apply(value: string) {
100-
return value.startsWith(this.prefix);
106+
if (this.ignoreCase) {
107+
return value.toLowerCase().startsWith(this.prefix.toLowerCase());
108+
} else {
109+
return value.startsWith(this.prefix);
110+
}
101111
}
102112

103113
toString() {
104-
return 'Prefix(' + this.prefix + ')';
114+
return 'Prefix(' + this.prefix + ', ignore_case=' + this.ignoreCase + ')';
105115
}
106116
}
107117

108118
export class SuffixValueMatcher implements ValueMatcher {
109-
constructor(private suffix: string) {}
119+
constructor(private suffix: string, private ignoreCase: boolean) {}
110120

111121
apply(value: string) {
112-
return value.endsWith(this.suffix);
122+
if (this.ignoreCase) {
123+
return value.toLowerCase().endsWith(this.suffix.toLowerCase());
124+
} else {
125+
return value.endsWith(this.suffix);
126+
}
127+
}
128+
129+
toString() {
130+
return 'Suffix(' + this.suffix + ', ignore_case=' + this.ignoreCase + ')';
131+
}
132+
}
133+
134+
export class ContainsValueMatcher implements ValueMatcher {
135+
constructor(private contains: string, private ignoreCase: boolean) {}
136+
137+
apply(value: string) {
138+
if (this.ignoreCase) {
139+
return value.toLowerCase().includes(this.contains.toLowerCase());
140+
} else {
141+
return value.includes(this.contains);
142+
}
113143
}
114144

115145
toString() {
116-
return 'Suffix(' + this.suffix + ')';
146+
return 'Contains(' + this.contains + + ', ignore_case=' + this.ignoreCase + ')';
117147
}
118148
}
119149

packages/grpc-js-xds/src/resolver-xds.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import { HeaderMatcher__Output } from './generated/envoy/config/route/v3/HeaderM
3737
import ConfigSelector = experimental.ConfigSelector;
3838
import LoadBalancingConfig = experimental.LoadBalancingConfig;
3939
import { XdsClusterManagerLoadBalancingConfig } from './load-balancer-xds-cluster-manager';
40-
import { ExactValueMatcher, FullMatcher, HeaderMatcher, Matcher, PathExactValueMatcher, PathPrefixValueMatcher, PathSafeRegexValueMatcher, PrefixValueMatcher, PresentValueMatcher, RangeValueMatcher, RejectValueMatcher, SafeRegexValueMatcher, SuffixValueMatcher, ValueMatcher } from './matcher';
40+
import { ContainsValueMatcher, ExactValueMatcher, FullMatcher, HeaderMatcher, Matcher, PathExactValueMatcher, PathPrefixValueMatcher, PathSafeRegexValueMatcher, PrefixValueMatcher, PresentValueMatcher, RangeValueMatcher, RejectValueMatcher, SafeRegexValueMatcher, SuffixValueMatcher, ValueMatcher } from './matcher';
4141
import { envoyFractionToFraction, Fraction } from "./fraction";
4242
import { RouteAction, SingleClusterRouteAction, WeightedCluster, WeightedClusterRouteAction } from './route-action';
4343
import { decodeSingleResource, HTTP_CONNECTION_MANGER_TYPE_URL } from './resources';
@@ -136,7 +136,7 @@ function getPredicateForHeaderMatcher(headerMatch: HeaderMatcher__Output): Match
136136
let valueChecker: ValueMatcher;
137137
switch (headerMatch.header_match_specifier) {
138138
case 'exact_match':
139-
valueChecker = new ExactValueMatcher(headerMatch.exact_match!);
139+
valueChecker = new ExactValueMatcher(headerMatch.exact_match!, false);
140140
break;
141141
case 'safe_regex_match':
142142
valueChecker = new SafeRegexValueMatcher(headerMatch.safe_regex_match!.regex);
@@ -150,10 +150,30 @@ function getPredicateForHeaderMatcher(headerMatch: HeaderMatcher__Output): Match
150150
valueChecker = new PresentValueMatcher();
151151
break;
152152
case 'prefix_match':
153-
valueChecker = new PrefixValueMatcher(headerMatch.prefix_match!);
153+
valueChecker = new PrefixValueMatcher(headerMatch.prefix_match!, false);
154154
break;
155155
case 'suffix_match':
156-
valueChecker = new SuffixValueMatcher(headerMatch.suffix_match!);
156+
valueChecker = new SuffixValueMatcher(headerMatch.suffix_match!, false);
157+
break;
158+
case 'string_match':
159+
const stringMatch = headerMatch.string_match!
160+
switch (stringMatch.match_pattern) {
161+
case 'exact':
162+
valueChecker = new ExactValueMatcher(stringMatch.exact!, stringMatch.ignore_case);
163+
break;
164+
case 'safe_regex':
165+
valueChecker = new SafeRegexValueMatcher(stringMatch.safe_regex!.regex);
166+
break;
167+
case 'prefix':
168+
valueChecker = new PrefixValueMatcher(stringMatch.prefix!, stringMatch.ignore_case);
169+
break;
170+
case 'suffix':
171+
valueChecker = new SuffixValueMatcher(stringMatch.suffix!, stringMatch.ignore_case);
172+
break;
173+
case 'contains':
174+
valueChecker = new ContainsValueMatcher(stringMatch.contains!, stringMatch.ignore_case);
175+
break;
176+
}
157177
break;
158178
default:
159179
valueChecker = new RejectValueMatcher();

packages/grpc-js-xds/src/xds-stream-state/rds-state.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ const SUPPPORTED_HEADER_MATCH_SPECIFIERS = [
2929
'range_match',
3030
'present_match',
3131
'prefix_match',
32-
'suffix_match'];
32+
'suffix_match',
33+
'string_match'];
3334
const SUPPORTED_CLUSTER_SPECIFIERS = ['cluster', 'weighted_clusters', 'cluster_header'];
3435

3536
const UINT32_MAX = 0xFFFFFFFF;

0 commit comments

Comments
 (0)