@@ -50,13 +50,14 @@ func mdValuesFromOutgoingCtx(md metadata.MD, key string) (string, bool) {
50
50
51
51
// HeaderExactMatcher matches on an exact match of the value of the header.
52
52
type HeaderExactMatcher struct {
53
- key string
54
- exact string
53
+ key string
54
+ exact string
55
+ invert bool
55
56
}
56
57
57
58
// NewHeaderExactMatcher returns a new HeaderExactMatcher.
58
- func NewHeaderExactMatcher (key , exact string ) * HeaderExactMatcher {
59
- return & HeaderExactMatcher {key : key , exact : exact }
59
+ func NewHeaderExactMatcher (key , exact string , invert bool ) * HeaderExactMatcher {
60
+ return & HeaderExactMatcher {key : key , exact : exact , invert : invert }
60
61
}
61
62
62
63
// Match returns whether the passed in HTTP Headers match according to the
@@ -66,7 +67,7 @@ func (hem *HeaderExactMatcher) Match(md metadata.MD) bool {
66
67
if ! ok {
67
68
return false
68
69
}
69
- return v == hem .exact
70
+ return ( v == hem .exact ) != hem . invert
70
71
}
71
72
72
73
func (hem * HeaderExactMatcher ) String () string {
@@ -76,13 +77,14 @@ func (hem *HeaderExactMatcher) String() string {
76
77
// HeaderRegexMatcher matches on whether the entire request header value matches
77
78
// the regex.
78
79
type HeaderRegexMatcher struct {
79
- key string
80
- re * regexp.Regexp
80
+ key string
81
+ re * regexp.Regexp
82
+ invert bool
81
83
}
82
84
83
85
// NewHeaderRegexMatcher returns a new HeaderRegexMatcher.
84
- func NewHeaderRegexMatcher (key string , re * regexp.Regexp ) * HeaderRegexMatcher {
85
- return & HeaderRegexMatcher {key : key , re : re }
86
+ func NewHeaderRegexMatcher (key string , re * regexp.Regexp , invert bool ) * HeaderRegexMatcher {
87
+ return & HeaderRegexMatcher {key : key , re : re , invert : invert }
86
88
}
87
89
88
90
// Match returns whether the passed in HTTP Headers match according to the
@@ -92,7 +94,7 @@ func (hrm *HeaderRegexMatcher) Match(md metadata.MD) bool {
92
94
if ! ok {
93
95
return false
94
96
}
95
- return grpcutil .FullMatchWithRegex (hrm .re , v )
97
+ return grpcutil .FullMatchWithRegex (hrm .re , v ) != hrm . invert
96
98
}
97
99
98
100
func (hrm * HeaderRegexMatcher ) String () string {
@@ -104,11 +106,12 @@ func (hrm *HeaderRegexMatcher) String() string {
104
106
type HeaderRangeMatcher struct {
105
107
key string
106
108
start , end int64 // represents [start, end).
109
+ invert bool
107
110
}
108
111
109
112
// NewHeaderRangeMatcher returns a new HeaderRangeMatcher.
110
- func NewHeaderRangeMatcher (key string , start , end int64 ) * HeaderRangeMatcher {
111
- return & HeaderRangeMatcher {key : key , start : start , end : end }
113
+ func NewHeaderRangeMatcher (key string , start , end int64 , invert bool ) * HeaderRangeMatcher {
114
+ return & HeaderRangeMatcher {key : key , start : start , end : end , invert : invert }
112
115
}
113
116
114
117
// Match returns whether the passed in HTTP Headers match according to the
@@ -119,9 +122,9 @@ func (hrm *HeaderRangeMatcher) Match(md metadata.MD) bool {
119
122
return false
120
123
}
121
124
if i , err := strconv .ParseInt (v , 10 , 64 ); err == nil && i >= hrm .start && i < hrm .end {
122
- return true
125
+ return ! hrm . invert
123
126
}
124
- return false
127
+ return hrm . invert
125
128
}
126
129
127
130
func (hrm * HeaderRangeMatcher ) String () string {
@@ -136,15 +139,18 @@ type HeaderPresentMatcher struct {
136
139
}
137
140
138
141
// NewHeaderPresentMatcher returns a new HeaderPresentMatcher.
139
- func NewHeaderPresentMatcher (key string , present bool ) * HeaderPresentMatcher {
142
+ func NewHeaderPresentMatcher (key string , present bool , invert bool ) * HeaderPresentMatcher {
143
+ if invert {
144
+ present = ! present
145
+ }
140
146
return & HeaderPresentMatcher {key : key , present : present }
141
147
}
142
148
143
149
// Match returns whether the passed in HTTP Headers match according to the
144
150
// HeaderPresentMatcher.
145
151
func (hpm * HeaderPresentMatcher ) Match (md metadata.MD ) bool {
146
152
vs , ok := mdValuesFromOutgoingCtx (md , hpm .key )
147
- present := ok && len (vs ) > 0
153
+ present := ok && len (vs ) > 0 // TODO: Are we sure we need this len(vs) > 0?
148
154
return present == hpm .present
149
155
}
150
156
@@ -157,11 +163,12 @@ func (hpm *HeaderPresentMatcher) String() string {
157
163
type HeaderPrefixMatcher struct {
158
164
key string
159
165
prefix string
166
+ invert bool
160
167
}
161
168
162
169
// NewHeaderPrefixMatcher returns a new HeaderPrefixMatcher.
163
- func NewHeaderPrefixMatcher (key string , prefix string ) * HeaderPrefixMatcher {
164
- return & HeaderPrefixMatcher {key : key , prefix : prefix }
170
+ func NewHeaderPrefixMatcher (key string , prefix string , invert bool ) * HeaderPrefixMatcher {
171
+ return & HeaderPrefixMatcher {key : key , prefix : prefix , invert : invert }
165
172
}
166
173
167
174
// Match returns whether the passed in HTTP Headers match according to the
@@ -171,7 +178,7 @@ func (hpm *HeaderPrefixMatcher) Match(md metadata.MD) bool {
171
178
if ! ok {
172
179
return false
173
180
}
174
- return strings .HasPrefix (v , hpm .prefix )
181
+ return strings .HasPrefix (v , hpm .prefix ) != hpm . invert
175
182
}
176
183
177
184
func (hpm * HeaderPrefixMatcher ) String () string {
@@ -183,11 +190,12 @@ func (hpm *HeaderPrefixMatcher) String() string {
183
190
type HeaderSuffixMatcher struct {
184
191
key string
185
192
suffix string
193
+ invert bool
186
194
}
187
195
188
196
// NewHeaderSuffixMatcher returns a new HeaderSuffixMatcher.
189
- func NewHeaderSuffixMatcher (key string , suffix string ) * HeaderSuffixMatcher {
190
- return & HeaderSuffixMatcher {key : key , suffix : suffix }
197
+ func NewHeaderSuffixMatcher (key string , suffix string , invert bool ) * HeaderSuffixMatcher {
198
+ return & HeaderSuffixMatcher {key : key , suffix : suffix , invert : invert }
191
199
}
192
200
193
201
// Match returns whether the passed in HTTP Headers match according to the
@@ -197,7 +205,7 @@ func (hsm *HeaderSuffixMatcher) Match(md metadata.MD) bool {
197
205
if ! ok {
198
206
return false
199
207
}
200
- return strings .HasSuffix (v , hsm .suffix )
208
+ return strings .HasSuffix (v , hsm .suffix ) != hsm . invert
201
209
}
202
210
203
211
func (hsm * HeaderSuffixMatcher ) String () string {
@@ -209,14 +217,15 @@ func (hsm *HeaderSuffixMatcher) String() string {
209
217
type HeaderContainsMatcher struct {
210
218
key string
211
219
contains string
220
+ invert bool
212
221
}
213
222
214
223
// NewHeaderContainsMatcher returns a new HeaderContainsMatcher. key is the HTTP
215
224
// Header key to match on, and contains is the value that the header should
216
225
// should contain for a successful match. An empty contains string does not
217
226
// work, use HeaderPresentMatcher in that case.
218
- func NewHeaderContainsMatcher (key string , contains string ) * HeaderContainsMatcher {
219
- return & HeaderContainsMatcher {key : key , contains : contains }
227
+ func NewHeaderContainsMatcher (key string , contains string , invert bool ) * HeaderContainsMatcher {
228
+ return & HeaderContainsMatcher {key : key , contains : contains , invert : invert }
220
229
}
221
230
222
231
// Match returns whether the passed in HTTP Headers match according to the
@@ -226,29 +235,9 @@ func (hcm *HeaderContainsMatcher) Match(md metadata.MD) bool {
226
235
if ! ok {
227
236
return false
228
237
}
229
- return strings .Contains (v , hcm .contains )
238
+ return strings .Contains (v , hcm .contains ) != hcm . invert
230
239
}
231
240
232
241
func (hcm * HeaderContainsMatcher ) String () string {
233
242
return fmt .Sprintf ("headerContains:%v%v" , hcm .key , hcm .contains )
234
243
}
235
-
236
- // InvertMatcher inverts the match result of the underlying header matcher.
237
- type InvertMatcher struct {
238
- m HeaderMatcher
239
- }
240
-
241
- // NewInvertMatcher returns a new InvertMatcher.
242
- func NewInvertMatcher (m HeaderMatcher ) * InvertMatcher {
243
- return & InvertMatcher {m : m }
244
- }
245
-
246
- // Match returns whether the passed in HTTP Headers match according to the
247
- // InvertMatcher.
248
- func (i * InvertMatcher ) Match (md metadata.MD ) bool {
249
- return ! i .m .Match (md )
250
- }
251
-
252
- func (i * InvertMatcher ) String () string {
253
- return fmt .Sprintf ("invert{%s}" , i .m )
254
- }
0 commit comments