Exemple #1
0
        // As Client
        private bool isValidResponse(ResponseHandshake response, out string message)
        {
            if (!response.IsWebSocketResponse)
            {
                message = "Invalid WebSocket response.";
                return(false);
            }

            if (!response.HeaderExists("Sec-WebSocket-Accept", createResponseKey()))
            {
                message = "Invalid Sec-WebSocket-Accept.";
                return(false);
            }

            if (response.HeaderExists("Sec-WebSocket-Version") &&
                !response.HeaderExists("Sec-WebSocket-Version", _version))
            {
                message = "Unsupported Sec-WebSocket-Version.";
                return(false);
            }

            if (response.HeaderExists("Sec-WebSocket-Protocol"))
            {
                _protocol = response.Headers["Sec-WebSocket-Protocol"];
            }

            if (response.HeaderExists("Sec-WebSocket-Extensions"))
            {
                _extensions = response.Headers["Sec-WebSocket-Extensions"];
            }

            message = String.Empty;
            return(true);
        }
Exemple #2
0
        // As Client
        private bool receiveResponseHandshake()
        {
            var res = ResponseHandshake.Parse(readHandshake());

      #if DEBUG
            Console.WriteLine("WS: Info@receiveResponseHandshake: Response handshake from server:\n");
            Console.WriteLine(res.ToString());
      #endif
            if (!checkResponseIsValid(res))
            {
                var msg = "Invalid response to the WebSocket connection request.";
                onError(msg);
                close(CloseStatusCode.ABNORMAL, msg);
                return(false);
            }

            if (res.HeaderExists("Sec-WebSocket-Protocol"))
            {
                _protocol = res.Headers["Sec-WebSocket-Protocol"];
            }

            if (res.HeaderExists("Sec-WebSocket-Extensions"))
            {
                _extensions = res.Headers["Sec-WebSocket-Extensions"];
            }

            return(true);
        }
        public static ResponseHandshake CreateCloseResponse(HttpStatusCode code)
        {
            var res = new ResponseHandshake(code);

            res.AddHeader("Connection", "close");

            return(res);
        }
Exemple #4
0
        // As Server
        private ResponseHandshake createResponseHandshake(HttpStatusCode code)
        {
            var res = ResponseHandshake.CreateCloseResponse(code);

            res.AddHeader("Sec-WebSocket-Version", _version);

            return(res);
        }
Exemple #5
0
        // As Server
        private ResponseHandshake createResponseHandshake()
        {
            var res = new ResponseHandshake();

            res.AddHeader("Sec-WebSocket-Accept", createResponseKey());

            return(res);
        }
Exemple #6
0
   // As Server
   private void sendResponseHandshake(ResponseHandshake response)
   {
 #if DEBUG
       Console.WriteLine("WS: Info@sendResponseHandshake: Response handshake from server:\n");
       Console.WriteLine(response.ToString());
 #endif
       writeHandshake(response);
   }
Exemple #7
0
 // As Client
 private bool checkResponseIsValid(ResponseHandshake response)
 {
     return(!response.IsWebSocketResponse
      ? false
      : !response.HeaderExists("Sec-WebSocket-Accept", createResponseKey())
        ? false
        : !response.HeaderExists("Sec-WebSocket-Version") || response.HeaderExists("Sec-WebSocket-Version", _version));
 }
   // As server
   private void send(ResponseHandshake response)
   {
 #if DEBUG
       Console.WriteLine("WS: Info@send: Response handshake to client:\n");
       Console.WriteLine(response.ToString());
 #endif
       _wsStream.Write(response);
   }
Exemple #9
0
        // As Client
        private ResponseHandshake receiveResponseHandshake()
        {
            var res = ResponseHandshake.Parse(readHandshake());

      #if DEBUG
            Console.WriteLine("WS: Info@receiveResponseHandshake: Response handshake from server:\n");
            Console.WriteLine(res.ToString());
      #endif
            return(res);
        }
        // As server
        private ResponseHandshake createResponseHandshake()
        {
            var res = new ResponseHandshake();

            res.AddHeader("Sec-WebSocket-Accept", createResponseKey());
            if (_cookies.Count > 0)
            {
                res.SetCookies(_cookies);
            }

            return(res);
        }
