Esempio n. 1
0
        public WebSocketContext AcceptWebSocket(string protocol)
#endif
        {
            if (_websocketContext != null)
                throw new InvalidOperationException("The accepting is already in progress.");

            if (protocol?.Length == 0)
                throw new ArgumentException("An empty string.", nameof(protocol));

            //if (!protocol.IsToken())
            //    throw new ArgumentException("Contains an invalid character.", "protocol");

#if COMPAT
            _websocketContext = new WebSocketContext(this, protocol, log);
#else
            _websocketContext = new WebSocketContext(this, protocol);
#endif
            _websocketContext.WebSocket.InternalAccept();

            return _websocketContext;
        }
Esempio n. 2
0
        // As server
        private bool CheckHandshakeRequest(WebSocketContext context, out string message)
        {
            message = null;

            if (context.RequestUri == null)
            {
                message = "Specifies an invalid Request-URI.";
                return false;
            }

            if (!context.IsWebSocketRequest)
            {
                message = "Not a WebSocket handshake request.";
                return false;
            }

            var headers = context.Headers;
            if (!ValidateSecWebSocketKeyHeader(headers["Sec-WebSocket-Key"]))
            {
                message = "Includes no Sec-WebSocket-Key header, or it has an invalid value.";
                return false;
            }

            if (!ValidateSecWebSocketVersionClientHeader(headers["Sec-WebSocket-Version"]))
            {
                message = "Includes no Sec-WebSocket-Version header, or it has an invalid value.";
                return false;
            }

            if (!ValidateSecWebSocketProtocolClientHeader(headers["Sec-WebSocket-Protocol"]))
            {
                message = "Includes an invalid Sec-WebSocket-Protocol header.";
                return false;
            }

            if (!IgnoreExtensions
                && !string.IsNullOrWhiteSpace(headers["Sec-WebSocket-Extensions"]))
            {
                message = "Includes an invalid Sec-WebSocket-Extensions header.";
                return false;
            }

            return true;
        }
Esempio n. 3
0
        // As server
        private void ReleaseServerResources()
        {
            if (_closeContext == null)
                return;

            _closeContext();
            _closeContext = null;
            _stream = null;
            _context = null;
        }
Esempio n. 4
0
        // As server
        internal WebSocket(WebSocketContext context, string protocol)
        {
            _context = context;
            _protocol = protocol;

            _closeContext = context.Close;
#if COMPAT
            Log = context.Log;
#endif
            _message = Messages;
            IsSecure = context.IsSecureConnection;
            _stream = context.Stream;
            _waitTime = TimeSpan.FromSeconds(1);

            Init();
        }
Esempio n. 5
0
 // As server
 private bool CustomCheckHandshakeRequest(WebSocketContext context, out string message)
 {
     message = null;
     return CustomHandshakeRequestChecker == null
            || (message = CustomHandshakeRequestChecker(context)) == null;
 }