/// <summary>
        /// Create the web socket response headers.
        /// </summary>
        /// <param name="webSocketContext">The current web socket context.</param>
        private void CreateResponseHeaders(Nequeo.Net.WebSockets.WebSocketContext webSocketContext)
        {
            // If this is a WebSocket request then complete the handshaking.
            if (webSocketContext.WebSocketRequest.IsWebSocketRequest)
            {
                // Get the sec web socket accept key.
                byte[] webSocketKey       = Nequeo.Cryptography.Hashcode.GetHashcodeSHA1Raw(webSocketContext.WebSocketRequest.SecWebSocketKey + Common.Resource.MagicGuid);
                string secWebSocketAccept = System.Convert.ToBase64String(webSocketKey);

                // Create the response and write the data back to the client.
                Nequeo.Net.WebSockets.WebSocketResponse response = webSocketContext.WebSocketResponse;
                response.SecWebSocketAccept = secWebSocketAccept;
                response.ContentLength      = 0;
                response.Server             = base.Name;

                // If protocols have been sent from the client
                // then assign the first one found.
                if (webSocketContext.WebSocketRequest.SecWebSocketProtocols != null && webSocketContext.WebSocketRequest.SecWebSocketProtocols.Length > 0)
                {
                    response.SecWebSocketProtocol = webSocketContext.WebSocketRequest.SecWebSocketProtocols[0];
                }

                // If extensions have been sent from the client
                // then assign the first one found.
                if (webSocketContext.WebSocketRequest.SecWebSocketExtensions != null && webSocketContext.WebSocketRequest.SecWebSocketExtensions.Length > 0)
                {
                    response.SecWebSocketExtensions = webSocketContext.WebSocketRequest.SecWebSocketExtensions[0];
                }

                // Indicate that handshaking is complete.
                webSocketContext.HandshakeComplete = true;
            }
        }
        /// <summary>
        /// Check to see if the request is a web socket.
        /// </summary>
        /// <param name="webSocketContext">The current web socket context.</param>
        private void CheckIfWebSocketRequest(Nequeo.Net.WebSockets.WebSocketContext webSocketContext)
        {
            // Make sure all handshaking has complete before continuing.
            if (!webSocketContext.HandshakeComplete)
            {
                // Get the request and response.
                Nequeo.Net.WebSockets.WebSocketRequest  request    = webSocketContext.WebSocketRequest;
                Nequeo.Net.WebSockets.WebSocketResponse response   = webSocketContext.WebSocketResponse;
                Nequeo.Net.Http.Common.HttpStatusCode   statusCode = null;

                // If not a web socket request.
                if (!webSocketContext.WebSocketRequest.IsWebSocketRequest)
                {
                    // Get the bad request.
                    statusCode = Nequeo.Net.Http.Utility.GetStatusCode(400);

                    // Send bad request.
                    response.Server            = base.Name;
                    response.ContentLength     = 0;
                    response.StatusCode        = statusCode.Code;
                    response.StatusDescription = statusCode.Description;
                    response.ProtocolVersion   = request.ProtocolVersion;
                }
                else
                {
                    // Get the internal error.
                    statusCode = Nequeo.Net.Http.Utility.GetStatusCode(500);

                    // Send internal error.
                    response.Server            = base.Name;
                    response.ContentLength     = 0;
                    response.StatusCode        = statusCode.Code;
                    response.StatusDescription = statusCode.Description;
                    response.ProtocolVersion   = request.ProtocolVersion;
                }

                // Not valid.
                throw new Exception("Not a valid WebSocket request.");
            }
        }
 /// <summary>
 /// Get a new web socket response stream.
 /// </summary>
 /// <param name="output">The http response output stream.</param>
 /// <returns>The web socket response stream.</returns>
 protected virtual Nequeo.Net.WebSockets.WebSocketResponse GetWebSocketResponse(System.IO.Stream output)
 {
     Nequeo.Net.WebSockets.WebSocketResponse response = new Nequeo.Net.WebSockets.WebSocketResponse();
     response.Output = output;
     return(response);
 }
        /// <summary>
        /// On received action handler.
        /// </summary>
        /// <param name="context">The current client context.</param>
        private void OnReceivedActionHandler(Nequeo.Net.Provider.Context context)
        {
            try
            {
                // Store data until all headers have been read.
                if (Nequeo.Net.Utility.IsParse2CRLF(context.Request.Input, context.RequestBufferStore, _maxHeaderBufferStore))
                {
                    // Create the web socket context and set the headers.
                    Nequeo.Net.WebSockets.WebSocketContext webSocketContext = CreateWebSocketContext(context);

                    // Get the headers from the stream and assign the request data.
                    bool headersExist = Nequeo.Net.WebSockets.Utility.SetRequestHeaders(webSocketContext,
                                                                                        base.HeaderTimeout, base.MaximumReadLength, context.RequestBufferStore);

                    // Create the response headers.
                    CreateResponseHeaders(webSocketContext);

                    // Make sure all handshaking has complete before continuing.
                    if (!webSocketContext.HandshakeComplete)
                    {
                        // Check to see if the request is a web socket.
                        CheckIfWebSocketRequest(webSocketContext);
                    }
                    else
                    {
                        // Indicate that all web sockets after handshaking
                        // is complete will run in async mode.
                        webSocketContext.IsAsyncMode = true;

                        // Create the central web socket context.
                        CentralWebSocketContext centralWebSocketContext = new CentralWebSocketContext(webSocketContext);
                        centralWebSocketContext.ConnectionID          = webSocketContext.ConnectionID;
                        centralWebSocketContext.Cookies               = webSocketContext.WebSocketRequest.Cookies;
                        centralWebSocketContext.Headers               = webSocketContext.WebSocketRequest.Headers;
                        centralWebSocketContext.IsAuthenticated       = webSocketContext.IsAuthenticated;
                        centralWebSocketContext.IsSecureConnection    = webSocketContext.IsSecureConnection;
                        centralWebSocketContext.SecWebSocketKey       = webSocketContext.WebSocketRequest.SecWebSocketKey;
                        centralWebSocketContext.SecWebSocketProtocols = webSocketContext.WebSocketRequest.SecWebSocketProtocols;
                        centralWebSocketContext.SecWebSocketVersion   = webSocketContext.WebSocketRequest.SecWebSocketVersion;
                        centralWebSocketContext.SessionID             = webSocketContext.SessionID;
                        centralWebSocketContext.SocketState           = webSocketContext.SocketState;
                        centralWebSocketContext.UniqueIdentifier      = webSocketContext.UniqueIdentifier;
                        centralWebSocketContext.Url  = webSocketContext.WebSocketRequest.Url;
                        centralWebSocketContext.User = webSocketContext.User;

                        // If the event has been assigned.
                        if (OnWebSocketContext != null)
                        {
                            OnWebSocketContext(this, centralWebSocketContext);
                        }

                        // Write the response to the client.
                        Nequeo.Net.WebSockets.WebSocketResponse response = webSocketContext.WebSocketResponse;
                        response.WriteWebSocketHeaders();

                        // Create and assign the web socket.
                        Nequeo.Net.WebSockets.WebSocket webSocket = new Nequeo.Net.WebSockets.WebSocket(centralWebSocketContext, response.ProtocolVersion);
                        centralWebSocketContext.WebSocket = webSocket;

                        // Save the web context state objects.
                        SaveWebContext(context, webSocketContext);
                    }
                }

                // If the maximum request buffer store has been reached then close the connection.
                if (context.RequestBufferStore.Length > _maxHeaderBufferStore)
                {
                    throw new Exception("Maximum request buffer store has been reached.");
                }
            }
            catch (Exception)
            {
                // Close the connection and release all resources used for communication.
                context.Close();
            }
        }