Skip to content

Commit 0269808

Browse files
committed
Move sniHostname to WebSocketClientConfiguration
1 parent 608d724 commit 0269808

File tree

3 files changed

+35
-64
lines changed

3 files changed

+35
-64
lines changed

Sources/WSClient/WebSocketClient.swift

Lines changed: 4 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public struct WebSocketClient {
5050
}
5151

5252
enum MultiPlatformTLSConfiguration: Sendable {
53-
case niossl(TLSConfiguration, serverName: String?)
53+
case niossl(TLSConfiguration)
5454
#if canImport(Network)
5555
case ts(TSTLSOptions)
5656
#endif
@@ -91,34 +91,7 @@ public struct WebSocketClient {
9191
self.configuration = configuration
9292
self.eventLoopGroup = eventLoopGroup
9393
self.logger = logger
94-
self.tlsConfiguration = tlsConfiguration.map { .niossl($0, serverName: nil) }
95-
}
96-
97-
/// Initialize websocket client
98-
///
99-
/// - Parametes:
100-
/// - url: URL of websocket
101-
/// - tlsConfiguration: TLS configuration
102-
/// - serverName: Server name indication
103-
/// - handler: WebSocket data handler
104-
/// - maxFrameSize: Max frame size for a single packet
105-
/// - eventLoopGroup: EventLoopGroup to run WebSocket client on
106-
/// - logger: Logger
107-
public init(
108-
url: String,
109-
configuration: WebSocketClientConfiguration = .init(),
110-
tlsConfiguration: TLSConfiguration,
111-
serverName: String,
112-
eventLoopGroup: EventLoopGroup = MultiThreadedEventLoopGroup.singleton,
113-
logger: Logger,
114-
handler: @escaping WebSocketDataHandler<Context>
115-
) {
116-
self.url = .init(url)
117-
self.handler = handler
118-
self.configuration = configuration
119-
self.eventLoopGroup = eventLoopGroup
120-
self.logger = logger
121-
self.tlsConfiguration = .niossl(tlsConfiguration, serverName: serverName)
94+
self.tlsConfiguration = tlsConfiguration.map { .niossl($0) }
12295
}
12396

12497
#if canImport(Network)
@@ -156,12 +129,12 @@ public struct WebSocketClient {
156129
let port = self.url.port ?? (requiresTLS ? 443 : 80)
157130
if requiresTLS {
158131
switch self.tlsConfiguration {
159-
case .niossl(let tlsConfiguration, let serverName):
132+
case .niossl(let tlsConfiguration):
160133
let client = try ClientConnection(
161134
TLSClientChannel(
162135
WebSocketClientChannel(handler: handler, url: url, configuration: self.configuration),
163136
tlsConfiguration: tlsConfiguration,
164-
serverHostname: serverName ?? host
137+
serverHostname: self.configuration.sniHostname ?? host
165138
),
166139
address: .hostname(host, port: port),
167140
eventLoopGroup: self.eventLoopGroup,
@@ -244,38 +217,6 @@ extension WebSocketClient {
244217
return try await ws.run()
245218
}
246219

247-
/// Create websocket client, connect and handle connection
248-
///
249-
/// - Parametes:
250-
/// - url: URL of websocket
251-
/// - tlsConfiguration: TLS configuration
252-
/// - serverName: Server name indication
253-
/// - maxFrameSize: Max frame size for a single packet
254-
/// - eventLoopGroup: EventLoopGroup to run WebSocket client on
255-
/// - logger: Logger
256-
/// - process: Closure handling webSocket
257-
/// - Returns: WebSocket close frame details if server returned any
258-
@discardableResult public static func connect(
259-
url: String,
260-
configuration: WebSocketClientConfiguration = .init(),
261-
tlsConfiguration: TLSConfiguration,
262-
serverName: String,
263-
eventLoopGroup: EventLoopGroup = MultiThreadedEventLoopGroup.singleton,
264-
logger: Logger,
265-
handler: @escaping WebSocketDataHandler<Context>
266-
) async throws -> WebSocketCloseFrame? {
267-
let ws = self.init(
268-
url: url,
269-
configuration: configuration,
270-
tlsConfiguration: tlsConfiguration,
271-
serverName: serverName,
272-
eventLoopGroup: eventLoopGroup,
273-
logger: logger,
274-
handler: handler
275-
)
276-
return try await ws.run()
277-
}
278-
279220
#if canImport(Network)
280221
/// Create websocket client, connect and handle connection
281222
///

Sources/WSClient/WebSocketClientConfiguration.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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,6 +39,7 @@ 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+
@_disfavoredOverload
4043
public init(
4144
maxFrameSize: Int = (1 << 14),
4245
additionalHeaders: HTTPFields = .init(),
@@ -51,5 +54,32 @@ public struct WebSocketClientConfiguration: Sendable {
5154
self.closeTimeout = closeTimeout
5255
self.autoPing = autoPing
5356
self.validateUTF8 = validateUTF8
57+
self.sniHostname = nil
58+
}
59+
60+
/// Initialize WebSocketClient configuration
61+
/// - Paramters
62+
/// - maxFrameSize: Max websocket frame size that can be sent/received
63+
/// - additionalHeaders: Additional headers to be sent with the initial HTTP request
64+
/// - extensions: WebSocket extensions
65+
/// - autoPing: Automatic Ping configuration
66+
/// - validateUTF8: Should text be checked to see if it is valid UTF8
67+
/// - sniHostname: Hostname used during TLS handshake
68+
public init(
69+
maxFrameSize: Int = (1 << 14),
70+
additionalHeaders: HTTPFields = .init(),
71+
extensions: [WebSocketExtensionFactory] = [],
72+
closeTimeout: Duration = .seconds(15),
73+
autoPing: AutoPingSetup = .disabled,
74+
validateUTF8: Bool = false,
75+
sniHostname: String? = nil
76+
) {
77+
self.maxFrameSize = maxFrameSize
78+
self.additionalHeaders = additionalHeaders
79+
self.extensions = extensions.map { $0.build() }
80+
self.closeTimeout = closeTimeout
81+
self.autoPing = autoPing
82+
self.validateUTF8 = validateUTF8
83+
self.sniHostname = sniHostname
5484
}
5585
}

Tests/WebSocketTests/ClientTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ final class WebSocketClientTests: XCTestCase {
2929
}()
3030
try await WebSocketClient.connect(
3131
url: "wss://echo.websocket.org/",
32+
configuration: .init(sniHostname: "echo.websocket.org"),
3233
tlsConfiguration: TLSConfiguration.makeClientConfiguration(),
33-
serverName: "echo.websocket.org",
3434
logger: clientLogger
3535
) { inbound, outbound, _ in
3636
var inboundIterator = inbound.messages(maxSize: .max).makeAsyncIterator()

0 commit comments

Comments
 (0)