dubbo.apache.org/dubbo-go/v3@v3.1.1/xds/utils/grpcutil/method.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 2018 gRPC authors.
    21   *
    22   */
    23  
    24  package grpcutil
    25  
    26  import (
    27  	"errors"
    28  	"strings"
    29  )
    30  
    31  // ParseMethod splits service and method from the input. It expects format
    32  // "/service/method".
    33  func ParseMethod(methodName string) (service, method string, _ error) {
    34  	if !strings.HasPrefix(methodName, "/") {
    35  		return "", "", errors.New("invalid method name: should start with /")
    36  	}
    37  	methodName = methodName[1:]
    38  
    39  	pos := strings.LastIndex(methodName, "/")
    40  	if pos < 0 {
    41  		return "", "", errors.New("invalid method name: suffix /method is missing")
    42  	}
    43  	return methodName[:pos], methodName[pos+1:], nil
    44  }
    45  
    46  const baseContentType = "application/grpc"
    47  
    48  // ContentSubtype returns the content-subtype for the given content-type.  The
    49  // given content-type must be a valid content-type that starts with
    50  // "application/grpc". A content-subtype will follow "application/grpc" after a
    51  // "+" or ";". See
    52  // https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md#requests for
    53  // more details.
    54  //
    55  // If contentType is not a valid content-type for gRPC, the boolean
    56  // will be false, otherwise true. If content-type == "application/grpc",
    57  // "application/grpc+", or "application/grpc;", the boolean will be true,
    58  // but no content-subtype will be returned.
    59  //
    60  // contentType is assumed to be lowercase already.
    61  func ContentSubtype(contentType string) (string, bool) {
    62  	if contentType == baseContentType {
    63  		return "", true
    64  	}
    65  	if !strings.HasPrefix(contentType, baseContentType) {
    66  		return "", false
    67  	}
    68  	// guaranteed since != baseContentType and has baseContentType prefix
    69  	switch contentType[len(baseContentType)] {
    70  	case '+', ';':
    71  		// this will return true for "application/grpc+" or "application/grpc;"
    72  		// which the previous validContentType function tested to be valid, so we
    73  		// just say that no content-subtype is specified in this case
    74  		return contentType[len(baseContentType)+1:], true
    75  	default:
    76  		return "", false
    77  	}
    78  }
    79  
    80  // ContentType builds full content type with the given sub-type.
    81  //
    82  // contentSubtype is assumed to be lowercase
    83  func ContentType(contentSubtype string) string {
    84  	if contentSubtype == "" {
    85  		return baseContentType
    86  	}
    87  	return baseContentType + "+" + contentSubtype
    88  }