ServerCallContext.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 Logging
  18. import NIO
  19. import NIOHPACK
  20. import NIOHTTP1
  21. import SwiftProtobuf
  22. /// Protocol declaring a minimum set of properties exposed by *all* types of call contexts.
  23. public protocol ServerCallContext: AnyObject {
  24. /// The event loop this call is served on.
  25. var eventLoop: EventLoop { get }
  26. /// Request headers for this request.
  27. var headers: HPACKHeaders { get }
  28. /// The logger used for this call.
  29. var logger: Logger { get }
  30. /// Whether compression should be enabled for responses, defaulting to `true`. Note that for
  31. /// this value to take effect compression must have been enabled on the server and a compression
  32. /// algorithm must have been negotiated with the client.
  33. var compressionEnabled: Bool { get set }
  34. }
  35. /// Base class providing data provided to the framework user for all server calls.
  36. open class ServerCallContextBase: ServerCallContext {
  37. public let eventLoop: EventLoop
  38. public let headers: HPACKHeaders
  39. public let logger: Logger
  40. public var compressionEnabled: Bool = true
  41. /// Metadata to return at the end of the RPC. If this is required it should be updated before
  42. /// the `responsePromise` or `statusPromise` is fulfilled.
  43. public var trailers = HPACKHeaders()
  44. public init(eventLoop: EventLoop, headers: HPACKHeaders, logger: Logger) {
  45. self.eventLoop = eventLoop
  46. self.headers = headers
  47. self.logger = logger
  48. }
  49. @available(*, deprecated, renamed: "init(eventLoop:headers:logger:)")
  50. public init(eventLoop: EventLoop, request: HTTPRequestHead, logger: Logger) {
  51. self.eventLoop = eventLoop
  52. self.headers = HPACKHeaders(httpHeaders: request.headers, normalizeHTTPHeaders: false)
  53. self.logger = logger
  54. }
  55. /// Processes an error, transforming it into a 'GRPCStatus' and any trailers to send to the peer.
  56. internal func processError(
  57. _ error: Error,
  58. delegate: ServerErrorDelegate?
  59. ) -> (GRPCStatus, HPACKHeaders) {
  60. // Observe the error if we have a delegate.
  61. delegate?.observeRequestHandlerError(error, headers: self.headers)
  62. // What status are we terminating this RPC with?
  63. // - If we have a delegate, try transforming the error. If the delegate returns trailers, merge
  64. // them with any on the call context.
  65. // - If we don't have a delegate, then try to transform the error to a status.
  66. // - Fallback to a generic error.
  67. let status: GRPCStatus
  68. let trailers: HPACKHeaders
  69. if let transformed = delegate?.transformRequestHandlerError(error, headers: self.headers) {
  70. status = transformed.status
  71. if var transformedTrailers = transformed.trailers {
  72. // The delegate returned trailers: merge in those from the context as well.
  73. transformedTrailers.add(contentsOf: self.trailers)
  74. trailers = transformedTrailers
  75. } else {
  76. trailers = self.trailers
  77. }
  78. } else if let grpcStatusTransformable = error as? GRPCStatusTransformable {
  79. status = grpcStatusTransformable.makeGRPCStatus()
  80. trailers = self.trailers
  81. } else {
  82. // Eh... well, we don't what status to use. Use a generic one.
  83. status = .processingError
  84. trailers = self.trailers
  85. }
  86. return (status, trailers)
  87. }
  88. }