CreateResponse() public static method

Creates a SIP response based on the passed in parameters.
public static CreateResponse ( int responseCode, string responseText, Dictionary
headers = null, string content = "", Message originalRequest = null ) : Message
responseCode int The response code (200 etc.)
responseText string The response text (OK etc.)
headers Dictionary
The SIP headers.
content string The SIP body content.
originalRequest Message The original request.
return Message
Exemplo n.º 1
0
        /// <summary>
        /// Raises an error (Service unavailable etc)
        /// </summary>
        /// <param name="transaction">The transaction.</param>
        /// <param name="error">The error.</param>
        public override void Error(Transaction transaction, string error)
        {
            if (transaction == null)
            {
                Transaction = null;
                if (!Request.Method.ToUpper().Contains("ACK"))
                {
                    Message response = Message.CreateResponse(503, "Service unavailable - " + error, null, null,
                                                              Request);
                    SendResponse(response);
                    return;
                }
                Debug.Assert(false, "Warning, dropping ACK");
            }
            ProxyBranch branch = GetBranch(transaction);

            if (branch == null)
            {
                return;
            }
            Transaction        = null;
            branch.Transaction = null;
            if (branch.RemoteCandidates != null && branch.RemoteCandidates.Count > 0)
            {
                RetryNextCandidate(branch);
            }
            else
            {
                ReceivedResponse(null,
                                 Message.CreateResponse(503, "Service unavailable - " + error, null, null,
                                                        branch.Request));
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates a SIP response.
        /// </summary>
        /// <param name="responseCode">The response code.</param>
        /// <param name="responseText">The response text.</param>
        /// <returns>The created SIP response.</returns>
        public virtual Message CreateResponse(int responseCode, string responseText)
        {
            Message m = null;

            if (Request != null && Server)
            {
                m = Message.CreateResponse(responseCode, responseText, null, null, Request);
                if (responseCode != 100 && !m.Headers["To"][0].Attributes.ContainsKey("tag"))
                {
                    m.Headers["To"][0].Attributes.Add("tag", Tag);
                }
            }
            return(m);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Handles request timeouts
        /// </summary>
        /// <param name="transaction">The transaction.</param>
        private void TimeOut(Transaction transaction)
        {
            ProxyBranch branch = GetBranch(transaction);

            if (branch == null)
            {
                return;
            }
            branch.Transaction = null;
            if (branch.RemoteCandidates != null && branch.RemoteCandidates.Count > 0)
            {
                RetryNextCandidate(branch);
            }
            else
            {
                ReceivedResponse(null, Message.CreateResponse(408, "Request timeout", null, null, branch.Request));
            }
        }
Exemplo n.º 4
0
 /// <summary>
 /// Triggered on timeout.
 /// </summary>
 /// <param name="transaction">The transaction.</param>
 public virtual void Timeout(Transaction transaction)
 {
     if ((transaction != null) && (transaction != Transaction))
     {
         return;
     }
     Transaction = null;
     if (Server == false)
     {
         if ((RemoteCandidates != null) && (RemoteCandidates.Count > 0))
         {
             RetryNextCandidate();
         }
         else
         {
             ReceivedResponse(null, Message.CreateResponse(408, "Request Timeout", null, null, Request));
         }
     }
 }
Exemplo n.º 5
0
        /// <summary>
        /// Creates a SIP response, filling in the necessary parameters from the dialog.
        /// </summary>
        /// <param name="response_code">The SIP response_code.</param>
        /// <param name="response_text">The SIP response_text.</param>
        /// <param name="content">The SIP body contents.</param>
        /// <param name="contentType">The type of the SIP body.</param>
        /// <returns>Message.</returns>
        public override Message CreateResponse(int response_code, string response_text, string content = null,
                                               string contentType = null)
        {
            if (Servers.Count == 0)
            {
                Debug.Assert(false, String.Format("No server transaction to create response"));
                return(null);
            }


            // TODO REMOVE THIS LOGIC
            //string branchID = ((Message)response).First("Via").Attributes["branch"];
            //Transaction = null;
            //foreach (Transaction transaction in Servers)
            //{
            //    if (branchID == transaction.Branch)
            //    {
            //        Transaction = transaction;
            //        Request = transaction.Request;
            //    }
            //}
            //if (Transaction == null)
            //{
            //    Debug.Assert(false, String.Format("No transactions in dialog matched"));
            //    return;
            //}
            if (Servers.Count > 1)
            {
                Console.WriteLine("Got some transaction servers");
            }
            Message request  = Servers[Servers.Count - 1].Request;
            Message response = Message.CreateResponse(response_code, response_text, null, content, request);

            if (!string.IsNullOrEmpty(contentType))
            {
                response.InsertHeader(new Header(contentType, "Content-Type"));
            }
            if (response.ResponseCode != 100 && !response.Headers["To"][0].Attributes.ContainsKey("tag"))
            {
                response.Headers["To"][0].Attributes["tag"] = LocalTag;
            }
            return(response);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Creates a SIP response given the response code and responseText
        /// </summary>
        /// <param name="responseCode">The response code.</param>
        /// <param name="responseText">The response text.</param>
        /// <param name="content">The content.</param>
        /// <param name="contentType">Type of the content.</param>
        /// <returns>Message.</returns>
        public virtual Message CreateResponse(int responseCode, string responseText, string content = null,
                                              string contentType = null)
        {
            if (Request == null)
            {
                Debug.Assert(false, String.Format("Invalid request in creating a response"));
                return(null);
            }
            Message responseMessage = Message.CreateResponse(responseCode, responseText, null, content, Request);

            if (contentType != null)
            {
                responseMessage.InsertHeader(new Header(contentType, "Content-Type"));
            }
            if (responseMessage.ResponseCode != 100 && !responseMessage.Headers["To"][0].ToString().Contains("tag"))
            {
                responseMessage.Headers["To"][0].Attributes.Add("tag", LocalTag);
            }
            return(responseMessage);
        }
Exemplo n.º 7
0
 /// <summary>
 /// Raises an error
 /// </summary>
 /// <param name="t">The transaction.</param>
 /// <param name="error">The error.</param>
 public virtual void Error(Transaction t, string error)
 {
     if ((t != null) && t != Transaction)
     {
         return;
     }
     Transaction = null;
     if (Server == false)
     {
         if ((RemoteCandidates != null) && (RemoteCandidates.Count > 0))
         {
             RetryNextCandidate();
         }
         else
         {
             ReceivedResponse(null,
                              Message.CreateResponse(503, "Service unavailable - " + error, null, null, Request));
         }
     }
 }
Exemplo n.º 8
0
        /// <summary>
        /// Handle received request
        /// </summary>
        /// <param name="m">The received message.</param>
        /// <param name="uri">The SIPURI that sent the message.</param>
        private void ReceivedRequest(Message m, SIPURI uri)
        {
            string      branch = m.Headers["Via"][0].Attributes["branch"];
            Transaction t;

            if (m.Method == "ACK")
            {
                if (branch == "0")
                {
                    t = null;
                }
                else
                {
                    t = FindTransaction(branch);
                    if (t == null || (t.LastResponse != null && t.LastResponse.Is2XX()))
                    {
                        t = FindTransaction(Transaction.CreateId(branch, m.Method));
                    }
                }
            }
            else
            {
                t = FindTransaction(Transaction.CreateId(branch, m.Method));
            }
            if (t == null)
            {
                UserAgent app = null; // Huh ?
                if ((m.Method != "CANCEL") && (m.Headers["To"][0].Attributes.ContainsKey("tag")))
                {
                    //In dialog request
                    Dialog d = FindDialog(m);
                    if (d == null)
                    {
                        if (m.Method != "ACK")
                        {
                            //Updated from latest code TODO
                            UserAgent u = CreateServer(m, uri);
                            if (u != null)
                            {
                                app = u;
                            }
                            else
                            {
                                // TODO: FIX NOTIFY ON SUBSCRIBE HANDLING
                                if (m.Method != "NOTIFY")
                                {
                                    Send(Message.CreateResponse(481, "Dialog does not exist", null, null, m));
                                    return;
                                }
                                else
                                {
                                    string branchID = m.Headers["Via"][0].Attributes["branch"];
                                    if (_seenNotifys.ContainsKey(branchID) && _seenNotifys[branchID] > 1)
                                    {
                                        Send(Message.CreateResponse(481, "Dialog does not exist", null, null, m));
                                        return;
                                    }
                                    else
                                    {
                                        if (_seenNotifys.ContainsKey(branchID))
                                        {
                                            _seenNotifys[branchID] = _seenNotifys[branchID] + 1;
                                        }
                                        else
                                        {
                                            _seenNotifys[branchID] = 1;
                                        }
                                    }
                                }
                                return;
                            }
                        }
                        else
                        {
                            _log.Info("No dialog for ACK, finding transaction");
                            if (t == null && branch != "0")
                            {
                                t = FindTransaction(Transaction.CreateId(branch, "INVITE"));
                            }
                            if (t != null && t.State != "terminated")
                            {
                                t.ReceivedRequest(m);
                                return;
                            }
                            else
                            {
                                _log.Info("No existing transaction for ACK \n");
                                UserAgent u = CreateServer(m, uri);
                                if (u != null)
                                {
                                    app = u;
                                }
                                else
                                {
                                    return;
                                }
                            }
                        }
                    }
                    else
                    {
                        app = d;
                    }
                }
                else if (m.Method != "CANCEL")
                {
                    //Out of dialog request
                    UserAgent u = CreateServer(m, uri);
                    if (u != null)
                    {
                        //TODO error.....
                        app = u;
                    }
                    else if (m.Method == "OPTIONS")
                    {
                        //Handle OPTIONS
                        Message reply = Message.CreateResponse(200, "OK", null, null, m);
                        reply.InsertHeader(new Header("INVITE,ACK,CANCEL,BYE,OPTION,MESSAGE,PUBLISH", "Allow"));
                        Send(m);
                        return;
                    }
                    else if (m.Method == "MESSAGE")
                    {
                        //Handle MESSAGE
                        UserAgent ua = new UserAgent(this)
                        {
                            Request = m
                        };

                        /*Message reply = ua.CreateResponse(200, "OK");
                         * Send(reply);*/
                        App.ReceivedRequest(ua, m, this);
                        return;
                    }
                    else if (m.Method == "PUBLISH")
                    {
                        UserAgent ua = new UserAgent(this)
                        {
                            Request = m
                        };
                        App.ReceivedRequest(ua, m, this);
                        return;
                    }
                    else if (m.Method != "ACK")
                    {
                        Send(Message.CreateResponse(405, "Method not allowed", null, null, m));
                        return;
                    }
                }
                else
                {
                    //Cancel Request
                    Transaction o =
                        FindTransaction(Transaction.CreateId(m.Headers["Via"][0].Attributes["branch"], "INVITE"));
                    if (o == null)
                    {
                        Send(Message.CreateResponse(481, "Original transaction does not exist", null, null, m));
                        return;
                    }
                    app = o.App;
                }

                if (app != null)
                {
                    //t = Transaction.CreateServer(app.Stack, app, app.Request, app.Stack.Transport, app.Stack.Tag);
                    // TODO: Check app or this ?
                    t = Transaction.CreateServer(this, app, m, Transport, Tag);
                }
                else if (m.Method != "ACK")
                {
                    Send(Message.CreateResponse(404, "Not found", null, null, m));
                }
            }
            else
            {
                t.ReceivedRequest(m);
            }
        }