@@ -128,7 +128,7 @@ public class ConnectionSettings : IEquatable<ConnectionSettings>
128
128
{
129
129
private readonly Address _address ;
130
130
private readonly List < Address > _addresses = new ( ) ;
131
- private readonly string _virtualHost = "/" ;
131
+ private readonly string _virtualHost = Consts . DefaultVirtualHost ;
132
132
private readonly string _containerId = "" ;
133
133
private readonly uint _maxFrameSize = Consts . DefaultMaxFrameSize ;
134
134
private readonly TlsSettings ? _tlsSettings ;
@@ -144,35 +144,9 @@ public class ConnectionSettings : IEquatable<ConnectionSettings>
144
144
*/
145
145
public ConnectionSettings ( Uri uri )
146
146
{
147
- string ? user = null ;
148
- string ? password = null ;
149
- string userInfo = uri . UserInfo ;
150
- if ( ! string . IsNullOrEmpty ( userInfo ) )
151
- {
152
- string [ ] userPass = userInfo . Split ( ':' ) ;
153
- if ( userPass . Length > 2 )
154
- {
155
- throw new ArgumentException ( $ "Bad user info in AMQP URI: { userInfo } ") ;
156
- }
147
+ ( string ? user , string ? password ) = ProcessUserInfo ( uri ) ;
157
148
158
- user = UriDecode ( userPass [ 0 ] ) ;
159
- if ( userPass . Length == 2 )
160
- {
161
- password = UriDecode ( userPass [ 1 ] ) ;
162
- }
163
- }
164
-
165
- // C# automatically changes URIs into a canonical form
166
- // that has at least the path segment "/"
167
- if ( uri . Segments . Length > 2 )
168
- {
169
- throw new ArgumentException ( $ "Multiple segments in path of AMQP URI: { string . Join ( ", " , uri . Segments ) } ") ;
170
- }
171
-
172
- if ( uri . Segments . Length == 2 )
173
- {
174
- _virtualHost = UriDecode ( uri . Segments [ 1 ] ) ;
175
- }
149
+ _virtualHost = ProcessUriSegmentsForVirtualHost ( uri ) ;
176
150
177
151
_address = new Address ( host : uri . Host ,
178
152
port : uri . Port ,
@@ -201,44 +175,18 @@ public ConnectionSettings(IEnumerable<Uri> uris)
201
175
202
176
foreach ( Uri uri in uris )
203
177
{
204
- string ? user = null ;
205
- string ? password = null ;
206
- string userInfo = uri . UserInfo ;
207
- if ( ! string . IsNullOrEmpty ( userInfo ) )
208
- {
209
- string [ ] userPass = userInfo . Split ( ':' ) ;
210
- if ( userPass . Length > 2 )
211
- {
212
- throw new ArgumentException ( $ "Bad user info in AMQP URI: { userInfo } ") ;
213
- }
178
+ ( string ? user , string ? password ) = ProcessUserInfo ( uri ) ;
214
179
215
- user = UriDecode ( userPass [ 0 ] ) ;
216
- if ( userPass . Length == 2 )
217
- {
218
- password = UriDecode ( userPass [ 1 ] ) ;
219
- }
220
- }
221
-
222
- // C# automatically changes URIs into a canonical form
223
- // that has at least the path segment "/"
224
- if ( uri . Segments . Length > 2 )
180
+ if ( tmpVirtualHost is null )
225
181
{
226
- throw new ArgumentException ( $ "Multiple segments in path of AMQP URI: { string . Join ( ", " , uri . Segments ) } " ) ;
182
+ tmpVirtualHost = ProcessUriSegmentsForVirtualHost ( uri ) ;
227
183
}
228
-
229
- if ( uri . Segments . Length == 2 )
184
+ else
230
185
{
231
- if ( tmpVirtualHost is null )
232
- {
233
- tmpVirtualHost = UriDecode ( uri . Segments [ 1 ] ) ;
234
- }
235
- else
186
+ string thisVirtualHost = ProcessUriSegmentsForVirtualHost ( uri ) ;
187
+ if ( false == thisVirtualHost . Equals ( tmpVirtualHost , StringComparison . InvariantCultureIgnoreCase ) )
236
188
{
237
- string thisVirtualHost = UriDecode ( uri . Segments [ 1 ] ) ;
238
- if ( false == thisVirtualHost . Equals ( tmpVirtualHost , StringComparison . InvariantCultureIgnoreCase ) )
239
- {
240
- throw new ArgumentException ( $ "All AMQP URIs must use the same virtual host. Expected '{ tmpVirtualHost } ', got '{ thisVirtualHost } '") ;
241
- }
189
+ throw new ArgumentException ( $ "All AMQP URIs must use the same virtual host. Expected '{ tmpVirtualHost } ', got '{ thisVirtualHost } '") ;
242
190
}
243
191
}
244
192
@@ -393,6 +341,48 @@ private static string UriDecode(string str)
393
341
{
394
342
return Uri . UnescapeDataString ( str . Replace ( "+" , "%2B" ) ) ;
395
343
}
344
+
345
+ private static ( string ? user , string ? password ) ProcessUserInfo ( Uri uri )
346
+ {
347
+ string ? user = null ;
348
+ string ? password = null ;
349
+ string userInfo = uri . UserInfo ;
350
+ if ( ! string . IsNullOrEmpty ( userInfo ) )
351
+ {
352
+ string [ ] userPass = userInfo . Split ( ':' ) ;
353
+ if ( userPass . Length > 2 )
354
+ {
355
+ throw new ArgumentException ( $ "Bad user info in AMQP URI: { userInfo } ") ;
356
+ }
357
+
358
+ user = UriDecode ( userPass [ 0 ] ) ;
359
+ if ( userPass . Length == 2 )
360
+ {
361
+ password = UriDecode ( userPass [ 1 ] ) ;
362
+ }
363
+ }
364
+
365
+ return ( user , password ) ;
366
+ }
367
+
368
+ private static string ProcessUriSegmentsForVirtualHost ( Uri uri )
369
+ {
370
+ // C# automatically changes URIs into a canonical form
371
+ // that has at least the path segment "/"
372
+ if ( uri . Segments . Length > 2 )
373
+ {
374
+ throw new ArgumentException ( $ "Multiple segments in path of AMQP URI: { string . Join ( ", " , uri . Segments ) } ") ;
375
+ }
376
+
377
+ if ( uri . Segments . Length == 2 )
378
+ {
379
+ return UriDecode ( uri . Segments [ 1 ] ) ;
380
+ }
381
+ else
382
+ {
383
+ return Consts . DefaultVirtualHost ;
384
+ }
385
+ }
396
386
}
397
387
398
388
public class TlsSettings
0 commit comments