void Parse(string address)
{
// amqp[s]://user:[email protected]:port/foo/bar
ParseState state = ParseState.Scheme;
int startIndex = 0;
for (int i = 0; i < address.Length; ++i)
{
switch (address[i])
{
case ':':
if (state == ParseState.Scheme)
{
this.Scheme = address.Substring(startIndex, i - startIndex);
state = ParseState.Slash1;
}
else if (state == ParseState.User)
{
this.User = address.Substring(startIndex, i - startIndex);
state = ParseState.Password;
startIndex = i + 1;
}
else if (state == ParseState.Host)
{
this.Host = address.Substring(startIndex, i - startIndex);
state = ParseState.Port;
startIndex = i + 1;
}
else
{
throw new AmqpException(ErrorCode.InvalidField,
Fx.Format(SRAmqp.InvalidAddressFormat));
}
break;
case '/':
if (state == ParseState.Slash1)
{
state = ParseState.Slash2;
}
else if (state == ParseState.Slash2)
{
state = ParseState.User;
startIndex = i + 1;
}
else if (state == ParseState.User || state == ParseState.Host)
{
this.Host = address.Substring(startIndex, i - startIndex);
state = ParseState.Path;
startIndex = i;
}
else if (state == ParseState.Port)
{
this.Port = int.Parse(address.Substring(startIndex, i - startIndex));
state = ParseState.Path;
startIndex = i;
}
else if (state == ParseState.Password)
{
this.Host = this.User;
this.User = null;
this.Port = int.Parse(address.Substring(startIndex, i - startIndex));
state = ParseState.Path;
startIndex = i;
}
break;
case '@':
if (state == ParseState.Password)
{
this.Password = address.Substring(startIndex, i - startIndex);
state = ParseState.Host;
startIndex = i + 1;
}
else
{
throw new AmqpException(ErrorCode.InvalidField,
Fx.Format(SRAmqp.InvalidAddressFormat));
}
break;
default:
break;
}
if (state == ParseState.Path)
{
this.Path = address.Substring(startIndex);
break;
}
}
// check state in case of no trailing slash
if (state == ParseState.User || state == ParseState.Host)
{
this.Host = address.Substring(startIndex);
}
else if (state == ParseState.Password)
{
this.Host = this.User;
this.User = null;
if (startIndex < address.Length - 1)
{
this.Port = int.Parse(address.Substring(startIndex));
}
}
else if (state == ParseState.Port)
{
this.Port = int.Parse(address.Substring(startIndex));
}
if (this.Password != null && this.Password.Length > 0)
{
this.Password = Uri.UnescapeDataString(this.Password);
}
if (this.User != null && this.User.Length > 0)
{
this.User = Uri.UnescapeDataString(this.User);
}
if (this.Host != null)
{
this.Host = Uri.UnescapeDataString(this.Host);
}
}