public static SIPViaHeader[] ParseSIPViaHeader(string viaHeaderStr)
{
List<SIPViaHeader> viaHeadersList = new List<SIPViaHeader>();
if (!viaHeaderStr.IsNullOrBlank())
{
viaHeaderStr = viaHeaderStr.Trim();
// Multiple Via headers can be contained in a single line by separating them with a comma.
string[] viaHeaders = SIPParameters.GetKeyValuePairsFromQuoted(viaHeaderStr, ',');
foreach (string viaHeaderStrItem in viaHeaders)
{
if (viaHeaderStrItem == null || viaHeaderStrItem.Trim().Length == 0)
{
throw new SIPValidationException(SIPValidationFieldsEnum.ViaHeader, "No Contact address.");
}
else
{
SIPViaHeader viaHeader = new SIPViaHeader();
string header = viaHeaderStrItem.Trim();
int firstSpacePosn = header.IndexOf(" ");
if (firstSpacePosn == -1)
{
throw new SIPValidationException(SIPValidationFieldsEnum.ViaHeader, "No Contact address.");
}
else
{
string versionAndTransport = header.Substring(0, firstSpacePosn);
viaHeader.Version = versionAndTransport.Substring(0, versionAndTransport.LastIndexOf('/'));
viaHeader.Transport = SIPProtocolsType.GetProtocolType(versionAndTransport.Substring(versionAndTransport.LastIndexOf('/') + 1));
string nextField = header.Substring(firstSpacePosn, header.Length - firstSpacePosn).Trim();
int delimIndex = nextField.IndexOf(';');
string contactAddress = null;
// Some user agents include branch but have the semi-colon missing, that's easy to cope with by replacing "branch" with ";branch".
if (delimIndex == -1 && nextField.Contains(m_branchKey))
{
nextField = nextField.Replace(m_branchKey, ";" + m_branchKey);
delimIndex = nextField.IndexOf(';');
}
if (delimIndex == -1)
{
//logger.Warn("Via header missing semi-colon: " + header + ".");
//parserError = SIPValidationError.NoBranchOnVia;
//return null;
contactAddress = nextField.Trim();
}
else
{
contactAddress = nextField.Substring(0, delimIndex).Trim();
viaHeader.ViaParameters = new SIPParameters(nextField.Substring(delimIndex, nextField.Length - delimIndex), m_paramDelimChar);
}
if (contactAddress == null || contactAddress.Trim().Length == 0)
{
// Check that the branch parameter is present, without it the Via header is illegal.
//if (!viaHeader.ViaParameters.Has(m_branchKey))
//{
// logger.Warn("Via header missing branch: " + header + ".");
// parserError = SIPValidationError.NoBranchOnVia;
// return null;
//}
throw new SIPValidationException(SIPValidationFieldsEnum.ViaHeader, "No Contact address.");
}
// Parse the contact address.
int colonIndex = contactAddress.IndexOf(m_hostDelimChar);
if (colonIndex != -1)
{
viaHeader.Host = contactAddress.Substring(0, colonIndex);
if (!Int32.TryParse(contactAddress.Substring(colonIndex + 1), out viaHeader.Port))
{
throw new SIPValidationException(SIPValidationFieldsEnum.ViaHeader, "Non-numeric port for IP address.");
}
else if (viaHeader.Port > SIPConstants.MAX_SIP_PORT)
{
throw new SIPValidationException(SIPValidationFieldsEnum.ViaHeader, "The port specified in a Via header exceeded the maximum allowed.");
}
}
else
{
viaHeader.Host = contactAddress;
}
viaHeadersList.Add(viaHeader);
}
}
}
}
if (viaHeadersList.Count > 0)
{
return viaHeadersList.ToArray();
}
else
{
throw new SIPValidationException(SIPValidationFieldsEnum.ViaHeader, "Via list was empty."); ;
}
}