public static ViaEntry ParseEntry(ITextReader reader) { //SIP/2.0/UDP erlang.bell-telephone.com:5060;branch=z9hG4bK87asdks7 var entry = new ViaEntry(); // read SIP/ reader.ConsumeWhiteSpaces(); entry.Protocol = reader.ReadUntil("/ \t"); if (entry.Protocol == null) throw new FormatException("Expected Via header to start with 'SIP' or 'SIPS'."); reader.ConsumeWhiteSpaces('/'); // read 2.0/ entry.SipVersion = reader.ReadUntil("/ \t"); if (entry.SipVersion == null) throw new FormatException("Expected to find sip version in Via header."); reader.ConsumeWhiteSpaces('/'); // read UDP or TCP entry.Transport = reader.ReadWord(); if (entry.Transport == null) throw new FormatException("Expected to find transport protocol after sip version in Via header."); reader.ConsumeWhiteSpaces(); entry.Domain = reader.ReadUntil(";: \t"); if (entry.Domain == null) throw new FormatException("Failed to find domain in via header."); reader.ConsumeWhiteSpaces(); if (reader.Current == ':') { reader.Read(); reader.ConsumeWhiteSpaces(); string temp = reader.ReadToEnd("; \t"); reader.ConsumeWhiteSpaces(); int port; if (!int.TryParse(temp, out port)) throw new FormatException("Invalid port specified."); entry.Port = port; } UriParser.ParseParameters(entry.Parameters, reader); string rport = entry.Parameters["rport"]; if (!string.IsNullOrEmpty(rport)) //parameter can exist, but be empty. = rport requested. { int value; if (!int.TryParse(rport, out value)) throw new FormatException("RPORT is not a number."); entry.Rport = value; } return entry; }
/// <summary> /// Parse a message value. /// </summary> /// <param name="name">Name of header being parsed.</param> /// <param name="reader">Reader containing the string that should be parsed.</param> /// <returns>Newly created header.</returns> /// <exception cref="ParseException">Header value is malformed.</exception> public IHeader Parse(string name, ITextReader reader) { string type = reader.ReadUntil("/"); reader.Consume('/'); string subType = reader.ReadToEnd(';'); reader.Consume(';', ' ', '\t'); var contentType = new ContentType {SubType = subType, Type = type}; UriParser.ParseParameters(contentType.Parameters, reader); return contentType; }
/// <summary> /// Parse a message value. /// </summary> /// <param name="name">Name of header being parsed.</param> /// <param name="reader">Reader containing the string that should be parsed.</param> /// <returns>Newly created header.</returns> /// <exception cref="ParseException">Header value is malformed.</exception> public IHeader Parse(string name, ITextReader reader) { string type = reader.ReadUntil("/"); reader.Consume('/'); string subType = reader.ReadToEnd(';'); reader.Consume(';', ' ', '\t'); var contentType = new ContentType { SubType = subType, Type = type }; UriParser.ParseParameters(contentType.Parameters, reader); return(contentType); }
/// <summary> /// Parse a message value. /// </summary> /// <param name="reader">Reader containing the string that should be parsed.</param> /// <returns>Newly created header.</returns> /// <exception cref="ParseException">Header value is malformed.</exception> /// <example> /// sip:[email protected]:5060 /// [email protected]:5060 /// jonas:[email protected] /// sip:[email protected] /// mailto:[email protected] /// </example> public static SipUri Parse(ITextReader reader) { string first = reader.ReadUntil("@:"); string scheme = null; string userName; string password = null; string domain; int port = 0; bool containsAt = reader.Contains('@'); // scheme:domain:port // scheme:domain // domain:port if (reader.Current == ':' && !containsAt) { reader.Consume(); //scheme:domain:port or scheme:domain if (IsValidScheme(first)) { scheme = first; domain = reader.ReadToEnd(":"); if (reader.EOF) { return(new SipUri(scheme, domain, 0)); } //scheme:domain:port reader.Consume(':'); first = reader.ReadToEnd(); } else // domain:port or just domain { domain = first; first = reader.ReadToEnd(":"); } if (!int.TryParse(first, out port)) { throw new ParseException("Port is not a number: " + first); } return(new SipUri(scheme, domain, port)); } // Can either be "scheme:username" // or "username:password" // or "scheme:username:password" else if (reader.Current == ':') { reader.Consume(); // Check if we got another colon (scheme:username:password) string second = reader.ReadUntil(":@"); if (reader.Current == ':') { scheme = first; userName = second; reader.Consume(); password = reader.ReadUntil('@'); } // it's "scheme:username" or "username:password" else { //TODO: Create a ProtocolProvider singleton if (first == "tel" || first == "sip" || first == "sips" || first == "mailto") { scheme = first; userName = second; } else { userName = first; password = second; } } } // only username else { userName = first; } reader.Consume(); // eat delimiter. domain = reader.ReadToEnd(":;"); if (reader.Current == '\r' || userName == null) { return(null); // domain was not specified. } // We got a port. if (reader.Current == ':') { reader.Consume(); string portStr = reader.ReadToEnd(';'); if (portStr != null) { if (!int.TryParse(portStr, out port)) { return(null); } } } // parse parameters var values = new KeyValueCollection(); if (reader.Current == ';') { ParseParameters(values, reader); } return(new SipUri(scheme, userName, password, domain, port, values)); }
public static ViaEntry ParseEntry(ITextReader reader) { //SIP/2.0/UDP erlang.bell-telephone.com:5060;branch=z9hG4bK87asdks7 var entry = new ViaEntry(); // read SIP/ reader.ConsumeWhiteSpaces(); entry.Protocol = reader.ReadUntil("/ \t"); if (entry.Protocol == null) { throw new FormatException("Expected Via header to start with 'SIP' or 'SIPS'."); } reader.ConsumeWhiteSpaces('/'); // read 2.0/ entry.SipVersion = reader.ReadUntil("/ \t"); if (entry.SipVersion == null) { throw new FormatException("Expected to find sip version in Via header."); } reader.ConsumeWhiteSpaces('/'); // read UDP or TCP entry.Transport = reader.ReadWord(); if (entry.Transport == null) { throw new FormatException("Expected to find transport protocol after sip version in Via header."); } reader.ConsumeWhiteSpaces(); entry.Domain = reader.ReadUntil(";: \t"); if (entry.Domain == null) { throw new FormatException("Failed to find domain in via header."); } reader.ConsumeWhiteSpaces(); if (reader.Current == ':') { reader.Read(); reader.ConsumeWhiteSpaces(); string temp = reader.ReadToEnd("; \t"); reader.ConsumeWhiteSpaces(); int port; if (!int.TryParse(temp, out port)) { throw new FormatException("Invalid port specified."); } entry.Port = port; } UriParser.ParseParameters(entry.Parameters, reader); string rport = entry.Parameters["rport"]; if (!string.IsNullOrEmpty(rport)) //parameter can exist, but be empty. = rport requested. { int value; if (!int.TryParse(rport, out value)) { throw new FormatException("RPORT is not a number."); } entry.Rport = value; } return(entry); }
/// <summary> /// Parse a message value. /// </summary> /// <param name="reader">Reader containing the string that should be parsed.</param> /// <returns>Newly created header.</returns> /// <exception cref="ParseException">Header value is malformed.</exception> /// <example> /// sip:[email protected]:5060 /// [email protected]:5060 /// jonas:[email protected] /// sip:[email protected] /// mailto:[email protected] /// </example> public static SipUri Parse(ITextReader reader) { string first = reader.ReadUntil("@:"); string scheme = null; string userName; string password = null; string domain; int port = 0; bool containsAt = reader.Contains('@'); // scheme:domain:port // scheme:domain // domain:port if (reader.Current == ':' && !containsAt) { reader.Consume(); //scheme:domain:port or scheme:domain if (IsValidScheme(first)) { scheme = first; domain = reader.ReadToEnd(":"); if (reader.EOF) return new SipUri(scheme, domain, 0); //scheme:domain:port reader.Consume(':'); first = reader.ReadToEnd(); } else // domain:port or just domain { domain = first; first = reader.ReadToEnd(":"); } if (!int.TryParse(first, out port)) throw new ParseException("Port is not a number: " + first); return new SipUri(scheme, domain, port); } // Can either be "scheme:username" // or "username:password" // or "scheme:username:password" else if (reader.Current == ':') { reader.Consume(); // Check if we got another colon (scheme:username:password) string second = reader.ReadUntil(":@"); if (reader.Current == ':') { scheme = first; userName = second; reader.Consume(); password = reader.ReadUntil('@'); } // it's "scheme:username" or "username:password" else { //TODO: Create a ProtocolProvider singleton if (first == "tel" || first == "sip" || first == "sips" || first == "mailto") { scheme = first; userName = second; } else { userName = first; password = second; } } } // only username else userName = first; reader.Consume(); // eat delimiter. domain = reader.ReadToEnd(":;"); if (reader.Current == '\r' || userName == null) return null; // domain was not specified. // We got a port. if (reader.Current == ':') { reader.Consume(); string portStr = reader.ReadToEnd(';'); if (portStr != null) { if (!int.TryParse(portStr, out port)) return null; } } // parse parameters var values = new KeyValueCollection(); if (reader.Current == ';') ParseParameters(values, reader); return new SipUri(scheme, userName, password, domain, port, values); }