dubbo.apache.org/dubbo-go/v3@v3.1.1/xds/client/resource/matcher_path.go (about)

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one or more
     3   * contributor license agreements.  See the NOTICE file distributed with
     4   * this work for additional information regarding copyright ownership.
     5   * The ASF licenses this file to You under the Apache License, Version 2.0
     6   * (the "License"); you may not use this file except in compliance with
     7   * the License.  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  /*
    19   *
    20   * Copyright 2021 gRPC authors.
    21   *
    22   */
    23  
    24  package resource
    25  
    26  import (
    27  	"regexp"
    28  	"strings"
    29  )
    30  
    31  import (
    32  	"dubbo.apache.org/dubbo-go/v3/xds/utils/matcher"
    33  )
    34  
    35  type pathMatcher interface {
    36  	match(path string) bool
    37  	String() string
    38  }
    39  
    40  type pathExactMatcher struct {
    41  	// fullPath is all upper case if caseInsensitive is true.
    42  	fullPath        string
    43  	caseInsensitive bool
    44  }
    45  
    46  func newPathExactMatcher(p string, caseInsensitive bool) *pathExactMatcher {
    47  	ret := &pathExactMatcher{
    48  		fullPath:        p,
    49  		caseInsensitive: caseInsensitive,
    50  	}
    51  	if caseInsensitive {
    52  		ret.fullPath = strings.ToUpper(p)
    53  	}
    54  	return ret
    55  }
    56  
    57  func (pem *pathExactMatcher) match(path string) bool {
    58  	if pem.caseInsensitive {
    59  		return pem.fullPath == strings.ToUpper(path)
    60  	}
    61  	return pem.fullPath == path
    62  }
    63  
    64  func (pem *pathExactMatcher) String() string {
    65  	return "pathExact:" + pem.fullPath
    66  }
    67  
    68  type pathPrefixMatcher struct {
    69  	// prefix is all upper case if caseInsensitive is true.
    70  	prefix          string
    71  	caseInsensitive bool
    72  }
    73  
    74  func newPathPrefixMatcher(p string, caseInsensitive bool) *pathPrefixMatcher {
    75  	ret := &pathPrefixMatcher{
    76  		prefix:          p,
    77  		caseInsensitive: caseInsensitive,
    78  	}
    79  	if caseInsensitive {
    80  		ret.prefix = strings.ToUpper(p)
    81  	}
    82  	return ret
    83  }
    84  
    85  func (ppm *pathPrefixMatcher) match(path string) bool {
    86  	if ppm.caseInsensitive {
    87  		return strings.HasPrefix(strings.ToUpper(path), ppm.prefix)
    88  	}
    89  	return strings.HasPrefix(path, ppm.prefix)
    90  }
    91  
    92  func (ppm *pathPrefixMatcher) String() string {
    93  	return "pathPrefix:" + ppm.prefix
    94  }
    95  
    96  type pathRegexMatcher struct {
    97  	re *regexp.Regexp
    98  }
    99  
   100  func newPathRegexMatcher(re *regexp.Regexp) *pathRegexMatcher {
   101  	return &pathRegexMatcher{re: re}
   102  }
   103  
   104  func (prm *pathRegexMatcher) match(path string) bool {
   105  	return matcher.FullMatchWithRegex(prm.re, path)
   106  }
   107  
   108  func (prm *pathRegexMatcher) String() string {
   109  	return "pathRegex:" + prm.re.String()
   110  }