ServerCallContext.swift 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 which is shared with the interceptor contexts for this RPC.
  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. /// The event loop this call is served on.
  40. public let eventLoop: EventLoop
  41. /// Request headers for this request.
  42. public let headers: HPACKHeaders
  43. /// The logger used for this call.
  44. public let logger: Logger
  45. /// Whether compression should be enabled for responses, defaulting to `true`. Note that for
  46. /// this value to take effect compression must have been enabled on the server and a compression
  47. /// algorithm must have been negotiated with the client.
  48. public var compressionEnabled: Bool = true
  49. /// - Important: While `UserInfo` has value-semantics, this property retrieves from, and sets a
  50. /// reference wrapped `UserInfo`. The contexts passed to interceptors provide the same
  51. /// reference. As such this may be used as a mechanism to pass information between interceptors
  52. /// and service providers.
  53. public var userInfo: UserInfo {
  54. get {
  55. return self.userInfoRef.value
  56. }
  57. set {
  58. self.userInfoRef.value = newValue
  59. }
  60. }
  61. /// A reference to an underlying `UserInfo`. We share this with the interceptors.
  62. @usableFromInline
  63. internal let userInfoRef: Ref<UserInfo>
  64. /// Metadata to return at the end of the RPC. If this is required it should be updated before
  65. /// the `responsePromise` or `statusPromise` is fulfilled.
  66. public var trailers = HPACKHeaders()
  67. public convenience init(
  68. eventLoop: EventLoop,
  69. headers: HPACKHeaders,
  70. logger: Logger,
  71. userInfo: UserInfo = UserInfo()
  72. ) {
  73. self.init(eventLoop: eventLoop, headers: headers, logger: logger, userInfoRef: .init(userInfo))
  74. }
  75. @inlinable
  76. internal init(
  77. eventLoop: EventLoop,
  78. headers: HPACKHeaders,
  79. logger: Logger,
  80. userInfoRef: Ref<UserInfo>
  81. ) {
  82. self.eventLoop = eventLoop
  83. self.headers = headers
  84. self.userInfoRef = userInfoRef
  85. self.logger = logger
  86. }
  87. }