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