github.com/cloudwego/kitex@v0.9.0/pkg/transmeta/http2.go (about)

     1  /*
     2   * Copyright 2021 CloudWeGo Authors
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  // Package transmeta metadata handler for translation
    18  package transmeta
    19  
    20  import (
    21  	"context"
    22  
    23  	"github.com/cloudwego/kitex/pkg/remote"
    24  	"github.com/cloudwego/kitex/pkg/remote/trans/nphttp2/metadata"
    25  	"github.com/cloudwego/kitex/pkg/remote/transmeta"
    26  	"github.com/cloudwego/kitex/pkg/rpcinfo"
    27  	"github.com/cloudwego/kitex/transport"
    28  )
    29  
    30  // ClientHTTP2Handler default global client metadata handler
    31  var ClientHTTP2Handler = &clientHTTP2Handler{}
    32  
    33  type clientHTTP2Handler struct{}
    34  
    35  var (
    36  	_ remote.MetaHandler          = ClientHTTP2Handler
    37  	_ remote.StreamingMetaHandler = ClientHTTP2Handler
    38  )
    39  
    40  func (*clientHTTP2Handler) OnConnectStream(ctx context.Context) (context.Context, error) {
    41  	ri := rpcinfo.GetRPCInfo(ctx)
    42  	if !isGRPC(ri) {
    43  		return ctx, nil
    44  	}
    45  	// append more meta into current metadata
    46  	md, ok := metadata.FromOutgoingContext(ctx)
    47  	if !ok {
    48  		md = metadata.MD{}
    49  	}
    50  	md.Append(transmeta.HTTPDestService, ri.To().ServiceName())
    51  	md.Append(transmeta.HTTPDestMethod, ri.To().Method())
    52  	md.Append(transmeta.HTTPSourceService, ri.From().ServiceName())
    53  	md.Append(transmeta.HTTPSourceMethod, ri.From().Method())
    54  	return metadata.NewOutgoingContext(ctx, md), nil
    55  }
    56  
    57  func (*clientHTTP2Handler) OnReadStream(ctx context.Context) (context.Context, error) {
    58  	return ctx, nil
    59  }
    60  
    61  func (ch *clientHTTP2Handler) WriteMeta(ctx context.Context, msg remote.Message) (context.Context, error) {
    62  	return ctx, nil
    63  }
    64  
    65  func (ch *clientHTTP2Handler) ReadMeta(ctx context.Context, msg remote.Message) (context.Context, error) {
    66  	return ctx, nil
    67  }
    68  
    69  // ServerHTTP2Handler default global server metadata handler
    70  var ServerHTTP2Handler = &serverHTTP2Handler{}
    71  
    72  type serverHTTP2Handler struct{}
    73  
    74  func (*serverHTTP2Handler) OnConnectStream(ctx context.Context) (context.Context, error) {
    75  	return ctx, nil
    76  }
    77  
    78  func (*serverHTTP2Handler) OnReadStream(ctx context.Context) (context.Context, error) {
    79  	ri := rpcinfo.GetRPCInfo(ctx)
    80  	if !isGRPC(ri) {
    81  		return ctx, nil
    82  	}
    83  	md, ok := metadata.FromIncomingContext(ctx)
    84  	if !ok {
    85  		return ctx, nil
    86  	}
    87  	ci := rpcinfo.AsMutableEndpointInfo(ri.From())
    88  	if ci != nil {
    89  		if v := md.Get(transmeta.HTTPSourceService); len(v) != 0 {
    90  			ci.SetServiceName(v[0])
    91  		}
    92  		if v := md.Get(transmeta.HTTPSourceMethod); len(v) != 0 {
    93  			ci.SetMethod(v[0])
    94  		}
    95  	}
    96  	return ctx, nil
    97  }
    98  
    99  func (sh *serverHTTP2Handler) WriteMeta(ctx context.Context, msg remote.Message) (context.Context, error) {
   100  	return ctx, nil
   101  }
   102  
   103  func (sh *serverHTTP2Handler) ReadMeta(ctx context.Context, msg remote.Message) (context.Context, error) {
   104  	return ctx, nil
   105  }
   106  
   107  func isGRPC(ri rpcinfo.RPCInfo) bool {
   108  	return ri.Config().TransportProtocol()&transport.GRPC == transport.GRPC
   109  }