github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/grpc/xds/internal/xdsclient/xdsresource/matcher_path.go (about)

     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  package xdsresource
    19  
    20  import (
    21  	"regexp"
    22  	"strings"
    23  
    24  	"github.com/hxx258456/ccgo/grpc/internal/grpcutil"
    25  )
    26  
    27  type pathMatcher interface {
    28  	match(path string) bool
    29  	String() string
    30  }
    31  
    32  type pathExactMatcher struct {
    33  	// fullPath is all upper case if caseInsensitive is true.
    34  	fullPath        string
    35  	caseInsensitive bool
    36  }
    37  
    38  func newPathExactMatcher(p string, caseInsensitive bool) *pathExactMatcher {
    39  	ret := &pathExactMatcher{
    40  		fullPath:        p,
    41  		caseInsensitive: caseInsensitive,
    42  	}
    43  	if caseInsensitive {
    44  		ret.fullPath = strings.ToUpper(p)
    45  	}
    46  	return ret
    47  }
    48  
    49  func (pem *pathExactMatcher) match(path string) bool {
    50  	if pem.caseInsensitive {
    51  		return pem.fullPath == strings.ToUpper(path)
    52  	}
    53  	return pem.fullPath == path
    54  }
    55  
    56  func (pem *pathExactMatcher) String() string {
    57  	return "pathExact:" + pem.fullPath
    58  }
    59  
    60  type pathPrefixMatcher struct {
    61  	// prefix is all upper case if caseInsensitive is true.
    62  	prefix          string
    63  	caseInsensitive bool
    64  }
    65  
    66  func newPathPrefixMatcher(p string, caseInsensitive bool) *pathPrefixMatcher {
    67  	ret := &pathPrefixMatcher{
    68  		prefix:          p,
    69  		caseInsensitive: caseInsensitive,
    70  	}
    71  	if caseInsensitive {
    72  		ret.prefix = strings.ToUpper(p)
    73  	}
    74  	return ret
    75  }
    76  
    77  func (ppm *pathPrefixMatcher) match(path string) bool {
    78  	if ppm.caseInsensitive {
    79  		return strings.HasPrefix(strings.ToUpper(path), ppm.prefix)
    80  	}
    81  	return strings.HasPrefix(path, ppm.prefix)
    82  }
    83  
    84  func (ppm *pathPrefixMatcher) String() string {
    85  	return "pathPrefix:" + ppm.prefix
    86  }
    87  
    88  type pathRegexMatcher struct {
    89  	re *regexp.Regexp
    90  }
    91  
    92  func newPathRegexMatcher(re *regexp.Regexp) *pathRegexMatcher {
    93  	return &pathRegexMatcher{re: re}
    94  }
    95  
    96  func (prm *pathRegexMatcher) match(path string) bool {
    97  	return grpcutil.FullMatchWithRegex(prm.re, path)
    98  }
    99  
   100  func (prm *pathRegexMatcher) String() string {
   101  	return "pathRegex:" + prm.re.String()
   102  }