1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2017-2021 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import CNIOSHA1
import NIOCore
import NIOHTTP1
let magicWebSocketGUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
@available(*, deprecated, renamed: "NIOWebSocketServerUpgrader")
public typealias WebSocketUpgrader = NIOWebSocketServerUpgrader
/// Errors that can be thrown by `NIOWebSocket` during protocol upgrade.
public struct NIOWebSocketUpgradeError: Error, Equatable {
private enum ActualError {
case invalidUpgradeHeader
case unsupportedWebSocketTarget
}
private let actualError: ActualError
private init(actualError: ActualError) {
self.actualError = actualError
}
/// A HTTP header on the upgrade request was invalid.
public static let invalidUpgradeHeader = NIOWebSocketUpgradeError(actualError: .invalidUpgradeHeader)
/// The HTTP request targets a websocket pipeline that does not support
/// it in some way.
public static let unsupportedWebSocketTarget = NIOWebSocketUpgradeError(actualError: .unsupportedWebSocketTarget)
}
fileprivate extension HTTPHeaders {
func nonListHeader(_ name: String) throws -> String {
let fields = self[canonicalForm: name]
guard fields.count == 1 else {
throw NIOWebSocketUpgradeError.invalidUpgradeHeader
}
return String(fields.first!)
}
}
/// A `HTTPServerProtocolUpgrader` that knows how to do the WebSocket upgrade dance.
///
/// Users may frequently want to offer multiple websocket endpoints on the same port. For this
/// reason, this `WebServerSocketUpgrader` only knows how to do the required parts of the upgrade and to
/// complete the handshake. Users are expected to provide a callback that examines the HTTP headers
/// (including the path) and determines whether this is a websocket upgrade request that is acceptable
/// to them.
///
/// This upgrader assumes that the `HTTPServerUpgradeHandler` will appropriately mutate the pipeline to
/// remove the HTTP `ChannelHandler`s.
public final class NIOWebSocketServerUpgrader: HTTPServerProtocolUpgrader, @unchecked Sendable {
// This type *is* Sendable but we can't express that properly until Swift 5.7. In the meantime
// the conformance is `@unchecked`.
// FIXME: remove @unchecked when 5.7 is the minimum supported version.
private typealias ShouldUpgrade = @Sendable (Channel, HTTPRequestHead) -> EventLoopFuture<HTTPHeaders?>
private typealias UpgradePipelineHandler = @Sendable (Channel, HTTPRequestHead) -> EventLoopFuture<Void>
/// RFC 6455 specs this as the required entry in the Upgrade header.
public let supportedProtocol: String = "websocket"
/// We deliberately do not actually set any required headers here, because the websocket
/// spec annoyingly does not actually force the client to send these in the Upgrade header,
/// which NIO requires. We check for these manually.
public let requiredUpgradeHeaders: [String] = []
private let shouldUpgrade: ShouldUpgrade
private let upgradePipelineHandler: UpgradePipelineHandler
private let maxFrameSize: Int
private let automaticErrorHandling: Bool
/// Create a new `NIOWebSocketServerUpgrader`.
///
/// - parameters:
/// - automaticErrorHandling: Whether the pipeline should automatically handle protocol
/// errors by sending error responses and closing the connection. Defaults to `true`,
/// may be set to `false` if the user wishes to handle their own errors.
/// - shouldUpgrade: A callback that determines whether the websocket request should be
/// upgraded. This callback is responsible for creating a `HTTPHeaders` object with
/// any headers that it needs on the response *except for* the `Upgrade`, `Connection`,
/// and `Sec-WebSocket-Accept` headers, which this upgrader will handle. Should return
/// an `EventLoopFuture` containing `nil` if the upgrade should be refused.
/// - upgradePipelineHandler: A function that will be called once the upgrade response is
/// flushed, and that is expected to mutate the `Channel` appropriately to handle the
/// websocket protocol. This only needs to add the user handlers: the
/// `WebSocketFrameEncoder` and `WebSocketFrameDecoder` will have been added to the
/// pipeline automatically.
@preconcurrency
public convenience init(
automaticErrorHandling: Bool = true,
shouldUpgrade: @escaping @Sendable (Channel, HTTPRequestHead) -> EventLoopFuture<HTTPHeaders?>,
upgradePipelineHandler: @escaping @Sendable (Channel, HTTPRequestHead) -> EventLoopFuture<Void>
) {
self.init(maxFrameSize: 1 << 14, automaticErrorHandling: automaticErrorHandling,
shouldUpgrade: shouldUpgrade, upgradePipelineHandler: upgradePipelineHandler)
}
/// Create a new `NIOWebSocketServerUpgrader`.
///
/// - parameters:
/// - maxFrameSize: The maximum frame size the decoder is willing to tolerate from the
/// remote peer. WebSockets in principle allows frame sizes up to `2**64` bytes, but
/// this is an objectively unreasonable maximum value (on AMD64 systems it is not
/// possible to even. Users may set this to any value up to `UInt32.max`.
/// - automaticErrorHandling: Whether the pipeline should automatically handle protocol
/// errors by sending error responses and closing the connection. Defaults to `true`,
/// may be set to `false` if the user wishes to handle their own errors.
/// - shouldUpgrade: A callback that determines whether the websocket request should be
/// upgraded. This callback is responsible for creating a `HTTPHeaders` object with
/// any headers that it needs on the response *except for* the `Upgrade`, `Connection`,
/// and `Sec-WebSocket-Accept` headers, which this upgrader will handle. Should return
/// an `EventLoopFuture` containing `nil` if the upgrade should be refused.
/// - upgradePipelineHandler: A function that will be called once the upgrade response is
/// flushed, and that is expected to mutate the `Channel` appropriately to handle the
/// websocket protocol. This only needs to add the user handlers: the
/// `WebSocketFrameEncoder` and `WebSocketFrameDecoder` will have been added to the
/// pipeline automatically.
@preconcurrency
public convenience init(
maxFrameSize: Int,
automaticErrorHandling: Bool = true,
shouldUpgrade: @escaping @Sendable (Channel, HTTPRequestHead) -> EventLoopFuture<HTTPHeaders?>,
upgradePipelineHandler: @escaping @Sendable (Channel, HTTPRequestHead) -> EventLoopFuture<Void>
) {
self.init(
_maxFrameSize: maxFrameSize,
automaticErrorHandling: automaticErrorHandling,
shouldUpgrade: shouldUpgrade,
upgradePipelineHandler: upgradePipelineHandler
)
}
private init(
_maxFrameSize maxFrameSize: Int,
automaticErrorHandling: Bool,
shouldUpgrade: @escaping ShouldUpgrade,
upgradePipelineHandler: @escaping UpgradePipelineHandler
) {
precondition(maxFrameSize <= UInt32.max, "invalid overlarge max frame size")
self.shouldUpgrade = shouldUpgrade
self.upgradePipelineHandler = upgradePipelineHandler
self.maxFrameSize = maxFrameSize
self.automaticErrorHandling = automaticErrorHandling
}
public func buildUpgradeResponse(channel: Channel, upgradeRequest: HTTPRequestHead, initialResponseHeaders: HTTPHeaders) -> EventLoopFuture<HTTPHeaders> {
return _buildUpgradeResponse(
channel: channel,
upgradeRequest: upgradeRequest,
initialResponseHeaders: initialResponseHeaders,
shouldUpgrade: self.shouldUpgrade
)
}
public func upgrade(context: ChannelHandlerContext, upgradeRequest: HTTPRequestHead) -> EventLoopFuture<Void> {
_upgrade(
channel: context.channel,
upgradeRequest: upgradeRequest,
maxFrameSize: self.maxFrameSize,
automaticErrorHandling: self.automaticErrorHandling,
upgradePipelineHandler: self.upgradePipelineHandler
)
}
}
#if !canImport(Darwin) || swift(>=5.10)
/// A `NIOTypedHTTPServerProtocolUpgrader` that knows how to do the WebSocket upgrade dance.
///
/// Users may frequently want to offer multiple websocket endpoints on the same port. For this
/// reason, this `WebServerSocketUpgrader` only knows how to do the required parts of the upgrade and to
/// complete the handshake. Users are expected to provide a callback that examines the HTTP headers
/// (including the path) and determines whether this is a websocket upgrade request that is acceptable
/// to them.
///
/// This upgrader assumes that the `HTTPServerUpgradeHandler` will appropriately mutate the pipeline to
/// remove the HTTP `ChannelHandler`s.
public final class NIOTypedWebSocketServerUpgrader<UpgradeResult: Sendable>: NIOTypedHTTPServerProtocolUpgrader, Sendable {
private typealias ShouldUpgrade = @Sendable (Channel, HTTPRequestHead) -> EventLoopFuture<HTTPHeaders?>
private typealias UpgradePipelineHandler = @Sendable (Channel, HTTPRequestHead) -> EventLoopFuture<UpgradeResult>
/// RFC 6455 specs this as the required entry in the Upgrade header.
public let supportedProtocol: String = "websocket"
/// We deliberately do not actually set any required headers here, because the websocket
/// spec annoyingly does not actually force the client to send these in the Upgrade header,
/// which NIO requires. We check for these manually.
public let requiredUpgradeHeaders: [String] = []
private let shouldUpgrade: ShouldUpgrade
private let upgradePipelineHandler: UpgradePipelineHandler
private let maxFrameSize: Int
private let enableAutomaticErrorHandling: Bool
/// Create a new ``NIOTypedWebSocketServerUpgrader``.
///
/// - Parameters:
/// - maxFrameSize: The maximum frame size the decoder is willing to tolerate from the
/// remote peer. WebSockets in principle allows frame sizes up to `2**64` bytes, but
/// this is an objectively unreasonable maximum value (on AMD64 systems it is not
/// possible to even. Users may set this to any value up to `UInt32.max`.
/// - automaticErrorHandling: Whether the pipeline should automatically handle protocol
/// errors by sending error responses and closing the connection. Defaults to `true`,
/// may be set to `false` if the user wishes to handle their own errors.
/// - shouldUpgrade: A callback that determines whether the websocket request should be
/// upgraded. This callback is responsible for creating a `HTTPHeaders` object with
/// any headers that it needs on the response *except for* the `Upgrade`, `Connection`,
/// and `Sec-WebSocket-Accept` headers, which this upgrader will handle. Should return
/// an `EventLoopFuture` containing `nil` if the upgrade should be refused.
/// - enableAutomaticErrorHandling: A function that will be called once the upgrade response is
/// flushed, and that is expected to mutate the `Channel` appropriately to handle the
/// websocket protocol. This only needs to add the user handlers: the
/// `WebSocketFrameEncoder` and `WebSocketFrameDecoder` will have been added to the
/// pipeline automatically.
public init(
maxFrameSize: Int = 1 << 14,
enableAutomaticErrorHandling: Bool = true,
shouldUpgrade: @escaping @Sendable (Channel, HTTPRequestHead) -> EventLoopFuture<HTTPHeaders?>,
upgradePipelineHandler: @escaping @Sendable (Channel, HTTPRequestHead) -> EventLoopFuture<UpgradeResult>
) {
precondition(maxFrameSize <= UInt32.max, "invalid overlarge max frame size")
self.shouldUpgrade = shouldUpgrade
self.upgradePipelineHandler = upgradePipelineHandler
self.maxFrameSize = maxFrameSize
self.enableAutomaticErrorHandling = enableAutomaticErrorHandling
}
public func buildUpgradeResponse(
channel: Channel,
upgradeRequest: HTTPRequestHead,
initialResponseHeaders: HTTPHeaders
) -> EventLoopFuture<HTTPHeaders> {
_buildUpgradeResponse(
channel: channel,
upgradeRequest: upgradeRequest,
initialResponseHeaders: initialResponseHeaders,
shouldUpgrade: self.shouldUpgrade
)
}
public func upgrade(channel: Channel, upgradeRequest: HTTPRequestHead) -> EventLoopFuture<UpgradeResult> {
_upgrade(
channel: channel,
upgradeRequest: upgradeRequest,
maxFrameSize: self.maxFrameSize,
automaticErrorHandling: self.enableAutomaticErrorHandling,
upgradePipelineHandler: self.upgradePipelineHandler
)
}
}
#endif
private func _buildUpgradeResponse(
channel: Channel,
upgradeRequest: HTTPRequestHead,
initialResponseHeaders: HTTPHeaders,
shouldUpgrade: @Sendable (Channel, HTTPRequestHead) -> EventLoopFuture<HTTPHeaders?>
) -> EventLoopFuture<HTTPHeaders> {
let key: String
let version: String
do {
key = try upgradeRequest.headers.nonListHeader("Sec-WebSocket-Key")
version = try upgradeRequest.headers.nonListHeader("Sec-WebSocket-Version")
} catch {
return channel.eventLoop.makeFailedFuture(error)
}
// The version must be 13.
guard version == "13" else {
return channel.eventLoop.makeFailedFuture(NIOWebSocketUpgradeError.invalidUpgradeHeader)
}
return shouldUpgrade(channel, upgradeRequest)
.flatMapThrowing { extraHeaders in
guard var extraHeaders = extraHeaders else {
throw NIOWebSocketUpgradeError.unsupportedWebSocketTarget
}
// Cool, we're good to go! Let's do our upgrade. We do this by concatenating the magic
// GUID to the base64-encoded key and taking a SHA1 hash of the result.
let acceptValue: String
do {
var hasher = SHA1()
hasher.update(string: key)
hasher.update(string: magicWebSocketGUID)
acceptValue = String(base64Encoding: hasher.finish())
}
extraHeaders.replaceOrAdd(name: "Upgrade", value: "websocket")
extraHeaders.add(name: "Sec-WebSocket-Accept", value: acceptValue)
extraHeaders.replaceOrAdd(name: "Connection", value: "upgrade")
return extraHeaders
}
}
private func _upgrade<UpgradeResult>(
channel: Channel,
upgradeRequest: HTTPRequestHead,
maxFrameSize: Int,
automaticErrorHandling: Bool,
upgradePipelineHandler: @escaping @Sendable (Channel, HTTPRequestHead) -> EventLoopFuture<UpgradeResult>
) -> EventLoopFuture<UpgradeResult> {
/// We never use the automatic error handling feature of the WebSocketFrameDecoder: we always use the separate channel
/// handler.
channel.eventLoop.makeCompletedFuture {
try channel.pipeline.syncOperations.addHandler(WebSocketFrameEncoder())
try channel.pipeline.syncOperations.addHandler(
ByteToMessageHandler(WebSocketFrameDecoder(maxFrameSize: maxFrameSize))
)
if automaticErrorHandling {
try channel.pipeline.syncOperations.addHandler(WebSocketProtocolErrorHandler())
}
}.flatMap {
upgradePipelineHandler(channel, upgradeRequest)
}
}
|