Exemple #11
0
        // As server
        private ResponseHandshake createResponseHandshake()
        {
            var res = new ResponseHandshake();
              res.AddHeader("Sec-WebSocket-Accept", createResponseKey());
              if (_extensions.Length > 0)
            res.AddHeader("Sec-WebSocket-Extensions", _extensions);

              if (_cookies.Count > 0)
            res.SetCookies(_cookies);

              return res;
        }
Exemple #12
0
 // As server
 private void send(ResponseHandshake response)
 {
     #if DEBUG
       Console.WriteLine("WS: Info@send: Response handshake to client:\n");
       Console.WriteLine(response.ToString());
       #endif
       _wsStream.WriteHandshake(response);
 }
Exemple #13
0
        // As client
        private bool processResponseHandshake(ResponseHandshake response)
        {
            var error = response.IsUnauthorized
                ? String.Format("An HTTP {0} authorization is required.", response.AuthChallenge.Scheme)
                : !isValidResponseHandshake(response)
                  ? "Invalid response to this WebSocket connection request."
                  : String.Empty;

              if (error.Length > 0)
              {
            onError(error);
            Close(CloseStatusCode.ABNORMAL, error);

            return false;
              }

              processResponseProtocol(response.Headers["Sec-WebSocket-Protocol"]);
              processResponseExtensions(response.Headers["Sec-WebSocket-Extensions"]);
              processResponseCookies(response.Cookies);

              return true;
        }
Exemple #14
0
 // As client
 private bool isValidResponseHandshake(ResponseHandshake response)
 {
     return !response.IsWebSocketResponse
      ? false
      : !response.ContainsHeader("Sec-WebSocket-Accept", createResponseKey())
        ? false
        : !response.ContainsHeader("Sec-WebSocket-Version") ||
          response.ContainsHeader("Sec-WebSocket-Version", _version);
 }
Exemple #15
0
        // As Server
        private void sendResponseHandshake(ResponseHandshake response)
        {
#if DEBUG
            Console.WriteLine("WS: Info@sendResponseHandshake: Response handshake from server:\n");
            Console.WriteLine(response.ToString());
#endif
            writeHandshake(response);
        }
Exemple #16
0
        // As Client
        private bool isValidResponse(ResponseHandshake response, out string message)
        {
            if (!response.IsWebSocketResponse)
            {
                message = "Invalid WebSocket response.";
                return false;
            }

            if (!response.HeaderExists("Sec-WebSocket-Accept", createResponseKey()))
            {
                message = "Invalid Sec-WebSocket-Accept.";
                return false;
            }

            if (response.HeaderExists("Sec-WebSocket-Version") &&
                !response.HeaderExists("Sec-WebSocket-Version", _version))
            {
                message = "Unsupported Sec-WebSocket-Version.";
                return false;
            }

            if (response.HeaderExists("Sec-WebSocket-Protocol"))
                _protocol = response.Headers["Sec-WebSocket-Protocol"];

            if (response.HeaderExists("Sec-WebSocket-Extensions"))
                _extensions = response.Headers["Sec-WebSocket-Extensions"];

            message = String.Empty;
            return true;
        }
Exemple #17
0
        // As Server
        private ResponseHandshake createResponseHandshake()
        {
            var res = new ResponseHandshake();
            res.AddHeader("Sec-WebSocket-Accept", createResponseKey());

            return res;
        }
        public static ResponseHandshake CreateCloseResponse(HttpStatusCode code)
        {
            var res = new ResponseHandshake(code);
              res.AddHeader("Connection", "close");

              return res;
        }
Exemple #19
0
 // As Client
 private bool checkResponseIsValid(ResponseHandshake response)
 {
     return !response.IsWebSocketResponse
      ? false
      : !response.HeaderExists("Sec-WebSocket-Accept", createResponseKey())
        ? false
        : !response.HeaderExists("Sec-WebSocket-Version") || response.HeaderExists("Sec-WebSocket-Version", _version);
 }