2
0

GRPCServerRequestRoutingHandler.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Copyright 2019, gRPC Authors All rights reserved.
  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. import Logging
  17. import NIO
  18. import NIOHTTP1
  19. import SwiftProtobuf
  20. /// Processes individual gRPC messages and stream-close events on an HTTP2 channel.
  21. public protocol GRPCCallHandler: ChannelHandler {}
  22. /// Provides `GRPCCallHandler` objects for the methods on a particular service name.
  23. ///
  24. /// Implemented by the generated code.
  25. public protocol CallHandlerProvider: AnyObject {
  26. /// The name of the service this object is providing methods for, including the package path.
  27. ///
  28. /// - Example: "io.grpc.Echo.EchoService"
  29. var serviceName: Substring { get }
  30. /// Determines, calls and returns the appropriate request handler (`GRPCCallHandler`), depending on the request's
  31. /// method. Returns nil for methods not handled by this service.
  32. func handleMethod(_ methodName: Substring, callHandlerContext: CallHandlerContext)
  33. -> GRPCCallHandler?
  34. }
  35. // This is public because it will be passed into generated code, all members are `internal` because
  36. // the context will get passed from generated code back into gRPC library code and all members should
  37. // be considered an implementation detail to the user.
  38. public struct CallHandlerContext {
  39. internal var errorDelegate: ServerErrorDelegate?
  40. internal var logger: Logger
  41. internal var encoding: ServerMessageEncoding
  42. internal var eventLoop: EventLoop
  43. internal var path: String
  44. }
  45. /// A call URI split into components.
  46. struct CallPath {
  47. /// The name of the service to call.
  48. var service: String.UTF8View.SubSequence
  49. /// The name of the method to call.
  50. var method: String.UTF8View.SubSequence
  51. /// Charater used to split the path into components.
  52. private let pathSplitDelimiter = UInt8(ascii: "/")
  53. /// Split a path into service and method.
  54. /// Split is done in UTF8 as this turns out to be approximately 10x faster than a simple split.
  55. /// URI format: "/package.Servicename/MethodName"
  56. init?(requestURI: String) {
  57. var utf8View = requestURI.utf8[...]
  58. // Check and remove the split character at the beginning.
  59. guard let prefix = utf8View.trimPrefix(to: self.pathSplitDelimiter), prefix.isEmpty else {
  60. return nil
  61. }
  62. guard let service = utf8View.trimPrefix(to: pathSplitDelimiter) else {
  63. return nil
  64. }
  65. guard let method = utf8View.trimPrefix(to: pathSplitDelimiter) else {
  66. return nil
  67. }
  68. self.service = service
  69. self.method = method
  70. }
  71. }
  72. extension Collection where Self == Self.SubSequence, Self.Element: Equatable {
  73. /// Trims out the prefix up to `separator`, and returns it.
  74. /// Sets self to the subsequence after the separator, and returns the subsequence before the separator.
  75. /// If self is emtpy returns `nil`
  76. /// - parameters:
  77. /// - separator : The Element between the head which is returned and the rest which is left in self.
  78. /// - returns: SubSequence containing everything between the beginnning and the first occurance of
  79. /// `separator`. If `separator` is not found this will be the entire Collection. If the collection is empty
  80. /// returns `nil`
  81. mutating func trimPrefix(to separator: Element) -> SubSequence? {
  82. guard !self.isEmpty else {
  83. return nil
  84. }
  85. if let separatorIndex = self.firstIndex(of: separator) {
  86. let indexAfterSeparator = self.index(after: separatorIndex)
  87. defer { self = self[indexAfterSeparator...] }
  88. return self[..<separatorIndex]
  89. } else {
  90. defer { self = self[self.endIndex...] }
  91. return self[...]
  92. }
  93. }
  94. }