GRPCServerRequestRoutingHandler.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. /// Attempts to route a request to a user-provided call handler. Also validates that the request has
  46. /// a suitable 'content-type' for gRPC.
  47. ///
  48. /// Once the request headers are available, asks the `CallHandlerProvider` corresponding to the request's service name
  49. /// for a `GRPCCallHandler` object. That object is then forwarded the individual gRPC messages.
  50. ///
  51. /// After the pipeline has been configured with the `GRPCCallHandler`, this handler removes itself
  52. /// from the pipeline.
  53. public final class GRPCServerRequestRoutingHandler {
  54. private let logger: Logger
  55. private let servicesByName: [Substring: CallHandlerProvider]
  56. private let encoding: ServerMessageEncoding
  57. private weak var errorDelegate: ServerErrorDelegate?
  58. private enum State: Equatable {
  59. case notConfigured
  60. case configuring([InboundOut])
  61. }
  62. private var state: State = .notConfigured
  63. public init(
  64. servicesByName: [Substring: CallHandlerProvider],
  65. encoding: ServerMessageEncoding,
  66. errorDelegate: ServerErrorDelegate?,
  67. logger: Logger
  68. ) {
  69. self.servicesByName = servicesByName
  70. self.encoding = encoding
  71. self.errorDelegate = errorDelegate
  72. self.logger = logger
  73. }
  74. }
  75. extension GRPCServerRequestRoutingHandler: ChannelInboundHandler, RemovableChannelHandler {
  76. public typealias InboundIn = HTTPServerRequestPart
  77. public typealias InboundOut = HTTPServerRequestPart
  78. public typealias OutboundOut = HTTPServerResponsePart
  79. public func errorCaught(context: ChannelHandlerContext, error: Error) {
  80. let status: GRPCStatus
  81. if let errorWithContext = error as? GRPCError.WithContext {
  82. self.errorDelegate?.observeLibraryError(errorWithContext.error)
  83. status = errorWithContext.error.makeGRPCStatus()
  84. } else {
  85. self.errorDelegate?.observeLibraryError(error)
  86. status = (error as? GRPCStatusTransformable)?.makeGRPCStatus() ?? .processingError
  87. }
  88. switch self.state {
  89. case .notConfigured:
  90. // We don't know what protocol we're speaking at this point. We'll just have to close the
  91. // channel.
  92. ()
  93. case let .configuring(messages):
  94. // first! is fine here: we only go from `.notConfigured` to `.configuring` when we receive
  95. // and validate the request head.
  96. let head = messages.compactMap { part -> HTTPRequestHead? in
  97. switch part {
  98. case let .head(head):
  99. return head
  100. default:
  101. return nil
  102. }
  103. }.first!
  104. let responseHead = self.makeResponseHead(requestHead: head, status: status)
  105. context.write(self.wrapOutboundOut(.head(responseHead)), promise: nil)
  106. context.write(self.wrapOutboundOut(.end(nil)), promise: nil)
  107. context.flush()
  108. }
  109. context.close(mode: .all, promise: nil)
  110. }
  111. public func channelRead(context: ChannelHandlerContext, data: NIOAny) {
  112. let requestPart = self.unwrapInboundIn(data)
  113. switch requestPart {
  114. case let .head(requestHead):
  115. precondition(self.state == .notConfigured)
  116. // Validate the 'content-type' is related to gRPC before proceeding.
  117. let maybeContentType = requestHead.headers.first(name: GRPCHeaderName.contentType)
  118. guard let contentType = maybeContentType,
  119. contentType.starts(with: ContentType.commonPrefix) else {
  120. self.logger.warning(
  121. "received request whose 'content-type' does not exist or start with '\(ContentType.commonPrefix)'",
  122. metadata: ["content-type": "\(String(describing: maybeContentType))"]
  123. )
  124. // From: https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md
  125. //
  126. // If 'content-type' does not begin with "application/grpc", gRPC servers SHOULD respond
  127. // with HTTP status of 415 (Unsupported Media Type). This will prevent other HTTP/2
  128. // clients from interpreting a gRPC error response, which uses status 200 (OK), as
  129. // successful.
  130. let responseHead = HTTPResponseHead(
  131. version: requestHead.version,
  132. status: .unsupportedMediaType
  133. )
  134. // Fail the call. Note: we're not speaking gRPC here, so no status or message.
  135. context.write(self.wrapOutboundOut(.head(responseHead)), promise: nil)
  136. context.writeAndFlush(self.wrapOutboundOut(.end(nil)), promise: nil)
  137. return
  138. }
  139. // Do we know how to handle this RPC?
  140. guard let callHandler = self.makeCallHandler(
  141. channel: context.channel,
  142. requestHead: requestHead
  143. ) else {
  144. self.logger.warning(
  145. "unable to make call handler; the RPC is not implemented on this server",
  146. metadata: ["uri": "\(requestHead.uri)"]
  147. )
  148. let status = GRPCError.RPCNotImplemented(rpc: requestHead.uri).makeGRPCStatus()
  149. let responseHead = self.makeResponseHead(requestHead: requestHead, status: status)
  150. // Write back a 'trailers-only' response.
  151. context.write(self.wrapOutboundOut(.head(responseHead)), promise: nil)
  152. context.writeAndFlush(self.wrapOutboundOut(.end(nil)), promise: nil)
  153. return
  154. }
  155. self.logger.debug("received request head, configuring pipeline")
  156. // Buffer the request head; we'll replay it in the next handler when we're removed from the
  157. // pipeline.
  158. self.state = .configuring([requestPart])
  159. // Configure the rest of the pipeline to serve the RPC.
  160. let httpToGRPC = HTTP1ToGRPCServerCodec(encoding: self.encoding, logger: self.logger)
  161. context.pipeline.addHandlers([httpToGRPC, callHandler], position: .after(self))
  162. .whenSuccess {
  163. context.pipeline.removeHandler(self, promise: nil)
  164. }
  165. case .body, .end:
  166. switch self.state {
  167. case .notConfigured:
  168. // We can reach this point if we're receiving messages for a method that isn't implemented,
  169. // in which case we just drop the messages; our response should already be in-flight.
  170. ()
  171. case var .configuring(buffer):
  172. // We received a message while the pipeline was being configured; hold on to it while we
  173. // finish configuring the pipeline.
  174. buffer.append(requestPart)
  175. self.state = .configuring(buffer)
  176. }
  177. }
  178. }
  179. public func handlerRemoved(context: ChannelHandlerContext) {
  180. switch self.state {
  181. case .notConfigured:
  182. ()
  183. case let .configuring(messages):
  184. for message in messages {
  185. context.fireChannelRead(self.wrapInboundOut(message))
  186. }
  187. }
  188. }
  189. /// A call URI split into components.
  190. struct CallPath {
  191. /// The name of the service to call.
  192. var service: String.UTF8View.SubSequence
  193. /// The name of the method to call.
  194. var method: String.UTF8View.SubSequence
  195. /// Charater used to split the path into components.
  196. private let pathSplitDelimiter = UInt8(ascii: "/")
  197. /// Split a path into service and method.
  198. /// Split is done in UTF8 as this turns out to be approximately 10x faster than a simple split.
  199. /// URI format: "/package.Servicename/MethodName"
  200. init?(requestURI: String) {
  201. var utf8View = requestURI.utf8[...]
  202. // Check and remove the split character at the beginning.
  203. guard let prefix = utf8View.trimPrefix(to: self.pathSplitDelimiter), prefix.isEmpty else {
  204. return nil
  205. }
  206. guard let service = utf8View.trimPrefix(to: pathSplitDelimiter) else {
  207. return nil
  208. }
  209. guard let method = utf8View.trimPrefix(to: pathSplitDelimiter) else {
  210. return nil
  211. }
  212. self.service = service
  213. self.method = method
  214. }
  215. }
  216. private func makeCallHandler(channel: Channel, requestHead: HTTPRequestHead) -> GRPCCallHandler? {
  217. // URI format: "/package.Servicename/MethodName", resulting in the following components separated by a slash:
  218. // - uriComponents[0]: empty
  219. // - uriComponents[1]: service name (including the package name);
  220. // `CallHandlerProvider`s should provide the service name including the package name.
  221. // - uriComponents[2]: method name.
  222. self.logger.debug("making call handler", metadata: ["path": "\(requestHead.uri)"])
  223. let uriComponents = CallPath(requestURI: requestHead.uri)
  224. let context = CallHandlerContext(
  225. errorDelegate: self.errorDelegate,
  226. logger: self.logger,
  227. encoding: self.encoding,
  228. eventLoop: channel.eventLoop,
  229. path: requestHead.uri
  230. )
  231. guard let callPath = uriComponents,
  232. let providerForServiceName = servicesByName[String.SubSequence(callPath.service)],
  233. let callHandler = providerForServiceName.handleMethod(
  234. String.SubSequence(callPath.method),
  235. callHandlerContext: context
  236. ) else {
  237. self.logger.notice("could not create handler", metadata: ["path": "\(requestHead.uri)"])
  238. return nil
  239. }
  240. return callHandler
  241. }
  242. private func makeResponseHead(requestHead: HTTPRequestHead,
  243. status: GRPCStatus) -> HTTPResponseHead {
  244. var headers: HTTPHeaders = [
  245. GRPCHeaderName.contentType: ContentType.protobuf.canonicalValue,
  246. GRPCHeaderName.statusCode: "\(status.code.rawValue)",
  247. ]
  248. if let message = status.message.flatMap(GRPCStatusMessageMarshaller.marshall) {
  249. headers.add(name: GRPCHeaderName.statusMessage, value: message)
  250. }
  251. return HTTPResponseHead(version: requestHead.version, status: .ok, headers: headers)
  252. }
  253. }
  254. extension Collection where Self == Self.SubSequence, Self.Element: Equatable {
  255. /// Trims out the prefix up to `separator`, and returns it.
  256. /// Sets self to the subsequence after the separator, and returns the subsequence before the separator.
  257. /// If self is emtpy returns `nil`
  258. /// - parameters:
  259. /// - separator : The Element between the head which is returned and the rest which is left in self.
  260. /// - returns: SubSequence containing everything between the beginnning and the first occurance of
  261. /// `separator`. If `separator` is not found this will be the entire Collection. If the collection is empty
  262. /// returns `nil`
  263. mutating func trimPrefix(to separator: Element) -> SubSequence? {
  264. guard !self.isEmpty else {
  265. return nil
  266. }
  267. if let separatorIndex = self.firstIndex(of: separator) {
  268. let indexAfterSeparator = self.index(after: separatorIndex)
  269. defer { self = self[indexAfterSeparator...] }
  270. return self[..<separatorIndex]
  271. } else {
  272. defer { self = self[self.endIndex...] }
  273. return self[...]
  274. }
  275. }
  276. }