HTTP1ToGRPCServerCodec.swift 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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 Foundation
  17. import NIO
  18. import NIOHTTP1
  19. import NIOFoundationCompat
  20. import Logging
  21. /// Incoming gRPC package with a fixed message type.
  22. ///
  23. /// - Important: This is **NOT** part of the public API.
  24. public enum _GRPCServerRequestPart<RequestPayload: GRPCPayload> {
  25. case head(HTTPRequestHead)
  26. case message(RequestPayload)
  27. case end
  28. }
  29. /// Outgoing gRPC package with a fixed message type.
  30. ///
  31. /// - Important: This is **NOT** part of the public API.
  32. public enum _GRPCServerResponsePart<ResponsePayload: GRPCPayload> {
  33. case headers(HTTPHeaders)
  34. case message(_MessageContext<ResponsePayload>)
  35. case statusAndTrailers(GRPCStatus, HTTPHeaders)
  36. }
  37. /// A simple channel handler that translates HTTP1 data types into gRPC packets, and vice versa.
  38. ///
  39. /// We use HTTP1 (instead of HTTP2) primitives, as these are easier to work with than raw HTTP2
  40. /// primitives while providing all the functionality we need. In addition, it allows us to support
  41. /// gRPC-Web (gRPC over HTTP1).
  42. ///
  43. /// The translation from HTTP2 to HTTP1 is done by `HTTP2ToHTTP1ServerCodec`.
  44. public final class HTTP1ToGRPCServerCodec<Request: GRPCPayload, Response: GRPCPayload> {
  45. public init(encoding: ServerMessageEncoding, logger: Logger) {
  46. self.encoding = encoding
  47. self.encodingHeaderValidator = MessageEncodingHeaderValidator(encoding: encoding)
  48. self.logger = logger
  49. var accessLog = Logger(subsystem: .serverAccess)
  50. accessLog[metadataKey: MetadataKey.requestID] = logger[metadataKey: MetadataKey.requestID]
  51. self.accessLog = accessLog
  52. self.messageReader = LengthPrefixedMessageReader()
  53. self.messageWriter = LengthPrefixedMessageWriter()
  54. }
  55. private var contentType: ContentType?
  56. private let encoding: ServerMessageEncoding
  57. private let encodingHeaderValidator: MessageEncodingHeaderValidator
  58. private var acceptEncodingHeader: String? = nil
  59. private var responseEncodingHeader: String? = nil
  60. private let logger: Logger
  61. private let accessLog: Logger
  62. private var stopwatch: Stopwatch?
  63. // The following buffers use force unwrapping explicitly. With optionals, developers
  64. // are encouraged to unwrap them using guard-else statements. These don't work cleanly
  65. // with structs, since the guard-else would create a new copy of the struct, which
  66. // would then have to be re-assigned into the class variable for the changes to take effect.
  67. // By force unwrapping, we avoid those reassignments, and the code is a bit cleaner.
  68. // Buffer to store binary encoded protos as they're being received if the proto is split across
  69. // multiple buffers.
  70. private var binaryRequestBuffer: NIO.ByteBuffer!
  71. // Buffers to store text encoded protos. Only used when content-type is application/grpc-web-text.
  72. // TODO(kaipi): Extract all gRPC Web processing logic into an independent handler only added on
  73. // the HTTP1.1 pipeline, as it's starting to get in the way of readability.
  74. private var requestTextBuffer: NIO.ByteBuffer!
  75. private var responseTextBuffer: NIO.ByteBuffer!
  76. var inboundState = InboundState.expectingHeaders {
  77. willSet {
  78. guard newValue != self.inboundState else { return }
  79. self.logger.debug("inbound state changed", metadata: ["old_state": "\(self.inboundState)", "new_state": "\(newValue)"])
  80. }
  81. }
  82. var outboundState = OutboundState.expectingHeaders {
  83. willSet {
  84. guard newValue != self.outboundState else { return }
  85. self.logger.debug("outbound state changed", metadata: ["old_state": "\(self.outboundState)", "new_state": "\(newValue)"])
  86. }
  87. }
  88. var messageReader: LengthPrefixedMessageReader
  89. var messageWriter: LengthPrefixedMessageWriter
  90. }
  91. extension HTTP1ToGRPCServerCodec {
  92. enum InboundState {
  93. case expectingHeaders
  94. case expectingBody
  95. // ignore any additional messages; e.g. we've seen .end or we've sent an error and are waiting for the stream to close.
  96. case ignore
  97. }
  98. enum OutboundState {
  99. case expectingHeaders
  100. case expectingBodyOrStatus
  101. case ignore
  102. }
  103. }
  104. extension HTTP1ToGRPCServerCodec: ChannelInboundHandler {
  105. public typealias InboundIn = HTTPServerRequestPart
  106. public typealias InboundOut = _GRPCServerRequestPart<Request>
  107. public func channelRead(context: ChannelHandlerContext, data: NIOAny) {
  108. if case .ignore = inboundState {
  109. self.logger.notice("ignoring read data", metadata: ["data": "\(data)"])
  110. return
  111. }
  112. do {
  113. switch self.unwrapInboundIn(data) {
  114. case .head(let requestHead):
  115. inboundState = try processHead(context: context, requestHead: requestHead)
  116. case .body(var body):
  117. inboundState = try processBody(context: context, body: &body)
  118. case .end(let trailers):
  119. inboundState = try processEnd(context: context, trailers: trailers)
  120. }
  121. } catch {
  122. context.fireErrorCaught(error)
  123. inboundState = .ignore
  124. }
  125. }
  126. func processHead(context: ChannelHandlerContext, requestHead: HTTPRequestHead) throws -> InboundState {
  127. self.logger.debug("processing request head", metadata: ["head": "\(requestHead)"])
  128. guard case .expectingHeaders = inboundState else {
  129. self.logger.error("invalid state while processing request head",
  130. metadata: ["state": "\(inboundState)", "head": "\(requestHead)"])
  131. throw GRPCError.InvalidState("expected state .expectingHeaders, got \(inboundState)").captureContext()
  132. }
  133. self.stopwatch = .start()
  134. self.accessLog.debug("rpc call started", metadata: [
  135. "path": "\(requestHead.uri)",
  136. "method": "\(requestHead.method)",
  137. "version": "\(requestHead.version)"
  138. ])
  139. if let contentType = requestHead.headers.first(name: GRPCHeaderName.contentType).flatMap(ContentType.init) {
  140. self.contentType = contentType
  141. } else {
  142. self.logger.debug("no 'content-type' header, assuming content type is 'application/grpc'")
  143. // If the Content-Type is not present, assume the request is binary encoded gRPC.
  144. self.contentType = .protobuf
  145. }
  146. if self.contentType == .webTextProtobuf {
  147. requestTextBuffer = context.channel.allocator.buffer(capacity: 0)
  148. }
  149. // What compression was used for sending requests?
  150. let encodingHeader = requestHead.headers.first(name: GRPCHeaderName.encoding)
  151. switch self.encodingHeaderValidator.validate(requestEncoding: encodingHeader) {
  152. case let .supported(algorithm, limit, acceptableEncoding):
  153. self.messageReader = LengthPrefixedMessageReader(compression: algorithm, decompressionLimit: limit)
  154. if acceptableEncoding.isEmpty {
  155. self.acceptEncodingHeader = nil
  156. } else {
  157. self.acceptEncodingHeader = acceptableEncoding.joined(separator: ",")
  158. }
  159. case .noCompression:
  160. self.messageReader = LengthPrefixedMessageReader()
  161. self.acceptEncodingHeader = nil
  162. case let .unsupported(header, acceptableEncoding):
  163. let message: String
  164. let headers: HTTPHeaders
  165. if acceptableEncoding.isEmpty {
  166. message = "compression is not supported"
  167. headers = .init()
  168. } else {
  169. let advertised = acceptableEncoding.joined(separator: ",")
  170. message = "'\(header)' compression is not supported, supported: \(advertised)"
  171. headers = [GRPCHeaderName.acceptEncoding: advertised]
  172. }
  173. let status = GRPCStatus(code: .unimplemented, message: message)
  174. defer {
  175. self.write(context: context, data: NIOAny(OutboundIn.statusAndTrailers(status, headers)), promise: nil)
  176. self.flush(context: context)
  177. }
  178. // We're about to fast-fail, so ignore any following inbound messages.
  179. return .ignore
  180. }
  181. // What compression should we use for writing responses?
  182. let clientAcceptableEncoding = requestHead.headers[canonicalForm: GRPCHeaderName.acceptEncoding]
  183. if let responseEncoding = self.selectResponseEncoding(from: clientAcceptableEncoding) {
  184. self.messageWriter = LengthPrefixedMessageWriter(compression: responseEncoding)
  185. self.responseEncodingHeader = responseEncoding.name
  186. } else {
  187. self.messageWriter = LengthPrefixedMessageWriter(compression: .none)
  188. self.responseEncodingHeader = nil
  189. }
  190. context.fireChannelRead(self.wrapInboundOut(.head(requestHead)))
  191. return .expectingBody
  192. }
  193. func processBody(context: ChannelHandlerContext, body: inout ByteBuffer) throws -> InboundState {
  194. self.logger.debug("processing body: \(body)")
  195. guard case .expectingBody = inboundState else {
  196. self.logger.error("invalid state while processing body",
  197. metadata: ["state": "\(inboundState)", "body": "\(body)"])
  198. throw GRPCError.InvalidState("expected state .expectingBody, got \(inboundState)").captureContext()
  199. }
  200. // If the contentType is text, then decode the incoming bytes as base64 encoded, and append
  201. // it to the binary buffer. If the request is chunked, this section will process the text
  202. // in the biggest chunk that is multiple of 4, leaving the unread bytes in the textBuffer
  203. // where it will expect a new incoming chunk.
  204. if self.contentType == .webTextProtobuf {
  205. precondition(requestTextBuffer != nil)
  206. requestTextBuffer.writeBuffer(&body)
  207. // Read in chunks of 4 bytes as base64 encoded strings will always be multiples of 4.
  208. let readyBytes = requestTextBuffer.readableBytes - (requestTextBuffer.readableBytes % 4)
  209. guard let base64Encoded = requestTextBuffer.readString(length: readyBytes),
  210. let decodedData = Data(base64Encoded: base64Encoded) else {
  211. throw GRPCError.Base64DecodeError().captureContext()
  212. }
  213. body.writeBytes(decodedData)
  214. }
  215. self.messageReader.append(buffer: &body)
  216. var requests: [Request] = []
  217. do {
  218. while var buffer = try self.messageReader.nextMessage() {
  219. requests.append(try Request(serializedByteBuffer: &buffer))
  220. }
  221. } catch let grpcError as GRPCError.WithContext {
  222. context.fireErrorCaught(grpcError)
  223. return .ignore
  224. } catch {
  225. context.fireErrorCaught(GRPCError.DeserializationFailure().captureContext())
  226. return .ignore
  227. }
  228. requests.forEach {
  229. context.fireChannelRead(self.wrapInboundOut(.message($0)))
  230. }
  231. return .expectingBody
  232. }
  233. private func processEnd(context: ChannelHandlerContext, trailers: HTTPHeaders?) throws -> InboundState {
  234. self.logger.debug("processing end")
  235. if let trailers = trailers {
  236. self.logger.error("unexpected trailers when processing stream end", metadata: ["trailers": "\(trailers)"])
  237. throw GRPCError.InvalidState("unexpected trailers received").captureContext()
  238. }
  239. context.fireChannelRead(self.wrapInboundOut(.end))
  240. return .ignore
  241. }
  242. }
  243. extension HTTP1ToGRPCServerCodec: ChannelOutboundHandler {
  244. public typealias OutboundIn = _GRPCServerResponsePart<Response>
  245. public typealias OutboundOut = HTTPServerResponsePart
  246. public func write(context: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise<Void>?) {
  247. if case .ignore = self.outboundState {
  248. self.logger.notice("ignoring written data: \(data)")
  249. promise?.fail(GRPCError.InvalidState("rpc has already finished").captureContext())
  250. return
  251. }
  252. switch self.unwrapOutboundIn(data) {
  253. case .headers(var headers):
  254. guard case .expectingHeaders = self.outboundState else {
  255. self.logger.error("invalid state while writing headers",
  256. metadata: ["state": "\(self.outboundState)", "headers": "\(headers)"])
  257. return
  258. }
  259. var version = HTTPVersion(major: 2, minor: 0)
  260. if let contentType = self.contentType {
  261. headers.add(name: GRPCHeaderName.contentType, value: contentType.canonicalValue)
  262. if contentType != .protobuf {
  263. version = .init(major: 1, minor: 1)
  264. }
  265. }
  266. if self.contentType == .webTextProtobuf {
  267. responseTextBuffer = context.channel.allocator.buffer(capacity: 0)
  268. }
  269. // Are we compressing responses?
  270. if let responseEncoding = self.responseEncodingHeader {
  271. headers.add(name: GRPCHeaderName.encoding, value: responseEncoding)
  272. }
  273. // The client may have sent us a message using an encoding we didn't advertise; we'll send
  274. // an accept-encoding header back if that's the case.
  275. if let acceptEncoding = self.acceptEncodingHeader {
  276. headers.add(name: GRPCHeaderName.acceptEncoding, value: acceptEncoding)
  277. }
  278. context.write(self.wrapOutboundOut(.head(HTTPResponseHead(version: version, status: .ok, headers: headers))), promise: promise)
  279. self.outboundState = .expectingBodyOrStatus
  280. case .message(let messageContext):
  281. guard case .expectingBodyOrStatus = self.outboundState else {
  282. self.logger.error("invalid state while writing message", metadata: ["state": "\(self.outboundState)"])
  283. return
  284. }
  285. do {
  286. if contentType == .webTextProtobuf {
  287. // Store the response into an independent buffer. We can't return the message directly as
  288. // it needs to be aggregated with all the responses plus the trailers, in order to have
  289. // the base64 response properly encoded in a single byte stream.
  290. precondition(self.responseTextBuffer != nil)
  291. try self.messageWriter.write(
  292. messageContext.message,
  293. into: &self.responseTextBuffer,
  294. compressed: messageContext.compressed
  295. )
  296. // Since we stored the written data, mark the write promise as successful so that the
  297. // ServerStreaming provider continues sending the data.
  298. promise?.succeed(())
  299. } else {
  300. var lengthPrefixedMessageBuffer = context.channel.allocator.buffer(capacity: 0)
  301. try self.messageWriter.write(
  302. messageContext.message,
  303. into: &lengthPrefixedMessageBuffer,
  304. compressed: messageContext.compressed
  305. )
  306. context.write(self.wrapOutboundOut(.body(.byteBuffer(lengthPrefixedMessageBuffer))), promise: promise)
  307. }
  308. } catch {
  309. let error = GRPCError.SerializationFailure().captureContext()
  310. promise?.fail(error)
  311. context.fireErrorCaught(error)
  312. self.outboundState = .ignore
  313. return
  314. }
  315. self.outboundState = .expectingBodyOrStatus
  316. case let .statusAndTrailers(status, trailers):
  317. // If we error before sending the initial headers then we won't have sent the request head.
  318. // NIOHTTP2 doesn't support sending a single frame as a "Trailers-Only" response so we still
  319. // need to loop back and send the request head first.
  320. if case .expectingHeaders = self.outboundState {
  321. self.write(context: context, data: NIOAny(OutboundIn.headers(HTTPHeaders())), promise: nil)
  322. }
  323. var trailers = trailers
  324. trailers.add(name: GRPCHeaderName.statusCode, value: String(describing: status.code.rawValue))
  325. if let message = status.message.flatMap(GRPCStatusMessageMarshaller.marshall) {
  326. trailers.add(name: GRPCHeaderName.statusMessage, value: message)
  327. }
  328. if contentType == .webTextProtobuf {
  329. precondition(responseTextBuffer != nil)
  330. // Encode the trailers into the response byte stream as a length delimited message, as per
  331. // https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-WEB.md
  332. let textTrailers = trailers.map { name, value in "\(name): \(value)" }.joined(separator: "\r\n")
  333. responseTextBuffer.writeInteger(UInt8(0x80))
  334. responseTextBuffer.writeInteger(UInt32(textTrailers.utf8.count))
  335. responseTextBuffer.writeString(textTrailers)
  336. // TODO: Binary responses that are non multiples of 3 will end = or == when encoded in
  337. // base64. Investigate whether this might have any effect on the transport mechanism and
  338. // client decoding. Initial results say that they are inocuous, but we might have to keep
  339. // an eye on this in case something trips up.
  340. if let binaryData = responseTextBuffer.readData(length: responseTextBuffer.readableBytes) {
  341. let encodedData = binaryData.base64EncodedString()
  342. responseTextBuffer.clear()
  343. responseTextBuffer.reserveCapacity(encodedData.utf8.count)
  344. responseTextBuffer.writeString(encodedData)
  345. }
  346. // After collecting all response for gRPC Web connections, send one final aggregated
  347. // response.
  348. context.write(self.wrapOutboundOut(.body(.byteBuffer(responseTextBuffer))), promise: promise)
  349. context.write(self.wrapOutboundOut(.end(nil)), promise: promise)
  350. } else {
  351. context.write(self.wrapOutboundOut(.end(trailers)), promise: promise)
  352. }
  353. // Log the call duration and status
  354. if let stopwatch = self.stopwatch {
  355. self.stopwatch = nil
  356. let millis = stopwatch.elapsedMillis()
  357. self.accessLog.debug("rpc call finished", metadata: [
  358. "duration_ms": "\(millis)",
  359. "status_code": "\(status.code.rawValue)"
  360. ])
  361. }
  362. self.outboundState = .ignore
  363. self.inboundState = .ignore
  364. }
  365. }
  366. }
  367. fileprivate extension HTTP1ToGRPCServerCodec {
  368. /// Selects an appropriate response encoding from the list of encodings sent to us by the client.
  369. /// Returns `nil` if there were no appropriate algorithms, in which case the server will send
  370. /// messages uncompressed.
  371. func selectResponseEncoding(from acceptableEncoding: [Substring]) -> CompressionAlgorithm? {
  372. guard case .enabled(let configuration) = self.encoding else {
  373. return nil
  374. }
  375. return acceptableEncoding.compactMap {
  376. CompressionAlgorithm(rawValue: String($0))
  377. }.first {
  378. configuration.enabledAlgorithms.contains($0)
  379. }
  380. }
  381. }
  382. struct MessageEncodingHeaderValidator {
  383. var encoding: ServerMessageEncoding
  384. enum ValidationResult {
  385. /// The requested compression is supported.
  386. case supported(algorithm: CompressionAlgorithm, decompressionLimit: DecompressionLimit, acceptEncoding: [String])
  387. /// The `requestEncoding` is not supported; `acceptEncoding` contains all algorithms we do
  388. /// support.
  389. case unsupported(requestEncoding: String, acceptEncoding: [String])
  390. /// No compression was requested.
  391. case noCompression
  392. }
  393. /// Validates the value of the 'grpc-encoding' header against compression algorithms supported and
  394. /// advertised by this peer.
  395. ///
  396. /// - Parameter requestEncoding: The value of the 'grpc-encoding' header.
  397. func validate(requestEncoding: String?) -> ValidationResult {
  398. switch (self.encoding, requestEncoding) {
  399. // Compression is enabled and the client sent a message encoding header. Do we support it?
  400. case (.enabled(let configuration), .some(let header)):
  401. guard let algorithm = CompressionAlgorithm(rawValue: header) else {
  402. return .unsupported(
  403. requestEncoding: header,
  404. acceptEncoding: configuration.enabledAlgorithms.map { $0.name }
  405. )
  406. }
  407. if configuration.enabledAlgorithms.contains(algorithm) {
  408. return .supported(
  409. algorithm: algorithm,
  410. decompressionLimit: configuration.decompressionLimit,
  411. acceptEncoding: []
  412. )
  413. } else {
  414. // From: https://github.com/grpc/grpc/blob/master/doc/compression.md
  415. //
  416. // Note that a peer MAY choose to not disclose all the encodings it supports. However, if
  417. // it receives a message compressed in an undisclosed but supported encoding, it MUST
  418. // include said encoding in the response's grpc-accept-encoding header.
  419. return .supported(
  420. algorithm: algorithm,
  421. decompressionLimit: configuration.decompressionLimit,
  422. acceptEncoding: configuration.enabledAlgorithms.map { $0.name } + CollectionOfOne(header)
  423. )
  424. }
  425. // Compression is disabled and the client sent a message encoding header. We clearly don't
  426. // support this. Note this is different to the supported but not advertised case since we have
  427. // explicitly not enabled compression.
  428. case (.disabled, .some(let header)):
  429. return .unsupported(requestEncoding: header, acceptEncoding: [])
  430. // The client didn't send a message encoding header.
  431. case (_, .none):
  432. return .noCompression
  433. }
  434. }
  435. }