Example #1
0
        /// <summary>
        /// The action to perform.
        /// </summary>
        /// <param name="chatState">The current chat state.</param>
        private void Action(Chat.ChatHttptState chatState)
        {
            try
            {
                // Select the member to execute.
                switch (chatState.ExecutionMember.ToUpper())
                {
                case "DATA":
                    // SendMessage
                    SendMessage(chatState);
                    break;

                default:
                    throw new Exception("Command not recognised.");
                }
            }
            catch (Exceptions.ErrorCodeException exc)
            {
                // Get the error code.
                chatState.ErrorCode = exc;
            }
            catch (Exception ex)
            {
                // Internal error.
                chatState.ErrorCode = new Exceptions.ErrorCodeException(ex.Message, 500);
            }
        }
Example #2
0
        /// <summary>
        /// SendMessageToClients
        /// </summary>
        /// <param name="chatState">The current chat state.</param>
        /// <param name="serviceNameSendTo">The service name the unique identifiers are connected to.</param>
        /// <param name="sendTo">Send the data to.</param>
        /// <returns>True if error; else false.</returns>
        private bool SendMessageEx(Chat.ChatHttptState chatState, string serviceNameSendTo, string[] sendTo = null)
        {
            bool isError = true;

            System.Web.HttpRequest  request  = chatState.HttpContext.Request;
            System.Web.HttpResponse response = chatState.HttpContext.Response;

            // If content exists.
            if (request.ContentLength > 0)
            {
                MemoryStream sendBuffer = null;
                try
                {
                    // Read the request stream and write to the send buffer.
                    sendBuffer = new MemoryStream();
                    bool copied = Nequeo.IO.Stream.Operation.CopyStream(request.InputStream, sendBuffer, request.ContentLength, base.RequestTimeout);

                    // If all the data has been copied.
                    if (copied)
                    {
                        // Send the data to client(s) on this machine
                        // and another machine if not on this machine.
                        Common.Helper.SendToReceivers(chatState.Member.UniqueIdentifier, base.ServiceName, serviceNameSendTo, sendTo, sendBuffer.ToArray());
                        isError = false;
                    }
                }
                catch (Exception ex)
                {
                    // Internal error.
                    isError             = true;
                    chatState.ErrorCode = new Exceptions.ErrorCodeException(ex.Message, 500);
                }
                finally
                {
                    // Dispose of the buffer.
                    if (sendBuffer != null)
                    {
                        sendBuffer.Dispose();
                    }
                }
            }

            // True if error else false.
            return(isError);
        }
Example #3
0
        /// <summary>
        /// Send the message to the client.
        /// </summary>
        /// <param name="chatState">The current chat state.</param>
        /// <returns>True if error; else false.</returns>
        private bool SendMessage(Chat.ChatHttptState chatState)
        {
            System.Web.HttpRequest  request  = chatState.HttpContext.Request;
            System.Web.HttpResponse response = chatState.HttpContext.Response;

            // Get the header data.
            string[] sendTo      = null;
            string   serviceName = request.Headers["FSN"];

            // If header send to.
            if (!String.IsNullOrEmpty(request.Headers["SENDTO"]))
            {
                sendTo = request.Headers["SENDTO"].Split(new char[] { ';' }, StringSplitOptions.None);
            }

            // If sending from a query.
            if (request.QueryString != null)
            {
                // If header from service name.
                if (!String.IsNullOrEmpty(request.QueryString["FSN"]))
                {
                    serviceName = request.QueryString["FSN"];
                }

                // If query send to.
                if (!String.IsNullOrEmpty(request.QueryString["SENDTO"]))
                {
                    // Get the send to query from base64 encoding.
                    byte[] sendToBytes  = Convert.FromBase64String(request.QueryString["SENDTO"]);
                    string sendToString = Encoding.Default.GetString(sendToBytes);
                    sendTo = sendToString.Split(new char[] { ';' }, StringSplitOptions.None);
                }
            }

            // Return the result.
            return(SendMessageEx(chatState, serviceName, sendTo));
        }
