HTTP2ToRawGRPCStateMachine.swift 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235
  1. /*
  2. * Copyright 2020, 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 NIOHPACK
  19. import NIOHTTP2
  20. struct HTTP2ToRawGRPCStateMachine {
  21. /// The current state.
  22. private var state: State
  23. /// Temporarily sets `self.state` to `._modifying` before calling the provided block and setting
  24. /// `self.state` to the `State` modified by the block.
  25. ///
  26. /// Since we hold state as associated data on our `State` enum, any modification to that state
  27. /// will trigger a copy on write for its heap allocated data. Temporarily setting the `self.state`
  28. /// to `._modifying` allows us to avoid an extra reference to any heap allocated data and
  29. /// therefore avoid a copy on write.
  30. @inlinable
  31. internal mutating func withStateAvoidingCoWs<Action>(_ body: (inout State) -> Action) -> Action {
  32. var state: State = ._modifying
  33. swap(&self.state, &state)
  34. defer {
  35. swap(&self.state, &state)
  36. }
  37. return body(&state)
  38. }
  39. internal init(
  40. services: [Substring: CallHandlerProvider],
  41. encoding: ServerMessageEncoding,
  42. normalizeHeaders: Bool = true
  43. ) {
  44. let state = RequestIdleResponseIdleState(
  45. services: services,
  46. encoding: encoding,
  47. normalizeHeaders: normalizeHeaders
  48. )
  49. self.state = .requestIdleResponseIdle(state)
  50. }
  51. }
  52. extension HTTP2ToRawGRPCStateMachine {
  53. enum State {
  54. // Both peers are idle. Nothing has happened to the stream.
  55. case requestIdleResponseIdle(RequestIdleResponseIdleState)
  56. // Received valid headers. Nothing has been sent in response.
  57. case requestOpenResponseIdle(RequestOpenResponseIdleState)
  58. // Received valid headers and request(s). Response headers have been sent.
  59. case requestOpenResponseOpen(RequestOpenResponseOpenState)
  60. // The request stream is closed. Nothing has been sent in response.
  61. case requestClosedResponseIdle(RequestClosedResponseIdleState)
  62. // The request stream is closed. Response headers have been sent.
  63. case requestClosedResponseOpen(RequestClosedResponseOpenState)
  64. // Both streams are closed. This state is terminal.
  65. case requestClosedResponseClosed
  66. // Not a real state. See 'withStateAvoidingCoWs'.
  67. case _modifying
  68. }
  69. struct RequestIdleResponseIdleState {
  70. /// The service providers, keyed by service name.
  71. var services: [Substring: CallHandlerProvider]
  72. /// The encoding configuration for this server.
  73. var encoding: ServerMessageEncoding
  74. /// Whether to normalize user-provided metadata.
  75. var normalizeHeaders: Bool
  76. }
  77. struct RequestOpenResponseIdleState {
  78. /// A length prefixed message reader for request messages.
  79. var reader: LengthPrefixedMessageReader
  80. /// A length prefixed message writer for response messages.
  81. var writer: LengthPrefixedMessageWriter
  82. /// The content type of the RPC.
  83. var contentType: ContentType
  84. /// An accept encoding header to send in the response headers indicating the message encoding
  85. /// that the server supports.
  86. var acceptEncoding: String?
  87. /// A message encoding header to send in the response headers indicating the encoding which will
  88. /// be used for responses.
  89. var responseEncoding: String?
  90. /// Whether to normalize user-provided metadata.
  91. var normalizeHeaders: Bool
  92. /// The pipeline configuration state.
  93. var configurationState: ConfigurationState
  94. }
  95. struct RequestClosedResponseIdleState {
  96. /// A length prefixed message reader for request messages.
  97. var reader: LengthPrefixedMessageReader
  98. /// A length prefixed message writer for response messages.
  99. var writer: LengthPrefixedMessageWriter
  100. /// The content type of the RPC.
  101. var contentType: ContentType
  102. /// An accept encoding header to send in the response headers indicating the message encoding
  103. /// that the server supports.
  104. var acceptEncoding: String?
  105. /// A message encoding header to send in the response headers indicating the encoding which will
  106. /// be used for responses.
  107. var responseEncoding: String?
  108. /// Whether to normalize user-provided metadata.
  109. var normalizeHeaders: Bool
  110. /// The pipeline configuration state.
  111. var configurationState: ConfigurationState
  112. init(from state: RequestOpenResponseIdleState) {
  113. self.reader = state.reader
  114. self.writer = state.writer
  115. self.contentType = state.contentType
  116. self.acceptEncoding = state.acceptEncoding
  117. self.responseEncoding = state.responseEncoding
  118. self.normalizeHeaders = state.normalizeHeaders
  119. self.configurationState = state.configurationState
  120. }
  121. }
  122. struct RequestOpenResponseOpenState {
  123. /// A length prefixed message reader for request messages.
  124. var reader: LengthPrefixedMessageReader
  125. /// A length prefixed message writer for response messages.
  126. var writer: LengthPrefixedMessageWriter
  127. /// Whether to normalize user-provided metadata.
  128. var normalizeHeaders: Bool
  129. init(from state: RequestOpenResponseIdleState) {
  130. self.reader = state.reader
  131. self.writer = state.writer
  132. self.normalizeHeaders = state.normalizeHeaders
  133. }
  134. }
  135. struct RequestClosedResponseOpenState {
  136. /// A length prefixed message reader for request messages.
  137. var reader: LengthPrefixedMessageReader
  138. /// A length prefixed message writer for response messages.
  139. var writer: LengthPrefixedMessageWriter
  140. /// Whether to normalize user-provided metadata.
  141. var normalizeHeaders: Bool
  142. init(from state: RequestOpenResponseOpenState) {
  143. self.reader = state.reader
  144. self.writer = state.writer
  145. self.normalizeHeaders = state.normalizeHeaders
  146. }
  147. init(from state: RequestClosedResponseIdleState) {
  148. self.reader = state.reader
  149. self.writer = state.writer
  150. self.normalizeHeaders = state.normalizeHeaders
  151. }
  152. }
  153. /// The pipeline configuration state.
  154. enum ConfigurationState {
  155. /// The pipeline is being configured. Any message data will be buffered into an appropriate
  156. /// message reader.
  157. case configuring(HPACKHeaders)
  158. /// The pipeline is configured.
  159. case configured
  160. /// Returns true if the configuration is in the `.configured` state.
  161. var isConfigured: Bool {
  162. switch self {
  163. case .configuring:
  164. return false
  165. case .configured:
  166. return true
  167. }
  168. }
  169. /// Configuration has completed.
  170. mutating func configured() -> HPACKHeaders {
  171. switch self {
  172. case .configured:
  173. preconditionFailure("Invalid state: already configured")
  174. case let .configuring(headers):
  175. self = .configured
  176. return headers
  177. }
  178. }
  179. }
  180. }
  181. extension HTTP2ToRawGRPCStateMachine {
  182. enum PipelineConfiguredAction {
  183. /// Forward the given headers.
  184. case forwardHeaders(HPACKHeaders)
  185. /// Forward the given headers and try reading the next message.
  186. case forwardHeadersAndRead(HPACKHeaders)
  187. }
  188. enum ReceiveHeadersAction {
  189. /// Configure the RPC to use the given server handler.
  190. case configure(GRPCServerHandlerProtocol)
  191. /// Reject the RPC by writing out the given headers and setting end-stream.
  192. case rejectRPC(HPACKHeaders)
  193. }
  194. enum ReadNextMessageAction {
  195. /// Do nothing.
  196. case none
  197. /// Forward the buffer.
  198. case forwardMessage(ByteBuffer)
  199. /// Forward the buffer and an 'end' of stream request part.
  200. case forwardMessageAndEnd(ByteBuffer)
  201. /// Forward the buffer and try reading the next message.
  202. case forwardMessageThenReadNextMessage(ByteBuffer)
  203. /// Forward the 'end' of stream request part.
  204. case forwardEnd
  205. /// Throw an error down the pipeline.
  206. case errorCaught(Error)
  207. }
  208. struct StateAndReceiveHeadersAction {
  209. /// The next state.
  210. var state: State
  211. /// The action to take.
  212. var action: ReceiveHeadersAction
  213. }
  214. struct StateAndReceiveDataAction {
  215. /// The next state.
  216. var state: State
  217. /// Whether the caller should try reading the next message.
  218. var tryReading: Bool
  219. }
  220. }
  221. // MARK: Receive Headers
  222. // This is the only state in which we can receive headers.
  223. extension HTTP2ToRawGRPCStateMachine.RequestIdleResponseIdleState {
  224. func receive(
  225. headers: HPACKHeaders,
  226. eventLoop: EventLoop,
  227. errorDelegate: ServerErrorDelegate?,
  228. remoteAddress: SocketAddress?,
  229. logger: Logger,
  230. allocator: ByteBufferAllocator,
  231. responseWriter: GRPCServerResponseWriter,
  232. closeFuture: EventLoopFuture<Void>
  233. ) -> HTTP2ToRawGRPCStateMachine.StateAndReceiveHeadersAction {
  234. // Extract and validate the content type. If it's nil we need to close.
  235. guard let contentType = self.extractContentType(from: headers) else {
  236. return self.unsupportedContentType()
  237. }
  238. // Now extract the request message encoding and setup an appropriate message reader.
  239. // We may send back a list of acceptable request message encodings as well.
  240. let reader: LengthPrefixedMessageReader
  241. let acceptableRequestEncoding: String?
  242. switch self.extractRequestEncoding(from: headers) {
  243. case let .valid(messageReader, acceptEncodingHeader):
  244. reader = messageReader
  245. acceptableRequestEncoding = acceptEncodingHeader
  246. case let .invalid(status, acceptableRequestEncoding):
  247. return self.invalidRequestEncoding(
  248. status: status,
  249. acceptableRequestEncoding: acceptableRequestEncoding,
  250. contentType: contentType
  251. )
  252. }
  253. // Figure out which encoding we should use for responses.
  254. let (writer, responseEncoding) = self.extractResponseEncoding(from: headers)
  255. // Parse the path, and create a call handler.
  256. guard let path = headers.first(name: ":path") else {
  257. return self.methodNotImplemented("", contentType: contentType)
  258. }
  259. guard let callPath = CallPath(requestURI: path),
  260. let service = self.services[Substring(callPath.service)] else {
  261. return self.methodNotImplemented(path, contentType: contentType)
  262. }
  263. // Create a call handler context, i.e. a bunch of 'stuff' we need to create the handler with,
  264. // some of which is exposed to service providers.
  265. let context = CallHandlerContext(
  266. errorDelegate: errorDelegate,
  267. logger: logger,
  268. encoding: self.encoding,
  269. eventLoop: eventLoop,
  270. path: path,
  271. remoteAddress: remoteAddress,
  272. responseWriter: responseWriter,
  273. allocator: allocator,
  274. closeFuture: closeFuture
  275. )
  276. // We have a matching service, hopefully we have a provider for the method too.
  277. let method = Substring(callPath.method)
  278. if let handler = service.handle(method: method, context: context) {
  279. let nextState = HTTP2ToRawGRPCStateMachine.RequestOpenResponseIdleState(
  280. reader: reader,
  281. writer: writer,
  282. contentType: contentType,
  283. acceptEncoding: acceptableRequestEncoding,
  284. responseEncoding: responseEncoding,
  285. normalizeHeaders: self.normalizeHeaders,
  286. configurationState: .configuring(headers)
  287. )
  288. return .init(
  289. state: .requestOpenResponseIdle(nextState),
  290. action: .configure(handler)
  291. )
  292. } else {
  293. return self.methodNotImplemented(path, contentType: contentType)
  294. }
  295. }
  296. /// The 'content-type' is not supported; close with status code 415.
  297. private func unsupportedContentType() -> HTTP2ToRawGRPCStateMachine.StateAndReceiveHeadersAction {
  298. // From: https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md
  299. //
  300. // If 'content-type' does not begin with "application/grpc", gRPC servers SHOULD respond
  301. // with HTTP status of 415 (Unsupported Media Type). This will prevent other HTTP/2
  302. // clients from interpreting a gRPC error response, which uses status 200 (OK), as
  303. // successful.
  304. let trailers = HPACKHeaders([(":status", "415")])
  305. return .init(
  306. state: .requestClosedResponseClosed,
  307. action: .rejectRPC(trailers)
  308. )
  309. }
  310. /// The RPC method is not implemented. Close with an appropriate status.
  311. private func methodNotImplemented(
  312. _ path: String,
  313. contentType: ContentType
  314. ) -> HTTP2ToRawGRPCStateMachine.StateAndReceiveHeadersAction {
  315. let trailers = HTTP2ToRawGRPCStateMachine.makeResponseTrailersOnly(
  316. for: GRPCStatus(code: .unimplemented, message: "'\(path)' is not implemented"),
  317. contentType: contentType,
  318. acceptableRequestEncoding: nil,
  319. userProvidedHeaders: nil,
  320. normalizeUserProvidedHeaders: self.normalizeHeaders
  321. )
  322. return .init(
  323. state: .requestClosedResponseClosed,
  324. action: .rejectRPC(trailers)
  325. )
  326. }
  327. /// The request encoding specified by the client is not supported. Close with an appropriate
  328. /// status.
  329. private func invalidRequestEncoding(
  330. status: GRPCStatus,
  331. acceptableRequestEncoding: String?,
  332. contentType: ContentType
  333. ) -> HTTP2ToRawGRPCStateMachine.StateAndReceiveHeadersAction {
  334. let trailers = HTTP2ToRawGRPCStateMachine.makeResponseTrailersOnly(
  335. for: status,
  336. contentType: contentType,
  337. acceptableRequestEncoding: acceptableRequestEncoding,
  338. userProvidedHeaders: nil,
  339. normalizeUserProvidedHeaders: self.normalizeHeaders
  340. )
  341. return .init(
  342. state: .requestClosedResponseClosed,
  343. action: .rejectRPC(trailers)
  344. )
  345. }
  346. /// Makes a 'GRPCStatus' and response trailers suitable for returning to the client when the
  347. /// request message encoding is not supported.
  348. ///
  349. /// - Parameters:
  350. /// - encoding: The unsupported request message encoding sent by the client.
  351. /// - acceptable: The list if acceptable request message encoding the client may use.
  352. /// - Returns: The status and trailers to return to the client.
  353. private func makeStatusAndTrailersForUnsupportedEncoding(
  354. _ encoding: String,
  355. advertisedEncoding: [String]
  356. ) -> (GRPCStatus, acceptEncoding: String?) {
  357. let status: GRPCStatus
  358. let acceptEncoding: String?
  359. if advertisedEncoding.isEmpty {
  360. // No compression is supported; there's nothing to tell the client about.
  361. status = GRPCStatus(code: .unimplemented, message: "compression is not supported")
  362. acceptEncoding = nil
  363. } else {
  364. // Return a list of supported encodings which we advertise. (The list we advertise may be a
  365. // subset of the encodings we support.)
  366. acceptEncoding = advertisedEncoding.joined(separator: ",")
  367. status = GRPCStatus(
  368. code: .unimplemented,
  369. message: "\(encoding) compression is not supported, supported algorithms are " +
  370. "listed in '\(GRPCHeaderName.acceptEncoding)'"
  371. )
  372. }
  373. return (status, acceptEncoding)
  374. }
  375. /// Extract and validate the 'content-type' sent by the client.
  376. /// - Parameter headers: The headers to extract the 'content-type' from
  377. private func extractContentType(from headers: HPACKHeaders) -> ContentType? {
  378. return headers.first(name: GRPCHeaderName.contentType).flatMap(ContentType.init)
  379. }
  380. /// The result of validating the request encoding header.
  381. private enum RequestEncodingValidation {
  382. /// The encoding was valid.
  383. case valid(messageReader: LengthPrefixedMessageReader, acceptEncoding: String?)
  384. /// The encoding was invalid, the RPC should be terminated with this status.
  385. case invalid(status: GRPCStatus, acceptEncoding: String?)
  386. }
  387. /// Extract and validate the request message encoding header.
  388. /// - Parameters:
  389. /// - headers: The headers to extract the message encoding header from.
  390. /// - Returns: `RequestEncodingValidation`, either a message reader suitable for decoding requests
  391. /// and an accept encoding response header if the request encoding was valid, or a pair of
  392. /// `GRPCStatus` and trailers to close the RPC with.
  393. private func extractRequestEncoding(from headers: HPACKHeaders) -> RequestEncodingValidation {
  394. let encodings = headers[canonicalForm: GRPCHeaderName.encoding]
  395. // Fail if there's more than one encoding header.
  396. if encodings.count > 1 {
  397. let status = GRPCStatus(
  398. code: .invalidArgument,
  399. message: "'\(GRPCHeaderName.encoding)' must contain no more than one value but was '\(encodings.joined(separator: ", "))'"
  400. )
  401. return .invalid(status: status, acceptEncoding: nil)
  402. }
  403. let encodingHeader = encodings.first
  404. let result: RequestEncodingValidation
  405. let validator = MessageEncodingHeaderValidator(encoding: self.encoding)
  406. switch validator.validate(requestEncoding: encodingHeader) {
  407. case let .supported(algorithm, decompressionLimit, acceptEncoding):
  408. // Request message encoding is valid and supported.
  409. result = .valid(
  410. messageReader: LengthPrefixedMessageReader(
  411. compression: algorithm,
  412. decompressionLimit: decompressionLimit
  413. ),
  414. acceptEncoding: acceptEncoding.isEmpty ? nil : acceptEncoding.joined(separator: ",")
  415. )
  416. case .noCompression:
  417. // No message encoding header was present. This means no compression is being used.
  418. result = .valid(
  419. messageReader: LengthPrefixedMessageReader(),
  420. acceptEncoding: nil
  421. )
  422. case let .unsupported(encoding, acceptable):
  423. // Request encoding is not supported.
  424. let (status, acceptEncoding) = self.makeStatusAndTrailersForUnsupportedEncoding(
  425. encoding,
  426. advertisedEncoding: acceptable
  427. )
  428. result = .invalid(status: status, acceptEncoding: acceptEncoding)
  429. }
  430. return result
  431. }
  432. /// Extract a suitable message encoding for responses.
  433. /// - Parameters:
  434. /// - headers: The headers to extract the acceptable response message encoding from.
  435. /// - configuration: The encoding configuration for the server.
  436. /// - Returns: A message writer and the response encoding header to send back to the client.
  437. private func extractResponseEncoding(
  438. from headers: HPACKHeaders
  439. ) -> (LengthPrefixedMessageWriter, String?) {
  440. let writer: LengthPrefixedMessageWriter
  441. let responseEncoding: String?
  442. switch self.encoding {
  443. case let .enabled(configuration):
  444. // Extract the encodings acceptable to the client for response messages.
  445. let acceptableResponseEncoding = headers[canonicalForm: GRPCHeaderName.acceptEncoding]
  446. // Select the first algorithm that we support and have enabled. If we don't find one then we
  447. // won't compress response messages.
  448. let algorithm = acceptableResponseEncoding.lazy.compactMap { value in
  449. CompressionAlgorithm(rawValue: value)
  450. }.first {
  451. configuration.enabledAlgorithms.contains($0)
  452. }
  453. writer = LengthPrefixedMessageWriter(compression: algorithm)
  454. responseEncoding = algorithm?.name
  455. case .disabled:
  456. // The server doesn't have compression enabled.
  457. writer = LengthPrefixedMessageWriter(compression: .none)
  458. responseEncoding = nil
  459. }
  460. return (writer, responseEncoding)
  461. }
  462. }
  463. // MARK: - Receive Data
  464. extension HTTP2ToRawGRPCStateMachine.RequestOpenResponseIdleState {
  465. mutating func receive(
  466. buffer: inout ByteBuffer,
  467. endStream: Bool
  468. ) -> HTTP2ToRawGRPCStateMachine.StateAndReceiveDataAction {
  469. // Append the bytes to the reader.
  470. self.reader.append(buffer: &buffer)
  471. let state: HTTP2ToRawGRPCStateMachine.State
  472. let tryReading: Bool
  473. switch (self.configurationState.isConfigured, endStream) {
  474. case (true, true):
  475. /// Configured and end stream: read from the buffer, end will be sent as a result of draining
  476. /// the reader in the next state.
  477. state = .requestClosedResponseIdle(.init(from: self))
  478. tryReading = true
  479. case (true, false):
  480. /// Configured but not end stream, just read from the buffer.
  481. state = .requestOpenResponseIdle(self)
  482. tryReading = true
  483. case (false, true):
  484. // Not configured yet, but end of stream. Request stream is now closed but there's no point
  485. // reading yet.
  486. state = .requestClosedResponseIdle(.init(from: self))
  487. tryReading = false
  488. case (false, false):
  489. // Not configured yet, not end stream. No point reading a message yet since we don't have
  490. // anywhere to deliver it.
  491. state = .requestOpenResponseIdle(self)
  492. tryReading = false
  493. }
  494. return .init(state: state, tryReading: tryReading)
  495. }
  496. }
  497. extension HTTP2ToRawGRPCStateMachine.RequestOpenResponseOpenState {
  498. mutating func receive(
  499. buffer: inout ByteBuffer,
  500. endStream: Bool
  501. ) -> HTTP2ToRawGRPCStateMachine.StateAndReceiveDataAction {
  502. self.reader.append(buffer: &buffer)
  503. let state: HTTP2ToRawGRPCStateMachine.State
  504. if endStream {
  505. // End stream, so move to the closed state. Any end of request stream events events will
  506. // happen as a result of reading from the closed state.
  507. state = .requestClosedResponseOpen(.init(from: self))
  508. } else {
  509. state = .requestOpenResponseOpen(self)
  510. }
  511. return .init(state: state, tryReading: true)
  512. }
  513. }
  514. // MARK: - Send Headers
  515. extension HTTP2ToRawGRPCStateMachine.RequestOpenResponseIdleState {
  516. func send(headers userProvidedHeaders: HPACKHeaders) -> HPACKHeaders {
  517. return HTTP2ToRawGRPCStateMachine.makeResponseHeaders(
  518. contentType: self.contentType,
  519. responseEncoding: self.responseEncoding,
  520. acceptableRequestEncoding: self.acceptEncoding,
  521. userProvidedHeaders: userProvidedHeaders,
  522. normalizeUserProvidedHeaders: self.normalizeHeaders
  523. )
  524. }
  525. }
  526. extension HTTP2ToRawGRPCStateMachine.RequestClosedResponseIdleState {
  527. func send(headers userProvidedHeaders: HPACKHeaders) -> HPACKHeaders {
  528. return HTTP2ToRawGRPCStateMachine.makeResponseHeaders(
  529. contentType: self.contentType,
  530. responseEncoding: self.responseEncoding,
  531. acceptableRequestEncoding: self.acceptEncoding,
  532. userProvidedHeaders: userProvidedHeaders,
  533. normalizeUserProvidedHeaders: self.normalizeHeaders
  534. )
  535. }
  536. }
  537. // MARK: - Send Data
  538. extension HTTP2ToRawGRPCStateMachine {
  539. static func writeGRPCFramedMessage(
  540. _ buffer: ByteBuffer,
  541. compress: Bool,
  542. allocator: ByteBufferAllocator,
  543. writer: LengthPrefixedMessageWriter
  544. ) -> Result<ByteBuffer, Error> {
  545. do {
  546. let prefixed = try writer.write(buffer: buffer, allocator: allocator, compressed: compress)
  547. return .success(prefixed)
  548. } catch {
  549. return .failure(error)
  550. }
  551. }
  552. }
  553. extension HTTP2ToRawGRPCStateMachine.RequestOpenResponseOpenState {
  554. func send(
  555. buffer: ByteBuffer,
  556. allocator: ByteBufferAllocator,
  557. compress: Bool
  558. ) -> Result<ByteBuffer, Error> {
  559. return HTTP2ToRawGRPCStateMachine.writeGRPCFramedMessage(
  560. buffer,
  561. compress: compress,
  562. allocator: allocator,
  563. writer: self.writer
  564. )
  565. }
  566. }
  567. extension HTTP2ToRawGRPCStateMachine.RequestClosedResponseOpenState {
  568. func send(
  569. buffer: ByteBuffer,
  570. allocator: ByteBufferAllocator,
  571. compress: Bool
  572. ) -> Result<ByteBuffer, Error> {
  573. return HTTP2ToRawGRPCStateMachine.writeGRPCFramedMessage(
  574. buffer,
  575. compress: compress,
  576. allocator: allocator,
  577. writer: self.writer
  578. )
  579. }
  580. }
  581. // MARK: - Send End
  582. extension HTTP2ToRawGRPCStateMachine.RequestOpenResponseIdleState {
  583. func send(
  584. status: GRPCStatus,
  585. trailers userProvidedTrailers: HPACKHeaders
  586. ) -> HPACKHeaders {
  587. return HTTP2ToRawGRPCStateMachine.makeResponseTrailersOnly(
  588. for: status,
  589. contentType: self.contentType,
  590. acceptableRequestEncoding: self.acceptEncoding,
  591. userProvidedHeaders: userProvidedTrailers,
  592. normalizeUserProvidedHeaders: self.normalizeHeaders
  593. )
  594. }
  595. }
  596. extension HTTP2ToRawGRPCStateMachine.RequestClosedResponseIdleState {
  597. func send(
  598. status: GRPCStatus,
  599. trailers userProvidedTrailers: HPACKHeaders
  600. ) -> HPACKHeaders {
  601. return HTTP2ToRawGRPCStateMachine.makeResponseTrailersOnly(
  602. for: status,
  603. contentType: self.contentType,
  604. acceptableRequestEncoding: self.acceptEncoding,
  605. userProvidedHeaders: userProvidedTrailers,
  606. normalizeUserProvidedHeaders: self.normalizeHeaders
  607. )
  608. }
  609. }
  610. extension HTTP2ToRawGRPCStateMachine.RequestClosedResponseOpenState {
  611. func send(
  612. status: GRPCStatus,
  613. trailers userProvidedTrailers: HPACKHeaders
  614. ) -> HPACKHeaders {
  615. return HTTP2ToRawGRPCStateMachine.makeResponseTrailers(
  616. for: status,
  617. userProvidedHeaders: userProvidedTrailers,
  618. normalizeUserProvidedHeaders: true
  619. )
  620. }
  621. }
  622. extension HTTP2ToRawGRPCStateMachine.RequestOpenResponseOpenState {
  623. func send(
  624. status: GRPCStatus,
  625. trailers userProvidedTrailers: HPACKHeaders
  626. ) -> HPACKHeaders {
  627. return HTTP2ToRawGRPCStateMachine.makeResponseTrailers(
  628. for: status,
  629. userProvidedHeaders: userProvidedTrailers,
  630. normalizeUserProvidedHeaders: true
  631. )
  632. }
  633. }
  634. // MARK: - Pipeline Configured
  635. extension HTTP2ToRawGRPCStateMachine.RequestOpenResponseIdleState {
  636. mutating func pipelineConfigured() -> HTTP2ToRawGRPCStateMachine.PipelineConfiguredAction {
  637. let headers = self.configurationState.configured()
  638. let action: HTTP2ToRawGRPCStateMachine.PipelineConfiguredAction
  639. // If there are unprocessed bytes then we need to read messages as well.
  640. let hasUnprocessedBytes = self.reader.unprocessedBytes != 0
  641. if hasUnprocessedBytes {
  642. // If there are unprocessed bytes, we need to try to read after sending the metadata.
  643. action = .forwardHeadersAndRead(headers)
  644. } else {
  645. // No unprocessed bytes; the reader is empty. Just send the metadata.
  646. action = .forwardHeaders(headers)
  647. }
  648. return action
  649. }
  650. }
  651. extension HTTP2ToRawGRPCStateMachine.RequestClosedResponseIdleState {
  652. mutating func pipelineConfigured() -> HTTP2ToRawGRPCStateMachine.PipelineConfiguredAction {
  653. let headers = self.configurationState.configured()
  654. // Since we're already closed, we need to forward the headers and start reading.
  655. return .forwardHeadersAndRead(headers)
  656. }
  657. }
  658. // MARK: - Read Next Request
  659. extension HTTP2ToRawGRPCStateMachine {
  660. static func read(
  661. from reader: inout LengthPrefixedMessageReader,
  662. requestStreamClosed: Bool
  663. ) -> HTTP2ToRawGRPCStateMachine.ReadNextMessageAction {
  664. do {
  665. // Try to read a message.
  666. guard let buffer = try reader.nextMessage() else {
  667. // We didn't read a message: if we're closed then there's no chance of receiving more bytes,
  668. // just forward the end of stream. If we're not closed then we could receive more bytes so
  669. // there's no need to take any action at this point.
  670. return requestStreamClosed ? .forwardEnd : .none
  671. }
  672. guard reader.unprocessedBytes == 0 else {
  673. // There are still unprocessed bytes, continue reading.
  674. return .forwardMessageThenReadNextMessage(buffer)
  675. }
  676. // If we're closed and there's nothing left to read, then we're done, forward the message and
  677. // end of stream. If we're closed we could still receive more bytes (or end stream) so just
  678. // forward the message.
  679. return requestStreamClosed ? .forwardMessageAndEnd(buffer) : .forwardMessage(buffer)
  680. } catch {
  681. return .errorCaught(error)
  682. }
  683. }
  684. }
  685. extension HTTP2ToRawGRPCStateMachine.RequestOpenResponseIdleState {
  686. mutating func readNextRequest() -> HTTP2ToRawGRPCStateMachine.ReadNextMessageAction {
  687. return HTTP2ToRawGRPCStateMachine.read(from: &self.reader, requestStreamClosed: false)
  688. }
  689. }
  690. extension HTTP2ToRawGRPCStateMachine.RequestOpenResponseOpenState {
  691. mutating func readNextRequest() -> HTTP2ToRawGRPCStateMachine.ReadNextMessageAction {
  692. return HTTP2ToRawGRPCStateMachine.read(from: &self.reader, requestStreamClosed: false)
  693. }
  694. }
  695. extension HTTP2ToRawGRPCStateMachine.RequestClosedResponseIdleState {
  696. mutating func readNextRequest() -> HTTP2ToRawGRPCStateMachine.ReadNextMessageAction {
  697. return HTTP2ToRawGRPCStateMachine.read(from: &self.reader, requestStreamClosed: true)
  698. }
  699. }
  700. extension HTTP2ToRawGRPCStateMachine.RequestClosedResponseOpenState {
  701. mutating func readNextRequest() -> HTTP2ToRawGRPCStateMachine.ReadNextMessageAction {
  702. return HTTP2ToRawGRPCStateMachine.read(from: &self.reader, requestStreamClosed: true)
  703. }
  704. }
  705. // MARK: - Top Level State Changes
  706. extension HTTP2ToRawGRPCStateMachine {
  707. /// Receive request headers.
  708. mutating func receive(
  709. headers: HPACKHeaders,
  710. eventLoop: EventLoop,
  711. errorDelegate: ServerErrorDelegate?,
  712. remoteAddress: SocketAddress?,
  713. logger: Logger,
  714. allocator: ByteBufferAllocator,
  715. responseWriter: GRPCServerResponseWriter,
  716. closeFuture: EventLoopFuture<Void>
  717. ) -> ReceiveHeadersAction {
  718. return self.withStateAvoidingCoWs { state in
  719. state.receive(
  720. headers: headers,
  721. eventLoop: eventLoop,
  722. errorDelegate: errorDelegate,
  723. remoteAddress: remoteAddress,
  724. logger: logger,
  725. allocator: allocator,
  726. responseWriter: responseWriter,
  727. closeFuture: closeFuture
  728. )
  729. }
  730. }
  731. /// Receive request buffer.
  732. /// - Parameters:
  733. /// - buffer: The received buffer.
  734. /// - endStream: Whether end stream was set.
  735. /// - Returns: Returns whether the caller should try to read a message from the buffer.
  736. mutating func receive(buffer: inout ByteBuffer, endStream: Bool) -> Bool {
  737. return self.withStateAvoidingCoWs { state in
  738. state.receive(buffer: &buffer, endStream: endStream)
  739. }
  740. }
  741. /// Send response headers.
  742. mutating func send(headers: HPACKHeaders) -> Result<HPACKHeaders, Error> {
  743. return self.withStateAvoidingCoWs { state in
  744. state.send(headers: headers)
  745. }
  746. }
  747. /// Send a response buffer.
  748. func send(
  749. buffer: ByteBuffer,
  750. allocator: ByteBufferAllocator,
  751. compress: Bool
  752. ) -> Result<ByteBuffer, Error> {
  753. return self.state.send(buffer: buffer, allocator: allocator, compress: compress)
  754. }
  755. /// Send status and trailers.
  756. mutating func send(
  757. status: GRPCStatus,
  758. trailers: HPACKHeaders
  759. ) -> Result<HPACKHeaders, Error> {
  760. return self.withStateAvoidingCoWs { state in
  761. state.send(status: status, trailers: trailers)
  762. }
  763. }
  764. /// The pipeline has been configured with a service provider.
  765. mutating func pipelineConfigured() -> PipelineConfiguredAction {
  766. return self.withStateAvoidingCoWs { state in
  767. state.pipelineConfigured()
  768. }
  769. }
  770. /// Try to read a request message.
  771. mutating func readNextRequest() -> ReadNextMessageAction {
  772. return self.withStateAvoidingCoWs { state in
  773. state.readNextRequest()
  774. }
  775. }
  776. }
  777. extension HTTP2ToRawGRPCStateMachine.State {
  778. mutating func pipelineConfigured() -> HTTP2ToRawGRPCStateMachine.PipelineConfiguredAction {
  779. switch self {
  780. case .requestIdleResponseIdle:
  781. preconditionFailure("Invalid state: pipeline configured before receiving request headers")
  782. case var .requestOpenResponseIdle(state):
  783. let action = state.pipelineConfigured()
  784. self = .requestOpenResponseIdle(state)
  785. return action
  786. case var .requestClosedResponseIdle(state):
  787. let action = state.pipelineConfigured()
  788. self = .requestClosedResponseIdle(state)
  789. return action
  790. case .requestOpenResponseOpen,
  791. .requestClosedResponseOpen,
  792. .requestClosedResponseClosed:
  793. preconditionFailure("Invalid state: response stream opened before pipeline was configured")
  794. case ._modifying:
  795. preconditionFailure("Left in modifying state")
  796. }
  797. }
  798. mutating func receive(
  799. headers: HPACKHeaders,
  800. eventLoop: EventLoop,
  801. errorDelegate: ServerErrorDelegate?,
  802. remoteAddress: SocketAddress?,
  803. logger: Logger,
  804. allocator: ByteBufferAllocator,
  805. responseWriter: GRPCServerResponseWriter,
  806. closeFuture: EventLoopFuture<Void>
  807. ) -> HTTP2ToRawGRPCStateMachine.ReceiveHeadersAction {
  808. switch self {
  809. // This is the only state in which we can receive headers. Everything else is invalid.
  810. case let .requestIdleResponseIdle(state):
  811. let stateAndAction = state.receive(
  812. headers: headers,
  813. eventLoop: eventLoop,
  814. errorDelegate: errorDelegate,
  815. remoteAddress: remoteAddress,
  816. logger: logger,
  817. allocator: allocator,
  818. responseWriter: responseWriter,
  819. closeFuture: closeFuture
  820. )
  821. self = stateAndAction.state
  822. return stateAndAction.action
  823. // We can't receive headers in any of these states.
  824. case .requestOpenResponseIdle,
  825. .requestOpenResponseOpen,
  826. .requestClosedResponseIdle,
  827. .requestClosedResponseOpen,
  828. .requestClosedResponseClosed:
  829. preconditionFailure("Invalid state")
  830. case ._modifying:
  831. preconditionFailure("Left in modifying state")
  832. }
  833. }
  834. /// Receive a buffer from the client.
  835. mutating func receive(buffer: inout ByteBuffer, endStream: Bool) -> Bool {
  836. switch self {
  837. case .requestIdleResponseIdle:
  838. /// This isn't allowed: we must receive the request headers first.
  839. preconditionFailure("Invalid state")
  840. case var .requestOpenResponseIdle(state):
  841. let stateAndAction = state.receive(buffer: &buffer, endStream: endStream)
  842. self = stateAndAction.state
  843. return stateAndAction.tryReading
  844. case var .requestOpenResponseOpen(state):
  845. let stateAndAction = state.receive(buffer: &buffer, endStream: endStream)
  846. self = stateAndAction.state
  847. return stateAndAction.tryReading
  848. case .requestClosedResponseIdle,
  849. .requestClosedResponseOpen:
  850. preconditionFailure("Invalid state: the request stream is already closed")
  851. case .requestClosedResponseClosed:
  852. // This is okay: we could have closed before receiving end.
  853. return false
  854. case ._modifying:
  855. preconditionFailure("Left in modifying state")
  856. }
  857. }
  858. mutating func readNextRequest() -> HTTP2ToRawGRPCStateMachine.ReadNextMessageAction {
  859. switch self {
  860. case .requestIdleResponseIdle:
  861. preconditionFailure("Invalid state")
  862. case var .requestOpenResponseIdle(state):
  863. let action = state.readNextRequest()
  864. self = .requestOpenResponseIdle(state)
  865. return action
  866. case var .requestOpenResponseOpen(state):
  867. let action = state.readNextRequest()
  868. self = .requestOpenResponseOpen(state)
  869. return action
  870. case var .requestClosedResponseIdle(state):
  871. let action = state.readNextRequest()
  872. self = .requestClosedResponseIdle(state)
  873. return action
  874. case var .requestClosedResponseOpen(state):
  875. let action = state.readNextRequest()
  876. self = .requestClosedResponseOpen(state)
  877. return action
  878. case .requestClosedResponseClosed:
  879. return .none
  880. case ._modifying:
  881. preconditionFailure("Left in modifying state")
  882. }
  883. }
  884. mutating func send(headers: HPACKHeaders) -> Result<HPACKHeaders, Error> {
  885. switch self {
  886. case .requestIdleResponseIdle:
  887. preconditionFailure("Invalid state: the request stream isn't open")
  888. case let .requestOpenResponseIdle(state):
  889. let headers = state.send(headers: headers)
  890. self = .requestOpenResponseOpen(.init(from: state))
  891. return .success(headers)
  892. case let .requestClosedResponseIdle(state):
  893. let headers = state.send(headers: headers)
  894. self = .requestClosedResponseOpen(.init(from: state))
  895. return .success(headers)
  896. case .requestOpenResponseOpen,
  897. .requestClosedResponseOpen,
  898. .requestClosedResponseClosed:
  899. return .failure(GRPCError.AlreadyComplete())
  900. case ._modifying:
  901. preconditionFailure("Left in modifying state")
  902. }
  903. }
  904. func send(
  905. buffer: ByteBuffer,
  906. allocator: ByteBufferAllocator,
  907. compress: Bool
  908. ) -> Result<ByteBuffer, Error> {
  909. switch self {
  910. case .requestIdleResponseIdle:
  911. preconditionFailure("Invalid state: the request stream is still closed")
  912. case .requestOpenResponseIdle,
  913. .requestClosedResponseIdle:
  914. let error = GRPCError.InvalidState("Response headers must be sent before response message")
  915. return .failure(error)
  916. case let .requestOpenResponseOpen(state):
  917. return state.send(
  918. buffer: buffer,
  919. allocator: allocator,
  920. compress: compress
  921. )
  922. case let .requestClosedResponseOpen(state):
  923. return state.send(
  924. buffer: buffer,
  925. allocator: allocator,
  926. compress: compress
  927. )
  928. case .requestClosedResponseClosed:
  929. return .failure(GRPCError.AlreadyComplete())
  930. case ._modifying:
  931. preconditionFailure("Left in modifying state")
  932. }
  933. }
  934. mutating func send(
  935. status: GRPCStatus,
  936. trailers: HPACKHeaders
  937. ) -> Result<HPACKHeaders, Error> {
  938. switch self {
  939. case .requestIdleResponseIdle:
  940. preconditionFailure("Invalid state: the request stream is still closed")
  941. case let .requestOpenResponseIdle(state):
  942. self = .requestClosedResponseClosed
  943. return .success(state.send(status: status, trailers: trailers))
  944. case let .requestClosedResponseIdle(state):
  945. self = .requestClosedResponseClosed
  946. return .success(state.send(status: status, trailers: trailers))
  947. case let .requestOpenResponseOpen(state):
  948. self = .requestClosedResponseClosed
  949. return .success(state.send(status: status, trailers: trailers))
  950. case let .requestClosedResponseOpen(state):
  951. self = .requestClosedResponseClosed
  952. return .success(state.send(status: status, trailers: trailers))
  953. case .requestClosedResponseClosed:
  954. return .failure(GRPCError.AlreadyComplete())
  955. case ._modifying:
  956. preconditionFailure("Left in modifying state")
  957. }
  958. }
  959. }
  960. // MARK: - Helpers
  961. extension HTTP2ToRawGRPCStateMachine {
  962. static func makeResponseHeaders(
  963. contentType: ContentType,
  964. responseEncoding: String?,
  965. acceptableRequestEncoding: String?,
  966. userProvidedHeaders: HPACKHeaders,
  967. normalizeUserProvidedHeaders: Bool
  968. ) -> HPACKHeaders {
  969. // 4 because ':status' and 'content-type' are required. We may send back 'grpc-encoding' and
  970. // 'grpc-accept-encoding' as well.
  971. let capacity = 4 + userProvidedHeaders.count
  972. var headers = HPACKHeaders()
  973. headers.reserveCapacity(capacity)
  974. headers.add(name: ":status", value: "200")
  975. headers.add(name: GRPCHeaderName.contentType, value: contentType.canonicalValue)
  976. if let responseEncoding = responseEncoding {
  977. headers.add(name: GRPCHeaderName.encoding, value: responseEncoding)
  978. }
  979. if let acceptEncoding = acceptableRequestEncoding {
  980. headers.add(name: GRPCHeaderName.acceptEncoding, value: acceptEncoding)
  981. }
  982. // Add user provided headers, normalizing if required.
  983. headers.add(contentsOf: userProvidedHeaders, normalize: normalizeUserProvidedHeaders)
  984. return headers
  985. }
  986. static func makeResponseTrailersOnly(
  987. for status: GRPCStatus,
  988. contentType: ContentType,
  989. acceptableRequestEncoding: String?,
  990. userProvidedHeaders: HPACKHeaders?,
  991. normalizeUserProvidedHeaders: Bool
  992. ) -> HPACKHeaders {
  993. // 5 because ':status', 'content-type', 'grpc-status' are required. We may also send back
  994. // 'grpc-message' and 'grpc-accept-encoding'.
  995. let capacity = 5 + (userProvidedHeaders.map { $0.count } ?? 0)
  996. var headers = HPACKHeaders()
  997. headers.reserveCapacity(capacity)
  998. // Add the required trailers.
  999. headers.add(name: ":status", value: "200")
  1000. headers.add(name: GRPCHeaderName.contentType, value: contentType.canonicalValue)
  1001. headers.add(name: GRPCHeaderName.statusCode, value: String(describing: status.code.rawValue))
  1002. if let message = status.message.flatMap(GRPCStatusMessageMarshaller.marshall) {
  1003. headers.add(name: GRPCHeaderName.statusMessage, value: message)
  1004. }
  1005. // We may include this if the requested encoding was not valid.
  1006. if let acceptEncoding = acceptableRequestEncoding {
  1007. headers.add(name: GRPCHeaderName.acceptEncoding, value: acceptEncoding)
  1008. }
  1009. if let userProvided = userProvidedHeaders {
  1010. headers.add(contentsOf: userProvided, normalize: normalizeUserProvidedHeaders)
  1011. }
  1012. return headers
  1013. }
  1014. static func makeResponseTrailers(
  1015. for status: GRPCStatus,
  1016. userProvidedHeaders: HPACKHeaders,
  1017. normalizeUserProvidedHeaders: Bool
  1018. ) -> HPACKHeaders {
  1019. // 2 because 'grpc-status' is required, we may also send back 'grpc-message'.
  1020. let capacity = 2 + userProvidedHeaders.count
  1021. var trailers = HPACKHeaders()
  1022. trailers.reserveCapacity(capacity)
  1023. // status code.
  1024. trailers.add(name: GRPCHeaderName.statusCode, value: String(describing: status.code.rawValue))
  1025. // status message, if present.
  1026. if let message = status.message.flatMap(GRPCStatusMessageMarshaller.marshall) {
  1027. trailers.add(name: GRPCHeaderName.statusMessage, value: message)
  1028. }
  1029. // user provided trailers.
  1030. trailers.add(contentsOf: userProvidedHeaders, normalize: normalizeUserProvidedHeaders)
  1031. return trailers
  1032. }
  1033. }
  1034. private extension HPACKHeaders {
  1035. mutating func add(contentsOf other: HPACKHeaders, normalize: Bool) {
  1036. if normalize {
  1037. self.add(contentsOf: other.lazy.map { name, value, indexable in
  1038. (name: name.lowercased(), value: value, indexable: indexable)
  1039. })
  1040. } else {
  1041. self.add(contentsOf: other)
  1042. }
  1043. }
  1044. }