diff --git a/websocket-sharp/Ext.cs b/websocket-sharp/Ext.cs index e1cb9606..45a966e4 100644 --- a/websocket-sharp/Ext.cs +++ b/websocket-sharp/Ext.cs @@ -251,33 +251,6 @@ namespace WebSocketSharp : null; } - internal static string CheckIfValidCloseParameters (this ushort code, string reason) - { - return !code.IsCloseStatusCode () - ? "An invalid close status code." - : code.IsNoStatus () && !reason.IsNullOrEmpty () - ? "NoStatus cannot have a reason." - : !reason.IsNullOrEmpty () && Encoding.UTF8.GetBytes (reason).Length > 123 - ? "A reason has greater than the allowable max size." - : null; - } - - internal static string CheckIfValidCloseParameters (this CloseStatusCode code, string reason) - { - return code.IsNoStatus () && !reason.IsNullOrEmpty () - ? "NoStatus cannot have a reason." - : !reason.IsNullOrEmpty () && Encoding.UTF8.GetBytes (reason).Length > 123 - ? "A reason has greater than the allowable max size." - : null; - } - - internal static string CheckIfValidCloseStatusCode (this ushort code) - { - return !code.IsCloseStatusCode () - ? "An invalid close status code." - : null; - } - internal static string CheckIfValidControlData (this byte[] data, string paramName) { return data.Length > 125 diff --git a/websocket-sharp/Server/HttpServer.cs b/websocket-sharp/Server/HttpServer.cs index 97ce3d11..222b30d3 100644 --- a/websocket-sharp/Server/HttpServer.cs +++ b/websocket-sharp/Server/HttpServer.cs @@ -757,7 +757,9 @@ namespace WebSocketSharp.Server public void Stop (ushort code, string reason) { lock (_sync) { - var msg = _state.CheckIfStart () ?? code.CheckIfValidCloseParameters (reason); + var msg = _state.CheckIfStart () ?? + WebSocket.CheckIfValidCloseParameters (code, reason, false); + if (msg != null) { _logger.Error (msg); return; @@ -793,7 +795,9 @@ namespace WebSocketSharp.Server public void Stop (CloseStatusCode code, string reason) { lock (_sync) { - var msg = _state.CheckIfStart () ?? code.CheckIfValidCloseParameters (reason); + var msg = _state.CheckIfStart () ?? + WebSocket.CheckIfValidCloseParameters (code, reason, false); + if (msg != null) { _logger.Error (msg); return; diff --git a/websocket-sharp/Server/WebSocketServer.cs b/websocket-sharp/Server/WebSocketServer.cs index 7ba2a52d..f6159b6d 100644 --- a/websocket-sharp/Server/WebSocketServer.cs +++ b/websocket-sharp/Server/WebSocketServer.cs @@ -842,7 +842,9 @@ namespace WebSocketSharp.Server public void Stop (ushort code, string reason) { lock (_sync) { - var msg = _state.CheckIfStart () ?? code.CheckIfValidCloseParameters (reason); + var msg = _state.CheckIfStart () ?? + WebSocket.CheckIfValidCloseParameters (code, reason, false); + if (msg != null) { _logger.Error (msg); return; @@ -877,7 +879,9 @@ namespace WebSocketSharp.Server public void Stop (CloseStatusCode code, string reason) { lock (_sync) { - var msg = _state.CheckIfStart () ?? code.CheckIfValidCloseParameters (reason); + var msg = _state.CheckIfStart () ?? + WebSocket.CheckIfValidCloseParameters (code, reason, false); + if (msg != null) { _logger.Error (msg); return; diff --git a/websocket-sharp/WebSocket.cs b/websocket-sharp/WebSocket.cs index eb2fca4b..5b6964b2 100644 --- a/websocket-sharp/WebSocket.cs +++ b/websocket-sharp/WebSocket.cs @@ -1560,6 +1560,35 @@ namespace WebSocketSharp #region Internal Methods + internal static string CheckIfValidCloseParameters (ushort code, string reason, bool client) + { + return !code.IsCloseStatusCode () + ? "An invalid close status code." + : code == (ushort) CloseStatusCode.NoStatus + ? (!reason.IsNullOrEmpty () ? "NoStatus cannot have a reason." : null) + : code == (ushort) CloseStatusCode.MandatoryExtension && !client + ? "MandatoryExtension cannot be used by the server." + : code == (ushort) CloseStatusCode.ServerError && client + ? "ServerError cannot be used by the client." + : !reason.IsNullOrEmpty () && Encoding.UTF8.GetBytes (reason).Length > 123 + ? "A reason has greater than the allowable max size." + : null; + } + + internal static string CheckIfValidCloseParameters ( + CloseStatusCode code, string reason, bool client) + { + return code == CloseStatusCode.NoStatus + ? (!reason.IsNullOrEmpty () ? "NoStatus cannot have a reason." : null) + : code == CloseStatusCode.MandatoryExtension && !client + ? "MandatoryExtension cannot be used by the server." + : code == CloseStatusCode.ServerError && client + ? "ServerError cannot be used by the client." + : !reason.IsNullOrEmpty () && Encoding.UTF8.GetBytes (reason).Length > 123 + ? "A reason has greater than the allowable max size." + : null; + } + // As server internal void Close (HttpResponse response) { @@ -1738,7 +1767,9 @@ namespace WebSocketSharp /// public void Close (ushort code) { - var msg = _readyState.CheckIfClosable () ?? code.CheckIfValidCloseStatusCode (); + var msg = _readyState.CheckIfClosable () ?? + CheckIfValidCloseParameters (code, null, _client); + if (msg != null) { _logger.Error (msg); error ("An error has occurred in closing the connection.", null); @@ -1765,7 +1796,9 @@ namespace WebSocketSharp /// public void Close (CloseStatusCode code) { - var msg = _readyState.CheckIfClosable (); + var msg = _readyState.CheckIfClosable () ?? + CheckIfValidCloseParameters (code, null, _client); + if (msg != null) { _logger.Error (msg); error ("An error has occurred in closing the connection.", null); @@ -1799,7 +1832,9 @@ namespace WebSocketSharp /// public void Close (ushort code, string reason) { - var msg = _readyState.CheckIfClosable () ?? code.CheckIfValidCloseParameters (reason); + var msg = _readyState.CheckIfClosable () ?? + CheckIfValidCloseParameters (code, reason, _client); + if (msg != null) { _logger.Error (msg); error ("An error has occurred in closing the connection.", null); @@ -1833,7 +1868,9 @@ namespace WebSocketSharp /// public void Close (CloseStatusCode code, string reason) { - var msg = _readyState.CheckIfClosable () ?? code.CheckIfValidCloseParameters (reason); + var msg = _readyState.CheckIfClosable () ?? + CheckIfValidCloseParameters (code, reason, _client); + if (msg != null) { _logger.Error (msg); error ("An error has occurred in closing the connection.", null); @@ -1887,7 +1924,9 @@ namespace WebSocketSharp /// public void CloseAsync (ushort code) { - var msg = _readyState.CheckIfClosable () ?? code.CheckIfValidCloseStatusCode (); + var msg = _readyState.CheckIfClosable () ?? + CheckIfValidCloseParameters (code, null, _client); + if (msg != null) { _logger.Error (msg); error ("An error has occurred in closing the connection.", null); @@ -1917,7 +1956,9 @@ namespace WebSocketSharp /// public void CloseAsync (CloseStatusCode code) { - var msg = _readyState.CheckIfClosable (); + var msg = _readyState.CheckIfClosable () ?? + CheckIfValidCloseParameters (code, null, _client); + if (msg != null) { _logger.Error (msg); error ("An error has occurred in closing the connection.", null); @@ -1956,7 +1997,9 @@ namespace WebSocketSharp /// public void CloseAsync (ushort code, string reason) { - var msg = _readyState.CheckIfClosable () ?? code.CheckIfValidCloseParameters (reason); + var msg = _readyState.CheckIfClosable () ?? + CheckIfValidCloseParameters (code, reason, _client); + if (msg != null) { _logger.Error (msg); error ("An error has occurred in closing the connection.", null); @@ -1996,7 +2039,9 @@ namespace WebSocketSharp /// public void CloseAsync (CloseStatusCode code, string reason) { - var msg = _readyState.CheckIfClosable () ?? code.CheckIfValidCloseParameters (reason); + var msg = _readyState.CheckIfClosable () ?? + CheckIfValidCloseParameters (code, reason, _client); + if (msg != null) { _logger.Error (msg); error ("An error has occurred in closing the connection.", null);