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

     1  /*
     2   *
     3   * Copyright 2021 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  
    19  // Package httpfilter contains the HTTPFilter interface and a registry for
    20  // storing and retrieving their implementations.
    21  package httpfilter
    22  
    23  import (
    24  	"github.com/golang/protobuf/proto"
    25  	iresolver "github.com/hxx258456/ccgo/grpc/internal/resolver"
    26  )
    27  
    28  // FilterConfig represents an opaque data structure holding configuration for a
    29  // filter.  Embed this interface to implement it.
    30  type FilterConfig interface {
    31  	isFilterConfig()
    32  }
    33  
    34  // Filter defines the parsing functionality of an HTTP filter.  A Filter may
    35  // optionally implement either ClientInterceptorBuilder or
    36  // ServerInterceptorBuilder or both, indicating it is capable of working on the
    37  // client side or server side or both, respectively.
    38  type Filter interface {
    39  	// TypeURLs are the proto message types supported by this filter.  A filter
    40  	// will be registered by each of its supported message types.
    41  	TypeURLs() []string
    42  	// ParseFilterConfig parses the provided configuration proto.Message from
    43  	// the LDS configuration of this filter.  This may be an anypb.Any, a
    44  	// udpa.type.v1.TypedStruct, or an xds.type.v3.TypedStruct for filters that
    45  	// do not accept a custom type. The resulting FilterConfig will later be
    46  	// passed to Build.
    47  	ParseFilterConfig(proto.Message) (FilterConfig, error)
    48  	// ParseFilterConfigOverride parses the provided override configuration
    49  	// proto.Message from the RDS override configuration of this filter.  This
    50  	// may be an anypb.Any, a udpa.type.v1.TypedStruct, or an
    51  	// xds.type.v3.TypedStruct for filters that do not accept a custom type.
    52  	// The resulting FilterConfig will later be passed to Build.
    53  	ParseFilterConfigOverride(proto.Message) (FilterConfig, error)
    54  	// IsTerminal returns whether this Filter is terminal or not (i.e. it must
    55  	// be last filter in the filter chain).
    56  	IsTerminal() bool
    57  }
    58  
    59  // ClientInterceptorBuilder constructs a Client Interceptor.  If this type is
    60  // implemented by a Filter, it is capable of working on a client.
    61  type ClientInterceptorBuilder interface {
    62  	// BuildClientInterceptor uses the FilterConfigs produced above to produce
    63  	// an HTTP filter interceptor for clients.  config will always be non-nil,
    64  	// but override may be nil if no override config exists for the filter.  It
    65  	// is valid for Build to return a nil Interceptor and a nil error.  In this
    66  	// case, the RPC will not be intercepted by this filter.
    67  	BuildClientInterceptor(config, override FilterConfig) (iresolver.ClientInterceptor, error)
    68  }
    69  
    70  // ServerInterceptorBuilder constructs a Server Interceptor.  If this type is
    71  // implemented by a Filter, it is capable of working on a server.
    72  type ServerInterceptorBuilder interface {
    73  	// BuildServerInterceptor uses the FilterConfigs produced above to produce
    74  	// an HTTP filter interceptor for servers.  config will always be non-nil,
    75  	// but override may be nil if no override config exists for the filter.  It
    76  	// is valid for Build to return a nil Interceptor and a nil error.  In this
    77  	// case, the RPC will not be intercepted by this filter.
    78  	BuildServerInterceptor(config, override FilterConfig) (iresolver.ServerInterceptor, error)
    79  }
    80  
    81  var (
    82  	// m is a map from scheme to filter.
    83  	m = make(map[string]Filter)
    84  )
    85  
    86  // Register registers the HTTP filter Builder to the filter map. b.TypeURLs()
    87  // will be used as the types for this filter.
    88  //
    89  // NOTE: this function must only be called during initialization time (i.e. in
    90  // an init() function), and is not thread-safe. If multiple filters are
    91  // registered with the same type URL, the one registered last will take effect.
    92  func Register(b Filter) {
    93  	for _, u := range b.TypeURLs() {
    94  		m[u] = b
    95  	}
    96  }
    97  
    98  // UnregisterForTesting unregisters the HTTP Filter for testing purposes.
    99  func UnregisterForTesting(typeURL string) {
   100  	delete(m, typeURL)
   101  }
   102  
   103  // Get returns the HTTPFilter registered with typeURL.
   104  //
   105  // If no filter is register with typeURL, nil will be returned.
   106  func Get(typeURL string) Filter {
   107  	return m[typeURL]
   108  }