ServerCallContext.swift 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. /// A 'UserInfo' dictionary.
  29. var userInfo: UserInfo { get set }
  30. /// The logger used for this call.
  31. var logger: Logger { get }
  32. /// Whether compression should be enabled for responses, defaulting to `true`. Note that for
  33. /// this value to take effect compression must have been enabled on the server and a compression
  34. /// algorithm must have been negotiated with the client.
  35. var compressionEnabled: Bool { get set }
  36. }
  37. /// Base class providing data provided to the framework user for all server calls.
  38. open class ServerCallContextBase: ServerCallContext {
  39. public let eventLoop: EventLoop
  40. public let headers: HPACKHeaders
  41. public let logger: Logger
  42. public var compressionEnabled: Bool = true
  43. /// - Important: While `UserInfo` has value-semantics, this property retrieves from, and sets a
  44. /// reference wrapped `UserInfo`. The contexts passed to interceptors provide the same
  45. /// reference. As such this may be used as a mechanism to pass information between interceptors
  46. /// and service providers.
  47. public var userInfo: UserInfo {
  48. get {
  49. return self.userInfoRef.value
  50. }
  51. set {
  52. self.userInfoRef.value = newValue
  53. }
  54. }
  55. /// A reference to an underlying `UserInfo`. We share this with the interceptors.
  56. @usableFromInline
  57. internal let userInfoRef: Ref<UserInfo>
  58. /// Metadata to return at the end of the RPC. If this is required it should be updated before
  59. /// the `responsePromise` or `statusPromise` is fulfilled.
  60. public var trailers = HPACKHeaders()
  61. public convenience init(
  62. eventLoop: EventLoop,
  63. headers: HPACKHeaders,
  64. logger: Logger,
  65. userInfo: UserInfo = UserInfo()
  66. ) {
  67. self.init(eventLoop: eventLoop, headers: headers, logger: logger, userInfoRef: .init(userInfo))
  68. }
  69. @inlinable
  70. internal init(
  71. eventLoop: EventLoop,
  72. headers: HPACKHeaders,
  73. logger: Logger,
  74. userInfoRef: Ref<UserInfo>
  75. ) {
  76. self.eventLoop = eventLoop
  77. self.headers = headers
  78. self.userInfoRef = userInfoRef
  79. self.logger = logger
  80. }
  81. /// Processes an error, transforming it into a 'GRPCStatus' and any trailers to send to the peer.
  82. internal func processObserverError(
  83. _ error: Error,
  84. delegate: ServerErrorDelegate?
  85. ) -> (GRPCStatus, HPACKHeaders) {
  86. // Observe the error if we have a delegate.
  87. delegate?.observeRequestHandlerError(error, headers: self.headers)
  88. // What status are we terminating this RPC with?
  89. // - If we have a delegate, try transforming the error. If the delegate returns trailers, merge
  90. // them with any on the call context.
  91. // - If we don't have a delegate, then try to transform the error to a status.
  92. // - Fallback to a generic error.
  93. let status: GRPCStatus
  94. let trailers: HPACKHeaders
  95. if let transformed = delegate?.transformRequestHandlerError(error, headers: self.headers) {
  96. status = transformed.status
  97. if var transformedTrailers = transformed.trailers {
  98. // The delegate returned trailers: merge in those from the context as well.
  99. transformedTrailers.add(contentsOf: self.trailers)
  100. trailers = transformedTrailers
  101. } else {
  102. trailers = self.trailers
  103. }
  104. } else if let grpcStatusTransformable = error as? GRPCStatusTransformable {
  105. status = grpcStatusTransformable.makeGRPCStatus()
  106. trailers = self.trailers
  107. } else {
  108. // Eh... well, we don't what status to use. Use a generic one.
  109. status = .processingError
  110. trailers = self.trailers
  111. }
  112. return (status, trailers)
  113. }
  114. }