google.golang.org/grpc@v1.62.1/xds/internal/httpfilter/router/router.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 router implements the Envoy Router HTTP filter.
    20  package router
    21  
    22  import (
    23  	"fmt"
    24  
    25  	iresolver "google.golang.org/grpc/internal/resolver"
    26  	"google.golang.org/grpc/xds/internal/httpfilter"
    27  	"google.golang.org/protobuf/proto"
    28  	"google.golang.org/protobuf/types/known/anypb"
    29  
    30  	pb "github.com/envoyproxy/go-control-plane/envoy/extensions/filters/http/router/v3"
    31  )
    32  
    33  // TypeURL is the message type for the Router configuration.
    34  const TypeURL = "type.googleapis.com/envoy.extensions.filters.http.router.v3.Router"
    35  
    36  func init() {
    37  	httpfilter.Register(builder{})
    38  }
    39  
    40  // IsRouterFilter returns true iff a HTTP filter is a Router filter.
    41  func IsRouterFilter(b httpfilter.Filter) bool {
    42  	_, ok := b.(builder)
    43  	return ok
    44  }
    45  
    46  type builder struct {
    47  }
    48  
    49  func (builder) TypeURLs() []string { return []string{TypeURL} }
    50  
    51  func (builder) ParseFilterConfig(cfg proto.Message) (httpfilter.FilterConfig, error) {
    52  	// The gRPC router filter does not currently use any fields from the
    53  	// config.  Verify type only.
    54  	if cfg == nil {
    55  		return nil, fmt.Errorf("router: nil configuration message provided")
    56  	}
    57  	any, ok := cfg.(*anypb.Any)
    58  	if !ok {
    59  		return nil, fmt.Errorf("router: error parsing config %v: unknown type %T", cfg, cfg)
    60  	}
    61  	msg := new(pb.Router)
    62  	if err := any.UnmarshalTo(msg); err != nil {
    63  		return nil, fmt.Errorf("router: error parsing config %v: %v", cfg, err)
    64  	}
    65  	return config{}, nil
    66  }
    67  
    68  func (builder) ParseFilterConfigOverride(override proto.Message) (httpfilter.FilterConfig, error) {
    69  	if override != nil {
    70  		return nil, fmt.Errorf("router: unexpected config override specified: %v", override)
    71  	}
    72  	return config{}, nil
    73  }
    74  
    75  func (builder) IsTerminal() bool {
    76  	return true
    77  }
    78  
    79  var (
    80  	_ httpfilter.ClientInterceptorBuilder = builder{}
    81  	_ httpfilter.ServerInterceptorBuilder = builder{}
    82  )
    83  
    84  func (builder) BuildClientInterceptor(cfg, override httpfilter.FilterConfig) (iresolver.ClientInterceptor, error) {
    85  	if _, ok := cfg.(config); !ok {
    86  		return nil, fmt.Errorf("router: incorrect config type provided (%T): %v", cfg, cfg)
    87  	}
    88  	if override != nil {
    89  		return nil, fmt.Errorf("router: unexpected override configuration specified: %v", override)
    90  	}
    91  	// The gRPC router is implemented within the xds resolver's config
    92  	// selector, not as a separate plugin.  So we return a nil HTTPFilter,
    93  	// which will not be invoked.
    94  	return nil, nil
    95  }
    96  
    97  func (builder) BuildServerInterceptor(cfg, override httpfilter.FilterConfig) (iresolver.ServerInterceptor, error) {
    98  	if _, ok := cfg.(config); !ok {
    99  		return nil, fmt.Errorf("router: incorrect config type provided (%T): %v", cfg, cfg)
   100  	}
   101  	if override != nil {
   102  		return nil, fmt.Errorf("router: unexpected override configuration specified: %v", override)
   103  	}
   104  	// The gRPC router is currently unimplemented on the server side. So we
   105  	// return a nil HTTPFilter, which will not be invoked.
   106  	return nil, nil
   107  }
   108  
   109  // The gRPC router filter does not currently support any configuration.  Verify
   110  // type only.
   111  type config struct {
   112  	httpfilter.FilterConfig
   113  }