Пример #1
0
        private static FromHeaderField ParseFromHeaderField(string p)
        {
            try {
                string name = (p.StartsWith(FromHeaderField.LongName) ? FromHeaderField.LongName : FromHeaderField.ShortName) + ":";
                name = name.ToLower();
                p    = p.ToLower();

                int    s = p.IndexOf(name);
                int    e = p.IndexOf("<");
                string d = p.Substring(s + name.Length + 1, e - s - name.Length - 1).Trim();

                s = e + 1;
                e = p.IndexOf(">");
                string          u   = p.Substring(s, e - s);
                int             t   = p.IndexOf("tag=") + 4;
                string          tag = p.Substring(t, p.Length - t);
                FromHeaderField f   = new FromHeaderField();
                u     = Regex.Replace(u, @"[^a-zA-Z:@\d\.]+", "");            //jira-icoc-233
                f.Uri = new SipUri(u);
                f.Tag = tag;
                return(f);
            } catch (Exception exception) {
                string error_text = String.Concat(Assembly.GetExecutingAssembly().GetName().Name, Dns.GetHostName()
                                                  , "[CipSipParser][ParseFromHeaderField]: " + exception.Message + " | innerException: " + (exception.InnerException != null && !string.IsNullOrEmpty(exception.InnerException.Message) ? exception.InnerException.Message : "")
                                                  , "p= " + p);
                Console.WriteLine(error_text);
                throw exception;
            }
        }
Пример #2
0
        public static SipResponse GetSipReponse(string message, byte[] networkData, int readSize)
        {
            try
            {
                string[] parts = message.Split('\n');
                if (parts.Length == 0)
                {
                    return(null);
                }

                StatusLine sl = ParseStatusLine(parts[0]);
                if (sl == null)
                {
                    return(null);
                }

                SipResponse response = new SipResponse();
                response.StatusLine = sl;

                foreach (string pt in parts)
                {
                    if (pt.StartsWith(ViaHeaderField.LongName))
                    {
                        string         p   = pt.Replace('\r', ' ').TrimEnd();
                        ViaHeaderField via = new ViaHeaderField();
                        via.Parse(p);
                        response.ViaHeaders.Add(via);
                    }
                    else if (pt.StartsWith(ToHeaderField.LongName))
                    {
                        string        p   = pt.Replace('\r', ' ').TrimEnd();
                        ToHeaderField thf = ParseToHeaderField(p);
                        response.To = thf;
                    }
                    else if (pt.StartsWith(FromHeaderField.LongName))
                    {
                        string          p   = pt.Replace('\r', ' ').TrimEnd();
                        FromHeaderField fhf = ParseFromHeaderField(p);
                        response.From = fhf;
                    }
                    else if (pt.StartsWith(CallIdHeaderField.LongName))
                    {
                        string            p   = pt.Replace('\r', ' ').TrimEnd();
                        CallIdHeaderField chf = new CallIdHeaderField();
                        chf.Parse(p);
                        response.CallId = chf;
                    }
                    else if (pt.StartsWith(CSeqHeaderField.LongName))
                    {
                        string          p    = pt.Replace('\r', ' ').TrimEnd();
                        CSeqHeaderField cshf = new CSeqHeaderField();
                        cshf.Parse(p);
                        response.CSeq = cshf;
                    }
                    else if (pt.StartsWith(ContentLengthHeaderField.LongName))
                    {
                        string p = pt.Replace('\r', ' ').TrimEnd();
                        ContentLengthHeaderField clhf = new ContentLengthHeaderField();
                        clhf.Parse(p);
                        response.ContentLength = clhf;
                        int len = clhf.Length;
                        if (len > 0)
                        {
                            response.Body = new byte[len];
                            Array.Copy(networkData, (readSize - len), response.Body, 0, len);
                        }
                    }
                    else if (pt.StartsWith(ContentTypeHeaderField.LongName))
                    {
                        string p = pt.Replace('\r', ' ').TrimEnd();
                        ContentTypeHeaderField cthf = new ContentTypeHeaderField();
                        cthf.Parse(p);
                        response.ContentType = cthf;
                    }
                }
                return(response);
            }
            catch (Exception exception)
            {
                string error_text = String.Concat(Assembly.GetExecutingAssembly().GetName().Name, Dns.GetHostName()
                                                  , "[CipSipParser][GetSipReponse]: " + message + " | exception: " + exception.Message + " | innerException: " + (exception.InnerException != null && !string.IsNullOrEmpty(exception.InnerException.Message) ? exception.InnerException.Message : "")
                                                  , exception.StackTrace);
                Console.WriteLine(error_text);
                throw exception;
            }
        }