1/* 2 * 3 * Copyright 2020 gRPC authors. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 */ 18 19package resolver 20 21import ( 22 "regexp" 23 "strings" 24) 25 26type pathMatcherInterface interface { 27 match(path string) bool 28 String() string 29} 30 31type pathExactMatcher struct { 32 // fullPath is all upper case if caseInsensitive is true. 33 fullPath string 34 caseInsensitive bool 35} 36 37func newPathExactMatcher(p string, caseInsensitive bool) *pathExactMatcher { 38 ret := &pathExactMatcher{ 39 fullPath: p, 40 caseInsensitive: caseInsensitive, 41 } 42 if caseInsensitive { 43 ret.fullPath = strings.ToUpper(p) 44 } 45 return ret 46} 47 48func (pem *pathExactMatcher) match(path string) bool { 49 if pem.caseInsensitive { 50 return pem.fullPath == strings.ToUpper(path) 51 } 52 return pem.fullPath == path 53} 54 55func (pem *pathExactMatcher) String() string { 56 return "pathExact:" + pem.fullPath 57} 58 59type pathPrefixMatcher struct { 60 // prefix is all upper case if caseInsensitive is true. 61 prefix string 62 caseInsensitive bool 63} 64 65func newPathPrefixMatcher(p string, caseInsensitive bool) *pathPrefixMatcher { 66 ret := &pathPrefixMatcher{ 67 prefix: p, 68 caseInsensitive: caseInsensitive, 69 } 70 if caseInsensitive { 71 ret.prefix = strings.ToUpper(p) 72 } 73 return ret 74} 75 76func (ppm *pathPrefixMatcher) match(path string) bool { 77 if ppm.caseInsensitive { 78 return strings.HasPrefix(strings.ToUpper(path), ppm.prefix) 79 } 80 return strings.HasPrefix(path, ppm.prefix) 81} 82 83func (ppm *pathPrefixMatcher) String() string { 84 return "pathPrefix:" + ppm.prefix 85} 86 87type pathRegexMatcher struct { 88 re *regexp.Regexp 89} 90 91func newPathRegexMatcher(re *regexp.Regexp) *pathRegexMatcher { 92 return &pathRegexMatcher{re: re} 93} 94 95func (prm *pathRegexMatcher) match(path string) bool { 96 return prm.re.MatchString(path) 97} 98 99func (prm *pathRegexMatcher) String() string { 100 return "pathRegex:" + prm.re.String() 101} 102