Exemple #1
0
        public static SipRequest GetSipRequest(string message, byte[] networkData, int readSize)
        {
            try
            {
                string[] parts = message.Split('\n');
                if (parts.Length == 0)
                {
                    return(null);
                }
                SipRequest request = SipRequest.CreateRequest(SipMethod.Message);

                RequestLine rl = ParseSipRequestLine(parts[0]);
                request.RequestLine = rl;

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