github.com/cs3org/reva/v2@v2.27.7/internal/grpc/interceptors/useragent/useragent.go (about)

     1  // Copyright 2018-2021 CERN
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  // In applying this license, CERN does not waive the privileges and immunities
    16  // granted to it by virtue of its status as an Intergovernmental Organization
    17  // or submit itself to any jurisdiction.
    18  
    19  package useragent
    20  
    21  import (
    22  	"context"
    23  
    24  	ctxpkg "github.com/cs3org/reva/v2/pkg/ctx"
    25  	"google.golang.org/grpc"
    26  	"google.golang.org/grpc/metadata"
    27  )
    28  
    29  // NewUnary returns a new unary interceptor that adds
    30  // the useragent to the context.
    31  func NewUnary() grpc.UnaryServerInterceptor {
    32  	interceptor := func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
    33  		if md, ok := metadata.FromIncomingContext(ctx); ok {
    34  			if lst, ok := md[ctxpkg.UserAgentHeader]; ok && len(lst) != 0 {
    35  				ctx = metadata.AppendToOutgoingContext(ctx, ctxpkg.UserAgentHeader, lst[0])
    36  			}
    37  		}
    38  		return handler(ctx, req)
    39  	}
    40  	return interceptor
    41  }
    42  
    43  // NewStream returns a new server stream interceptor
    44  // that adds the user agent to the context.
    45  func NewStream() grpc.StreamServerInterceptor {
    46  	interceptor := func(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
    47  		ctx := ss.Context()
    48  		if md, ok := metadata.FromIncomingContext(ctx); ok {
    49  			if lst, ok := md[ctxpkg.UserAgentHeader]; ok && len(lst) != 0 {
    50  				ctx = metadata.AppendToOutgoingContext(ctx, ctxpkg.UserAgentHeader, lst[0])
    51  			}
    52  		}
    53  		wrapped := newWrappedServerStream(ctx, ss)
    54  		return handler(srv, wrapped)
    55  	}
    56  	return interceptor
    57  }
    58  
    59  func newWrappedServerStream(ctx context.Context, ss grpc.ServerStream) *wrappedServerStream {
    60  	return &wrappedServerStream{ServerStream: ss, newCtx: ctx}
    61  }
    62  
    63  type wrappedServerStream struct {
    64  	grpc.ServerStream
    65  	newCtx context.Context
    66  }
    67  
    68  func (ss *wrappedServerStream) Context() context.Context {
    69  	return ss.newCtx
    70  }