Skip to content

Commit 56b7c83

Browse files
author
Alexandru Scvortov
committed
uri may be an Uri, not just a string
1 parent 2ea460d commit 56b7c83

File tree

1 file changed

+57
-47
lines changed

1 file changed

+57
-47
lines changed

projects/client/RabbitMQ.Client/src/client/api/ConnectionFactory.cs

Lines changed: 57 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -174,55 +174,14 @@ public AmqpTcpEndpoint Endpoint
174174
}
175175
}
176176

177+
public Uri uri
178+
{
179+
set { SetUri(value); }
180+
}
181+
177182
public String Uri
178183
{
179-
set
180-
{
181-
Endpoint = new AmqpTcpEndpoint();
182-
183-
Uri uri = new Uri(value, UriKind.Absolute);
184-
185-
if ("amqp".CompareTo(uri.Scheme.ToLower()) == 0) {
186-
// nothing special to do
187-
} else if ("amqps".CompareTo(uri.Scheme.ToLower()) == 0) {
188-
Ssl.Enabled = true;
189-
Ssl.AcceptablePolicyErrors = SslPolicyErrors.RemoteCertificateNameMismatch;
190-
Port = AmqpTcpEndpoint.DefaultAmqpSslPort;
191-
} else {
192-
throw new ArgumentException("Wrong scheme in AMQP URI: " +
193-
value);
194-
}
195-
string host = uri.Host;
196-
if (!String.IsNullOrEmpty(host)) {
197-
HostName = host;
198-
}
199-
Ssl.ServerName = HostName;
200-
201-
int port = uri.Port;
202-
if (port != -1) {
203-
Port = port;
204-
}
205-
206-
string userInfo = uri.UserInfo;
207-
if (!String.IsNullOrEmpty(userInfo)) {
208-
string[] userPass = userInfo.Split(':');
209-
if (userPass.Length > 2) {
210-
throw new ArgumentException("Bad user info in AMQP URI: " + value);
211-
}
212-
UserName = UriDecode(userPass[0]);
213-
if (userPass.Length == 2) {
214-
Password = UriDecode(userPass[1]);
215-
}
216-
}
217-
218-
/* C# automatically changes URIs into a canonical form
219-
that has at least the path segment "/". */
220-
if (uri.Segments.Length > 2) {
221-
throw new ArgumentException("Multiple segments in path of AMQP URI: " + String.Join(", ", uri.Segments));
222-
} else if (uri.Segments.Length == 2) {
223-
VirtualHost = UriDecode(uri.Segments[1]);
224-
}
225-
}
184+
set { SetUri(new Uri(value, UriKind.Absolute)); }
226185
}
227186

228187
///<summary>Construct a fresh instance, with all fields set to
@@ -374,6 +333,57 @@ public AuthMechanismFactory AuthMechanismFactory(string[] mechs) {
374333
return null;
375334
}
376335

336+
337+
private void SetUri(Uri uri)
338+
{
339+
Endpoint = new AmqpTcpEndpoint();
340+
341+
if ("amqp".CompareTo(uri.Scheme.ToLower()) == 0) {
342+
// nothing special to do
343+
} else if ("amqps".CompareTo(uri.Scheme.ToLower()) == 0) {
344+
Ssl.Enabled = true;
345+
Ssl.AcceptablePolicyErrors =
346+
SslPolicyErrors.RemoteCertificateNameMismatch;
347+
Port = AmqpTcpEndpoint.DefaultAmqpSslPort;
348+
} else {
349+
throw new ArgumentException("Wrong scheme in AMQP URI: " +
350+
uri.Scheme);
351+
}
352+
string host = uri.Host;
353+
if (!String.IsNullOrEmpty(host)) {
354+
HostName = host;
355+
}
356+
Ssl.ServerName = HostName;
357+
358+
int port = uri.Port;
359+
if (port != -1) {
360+
Port = port;
361+
}
362+
363+
string userInfo = uri.UserInfo;
364+
if (!String.IsNullOrEmpty(userInfo)) {
365+
string[] userPass = userInfo.Split(':');
366+
if (userPass.Length > 2) {
367+
throw new ArgumentException("Bad user info in AMQP " +
368+
"URI: " + userInfo);
369+
}
370+
UserName = UriDecode(userPass[0]);
371+
if (userPass.Length == 2) {
372+
Password = UriDecode(userPass[1]);
373+
}
374+
}
375+
376+
/* C# automatically changes URIs into a canonical form
377+
that has at least the path segment "/". */
378+
if (uri.Segments.Length > 2) {
379+
throw new ArgumentException("Multiple segments in " +
380+
"path of AMQP URI: " +
381+
String.Join(", ", uri.Segments));
382+
} else if (uri.Segments.Length == 2) {
383+
VirtualHost = UriDecode(uri.Segments[1]);
384+
}
385+
}
386+
377387
//<summary>Unescape a string, protecting '+'.</summary>
378388
private string UriDecode(string uri) {
379389
return Uri.UnescapeDataString(uri.Replace("+", "%2B"));

0 commit comments

Comments
 (0)