Skip to content

Commit 0fc5fe2

Browse files
authored
Merge pull request #20 from hummingbird-project/server-name
Add WebSocketClientConfiguration.sniHostname
2 parents 588cd94 + 4bdd2e9 commit 0fc5fe2

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed

Sources/WSClient/WebSocketClient.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Hummingbird server framework project
44
//
5-
// Copyright (c) 2024 the Hummingbird authors
5+
// Copyright (c) 2024-2025 the Hummingbird authors
66
// Licensed under Apache License v2.0
77
//
88
// See LICENSE.txt for license information
@@ -134,7 +134,7 @@ public struct WebSocketClient {
134134
TLSClientChannel(
135135
WebSocketClientChannel(handler: handler, url: url, configuration: self.configuration),
136136
tlsConfiguration: tlsConfiguration,
137-
serverHostname: host
137+
serverHostname: self.configuration.sniHostname ?? host
138138
),
139139
address: .hostname(host, port: port),
140140
eventLoopGroup: self.eventLoopGroup,

Sources/WSClient/WebSocketClientConfiguration.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Hummingbird server framework project
44
//
5-
// Copyright (c) 2023-2024 the Hummingbird authors
5+
// Copyright (c) 2023-2025 the Hummingbird authors
66
// Licensed under Apache License v2.0
77
//
88
// See LICENSE.txt for license information
@@ -29,6 +29,8 @@ public struct WebSocketClientConfiguration: Sendable {
2929
public var autoPing: AutoPingSetup
3030
/// Should text be validated to be UTF8
3131
public var validateUTF8: Bool
32+
/// Hostname used during TLS handshake
33+
public var sniHostname: String?
3234

3335
/// Initialize WebSocketClient configuration
3436
/// - Paramters
@@ -37,19 +39,23 @@ public struct WebSocketClientConfiguration: Sendable {
3739
/// - extensions: WebSocket extensions
3840
/// - autoPing: Automatic Ping configuration
3941
/// - validateUTF8: Should text be checked to see if it is valid UTF8
42+
/// - sniHostname: Hostname used during TLS handshake
43+
@_disfavoredOverload
4044
public init(
4145
maxFrameSize: Int = (1 << 14),
4246
additionalHeaders: HTTPFields = .init(),
4347
extensions: [WebSocketExtensionFactory] = [],
4448
closeTimeout: Duration = .seconds(15),
4549
autoPing: AutoPingSetup = .disabled,
46-
validateUTF8: Bool = false
50+
validateUTF8: Bool = false,
51+
sniHostname: String? = nil
4752
) {
4853
self.maxFrameSize = maxFrameSize
4954
self.additionalHeaders = additionalHeaders
5055
self.extensions = extensions.map { $0.build() }
5156
self.closeTimeout = closeTimeout
5257
self.autoPing = autoPing
5358
self.validateUTF8 = validateUTF8
59+
self.sniHostname = nil
5460
}
5561
}

Tests/WebSocketTests/ClientTests.swift

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Hummingbird server framework project
44
//
5-
// Copyright (c) 2024 the Hummingbird authors
5+
// Copyright (c) 2024-2025 the Hummingbird authors
66
// Licensed under Apache License v2.0
77
//
88
// See LICENSE.txt for license information
@@ -39,4 +39,24 @@ final class WebSocketClientTests: XCTestCase {
3939
}
4040
}
4141
}
42+
43+
func testEchoServerWithSNIHostname() async throws {
44+
let clientLogger = {
45+
var logger = Logger(label: "client")
46+
logger.logLevel = .trace
47+
return logger
48+
}()
49+
try await WebSocketClient.connect(
50+
url: "wss://echo.websocket.org/",
51+
configuration: .init(sniHostname: "echo.websocket.org"),
52+
tlsConfiguration: TLSConfiguration.makeClientConfiguration(),
53+
logger: clientLogger
54+
) { inbound, outbound, _ in
55+
var inboundIterator = inbound.messages(maxSize: .max).makeAsyncIterator()
56+
try await outbound.write(.text("hello"))
57+
if let msg = try await inboundIterator.next() {
58+
print(msg)
59+
}
60+
}
61+
}
4262
}

0 commit comments

Comments
 (0)