@@ -32,15 +32,15 @@ public struct AMQPConnectionConfiguration: Sendable {
32
32
public var vhost : String
33
33
public var timeout : TimeAmount
34
34
public var connectionName : String
35
-
35
+
36
36
public init ( host: String ? = nil ,
37
37
port: Int ? = nil ,
38
38
user: String ? = nil ,
39
39
password: String ? = nil ,
40
40
vhost: String ? = nil ,
41
41
timeout: TimeAmount ? = nil ,
42
42
connectionName: String ? = nil ) {
43
-
43
+
44
44
self . host = host ?? Defaults . host
45
45
self . port = port ?? Defaults . port
46
46
self . user = user ?? Defaults . user
@@ -62,25 +62,43 @@ public extension AMQPConnectionConfiguration {
62
62
enum UrlScheme : String {
63
63
case amqp = " amqp "
64
64
case amqps = " amqps "
65
-
65
+
66
66
var defaultPort : Int {
67
67
switch self {
68
68
case . amqp: return Server . Defaults. port
69
69
case . amqps: return Server . Defaults. tlsPort
70
70
}
71
71
}
72
72
}
73
-
74
- init ( url: String , tls: TLSConfiguration ? = nil ) throws {
73
+
74
+ /// Convenience init to create connection configuration from a URL string + key options.
75
+ ///
76
+ /// - Parameters:
77
+ /// - url: A string that contains the URL to create configuration from.
78
+ /// - tls: Optional TLS configuration. If `nil`, default will be used.
79
+ /// - sniServerName: Server name to use for TLS connection. If `nil`, default will be used.
80
+ /// - timeout: Optional connection timeout. If `nil`, default timeout will be used.
81
+ /// - connectionName: Optional connection name. If `nil`, default connection name will be used.
82
+ /// - Throws: `AMQPConnectionError.invalidUrl` if URL is invalid, or `AMQPConnectionError.invalidUrlScheme` if URL scheme is not supported.
83
+ init ( url: String , tls: TLSConfiguration ? = nil , sniServerName: String ? = nil , timeout: TimeAmount ? = nil , connectionName: String ? = nil ) throws {
75
84
guard let url = URL ( string: url) else { throw AMQPConnectionError . invalidUrl }
76
- try self . init ( url: url, tls: tls)
85
+ try self . init ( url: url, tls: tls, sniServerName : sniServerName , timeout : timeout , connectionName : connectionName )
77
86
}
78
87
79
- init ( url: URL , tls: TLSConfiguration ? = nil ) throws {
88
+ /// Convenience init to create connection configuration from a URL + key options.
89
+ ///
90
+ /// - Parameters:
91
+ /// - url: A URL to create the configuration from.
92
+ /// - tls: Optional TLS configuration. If `nil`, default will be used.
93
+ /// - sniServerName: Server name to use for TLS connection. If `nil`, default will be used.
94
+ /// - timeout: Optional connection timeout. If `nil`, default timeout will be used.
95
+ /// - connectionName: Optional connection name. If `nil`, default connection name will be used.
96
+ /// - Throws: `AMQPConnectionError.invalidUrlScheme` if URL scheme is not supported.
97
+ init ( url: URL , tls: TLSConfiguration ? = nil , sniServerName: String ? = nil , timeout: TimeAmount ? = nil , connectionName: String ? = nil ) throws {
80
98
guard let scheme = UrlScheme ( rawValue: url. scheme ?? " " ) else { throw AMQPConnectionError . invalidUrlScheme }
81
-
99
+
82
100
// there is no such thing as a "" host
83
- let host = url. host? . isEmpty == true ? nil : url. host
101
+ let host = url. host? . isEmpty == true ? nil : url. host? . removingPercentEncoding
84
102
//special path magic for vhost interpretation (see https://www.rabbitmq.com/uri-spec.html)
85
103
var vhost = url. path. isEmpty ? nil : String ( url. path. removingPercentEncoding? . dropFirst ( ) ?? " " )
86
104
@@ -89,11 +107,15 @@ public extension AMQPConnectionConfiguration {
89
107
vhost = " / "
90
108
}
91
109
92
- let server = Server ( host: host, port: url. port ?? scheme. defaultPort, user: url. user, password: url. password? . removingPercentEncoding, vhost: vhost)
93
-
110
+ let server = Server (
111
+ host: host, port: url. port ?? scheme. defaultPort,
112
+ user: url. user? . removingPercentEncoding, password: url. password? . removingPercentEncoding,
113
+ vhost: vhost, timeout: timeout, connectionName: connectionName
114
+ )
115
+
94
116
switch scheme {
95
117
case . amqp: self = . init( connection: . plain, server: server)
96
- case . amqps: self = . init( connection: . tls( tls, sniServerName: nil ) , server: server)
118
+ case . amqps: self = . init( connection: . tls( tls, sniServerName: sniServerName ) , server: server)
97
119
}
98
120
}
99
121
}
0 commit comments