SIPSorcery.SIP.SIPURI.ParseSIPURI C# (CSharp) Method

ParseSIPURI() public static method

public static ParseSIPURI ( string uri ) : SIPURI
uri string
return SIPURI
        public static SIPURI ParseSIPURI(string uri)
        {
            try
            {
                SIPURI sipURI = new SIPURI();

                if (uri == null || uri.Trim().Length == 0)
                {
                    throw new SIPValidationException(SIPValidationFieldsEnum.URI, "A SIP URI cannot be parsed from an empty string.");
                }
                else
                {
                    if (uri == m_sipRegisterRemoveAll)
                    {
                        sipURI.Host = m_sipRegisterRemoveAll;
                    }
                    else
                    {
                        int colonPosn = uri.IndexOf(SCHEME_ADDR_SEPARATOR);

                        if (colonPosn == -1)
                        {
                            throw new SIPValidationException(SIPValidationFieldsEnum.URI, "SIP URI did not contain compulsory colon");
                        }
                        else
                        {
                            try
                            {
                                sipURI.Scheme = SIPSchemesType.GetSchemeType(uri.Substring(0, colonPosn));
                            }
                            catch
                            {
                                throw new SIPValidationException(SIPValidationFieldsEnum.URI, SIPResponseStatusCodesEnum.UnsupportedURIScheme, "SIP scheme " + uri.Substring(0, colonPosn) + " was not understood");
                            }

                            string uriHostPortion = uri.Substring(colonPosn + 1);
                            int ampPosn = uriHostPortion.IndexOf(USER_HOST_SEPARATOR);
                            int paramHeaderPosn = -1;
                            if (ampPosn != -1)
                            {
                                paramHeaderPosn = uriHostPortion.IndexOfAny(new char[] { PARAM_TAG_DELIMITER, HEADER_START_DELIMITER }, ampPosn);
                            }
                            else
                            {
                                // Host only SIP URI.
                                paramHeaderPosn = uriHostPortion.IndexOfAny(new char[] { PARAM_TAG_DELIMITER, HEADER_START_DELIMITER });
                            }

                            if (ampPosn != -1 && paramHeaderPosn != -1)
                            {
                                sipURI.User = uriHostPortion.Substring(0, ampPosn);
                                sipURI.Host = uriHostPortion.Substring(ampPosn + 1, paramHeaderPosn - ampPosn - 1);
                                string paramsAndHeaders = uriHostPortion.Substring(paramHeaderPosn);

                                sipURI.ParseParamsAndHeaders(paramsAndHeaders);
                            }
                            else if (ampPosn == -1 && paramHeaderPosn == 0)
                            {
                                throw new SIPValidationException(SIPValidationFieldsEnum.URI, "No Host portion in SIP URI");
                            }
                            else if (ampPosn == -1 && paramHeaderPosn != -1)
                            {
                                sipURI.Host = uriHostPortion.Substring(0, paramHeaderPosn);

                                string paramsAndHeaders = uriHostPortion.Substring(paramHeaderPosn);

                                sipURI.ParseParamsAndHeaders(paramsAndHeaders);
                            }
                            else if (ampPosn != -1)
                            {
                                sipURI.User = uriHostPortion.Substring(0, ampPosn);
                                sipURI.Host = uriHostPortion.Substring(ampPosn + 1, uriHostPortion.Length - ampPosn - 1);
                            }
                            else
                            {
                                sipURI.Host = uriHostPortion;
                            }

                            if (sipURI.Host.IndexOfAny(m_invalidSIPHostChars) != -1)
                            {
                                throw new SIPValidationException(SIPValidationFieldsEnum.URI, "The SIP URI host portion contained an invalid character.");
                            }
                        }
                    }

                    return sipURI;
                }
            }
            catch (SIPValidationException)
            {
                throw;
            }
            catch (Exception excp)
            {
                logger.Error("Exception ParseSIPURI (URI=" + uri + "). " + excp.Message);
                throw new SIPValidationException(SIPValidationFieldsEnum.URI, "Unknown error parsing SIP URI.");
            }
        }

Usage Example

Example #1
0
            public void AckRecognitionUnitTest()
            {
                SIPTransport clientTransport = null;
                SIPTransport serverTransport = null;

                try
                {
                    SIPTransactionEngine clientEngine   = new SIPTransactionEngine();   // Client side of the INVITE.
                    SIPEndPoint          clientEndPoint = new SIPEndPoint(SIPProtocolsEnum.udp, new IPEndPoint(IPAddress.Loopback, 12013));
                    clientTransport = new SIPTransport(MockSIPDNSManager.Resolve, clientEngine, new SIPUDPChannel(clientEndPoint.GetIPEndPoint()), false);
                    SetTransportTraceEvents(clientTransport);

                    SIPTransactionEngine serverEngine      = new SIPTransactionEngine(); // Server side of the INVITE.
                    UASInviteTransaction serverTransaction = null;
                    SIPEndPoint          serverEndPoint    = new SIPEndPoint(new IPEndPoint(IPAddress.Loopback, 12014));
                    serverTransport = new SIPTransport(MockSIPDNSManager.Resolve, serverEngine, new SIPUDPChannel(serverEndPoint.GetIPEndPoint()), false);
                    SetTransportTraceEvents(serverTransport);
                    serverTransport.SIPTransportRequestReceived += (localEndPoint, remoteEndPoint, sipRequest) =>
                    {
                        Console.WriteLine("Server Transport Request In: " + sipRequest.Method + ".");
                        serverTransaction = serverTransport.CreateUASTransaction(sipRequest, remoteEndPoint, localEndPoint, null);
                        SetTransactionTraceEvents(serverTransaction);
                        serverTransaction.GotRequest(localEndPoint, remoteEndPoint, sipRequest);
                    };

                    SIPURI     dummyURI      = SIPURI.ParseSIPURI("sip:dummy@" + serverEndPoint);
                    SIPRequest inviteRequest = GetDummyINVITERequest(dummyURI);
                    inviteRequest.LocalSIPEndPoint = clientTransport.GetDefaultTransportContact(SIPProtocolsEnum.udp);

                    // Send the invite to the server side.
                    UACInviteTransaction clientTransaction = new UACInviteTransaction(clientTransport, inviteRequest, serverEndPoint, clientEndPoint, null);
                    SetTransactionTraceEvents(clientTransaction);
                    clientEngine.AddTransaction(clientTransaction);
                    clientTransaction.SendInviteRequest(serverEndPoint, inviteRequest);

                    Thread.Sleep(500);

                    Assert.IsTrue(clientTransaction.TransactionState == SIPTransactionStatesEnum.Completed, "Client transaction in incorrect state.");
                    Assert.IsTrue(serverTransaction.TransactionState == SIPTransactionStatesEnum.Confirmed, "Server transaction in incorrect state.");
                }
                finally
                {
                    if (clientTransport != null)
                    {
                        clientTransport.Shutdown();
                    }

                    if (serverTransport != null)
                    {
                        serverTransport.Shutdown();
                    }
                }
            }
All Usage Examples Of SIPSorcery.SIP.SIPURI::ParseSIPURI