public static SIPURI ParseSIPURI(string uri)
{
try
{
SIPURI sipURI = new SIPURI();
if (String.IsNullOrEmpty(uri))
{
throw new SIPValidationException(SIPValidationFieldsEnum.URI, "A SIP URI cannot be parsed from an empty string.");
}
else
{
if (uri == m_sipRegisterRemoveAll)
{
sipURI.Host = m_sipRegisterRemoveAll;
}
else
{
int colonPosn = uri.IndexOf(SCHEME_ADDR_SEPARATOR);
if (colonPosn == -1)
{
throw new SIPValidationException(SIPValidationFieldsEnum.URI, "SIP URI did not contain compulsory colon");
}
else
{
try
{
sipURI.Scheme = SIPSchemesType.GetSchemeType(uri.Substring(0, colonPosn));
}
catch
{
throw new SIPValidationException(SIPValidationFieldsEnum.URI, SIPResponseStatusCodesEnum.UnsupportedURIScheme, "SIP scheme " + uri.Substring(0, colonPosn) + " was not understood");
}
string uriHostPortion = uri.Substring(colonPosn + 1);
int ampPosn = uriHostPortion.IndexOf(USER_HOST_SEPARATOR);
int paramHeaderPosn = -1;
if (ampPosn != -1)
{
paramHeaderPosn = uriHostPortion.IndexOfAny(new char[] { PARAM_TAG_DELIMITER, HEADER_START_DELIMITER }, ampPosn);
}
else
{
// Host only SIP URI.
paramHeaderPosn = uriHostPortion.IndexOfAny(new char[] { PARAM_TAG_DELIMITER, HEADER_START_DELIMITER });
}
if (ampPosn != -1 && paramHeaderPosn != -1)
{
sipURI.User = uriHostPortion.Substring(0, ampPosn);
sipURI.Host = uriHostPortion.Substring(ampPosn + 1, paramHeaderPosn - ampPosn - 1);
string paramsAndHeaders = uriHostPortion.Substring(paramHeaderPosn);
sipURI.ParseParamsAndHeaders(paramsAndHeaders);
}
else if (ampPosn == -1 && paramHeaderPosn == 0)
{
throw new SIPValidationException(SIPValidationFieldsEnum.URI, "No Host portion in SIP URI");
}
else if (ampPosn == -1 && paramHeaderPosn != -1)
{
sipURI.Host = uriHostPortion.Substring(0, paramHeaderPosn);
string paramsAndHeaders = uriHostPortion.Substring(paramHeaderPosn);
sipURI.ParseParamsAndHeaders(paramsAndHeaders);
}
else if (ampPosn != -1)
{
sipURI.User = uriHostPortion.Substring(0, ampPosn);
sipURI.Host = uriHostPortion.Substring(ampPosn + 1, uriHostPortion.Length - ampPosn - 1);
}
else
{
sipURI.Host = uriHostPortion;
}
if (sipURI.Host.IndexOfAny(m_invalidSIPHostChars) != -1)
{
throw new SIPValidationException(SIPValidationFieldsEnum.URI, "The SIP URI host portion contained an invalid character.");
}
else if (sipURI.Host.IndexOf(':') != sipURI.Host.LastIndexOf(':'))
{
// If the host contains multiple ':' characters then it must be an IPv6 address which require a start '[' and an end ']'.
if (sipURI.Host.ToCharArray()[0] != '[')
{
throw new SIPValidationException(SIPValidationFieldsEnum.URI, "The SIP URI host portion contained an IPv6 address that was missing the start '['.");
}
else if (!sipURI.Host.EndsWith("]") &&
(sipURI.Host.ToCharArray().Length < sipURI.Host.LastIndexOf(':') + 1 ||
sipURI.Host.ToCharArray()[sipURI.Host.LastIndexOf(':') - 1] != ']'))
{
throw new SIPValidationException(SIPValidationFieldsEnum.URI, "The SIP URI host portion contained an IPv6 address that was missing the end ']'.");
}
//rj2: apply robustness principle mentioned in RFC 5118 4.10
while (sipURI.Host.Contains(":::"))
{
sipURI.Host = sipURI.Host.Replace(":::", "::");
}
}
}
}
return(sipURI);
}
}
catch (SIPValidationException)
{
throw;
}
catch (Exception excp)
{
logger.LogError("Exception ParseSIPURI (URI=" + uri + "). " + excp.Message);
throw new SIPValidationException(SIPValidationFieldsEnum.URI, "Unknown error parsing SIP URI.");
}
}