Exemplo n.º 1
0
        /// <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);
        }
Exemplo n.º 2
0
        /// <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>
        /// <example>
        /// Authorization: Digest username="******",
        ///                 realm="biloxi.com",
        ///                 nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093",
        ///                 uri="sip:[email protected]",
        ///                 qop=auth,
        ///                 nc=00000001,
        ///                 cnonce="0a4f113b",
        ///                 response="6629fae49393a05397450978507c4ef1",
        ///                 opaque="5ccc069c403ebaf9f0171e9517f40e41"
        /// </example>
        public IHeader Parse(string name, ITextReader reader)
        {
            reader.ConsumeWhiteSpaces();
            string digest = reader.ReadWord().ToLower();

            if (digest != "digest")
            {
                throw new ParseException("Authorization header is not digest authentication");
            }

            reader.ConsumeWhiteSpaces();
            var parameters = new KeyValueCollection();

            UriParser.ParseParameters(parameters, reader, ',');

            var header = new Authorization(name)
            {
                UserName    = parameters["username"],
                Realm       = parameters["realm"],
                Nonce       = parameters["nonce"],
                Qop         = parameters["qop"],
                ClientNonce = parameters["cnonce"],
                Opaque      = parameters["opaque"],
                Response    = parameters["response"],
                Uri         = UriParser.Parse(parameters["uri"])
            };

            try
            {
                header.NonceCounter = int.Parse(parameters["nc"]);
            }
            catch (Exception err)
            {
                throw new ParseException("Failed to parse 'nc' in Authorization header.", err);
            }

            return(header);
        }
Exemplo n.º 3
0
        /// <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>
        /// <example>
        /// Digest realm="atlanta.com",
        /// domain="sip:boxesbybob.com", qop="auth",
        /// nonce="f84f1cec41e6cbe5aea9c8e88d359",
        /// opaque="", stale=FALSE, algorithm=MD5
        /// </example>
        public IHeader Parse(string name, ITextReader reader)
        {
            reader.ConsumeWhiteSpaces();
            string digest = reader.ReadWord().ToLower();

            if (digest != "digest")
            {
                throw new ParseException("Authorization header is not digest authentication");
            }

            reader.ConsumeWhiteSpaces();
            var parameters = new KeyValueCollection();

            UriParser.ParseParameters(parameters, reader, ',');

            var header = new Authenticate(name)
            {
                Algortihm = parameters["algorithm"],
                Domain    = UriParser.Parse(parameters["domain"]),
                Realm     = parameters["realm"],
                Nonce     = parameters["nonce"],
                Qop       = parameters["qop"],
                Opaque    = parameters["opaque"]
            };

            try
            {
                header.Stale = bool.Parse(parameters["stale"]);
            }
            catch (Exception err)
            {
                throw new ParseException("Failed to parse 'stale' in WWW-Authenticate header.", err);
            }

            return(header);
        }
Exemplo n.º 4
0
        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);
        }