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