Exemple #1
0
        public void ShouldThrowWhenUnsupportedType()
        {
            var request = new WebSocketHttpRequest {
                Headers = { { "Bad", "Request" } }
            };

            Assert.Throws <WebSocketException>(() => HandlerFactory.BuildHandler(request, x => {}, () => {}, x => { }, x => { }, x => { }));
        }
Exemple #2
0
        public void ShouldReturnHandlerForValidHeaders()
        {
            var request = new WebSocketHttpRequest {
                Headers = { { "Sec-WebSocket-Key1", "BLAH" } }
            };
            var handler = HandlerFactory.BuildHandler(request, x => { }, () => { }, x => { }, x => { }, x => { });

            Assert.IsNotNull(handler);
        }
Exemple #3
0
        private void OnClientConnect(ISocket clientSocket)
        {
            if (clientSocket == null)
            {
                return; // socket closed
            }
            FleckLog.Debug(string.Format("Client connected from {0}:{1}", clientSocket.RemoteIpAddress, clientSocket.RemotePort.ToString()));
            ListenForClients();

            WebSocketConnection connection = null;

            connection = new WebSocketConnection(
                clientSocket,
                _config,
                bytes => RequestParser.Parse(bytes, _scheme),
                r => {
                try {
                    return(HandlerFactory.BuildHandler(
                               r, s => connection.OnMessage(s), connection.Close, b => connection.OnBinary(b),
                               b => connection.OnPing(b), b => connection.OnPong(b)
                               ));
                } catch (WebSocketException) {
                    const string responseMsg = "HTTP/1.1 200 OK\r\n"
                                               + "Date: {0}\r\n"
                                               + "Server: SharpChat\r\n"
                                               + "Content-Length: {1}\r\n"
                                               + "Content-Type: text/html; charset=utf-8\r\n"
                                               + "Connection: close\r\n"
                                               + "\r\n"
                                               + "{2}";
                    string responseBody = File.Exists(@"http-motd.txt") ? File.ReadAllText(@"http-motd.txt") : @"SharpChat";

                    clientSocket.Stream.Write(Encoding.UTF8.GetBytes(string.Format(
                                                                         responseMsg, DateTimeOffset.Now.ToString(@"r"), Encoding.UTF8.GetByteCount(responseBody), responseBody
                                                                         )));
                    clientSocket.Close();
                    return(null);
                }
            },
                s => SubProtocolNegotiator.Negotiate(SupportedSubProtocols, s));

            if (IsSecure)
            {
                FleckLog.Debug("Authenticating Secure Connection");
                clientSocket
                .Authenticate(Certificate,
                              EnabledSslProtocols,
                              connection.StartReceiving,
                              e => FleckLog.Warn("Failed to Authenticate", e));
            }
            else
            {
                connection.StartReceiving();
            }
        }