method.go 3.0 KB

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