Example #4
0
        /// <summary>
        /// Validate the current user token.
        /// </summary>
        /// <param name="chatState">The current chat state.</param>
        /// <returns>True if valid; else false.</returns>
        private bool ValidateToken(Chat.ChatHttptState chatState)
        {
            string uniqueIdentifier = null;
            string token            = null;

            try
            {
                uniqueIdentifier = chatState.HttpContext.Request.Headers["FUI"];
                token            = chatState.HttpContext.Request.Headers["TOKEN"];

                // If sending from a query.
                if (chatState.HttpContext.Request.QueryString != null)
                {
                    // If header from unique identifier.
                    if (!String.IsNullOrEmpty(chatState.HttpContext.Request.QueryString["FUI"]))
                    {
                        uniqueIdentifier = chatState.HttpContext.Request.QueryString["FUI"];
                    }

                    // If header from token.
                    if (!String.IsNullOrEmpty(chatState.HttpContext.Request.QueryString["TOKEN"]))
                    {
                        token = chatState.HttpContext.Request.QueryString["TOKEN"];
                    }
                }
            }
            catch (Exception)
            {
                // Bad request exception.
                throw new Exceptions.ErrorCodeException("Invalid Request.", 400);
            }

            // If the unique identifier
            // has not been set then the
            // token has not been verified.
            if (String.IsNullOrEmpty(chatState.Member.UniqueIdentifier))
            {
                // Is token valid.
                _token.IsValid(uniqueIdentifier, base.ServiceName, token, (result, permission, state) =>
                {
                    Chat.ChatHttptState stateChat = null;

                    try
                    {
                        // Get the state chat.
                        stateChat = (Chat.ChatHttptState)state;

                        // If the token is valid.
                        if (result)
                        {
                            // Set the current unique identifier
                            // and the service name.
                            stateChat.Member.ServiceName      = base.ServiceName;
                            stateChat.Member.UniqueIdentifier = uniqueIdentifier;

                            // Perform the action.
                            Action(stateChat);
                        }
                        else
                        {
                            // Bad request.
                            stateChat.ErrorCode = new Exceptions.ErrorCodeException("The token is not valid.", 400);
                        }
                    }
                    catch (Exception ex)
                    {
                        // Internal error.
                        stateChat.ErrorCode = new Exceptions.ErrorCodeException(ex.Message, 500);
                    }
                }, uniqueIdentifier, chatState);

                // Return false not valid.
                return(false);
            }
            else
            {
                // Perform the action.
                Action(chatState);

                // Token is valid.
                return(true);
            }
        }
Example #5
0
        /// <summary>
        /// Http context handler.
        /// </summary>
        /// <param name="context">The http context.</param>
        public override void HttpContext(HttpContext context)
        {
            System.Web.HttpRequest  request  = null;
            System.Web.HttpResponse response = null;
            bool isServerError = false;
            bool keepAlive     = true;
            bool isError       = true;

            try
            {
                request  = context.Request;
                response = context.Response;

                // If headers exist.
                if (request.Headers != null)
                {
                    // Get the execution member.
                    if (!String.IsNullOrEmpty(request.Headers["Member"]))
                    {
                        // Get the execution member.
                        // Set the calling member.
                        string executionMember = request.Headers["Member"].Trim();
                        response.AddHeader("Member", executionMember);
                        response.AddHeader("ActionName", request.Headers["ActionName"]);

                        // Create the chat state.
                        Chat.ChatHttptState chatState = new Chat.ChatHttptState()
                        {
                            HttpContext     = context,
                            Member          = new Net.Http.HttpContextMember(context),
                            ExecutionMember = executionMember,
                            ErrorCode       = new Exceptions.ErrorCodeException("OK", 200)
                        };

                        try
                        {
                            // Validate the current user token.
                            bool isTokenValid = ValidateToken(chatState);
                        }
                        catch (Exceptions.ErrorCodeException exc)
                        {
                            // Get the error code.
                            chatState.ErrorCode = exc;
                        }
                        catch (Exception ex)
                        {
                            // Internal error.
                            chatState.ErrorCode = new Exceptions.ErrorCodeException(ex.Message, 500);
                        }

                        // Send a message back to the client indicating that
                        // the message was recivied and was sent.
                        CreateResponse(response, true, chatState.ErrorCode.ErrorCode, statusDescription: chatState.ErrorCode.Message);
                        isError = false;
                    }
                }
                else
                {
                    // No headers have been found.
                    keepAlive = false;
                    throw new Exception("No headers have been found.");
                }

                // If error has occured.
                if (isError)
                {
                    // Send an error response.
                    response.StatusCode        = 400;
                    response.StatusDescription = "Bad Request";
                    response.AddHeader("Content-Length", (0).ToString());
                    response.Flush();
                }
            }
            catch (Exception) { isServerError = true; }

            // If a server error has occured.
            if (isServerError)
            {
                // Make sure the response exists.
                if (response != null)
                {
                    try
                    {
                        // Send an error response.
                        response.StatusCode        = 500;
                        response.StatusDescription = "Internal server error";
                        response.AddHeader("Content-Length", (0).ToString());
                        response.Flush();
                    }
                    catch { keepAlive = false; }
                }
            }

            // If do not keep alive.
            if (!keepAlive)
            {
                // Close the connection.
                if (response != null)
                {
                    try
                    {
                        // Close the connection.
                        response.Close();
                    }
                    catch { }
                }
            }
        }